1 42 43 package org.jfree.chart.junit; 44 45 import java.awt.BasicStroke ; 46 import java.io.ByteArrayInputStream ; 47 import java.io.ByteArrayOutputStream ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectInputStream ; 50 import java.io.ObjectOutput ; 51 import java.io.ObjectOutputStream ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.chart.StrokeMap; 58 59 62 public class StrokeMapTests extends TestCase { 63 64 69 public static Test suite() { 70 return new TestSuite(StrokeMapTests.class); 71 } 72 73 78 public StrokeMapTests(String name) { 79 super(name); 80 } 81 82 85 public void testGetStroke() { 86 StrokeMap m1 = new StrokeMap(); 87 assertEquals(null, m1.getStroke("A")); 88 m1.put("A", new BasicStroke (1.1f)); 89 assertEquals(new BasicStroke (1.1f), m1.getStroke("A")); 90 m1.put("A", null); 91 assertEquals(null, m1.getStroke("A")); 92 93 boolean pass = false; 95 try { 96 m1.getStroke(null); 97 } 98 catch (IllegalArgumentException e) { 99 pass = true; 100 } 101 assertTrue(pass); 102 } 103 104 107 public void testPut() { 108 StrokeMap m1 = new StrokeMap(); 109 m1.put("A", new BasicStroke (1.1f)); 110 assertEquals(new BasicStroke (1.1f), m1.getStroke("A")); 111 112 boolean pass = false; 114 try { 115 m1.put(null, new BasicStroke (1.1f)); 116 } 117 catch (IllegalArgumentException e) { 118 pass = true; 119 } 120 assertTrue(pass); 121 } 122 123 126 public void testEquals() { 127 StrokeMap m1 = new StrokeMap(); 128 StrokeMap m2 = new StrokeMap(); 129 assertTrue(m1.equals(m1)); 130 assertTrue(m1.equals(m2)); 131 assertFalse(m1.equals(null)); 132 assertFalse(m1.equals("ABC")); 133 134 m1.put("K1", new BasicStroke (1.1f)); 135 assertFalse(m1.equals(m2)); 136 m2.put("K1", new BasicStroke (1.1f)); 137 assertTrue(m1.equals(m2)); 138 139 m1.put("K2", new BasicStroke (2.2f)); 140 assertFalse(m1.equals(m2)); 141 m2.put("K2", new BasicStroke (2.2f)); 142 assertTrue(m1.equals(m2)); 143 144 m1.put("K2", null); 145 assertFalse(m1.equals(m2)); 146 m2.put("K2", null); 147 assertTrue(m1.equals(m2)); 148 } 149 150 153 public void testCloning() { 154 StrokeMap m1 = new StrokeMap(); 155 StrokeMap m2 = null; 156 try { 157 m2 = (StrokeMap) m1.clone(); 158 } 159 catch (CloneNotSupportedException e) { 160 e.printStackTrace(); 161 } 162 assertTrue(m1.equals(m2)); 163 164 m1.put("K1", new BasicStroke (1.1f)); 165 m1.put("K2", new BasicStroke (2.2f)); 166 try { 167 m2 = (StrokeMap) m1.clone(); 168 } 169 catch (CloneNotSupportedException e) { 170 e.printStackTrace(); 171 } 172 assertTrue(m1.equals(m2)); 173 } 174 175 178 public void testSerialization1() { 179 StrokeMap m1 = new StrokeMap(); 180 StrokeMap m2 = null; 181 try { 182 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 183 ObjectOutput out = new ObjectOutputStream (buffer); 184 out.writeObject(m1); 185 out.close(); 186 187 ObjectInput in = new ObjectInputStream (new ByteArrayInputStream ( 188 buffer.toByteArray())); 189 m2 = (StrokeMap) in.readObject(); 190 in.close(); 191 } 192 catch (Exception e) { 193 e.printStackTrace(); 194 } 195 assertEquals(m1, m2); 196 } 197 198 201 public void testSerialization2() { 202 StrokeMap m1 = new StrokeMap(); 203 m1.put("K1", new BasicStroke (1.1f)); 204 m1.put("K2", new BasicStroke (2.2f)); 205 StrokeMap m2 = null; 206 try { 207 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 208 ObjectOutput out = new ObjectOutputStream (buffer); 209 out.writeObject(m1); 210 out.close(); 211 212 ObjectInput in = new ObjectInputStream (new ByteArrayInputStream ( 213 buffer.toByteArray())); 214 m2 = (StrokeMap) in.readObject(); 215 in.close(); 216 } 217 catch (Exception e) { 218 e.printStackTrace(); 219 } 220 assertEquals(m1, m2); 221 } 222 223 } 224 225 | Popular Tags |