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