KeyedObjectsTest.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * KeyedObjectsTest.java
  29. * ---------------------
  30. * (C) Copyright 2004-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 27-Jan-2004 : Version 1 (DG);
  38. * 28-Sep-2007 : Added testCloning2() (DG);
  39. * 03-Oct-2007 : New tests (DG);
  40. *
  41. */
  42. package org.jfree.data;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.junit.Assert.assertNull;
  46. import java.util.ArrayList;
  47. import org.jfree.chart.TestUtilities;
  48. import org.jfree.data.general.DefaultPieDataset;
  49. import org.junit.Test;
  50. /**
  51. * Tests for the {@link KeyedObjects} class.
  52. */
  53. public class KeyedObjectsTest {
  54. /**
  55. * Confirm that cloning works.
  56. */
  57. @Test
  58. public void testCloning() throws CloneNotSupportedException {
  59. KeyedObjects ko1 = new KeyedObjects();
  60. ko1.addObject("V1", new Integer(1));
  61. ko1.addObject("V2", null);
  62. ko1.addObject("V3", new Integer(3));
  63. KeyedObjects ko2 = (KeyedObjects) ko1.clone();
  64. assertTrue(ko1 != ko2);
  65. assertTrue(ko1.getClass() == ko2.getClass());
  66. assertTrue(ko1.equals(ko2));
  67. }
  68. /**
  69. * Confirm special features of cloning.
  70. */
  71. @Test
  72. public void testCloning2() throws CloneNotSupportedException {
  73. // case 1 - object is mutable but not PublicCloneable
  74. Object obj1 = new ArrayList();
  75. KeyedObjects ko1 = new KeyedObjects();
  76. ko1.addObject("K1", obj1);
  77. KeyedObjects ko2 = (KeyedObjects) ko1.clone();
  78. assertTrue(ko1 != ko2);
  79. assertTrue(ko1.getClass() == ko2.getClass());
  80. assertTrue(ko1.equals(ko2));
  81. // the clone contains a reference to the original object
  82. assertTrue(ko2.getObject("K1") == obj1);
  83. // CASE 2 - object is mutable AND PublicCloneable
  84. obj1 = new DefaultPieDataset();
  85. ko1 = new KeyedObjects();
  86. ko1.addObject("K1", obj1);
  87. ko2 = (KeyedObjects) ko1.clone();
  88. assertTrue(ko1 != ko2);
  89. assertTrue(ko1.getClass() == ko2.getClass());
  90. assertTrue(ko1.equals(ko2));
  91. // the clone contains a reference to a CLONE of the original object
  92. assertTrue(ko2.getObject("K1") != obj1);
  93. }
  94. /**
  95. * Check that inserting and retrieving values works as expected.
  96. */
  97. @Test
  98. public void testInsertAndRetrieve() {
  99. KeyedObjects data = new KeyedObjects();
  100. data.addObject("A", new Double(1.0));
  101. data.addObject("B", new Double(2.0));
  102. data.addObject("C", new Double(3.0));
  103. data.addObject("D", null);
  104. // check key order
  105. assertEquals(data.getKey(0), "A");
  106. assertEquals(data.getKey(1), "B");
  107. assertEquals(data.getKey(2), "C");
  108. assertEquals(data.getKey(3), "D");
  109. // check retrieve value by key
  110. assertEquals(data.getObject("A"), new Double(1.0));
  111. assertEquals(data.getObject("B"), new Double(2.0));
  112. assertEquals(data.getObject("C"), new Double(3.0));
  113. assertEquals(data.getObject("D"), null);
  114. boolean pass = false;
  115. try {
  116. data.getObject("Not a key");
  117. }
  118. catch (UnknownKeyException e) {
  119. pass = true;
  120. }
  121. assertTrue(pass);
  122. // check retrieve value by index
  123. assertEquals(data.getObject(0), new Double(1.0));
  124. assertEquals(data.getObject(1), new Double(2.0));
  125. assertEquals(data.getObject(2), new Double(3.0));
  126. assertEquals(data.getObject(3), null);
  127. }
  128. /**
  129. * Serialize an instance, restore it, and check for equality.
  130. */
  131. @Test
  132. public void testSerialization() {
  133. KeyedObjects ko1 = new KeyedObjects();
  134. ko1.addObject("Key 1", "Object 1");
  135. ko1.addObject("Key 2", null);
  136. ko1.addObject("Key 3", "Object 2");
  137. KeyedObjects ko2 = (KeyedObjects) TestUtilities.serialised(ko1);
  138. assertEquals(ko1, ko2);
  139. }
  140. /**
  141. * Simple checks for the getObject(int) method.
  142. */
  143. @Test
  144. public void testGetObject() {
  145. // retrieve an item
  146. KeyedObjects ko1 = new KeyedObjects();
  147. ko1.addObject("Key 1", "Object 1");
  148. ko1.addObject("Key 2", null);
  149. ko1.addObject("Key 3", "Object 2");
  150. assertEquals("Object 1", ko1.getObject(0));
  151. assertNull(ko1.getObject(1));
  152. assertEquals("Object 2", ko1.getObject(2));
  153. // request with a negative index
  154. boolean pass = false;
  155. try {
  156. ko1.getObject(-1);
  157. }
  158. catch (IndexOutOfBoundsException e) {
  159. pass = true;
  160. }
  161. assertTrue(pass);
  162. // request width index == itemCount
  163. pass = false;
  164. try {
  165. ko1.getObject(3);
  166. }
  167. catch (IndexOutOfBoundsException e) {
  168. pass = true;
  169. }
  170. assertTrue(pass);
  171. }
  172. /**
  173. * Simple checks for the getKey(int) method.
  174. */
  175. @Test
  176. public void testGetKey() {
  177. // retrieve an item
  178. KeyedObjects ko1 = new KeyedObjects();
  179. ko1.addObject("Key 1", "Object 1");
  180. ko1.addObject("Key 2", null);
  181. ko1.addObject("Key 3", "Object 2");
  182. assertEquals("Key 1", ko1.getKey(0));
  183. assertEquals("Key 2", ko1.getKey(1));
  184. assertEquals("Key 3", ko1.getKey(2));
  185. // request with a negative index
  186. boolean pass = false;
  187. try {
  188. ko1.getKey(-1);
  189. }
  190. catch (IndexOutOfBoundsException e) {
  191. pass = true;
  192. }
  193. assertTrue(pass);
  194. // request width index == itemCount
  195. pass = false;
  196. try {
  197. ko1.getKey(3);
  198. }
  199. catch (IndexOutOfBoundsException e) {
  200. pass = true;
  201. }
  202. assertTrue(pass);
  203. }
  204. /**
  205. * Simple checks for the getIndex(Comparable) method.
  206. */
  207. @Test
  208. public void testGetIndex() {
  209. KeyedObjects ko1 = new KeyedObjects();
  210. ko1.addObject("Key 1", "Object 1");
  211. ko1.addObject("Key 2", null);
  212. ko1.addObject("Key 3", "Object 2");
  213. assertEquals(0, ko1.getIndex("Key 1"));
  214. assertEquals(1, ko1.getIndex("Key 2"));
  215. assertEquals(2, ko1.getIndex("Key 3"));
  216. // check null argument
  217. boolean pass = false;
  218. try {
  219. ko1.getIndex(null);
  220. }
  221. catch (IllegalArgumentException e) {
  222. pass = true;
  223. }
  224. assertTrue(pass);
  225. }
  226. /**
  227. * Some checks for the setObject(Comparable, Object) method.
  228. */
  229. @Test
  230. public void testSetObject() {
  231. KeyedObjects ko1 = new KeyedObjects();
  232. ko1.setObject("Key 1", "Object 1");
  233. ko1.setObject("Key 2", null);
  234. ko1.setObject("Key 3", "Object 2");
  235. assertEquals("Object 1", ko1.getObject("Key 1"));
  236. assertEquals(null, ko1.getObject("Key 2"));
  237. assertEquals("Object 2", ko1.getObject("Key 3"));
  238. // replace an existing value
  239. ko1.setObject("Key 2", "AAA");
  240. ko1.setObject("Key 3", "BBB");
  241. assertEquals("AAA", ko1.getObject("Key 2"));
  242. assertEquals("BBB", ko1.getObject("Key 3"));
  243. // try a null key - should throw an exception
  244. boolean pass = false;
  245. try {
  246. ko1.setObject(null, "XX");
  247. }
  248. catch (IllegalArgumentException e) {
  249. pass = true;
  250. }
  251. assertTrue(pass);
  252. }
  253. /**
  254. * Some checks for the removeValue() methods.
  255. */
  256. @Test
  257. public void testRemoveValue() {
  258. KeyedObjects ko1 = new KeyedObjects();
  259. ko1.setObject("Key 1", "Object 1");
  260. ko1.setObject("Key 2", null);
  261. ko1.setObject("Key 3", "Object 2");
  262. ko1.removeValue(1);
  263. assertEquals(2, ko1.getItemCount());
  264. assertEquals(1, ko1.getIndex("Key 3"));
  265. ko1.removeValue("Key 1");
  266. assertEquals(1, ko1.getItemCount());
  267. assertEquals(0, ko1.getIndex("Key 3"));
  268. // try unknown key
  269. boolean pass = false;
  270. try {
  271. ko1.removeValue("UNKNOWN");
  272. }
  273. catch (UnknownKeyException e) {
  274. pass = true;
  275. }
  276. assertTrue(pass);
  277. // try null argument
  278. pass = false;
  279. try {
  280. ko1.removeValue(null);
  281. }
  282. catch (IllegalArgumentException e) {
  283. pass = true;
  284. }
  285. assertTrue(pass);
  286. }
  287. /**
  288. * Some checks for the removeValue(int) method.
  289. */
  290. @Test
  291. public void testRemoveValueInt() {
  292. KeyedObjects ko1 = new KeyedObjects();
  293. ko1.setObject("Key 1", "Object 1");
  294. ko1.setObject("Key 2", null);
  295. ko1.setObject("Key 3", "Object 2");
  296. ko1.removeValue(1);
  297. assertEquals(2, ko1.getItemCount());
  298. assertEquals(1, ko1.getIndex("Key 3"));
  299. // try negative key index
  300. boolean pass = false;
  301. try {
  302. ko1.removeValue(-1);
  303. }
  304. catch (IndexOutOfBoundsException e) {
  305. pass = true;
  306. }
  307. assertTrue(pass);
  308. // try key index == itemCount
  309. pass = false;
  310. try {
  311. ko1.removeValue(2);
  312. }
  313. catch (IndexOutOfBoundsException e) {
  314. pass = true;
  315. }
  316. assertTrue(pass);
  317. }
  318. }