1 42 43 package org.jfree.data.junit; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jfree.data.KeyToGroupMap; 57 58 61 public class KeyToGroupMapTests extends TestCase { 62 63 68 public static Test suite() { 69 return new TestSuite(KeyToGroupMapTests.class); 70 } 71 72 77 public KeyToGroupMapTests(String name) { 78 super(name); 79 } 80 81 84 public void testMapKeyToGroup() { 85 KeyToGroupMap m1 = new KeyToGroupMap("G1"); 86 87 m1.mapKeyToGroup("K1", "G1"); 89 assertEquals("G1", m1.getGroup("K1")); 90 91 m1.mapKeyToGroup("K2", "G2"); 93 assertEquals("G2", m1.getGroup("K2")); 94 95 m1.mapKeyToGroup("K2", null); 97 assertEquals("G1", m1.getGroup("K2")); 100 boolean pass = false; 102 try { 103 m1.mapKeyToGroup(null, "G1"); 104 } 105 catch (IllegalArgumentException e) { 106 pass = true; 107 } 108 assertTrue(pass); 109 } 110 111 115 public void testGroupCount() { 116 KeyToGroupMap m1 = new KeyToGroupMap("Default Group"); 117 118 assertEquals(1, m1.getGroupCount()); 120 121 m1.mapKeyToGroup("C1", "G1"); 124 assertEquals(2, m1.getGroupCount()); 125 126 m1.mapKeyToGroup("C2", "Default Group"); 129 assertEquals(2, m1.getGroupCount()); 130 131 m1.mapKeyToGroup("C3", "Default Group"); 133 m1.mapKeyToGroup("C4", "G2"); 134 m1.mapKeyToGroup("C5", "G2"); 135 m1.mapKeyToGroup("C6", "Default Group"); 136 assertEquals(3, m1.getGroupCount()); 137 138 m1.mapKeyToGroup("C4", "G1"); 140 m1.mapKeyToGroup("C5", "G1"); 141 assertEquals(2, m1.getGroupCount()); 142 } 143 144 148 public void testKeyCount() { 149 KeyToGroupMap m1 = new KeyToGroupMap("Default Group"); 150 151 assertEquals(0, m1.getKeyCount("Default Group")); 153 154 m1.mapKeyToGroup("K1", "G1"); 156 assertEquals(1, m1.getKeyCount("G1")); 157 m1.mapKeyToGroup("K1", null); 158 assertEquals(0, m1.getKeyCount("G1")); 159 160 m1.mapKeyToGroup("K2", "Default Group"); 162 assertEquals(1, m1.getKeyCount("Default Group")); 163 164 m1.mapKeyToGroup("K3", "Default Group"); 166 m1.mapKeyToGroup("K4", "G2"); 167 m1.mapKeyToGroup("K5", "G2"); 168 m1.mapKeyToGroup("K6", "Default Group"); 169 assertEquals(3, m1.getKeyCount("Default Group")); 170 assertEquals(2, m1.getKeyCount("G2")); 171 172 m1.mapKeyToGroup("K4", "G1"); 174 m1.mapKeyToGroup("K5", "G1"); 175 assertEquals(2, m1.getKeyCount("G1")); 176 assertEquals(0, m1.getKeyCount("G2")); 177 } 178 179 182 public void testGetGroupIndex() { 183 KeyToGroupMap m1 = new KeyToGroupMap("Default Group"); 184 185 assertEquals(0, m1.getGroupIndex("Default Group")); 187 188 assertEquals(-1, m1.getGroupIndex("G3")); 190 191 m1.mapKeyToGroup("K3", "G3"); 193 m1.mapKeyToGroup("K1", "G1"); 194 m1.mapKeyToGroup("K2", "G2"); 195 assertEquals(1, m1.getGroupIndex("G3")); 196 assertEquals(2, m1.getGroupIndex("G1")); 197 assertEquals(3, m1.getGroupIndex("G2")); 198 } 199 200 203 public void testGetGroup() { 204 KeyToGroupMap m1 = new KeyToGroupMap("Default Group"); 205 206 assertEquals("Default Group", m1.getGroup("K1")); 208 209 m1.mapKeyToGroup("K1", "G1"); 210 assertEquals("G1", m1.getGroup("K1")); 211 m1.mapKeyToGroup("K1", "G2"); 212 assertEquals("G2", m1.getGroup("K1")); 213 m1.mapKeyToGroup("K1", null); 214 assertEquals("Default Group", m1.getGroup("K1")); 215 216 boolean pass = false; 218 try { 219 Comparable g = m1.getGroup(null); 220 System.out.println(g); 221 } 222 catch (IllegalArgumentException e) { 223 pass = true; 224 } 225 assertTrue(pass); 226 } 227 228 231 public void testEquals() { 232 KeyToGroupMap m1 = new KeyToGroupMap("Default Group"); 233 KeyToGroupMap m2 = new KeyToGroupMap("Default Group"); 234 assertTrue(m1.equals(m2)); 235 assertTrue(m2.equals(m1)); 236 237 m1.mapKeyToGroup("K1", "G1"); 238 assertFalse(m1.equals(m2)); 239 m2.mapKeyToGroup("K1", "G1"); 240 assertTrue(m1.equals(m2)); 241 } 242 243 246 public void testCloning() { 247 KeyToGroupMap m1 = new KeyToGroupMap("Test"); 248 m1.mapKeyToGroup("K1", "G1"); 249 KeyToGroupMap m2 = null; 250 try { 251 m2 = (KeyToGroupMap) m1.clone(); 252 } 253 catch (CloneNotSupportedException e) { 254 System.err.println("Failed to clone."); 255 } 256 assertTrue(m1 != m2); 257 assertTrue(m1.getClass() == m2.getClass()); 258 assertTrue(m1.equals(m2)); 259 260 m1.mapKeyToGroup("K1", "G2"); 262 assertFalse(m1.equals(m2)); 263 m2.mapKeyToGroup("K1", "G2"); 264 assertTrue(m1.equals(m2)); 265 } 266 267 270 public void testSerialization() { 271 272 KeyToGroupMap m1 = new KeyToGroupMap("Test"); 273 KeyToGroupMap m2 = null; 274 275 try { 276 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 277 ObjectOutput out = new ObjectOutputStream (buffer); 278 out.writeObject(m1); 279 out.close(); 280 281 ObjectInput in = new ObjectInputStream ( 282 new ByteArrayInputStream (buffer.toByteArray()) 283 ); 284 m2 = (KeyToGroupMap) in.readObject(); 285 in.close(); 286 } 287 catch (Exception e) { 288 System.out.println(e.toString()); 289 } 290 assertEquals(m1, m2); 291 292 } 293 294 } 295 | Popular Tags |