DefaultKeyedValuesTest.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. * DefaultKeyedValuesTest.java
  29. * ---------------------------
  30. * (C) Copyright 2003-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 05-Mar-2003 : Version 1 (DG);
  38. * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
  39. * 31-Jul-2006 : Added test for new clear() method (DG);
  40. * 01-Aug-2006 : Extended testGetIndex() method (DG);
  41. * 30-Apr-2007 : Added some new tests (DG);
  42. * 03-Oct-2007 : Updated testRemoveValue() (DG);
  43. * 21-Nov-2007 : Added testGetIndex2() method (DG);
  44. *
  45. */
  46. package org.jfree.data;
  47. import java.util.List;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.junit.Assert.assertTrue;
  50. import static org.junit.Assert.assertFalse;
  51. import static org.junit.Assert.assertNull;
  52. import org.jfree.chart.TestUtilities;
  53. import org.jfree.util.SortOrder;
  54. import org.junit.Test;
  55. /**
  56. * Tests for the {@link DefaultKeyedValues} class.
  57. */
  58. public class DefaultKeyedValuesTest {
  59. /**
  60. * Checks that a new instance is empty.
  61. */
  62. @Test
  63. public void testConstructor() {
  64. DefaultKeyedValues d = new DefaultKeyedValues();
  65. assertEquals(0, d.getItemCount());
  66. }
  67. /**
  68. * Some checks for the getItemCount() method.
  69. */
  70. @Test
  71. public void testGetItemCount() {
  72. DefaultKeyedValues d = new DefaultKeyedValues();
  73. assertEquals(0, d.getItemCount());
  74. d.addValue("A", 1.0);
  75. assertEquals(1, d.getItemCount());
  76. d.addValue("B", 2.0);
  77. assertEquals(2, d.getItemCount());
  78. d.clear();
  79. assertEquals(0, d.getItemCount());
  80. }
  81. /**
  82. * Some checks for the getKeys() method.
  83. */
  84. @Test
  85. public void testGetKeys() {
  86. DefaultKeyedValues d = new DefaultKeyedValues();
  87. List keys = d.getKeys();
  88. assertTrue(keys.isEmpty());
  89. d.addValue("A", 1.0);
  90. keys = d.getKeys();
  91. assertEquals(1, keys.size());
  92. assertTrue(keys.contains("A"));
  93. d.addValue("B", 2.0);
  94. keys = d.getKeys();
  95. assertEquals(2, keys.size());
  96. assertTrue(keys.contains("A"));
  97. assertTrue(keys.contains("B"));
  98. d.clear();
  99. keys = d.getKeys();
  100. assertEquals(0, keys.size());
  101. }
  102. /**
  103. * A simple test for the clear() method.
  104. */
  105. @Test
  106. public void testClear() {
  107. DefaultKeyedValues v1 = new DefaultKeyedValues();
  108. v1.addValue("A", 1.0);
  109. v1.addValue("B", 2.0);
  110. assertEquals(2, v1.getItemCount());
  111. v1.clear();
  112. assertEquals(0, v1.getItemCount());
  113. }
  114. /**
  115. * Some checks for the getValue() methods.
  116. */
  117. @Test
  118. public void testGetValue() {
  119. DefaultKeyedValues v1 = new DefaultKeyedValues();
  120. try {
  121. /* Number n = */ v1.getValue(-1);
  122. assertTrue(false);
  123. }
  124. catch (IndexOutOfBoundsException e) {
  125. // expected
  126. }
  127. try {
  128. /* Number n = */ v1.getValue(0);
  129. assertTrue(false);
  130. }
  131. catch (IndexOutOfBoundsException e) {
  132. // expected
  133. }
  134. DefaultKeyedValues v2 = new DefaultKeyedValues();
  135. v2.addValue("K1", new Integer(1));
  136. v2.addValue("K2", new Integer(2));
  137. v2.addValue("K3", new Integer(3));
  138. assertEquals(new Integer(3), v2.getValue(2));
  139. boolean pass = false;
  140. try {
  141. /* Number n = */ v2.getValue("KK");
  142. }
  143. catch (UnknownKeyException e) {
  144. pass = true;
  145. }
  146. assertTrue(pass);
  147. }
  148. /**
  149. * Some checks for the getKey() methods.
  150. */
  151. @Test
  152. public void testGetKey() {
  153. DefaultKeyedValues v1 = new DefaultKeyedValues();
  154. try {
  155. /* Comparable k = */ v1.getKey(-1);
  156. assertTrue(false);
  157. }
  158. catch (IndexOutOfBoundsException e) {
  159. // expected
  160. }
  161. try {
  162. /* Comparable k = */ v1.getKey(0);
  163. assertTrue(false);
  164. }
  165. catch (IndexOutOfBoundsException e) {
  166. // expected
  167. }
  168. DefaultKeyedValues v2 = new DefaultKeyedValues();
  169. v2.addValue("K1", new Integer(1));
  170. v2.addValue("K2", new Integer(2));
  171. v2.addValue("K3", new Integer(3));
  172. assertEquals("K2", v2.getKey(1));
  173. }
  174. /**
  175. * Some checks for the getIndex() methods.
  176. */
  177. @Test
  178. public void testGetIndex() {
  179. DefaultKeyedValues v1 = new DefaultKeyedValues();
  180. assertEquals(-1, v1.getIndex("K1"));
  181. DefaultKeyedValues v2 = new DefaultKeyedValues();
  182. v2.addValue("K1", new Integer(1));
  183. v2.addValue("K2", new Integer(2));
  184. v2.addValue("K3", new Integer(3));
  185. assertEquals(2, v2.getIndex("K3"));
  186. // try null
  187. boolean pass = false;
  188. try {
  189. v2.getIndex(null);
  190. }
  191. catch (IllegalArgumentException e) {
  192. pass = true;
  193. }
  194. assertTrue(pass);
  195. }
  196. /**
  197. * Another check for the getIndex(Comparable) method.
  198. */
  199. @Test
  200. public void testGetIndex2() {
  201. DefaultKeyedValues v = new DefaultKeyedValues();
  202. assertEquals(-1, v.getIndex("K1"));
  203. v.addValue("K1", 1.0);
  204. assertEquals(0, v.getIndex("K1"));
  205. v.removeValue("K1");
  206. assertEquals(-1, v.getIndex("K1"));
  207. }
  208. /**
  209. * Some checks for the addValue() method.
  210. */
  211. @Test
  212. public void testAddValue() {
  213. DefaultKeyedValues v1 = new DefaultKeyedValues();
  214. v1.addValue("A", 1.0);
  215. assertEquals(new Double(1.0), v1.getValue("A"));
  216. v1.addValue("B", 2.0);
  217. assertEquals(new Double(2.0), v1.getValue("B"));
  218. v1.addValue("B", 3.0);
  219. assertEquals(new Double(3.0), v1.getValue("B"));
  220. assertEquals(2, v1.getItemCount());
  221. v1.addValue("A", null);
  222. assertNull(v1.getValue("A"));
  223. assertEquals(2, v1.getItemCount());
  224. boolean pass = false;
  225. try {
  226. v1.addValue(null, 99.9);
  227. }
  228. catch (IllegalArgumentException e) {
  229. pass = true;
  230. }
  231. assertTrue(pass);
  232. }
  233. /**
  234. * Some checks for the insertValue() method.
  235. */
  236. @Test
  237. public void testInsertValue() {
  238. DefaultKeyedValues v1 = new DefaultKeyedValues();
  239. v1.insertValue(0, "A", 1.0);
  240. assertEquals(new Double(1.0), v1.getValue(0));
  241. v1.insertValue(0, "B", 2.0);
  242. assertEquals(new Double(2.0), v1.getValue(0));
  243. assertEquals(new Double(1.0), v1.getValue(1));
  244. // it's OK to use an index equal to the size of the list
  245. v1.insertValue(2, "C", 3.0);
  246. assertEquals(new Double(2.0), v1.getValue(0));
  247. assertEquals(new Double(1.0), v1.getValue(1));
  248. assertEquals(new Double(3.0), v1.getValue(2));
  249. // try replacing an existing value
  250. v1.insertValue(2, "B", 4.0);
  251. assertEquals(new Double(1.0), v1.getValue(0));
  252. assertEquals(new Double(3.0), v1.getValue(1));
  253. assertEquals(new Double(4.0), v1.getValue(2));
  254. }
  255. /**
  256. * Some checks for the clone() method.
  257. */
  258. @Test
  259. public void testCloning() throws CloneNotSupportedException {
  260. DefaultKeyedValues v1 = new DefaultKeyedValues();
  261. v1.addValue("V1", new Integer(1));
  262. v1.addValue("V2", null);
  263. v1.addValue("V3", new Integer(3));
  264. DefaultKeyedValues v2 = (DefaultKeyedValues) v1.clone();
  265. assertTrue(v1 != v2);
  266. assertTrue(v1.getClass() == v2.getClass());
  267. assertTrue(v1.equals(v2));
  268. // confirm that the clone is independent of the original
  269. v2.setValue("V1", new Integer(44));
  270. assertFalse(v1.equals(v2));
  271. }
  272. /**
  273. * Check that inserting and retrieving values works as expected.
  274. */
  275. @Test
  276. public void testInsertAndRetrieve() {
  277. DefaultKeyedValues data = new DefaultKeyedValues();
  278. data.addValue("A", new Double(1.0));
  279. data.addValue("B", new Double(2.0));
  280. data.addValue("C", new Double(3.0));
  281. data.addValue("D", null);
  282. // check key order
  283. assertEquals(data.getKey(0), "A");
  284. assertEquals(data.getKey(1), "B");
  285. assertEquals(data.getKey(2), "C");
  286. assertEquals(data.getKey(3), "D");
  287. // check retrieve value by key
  288. assertEquals(data.getValue("A"), new Double(1.0));
  289. assertEquals(data.getValue("B"), new Double(2.0));
  290. assertEquals(data.getValue("C"), new Double(3.0));
  291. assertEquals(data.getValue("D"), null);
  292. // check retrieve value by index
  293. assertEquals(data.getValue(0), new Double(1.0));
  294. assertEquals(data.getValue(1), new Double(2.0));
  295. assertEquals(data.getValue(2), new Double(3.0));
  296. assertEquals(data.getValue(3), null);
  297. }
  298. /**
  299. * Some tests for the removeValue() method.
  300. */
  301. @Test
  302. public void testRemoveValue() {
  303. DefaultKeyedValues data = new DefaultKeyedValues();
  304. data.addValue("A", new Double(1.0));
  305. data.addValue("B", null);
  306. data.addValue("C", new Double(3.0));
  307. data.addValue("D", new Double(2.0));
  308. assertEquals(1, data.getIndex("B"));
  309. data.removeValue("B");
  310. assertEquals(-1, data.getIndex("B"));
  311. boolean pass = false;
  312. try {
  313. data.removeValue("XXX");
  314. }
  315. catch (UnknownKeyException e) {
  316. pass = true;
  317. }
  318. assertTrue(pass);
  319. }
  320. /**
  321. * Tests sorting of data by key (ascending).
  322. */
  323. @Test
  324. public void testSortByKeyAscending() {
  325. DefaultKeyedValues data = new DefaultKeyedValues();
  326. data.addValue("C", new Double(1.0));
  327. data.addValue("B", null);
  328. data.addValue("D", new Double(3.0));
  329. data.addValue("A", new Double(2.0));
  330. data.sortByKeys(SortOrder.ASCENDING);
  331. // check key order
  332. assertEquals(data.getKey(0), "A");
  333. assertEquals(data.getKey(1), "B");
  334. assertEquals(data.getKey(2), "C");
  335. assertEquals(data.getKey(3), "D");
  336. // check retrieve value by key
  337. assertEquals(data.getValue("A"), new Double(2.0));
  338. assertEquals(data.getValue("B"), null);
  339. assertEquals(data.getValue("C"), new Double(1.0));
  340. assertEquals(data.getValue("D"), new Double(3.0));
  341. // check retrieve value by index
  342. assertEquals(data.getValue(0), new Double(2.0));
  343. assertEquals(data.getValue(1), null);
  344. assertEquals(data.getValue(2), new Double(1.0));
  345. assertEquals(data.getValue(3), new Double(3.0));
  346. }
  347. /**
  348. * Tests sorting of data by key (descending).
  349. */
  350. @Test
  351. public void testSortByKeyDescending() {
  352. DefaultKeyedValues data = new DefaultKeyedValues();
  353. data.addValue("C", new Double(1.0));
  354. data.addValue("B", null);
  355. data.addValue("D", new Double(3.0));
  356. data.addValue("A", new Double(2.0));
  357. data.sortByKeys(SortOrder.DESCENDING);
  358. // check key order
  359. assertEquals(data.getKey(0), "D");
  360. assertEquals(data.getKey(1), "C");
  361. assertEquals(data.getKey(2), "B");
  362. assertEquals(data.getKey(3), "A");
  363. // check retrieve value by key
  364. assertEquals(data.getValue("A"), new Double(2.0));
  365. assertEquals(data.getValue("B"), null);
  366. assertEquals(data.getValue("C"), new Double(1.0));
  367. assertEquals(data.getValue("D"), new Double(3.0));
  368. // check retrieve value by index
  369. assertEquals(data.getValue(0), new Double(3.0));
  370. assertEquals(data.getValue(1), new Double(1.0));
  371. assertEquals(data.getValue(2), null);
  372. assertEquals(data.getValue(3), new Double(2.0));
  373. }
  374. /**
  375. * Tests sorting of data by value (ascending).
  376. */
  377. @Test
  378. public void testSortByValueAscending() {
  379. DefaultKeyedValues data = new DefaultKeyedValues();
  380. data.addValue("C", new Double(1.0));
  381. data.addValue("B", null);
  382. data.addValue("D", new Double(3.0));
  383. data.addValue("A", new Double(2.0));
  384. data.sortByValues(SortOrder.ASCENDING);
  385. // check key order
  386. assertEquals(data.getKey(0), "C");
  387. assertEquals(data.getKey(1), "A");
  388. assertEquals(data.getKey(2), "D");
  389. assertEquals(data.getKey(3), "B");
  390. // check retrieve value by key
  391. assertEquals(data.getValue("A"), new Double(2.0));
  392. assertEquals(data.getValue("B"), null);
  393. assertEquals(data.getValue("C"), new Double(1.0));
  394. assertEquals(data.getValue("D"), new Double(3.0));
  395. // check retrieve value by index
  396. assertEquals(data.getValue(0), new Double(1.0));
  397. assertEquals(data.getValue(1), new Double(2.0));
  398. assertEquals(data.getValue(2), new Double(3.0));
  399. assertEquals(data.getValue(3), null);
  400. }
  401. /**
  402. * Tests sorting of data by key (descending).
  403. */
  404. @Test
  405. public void testSortByValueDescending() {
  406. DefaultKeyedValues data = new DefaultKeyedValues();
  407. data.addValue("C", new Double(1.0));
  408. data.addValue("B", null);
  409. data.addValue("D", new Double(3.0));
  410. data.addValue("A", new Double(2.0));
  411. data.sortByValues(SortOrder.DESCENDING);
  412. // check key order
  413. assertEquals(data.getKey(0), "D");
  414. assertEquals(data.getKey(1), "A");
  415. assertEquals(data.getKey(2), "C");
  416. assertEquals(data.getKey(3), "B");
  417. // check retrieve value by key
  418. assertEquals(data.getValue("A"), new Double(2.0));
  419. assertEquals(data.getValue("B"), null);
  420. assertEquals(data.getValue("C"), new Double(1.0));
  421. assertEquals(data.getValue("D"), new Double(3.0));
  422. // check retrieve value by index
  423. assertEquals(data.getValue(0), new Double(3.0));
  424. assertEquals(data.getValue(1), new Double(2.0));
  425. assertEquals(data.getValue(2), new Double(1.0));
  426. assertEquals(data.getValue(3), null);
  427. }
  428. /**
  429. * Serialize an instance, restore it, and check for equality.
  430. */
  431. @Test
  432. public void testSerialization() {
  433. DefaultKeyedValues v1 = new DefaultKeyedValues();
  434. v1.addValue("Key 1", new Double(23));
  435. v1.addValue("Key 2", null);
  436. v1.addValue("Key 3", new Double(42));
  437. DefaultKeyedValues v2 = (DefaultKeyedValues)
  438. TestUtilities.serialised(v1);
  439. assertEquals(v1, v2);
  440. }
  441. }