DefaultKeyedValues2DTest.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -----------------------------
  28. * DefaultKeyedValues2DTest.java
  29. * -----------------------------
  30. * (C) Copyright 2003-2013 by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 13-Mar-2003 : Version 1 (DG);
  38. * 15-Sep-2004 : Updated cloning test (DG);
  39. * 06-Oct-2005 : Added testEquals() (DG);
  40. * 18-Jan-2007 : Added testSparsePopulation() (DG);
  41. * 26-Feb-2007 : Added some basic tests (DG);
  42. * 30-Mar-2007 : Added a test for bug 1690654 (DG);
  43. * 21-Nov-2007 : Added testRemoveColumnByKey() method (DG);
  44. *
  45. */
  46. package org.jfree.data;
  47. import static org.junit.Assert.assertEquals;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.assertFalse;
  50. import static org.junit.Assert.assertNull;
  51. import org.jfree.chart.TestUtilities;
  52. import org.junit.Test;
  53. /**
  54. * Tests for the {@link DefaultKeyedValues2D} class.
  55. */
  56. public class DefaultKeyedValues2DTest {
  57. /**
  58. * Some checks for the getValue() method.
  59. */
  60. @Test
  61. public void testGetValue() {
  62. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  63. d.addValue(new Double(1.0), "R1", "C1");
  64. assertEquals(new Double(1.0), d.getValue("R1", "C1"));
  65. boolean pass = false;
  66. try {
  67. d.getValue("XX", "C1");
  68. }
  69. catch (UnknownKeyException e) {
  70. pass = true;
  71. }
  72. assertTrue(pass);
  73. pass = false;
  74. try {
  75. d.getValue("R1", "XX");
  76. }
  77. catch (UnknownKeyException e) {
  78. pass = true;
  79. }
  80. assertTrue(pass);
  81. }
  82. /**
  83. * Some checks for the clone() method.
  84. */
  85. @Test
  86. public void testCloning() throws CloneNotSupportedException {
  87. DefaultKeyedValues2D v1 = new DefaultKeyedValues2D();
  88. v1.setValue(new Integer(1), "V1", "C1");
  89. v1.setValue(null, "V2", "C1");
  90. v1.setValue(new Integer(3), "V3", "C2");
  91. DefaultKeyedValues2D v2 = (DefaultKeyedValues2D) v1.clone();
  92. assertTrue(v1 != v2);
  93. assertTrue(v1.getClass() == v2.getClass());
  94. assertTrue(v1.equals(v2));
  95. // check that clone is independent of the original
  96. v2.setValue(new Integer(2), "V2", "C1");
  97. assertFalse(v1.equals(v2));
  98. }
  99. /**
  100. * Serialize an instance, restore it, and check for equality.
  101. */
  102. @Test
  103. public void testSerialization() {
  104. DefaultKeyedValues2D kv2D1 = new DefaultKeyedValues2D();
  105. kv2D1.addValue(new Double(234.2), "Row1", "Col1");
  106. kv2D1.addValue(null, "Row1", "Col2");
  107. kv2D1.addValue(new Double(345.9), "Row2", "Col1");
  108. kv2D1.addValue(new Double(452.7), "Row2", "Col2");
  109. DefaultKeyedValues2D kv2D2 = (DefaultKeyedValues2D)
  110. TestUtilities.serialised(kv2D1);
  111. assertEquals(kv2D1, kv2D2);
  112. }
  113. /**
  114. * Some checks for the equals() method.
  115. */
  116. @Test
  117. public void testEquals() {
  118. DefaultKeyedValues2D d1 = new DefaultKeyedValues2D();
  119. DefaultKeyedValues2D d2 = new DefaultKeyedValues2D();
  120. assertTrue(d1.equals(d2));
  121. assertTrue(d2.equals(d1));
  122. d1.addValue(new Double(1.0), new Double(2.0), "S1");
  123. assertFalse(d1.equals(d2));
  124. d2.addValue(new Double(1.0), new Double(2.0), "S1");
  125. assertTrue(d1.equals(d2));
  126. }
  127. /**
  128. * Populates a data structure with sparse entries, then checks that
  129. * the unspecified entries return null.
  130. */
  131. @Test
  132. public void testSparsePopulation() {
  133. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  134. d.addValue(new Integer(11), "R1", "C1");
  135. d.addValue(new Integer(22), "R2", "C2");
  136. assertEquals(new Integer(11), d.getValue("R1", "C1"));
  137. assertNull(d.getValue("R1", "C2"));
  138. assertEquals(new Integer(22), d.getValue("R2", "C2"));
  139. assertNull(d.getValue("R2", "C1"));
  140. }
  141. /**
  142. * Some basic checks for the getRowCount() method.
  143. */
  144. @Test
  145. public void testRowCount() {
  146. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  147. assertEquals(0, d.getRowCount());
  148. d.addValue(new Double(1.0), "R1", "C1");
  149. assertEquals(1, d.getRowCount());
  150. d.addValue(new Double(2.0), "R2", "C1");
  151. assertEquals(2, d.getRowCount());
  152. }
  153. /**
  154. * Some basic checks for the getColumnCount() method.
  155. */
  156. @Test
  157. public void testColumnCount() {
  158. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  159. assertEquals(0, d.getColumnCount());
  160. d.addValue(new Double(1.0), "R1", "C1");
  161. assertEquals(1, d.getColumnCount());
  162. d.addValue(new Double(2.0), "R1", "C2");
  163. assertEquals(2, d.getColumnCount());
  164. }
  165. private static final double EPSILON = 0.0000000001;
  166. /**
  167. * Some basic checks for the getValue(int, int) method.
  168. */
  169. @Test
  170. public void testGetValue2() {
  171. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  172. boolean pass = false;
  173. try {
  174. d.getValue(0, 0);
  175. }
  176. catch (IndexOutOfBoundsException e) {
  177. pass = true;
  178. }
  179. assertTrue(pass);
  180. d.addValue(new Double(1.0), "R1", "C1");
  181. assertEquals(1.0, d.getValue(0, 0).doubleValue(), EPSILON);
  182. d.addValue(new Double(2.0), "R2", "C2");
  183. assertEquals(2.0, d.getValue(1, 1).doubleValue(), EPSILON);
  184. assertNull(d.getValue(1, 0));
  185. assertNull(d.getValue(0, 1));
  186. pass = false;
  187. try {
  188. d.getValue(2, 0);
  189. }
  190. catch (IndexOutOfBoundsException e) {
  191. pass = true;
  192. }
  193. assertTrue(pass);
  194. }
  195. /**
  196. * Some basic checks for the getRowKey() method.
  197. */
  198. @Test
  199. public void testGetRowKey() {
  200. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  201. boolean pass = false;
  202. try {
  203. d.getRowKey(0);
  204. }
  205. catch (IndexOutOfBoundsException e) {
  206. pass = true;
  207. }
  208. assertTrue(pass);
  209. d.addValue(new Double(1.0), "R1", "C1");
  210. d.addValue(new Double(1.0), "R2", "C1");
  211. assertEquals("R1", d.getRowKey(0));
  212. assertEquals("R2", d.getRowKey(1));
  213. // check sorted rows
  214. d = new DefaultKeyedValues2D(true);
  215. d.addValue(new Double(1.0), "R1", "C1");
  216. assertEquals("R1", d.getRowKey(0));
  217. d.addValue(new Double(0.0), "R0", "C1");
  218. assertEquals("R0", d.getRowKey(0));
  219. assertEquals("R1", d.getRowKey(1));
  220. }
  221. /**
  222. * Some basic checks for the getColumnKey() method.
  223. */
  224. @Test
  225. public void testGetColumnKey() {
  226. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  227. boolean pass = false;
  228. try {
  229. d.getColumnKey(0);
  230. }
  231. catch (IndexOutOfBoundsException e) {
  232. pass = true;
  233. }
  234. assertTrue(pass);
  235. d.addValue(new Double(1.0), "R1", "C1");
  236. d.addValue(new Double(1.0), "R1", "C2");
  237. assertEquals("C1", d.getColumnKey(0));
  238. assertEquals("C2", d.getColumnKey(1));
  239. }
  240. /**
  241. * Some basic checks for the removeValue() method.
  242. */
  243. @Test
  244. public void testRemoveValue() {
  245. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  246. d.removeValue("R1", "C1");
  247. d.addValue(new Double(1.0), "R1", "C1");
  248. d.removeValue("R1", "C1");
  249. assertEquals(0, d.getRowCount());
  250. assertEquals(0, d.getColumnCount());
  251. d.addValue(new Double(1.0), "R1", "C1");
  252. d.addValue(new Double(2.0), "R2", "C1");
  253. d.removeValue("R1", "C1");
  254. assertEquals(new Double(2.0), d.getValue(0, 0));
  255. }
  256. /**
  257. * A test for bug 1690654.
  258. */
  259. @Test
  260. public void testRemoveValueBug1690654() {
  261. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  262. d.addValue(new Double(1.0), "R1", "C1");
  263. d.addValue(new Double(2.0), "R2", "C2");
  264. assertEquals(2, d.getColumnCount());
  265. assertEquals(2, d.getRowCount());
  266. d.removeValue("R2", "C2");
  267. assertEquals(1, d.getColumnCount());
  268. assertEquals(1, d.getRowCount());
  269. assertEquals(new Double(1.0), d.getValue(0, 0));
  270. }
  271. /**
  272. * Some basic checks for the removeRow() method.
  273. */
  274. @Test
  275. public void testRemoveRow() {
  276. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  277. boolean pass = false;
  278. try {
  279. d.removeRow(0);
  280. }
  281. catch (IndexOutOfBoundsException e) {
  282. pass = true;
  283. }
  284. assertTrue(pass);
  285. }
  286. /**
  287. * Some basic checks for the removeColumn(Comparable) method.
  288. */
  289. @Test
  290. public void testRemoveColumnByKey() {
  291. DefaultKeyedValues2D d = new DefaultKeyedValues2D();
  292. d.addValue(new Double(1.0), "R1", "C1");
  293. d.addValue(new Double(2.0), "R2", "C2");
  294. d.removeColumn("C2");
  295. d.addValue(new Double(3.0), "R2", "C2");
  296. assertEquals(3.0, d.getValue("R2", "C2").doubleValue(), EPSILON);
  297. // check for unknown column
  298. boolean pass = false;
  299. try {
  300. d.removeColumn("XXX");
  301. }
  302. catch (UnknownKeyException e) {
  303. pass = true;
  304. }
  305. assertTrue(pass);
  306. }
  307. }