KeyToGroupMapTest.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. * KeyToGroupMapTests.java
  29. * -----------------------
  30. * (C) Copyright 2004-2008, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 29-Apr-2004 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.data;
  41. import org.jfree.chart.TestUtilities;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertTrue;
  45. import org.junit.Test;
  46. /**
  47. * Tests for the {@link KeyToGroupMap} class.
  48. */
  49. public class KeyToGroupMapTest {
  50. /**
  51. * Tests the mapKeyToGroup() method.
  52. */
  53. @Test
  54. public void testMapKeyToGroup() {
  55. KeyToGroupMap m1 = new KeyToGroupMap("G1");
  56. // map a key to the default group
  57. m1.mapKeyToGroup("K1", "G1");
  58. assertEquals("G1", m1.getGroup("K1"));
  59. // map a key to a new group
  60. m1.mapKeyToGroup("K2", "G2");
  61. assertEquals("G2", m1.getGroup("K2"));
  62. // clear a mapping
  63. m1.mapKeyToGroup("K2", null);
  64. assertEquals("G1", m1.getGroup("K2")); // after clearing, reverts to
  65. // default group
  66. // check handling of null key
  67. boolean pass = false;
  68. try {
  69. m1.mapKeyToGroup(null, "G1");
  70. }
  71. catch (IllegalArgumentException e) {
  72. pass = true;
  73. }
  74. assertTrue(pass);
  75. }
  76. /**
  77. * Tests that the getGroupCount() method returns the correct values under
  78. * various circumstances.
  79. */
  80. @Test
  81. public void testGroupCount() {
  82. KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  83. // a new map always has 1 group (the default group)
  84. assertEquals(1, m1.getGroupCount());
  85. // if the default group is not mapped to, it should still count towards
  86. // the group count...
  87. m1.mapKeyToGroup("C1", "G1");
  88. assertEquals(2, m1.getGroupCount());
  89. // now when the default group is mapped to, it shouldn't increase the
  90. // group count...
  91. m1.mapKeyToGroup("C2", "Default Group");
  92. assertEquals(2, m1.getGroupCount());
  93. // complicate things a little...
  94. m1.mapKeyToGroup("C3", "Default Group");
  95. m1.mapKeyToGroup("C4", "G2");
  96. m1.mapKeyToGroup("C5", "G2");
  97. m1.mapKeyToGroup("C6", "Default Group");
  98. assertEquals(3, m1.getGroupCount());
  99. // now overwrite group "G2"...
  100. m1.mapKeyToGroup("C4", "G1");
  101. m1.mapKeyToGroup("C5", "G1");
  102. assertEquals(2, m1.getGroupCount());
  103. }
  104. /**
  105. * Tests that the getKeyCount() method returns the correct values under
  106. * various circumstances.
  107. */
  108. @Test
  109. public void testKeyCount() {
  110. KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  111. // a new map always has 1 group (the default group)
  112. assertEquals(0, m1.getKeyCount("Default Group"));
  113. // simple case
  114. m1.mapKeyToGroup("K1", "G1");
  115. assertEquals(1, m1.getKeyCount("G1"));
  116. m1.mapKeyToGroup("K1", null);
  117. assertEquals(0, m1.getKeyCount("G1"));
  118. // if there is an explicit mapping to the default group, it is counted
  119. m1.mapKeyToGroup("K2", "Default Group");
  120. assertEquals(1, m1.getKeyCount("Default Group"));
  121. // complicate things a little...
  122. m1.mapKeyToGroup("K3", "Default Group");
  123. m1.mapKeyToGroup("K4", "G2");
  124. m1.mapKeyToGroup("K5", "G2");
  125. m1.mapKeyToGroup("K6", "Default Group");
  126. assertEquals(3, m1.getKeyCount("Default Group"));
  127. assertEquals(2, m1.getKeyCount("G2"));
  128. // now overwrite group "G2"...
  129. m1.mapKeyToGroup("K4", "G1");
  130. m1.mapKeyToGroup("K5", "G1");
  131. assertEquals(2, m1.getKeyCount("G1"));
  132. assertEquals(0, m1.getKeyCount("G2"));
  133. }
  134. /**
  135. * Tests the getGroupIndex() method.
  136. */
  137. @Test
  138. public void testGetGroupIndex() {
  139. KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  140. // the default group is always at index 0
  141. assertEquals(0, m1.getGroupIndex("Default Group"));
  142. // a non-existent group should return -1
  143. assertEquals(-1, m1.getGroupIndex("G3"));
  144. // indices are assigned in the order that groups are originally mapped
  145. m1.mapKeyToGroup("K3", "G3");
  146. m1.mapKeyToGroup("K1", "G1");
  147. m1.mapKeyToGroup("K2", "G2");
  148. assertEquals(1, m1.getGroupIndex("G3"));
  149. assertEquals(2, m1.getGroupIndex("G1"));
  150. assertEquals(3, m1.getGroupIndex("G2"));
  151. }
  152. /**
  153. * Tests the getGroup() method.
  154. */
  155. @Test
  156. public void testGetGroup() {
  157. KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  158. // a key that hasn't been mapped should return the default group
  159. assertEquals("Default Group", m1.getGroup("K1"));
  160. m1.mapKeyToGroup("K1", "G1");
  161. assertEquals("G1", m1.getGroup("K1"));
  162. m1.mapKeyToGroup("K1", "G2");
  163. assertEquals("G2", m1.getGroup("K1"));
  164. m1.mapKeyToGroup("K1", null);
  165. assertEquals("Default Group", m1.getGroup("K1"));
  166. // a null argument should throw an exception
  167. boolean pass = false;
  168. try {
  169. Comparable g = m1.getGroup(null);
  170. }
  171. catch (IllegalArgumentException e) {
  172. pass = true;
  173. }
  174. assertTrue(pass);
  175. }
  176. /**
  177. * Confirm that the equals method can distinguish all the required fields.
  178. */
  179. @Test
  180. public void testEquals() {
  181. KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  182. KeyToGroupMap m2 = new KeyToGroupMap("Default Group");
  183. assertTrue(m1.equals(m2));
  184. assertTrue(m2.equals(m1));
  185. m1.mapKeyToGroup("K1", "G1");
  186. assertFalse(m1.equals(m2));
  187. m2.mapKeyToGroup("K1", "G1");
  188. assertTrue(m1.equals(m2));
  189. }
  190. /**
  191. * Confirm that cloning works.
  192. */
  193. @Test
  194. public void testCloning() throws CloneNotSupportedException {
  195. KeyToGroupMap m1 = new KeyToGroupMap("Test");
  196. m1.mapKeyToGroup("K1", "G1");
  197. KeyToGroupMap m2 = (KeyToGroupMap) m1.clone();
  198. assertTrue(m1 != m2);
  199. assertTrue(m1.getClass() == m2.getClass());
  200. assertTrue(m1.equals(m2));
  201. // a small check for independence
  202. m1.mapKeyToGroup("K1", "G2");
  203. assertFalse(m1.equals(m2));
  204. m2.mapKeyToGroup("K1", "G2");
  205. assertTrue(m1.equals(m2));
  206. }
  207. /**
  208. * Serialize an instance, restore it, and check for equality.
  209. */
  210. @Test
  211. public void testSerialization() {
  212. KeyToGroupMap m1 = new KeyToGroupMap("Test");
  213. KeyToGroupMap m2 = (KeyToGroupMap) TestUtilities.serialised(m1);
  214. assertEquals(m1, m2);
  215. }
  216. }