KeyedObjects2DTest.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. * KeyedObjects2DTest.java
  29. * -----------------------
  30. * (C) Copyright 2004-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. * 01-Mar-2004 : Version 1 (DG);
  38. * 28-Sep-2007 : Added testEquals() and enhanced testClone() (DG);
  39. * 03-Oct-2007 : Added new tests (DG);
  40. *
  41. */
  42. package org.jfree.data;
  43. import org.jfree.chart.TestUtilities;
  44. import org.junit.Test;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.assertNull;
  49. /**
  50. * Tests for the {@link KeyedObjects2D} class.
  51. */
  52. public class KeyedObjects2DTest {
  53. /**
  54. * Some checks for the equals() method.
  55. */
  56. @Test
  57. public void testEquals() {
  58. KeyedObjects2D k1 = new KeyedObjects2D();
  59. KeyedObjects2D k2 = new KeyedObjects2D();
  60. assertTrue(k1.equals(k2));
  61. assertTrue(k2.equals(k1));
  62. k1.addObject(new Integer(99), "R1", "C1");
  63. assertFalse(k1.equals(k2));
  64. k2.addObject(new Integer(99), "R1", "C1");
  65. assertTrue(k1.equals(k2));
  66. }
  67. /**
  68. * Confirm that cloning works.
  69. */
  70. @Test
  71. public void testCloning() throws CloneNotSupportedException {
  72. KeyedObjects2D o1 = new KeyedObjects2D();
  73. o1.setObject(new Integer(1), "V1", "C1");
  74. o1.setObject(null, "V2", "C1");
  75. o1.setObject(new Integer(3), "V3", "C2");
  76. KeyedObjects2D o2 = (KeyedObjects2D) o1.clone();
  77. assertTrue(o1 != o2);
  78. assertTrue(o1.getClass() == o2.getClass());
  79. assertTrue(o1.equals(o2));
  80. // check independence
  81. o1.addObject("XX", "R1", "C1");
  82. assertFalse(o1.equals(o2));
  83. }
  84. /**
  85. * Serialize an instance, restore it, and check for equality.
  86. */
  87. @Test
  88. public void testSerialization() {
  89. KeyedObjects2D ko2D1 = new KeyedObjects2D();
  90. ko2D1.addObject(new Double(234.2), "Row1", "Col1");
  91. ko2D1.addObject(null, "Row1", "Col2");
  92. ko2D1.addObject(new Double(345.9), "Row2", "Col1");
  93. ko2D1.addObject(new Double(452.7), "Row2", "Col2");
  94. KeyedObjects2D ko2D2 = (KeyedObjects2D) TestUtilities.serialised(ko2D1);
  95. assertEquals(ko2D1, ko2D2);
  96. }
  97. /**
  98. * Some checks for the getValue(int, int) method.
  99. */
  100. @Test
  101. public void testGetValueByIndex() {
  102. KeyedObjects2D data = new KeyedObjects2D();
  103. data.addObject("Obj1", "R1", "C1");
  104. data.addObject("Obj2", "R2", "C2");
  105. assertEquals("Obj1", data.getObject(0, 0));
  106. assertEquals("Obj2", data.getObject(1, 1));
  107. assertNull(data.getObject(0, 1));
  108. assertNull(data.getObject(1, 0));
  109. // check invalid indices
  110. boolean pass = false;
  111. try {
  112. data.getObject(-1, 0);
  113. }
  114. catch (IndexOutOfBoundsException e) {
  115. pass = true;
  116. }
  117. assertTrue(pass);
  118. pass = false;
  119. try {
  120. data.getObject(0, -1);
  121. }
  122. catch (IndexOutOfBoundsException e) {
  123. pass = true;
  124. }
  125. assertTrue(pass);
  126. pass = false;
  127. try {
  128. data.getObject(2, 0);
  129. }
  130. catch (IndexOutOfBoundsException e) {
  131. pass = true;
  132. }
  133. assertTrue(pass);
  134. pass = false;
  135. try {
  136. data.getObject(0, 2);
  137. }
  138. catch (IndexOutOfBoundsException e) {
  139. pass = true;
  140. }
  141. assertTrue(pass);
  142. }
  143. /**
  144. * Some checks for the getValue(Comparable, Comparable) method.
  145. */
  146. @Test
  147. public void testGetValueByKey() {
  148. KeyedObjects2D data = new KeyedObjects2D();
  149. data.addObject("Obj1", "R1", "C1");
  150. data.addObject("Obj2", "R2", "C2");
  151. assertEquals("Obj1", data.getObject("R1", "C1"));
  152. assertEquals("Obj2", data.getObject("R2", "C2"));
  153. assertNull(data.getObject("R1", "C2"));
  154. assertNull(data.getObject("R2", "C1"));
  155. // check invalid indices
  156. boolean pass = false;
  157. try {
  158. data.getObject("XX", "C1");
  159. }
  160. catch (UnknownKeyException e) {
  161. pass = true;
  162. }
  163. assertTrue(pass);
  164. pass = false;
  165. try {
  166. data.getObject("R1", "XX");
  167. }
  168. catch (UnknownKeyException e) {
  169. pass = true;
  170. }
  171. assertTrue(pass);
  172. pass = false;
  173. try {
  174. data.getObject("XX", "C1");
  175. }
  176. catch (UnknownKeyException e) {
  177. pass = true;
  178. }
  179. assertTrue(pass);
  180. pass = false;
  181. try {
  182. data.getObject("R1", "XX");
  183. }
  184. catch (UnknownKeyException e) {
  185. pass = true;
  186. }
  187. assertTrue(pass);
  188. }
  189. /**
  190. * Some checks for the setObject(Object, Comparable, Comparable) method.
  191. */
  192. @Test
  193. public void testSetObject() {
  194. KeyedObjects2D data = new KeyedObjects2D();
  195. data.setObject("Obj1", "R1", "C1");
  196. data.setObject("Obj2", "R2", "C2");
  197. assertEquals("Obj1", data.getObject("R1", "C1"));
  198. assertEquals("Obj2", data.getObject("R2", "C2"));
  199. assertNull(data.getObject("R1", "C2"));
  200. assertNull(data.getObject("R2", "C1"));
  201. // confirm overwriting an existing value
  202. data.setObject("ABC", "R2", "C2");
  203. assertEquals("ABC", data.getObject("R2", "C2"));
  204. // try null keys
  205. boolean pass = false;
  206. try {
  207. data.setObject("X", null, "C1");
  208. }
  209. catch (IllegalArgumentException e) {
  210. pass = true;
  211. }
  212. assertTrue(pass);
  213. pass = false;
  214. try {
  215. data.setObject("X", "R1", null);
  216. }
  217. catch (IllegalArgumentException e) {
  218. pass = true;
  219. }
  220. assertTrue(pass);
  221. }
  222. /**
  223. * Some checks for the removeRow(int) method.
  224. */
  225. @Test
  226. public void testRemoveRowByIndex() {
  227. KeyedObjects2D data = new KeyedObjects2D();
  228. data.setObject("Obj1", "R1", "C1");
  229. data.setObject("Obj2", "R2", "C2");
  230. data.removeRow(0);
  231. assertEquals(1, data.getRowCount());
  232. assertEquals("Obj2", data.getObject(0, 1));
  233. // try negative row index
  234. boolean pass = false;
  235. try {
  236. data.removeRow(-1);
  237. }
  238. catch (IndexOutOfBoundsException e) {
  239. pass = true;
  240. }
  241. assertTrue(pass);
  242. // try row index too high
  243. pass = false;
  244. try {
  245. data.removeRow(data.getRowCount());
  246. }
  247. catch (IndexOutOfBoundsException e) {
  248. pass = true;
  249. }
  250. assertTrue(pass);
  251. }
  252. /**
  253. * Some checks for the removeColumn(int) method.
  254. */
  255. @Test
  256. public void testRemoveColumnByIndex() {
  257. KeyedObjects2D data = new KeyedObjects2D();
  258. data.setObject("Obj1", "R1", "C1");
  259. data.setObject("Obj2", "R2", "C2");
  260. data.removeColumn(0);
  261. assertEquals(1, data.getColumnCount());
  262. assertEquals("Obj2", data.getObject(1, 0));
  263. // try negative column index
  264. boolean pass = false;
  265. try {
  266. data.removeColumn(-1);
  267. }
  268. catch (IndexOutOfBoundsException e) {
  269. pass = true;
  270. }
  271. assertTrue(pass);
  272. // try column index too high
  273. pass = false;
  274. try {
  275. data.removeColumn(data.getColumnCount());
  276. }
  277. catch (IndexOutOfBoundsException e) {
  278. pass = true;
  279. }
  280. assertTrue(pass);
  281. }
  282. /**
  283. * Some checks for the removeRow(Comparable) method.
  284. */
  285. @Test
  286. public void testRemoveRowByKey() {
  287. KeyedObjects2D data = new KeyedObjects2D();
  288. data.setObject("Obj1", "R1", "C1");
  289. data.setObject("Obj2", "R2", "C2");
  290. data.removeRow("R2");
  291. assertEquals(1, data.getRowCount());
  292. assertEquals("Obj1", data.getObject(0, 0));
  293. // try unknown row key
  294. boolean pass = false;
  295. try {
  296. data.removeRow("XXX");
  297. }
  298. catch (UnknownKeyException e) {
  299. pass = true;
  300. }
  301. assertTrue(pass);
  302. // try null row key
  303. pass = false;
  304. try {
  305. data.removeRow(null);
  306. }
  307. catch (IllegalArgumentException e) {
  308. pass = true;
  309. }
  310. assertTrue(pass);
  311. }
  312. /**
  313. * Some checks for the removeColumn(Comparable) method.
  314. */
  315. @Test
  316. public void testRemoveColumnByKey() {
  317. KeyedObjects2D data = new KeyedObjects2D();
  318. data.setObject("Obj1", "R1", "C1");
  319. data.setObject("Obj2", "R2", "C2");
  320. data.removeColumn("C2");
  321. assertEquals(1, data.getColumnCount());
  322. assertEquals("Obj1", data.getObject(0, 0));
  323. // try unknown column key
  324. boolean pass = false;
  325. try {
  326. data.removeColumn("XXX");
  327. }
  328. catch (UnknownKeyException e) {
  329. pass = true;
  330. }
  331. assertTrue(pass);
  332. // try null column key
  333. pass = false;
  334. try {
  335. data.removeColumn(null);
  336. }
  337. catch (IllegalArgumentException e) {
  338. pass = true;
  339. }
  340. assertTrue(pass);
  341. }
  342. /**
  343. * A simple check for the removeValue() method.
  344. */
  345. @Test
  346. public void testRemoveValue() {
  347. KeyedObjects2D data = new KeyedObjects2D();
  348. data.setObject("Obj1", "R1", "C1");
  349. data.setObject("Obj2", "R2", "C2");
  350. data.removeObject("R2", "C2");
  351. assertEquals(1, data.getRowCount());
  352. assertEquals(1, data.getColumnCount());
  353. assertEquals("Obj1", data.getObject(0, 0));
  354. }
  355. }