1 43 44 package org.jfree.chart.annotations.junit; 45 46 import java.awt.BasicStroke ; 47 import java.awt.Color ; 48 import java.awt.GradientPaint ; 49 import java.awt.geom.Rectangle2D ; 50 import java.io.ByteArrayInputStream ; 51 import java.io.ByteArrayOutputStream ; 52 import java.io.ObjectInput ; 53 import java.io.ObjectInputStream ; 54 import java.io.ObjectOutput ; 55 import java.io.ObjectOutputStream ; 56 57 import junit.framework.Test; 58 import junit.framework.TestCase; 59 import junit.framework.TestSuite; 60 61 import org.jfree.chart.annotations.XYShapeAnnotation; 62 63 66 public class XYShapeAnnotationTests extends TestCase { 67 68 73 public static Test suite() { 74 return new TestSuite(XYShapeAnnotationTests.class); 75 } 76 77 82 public XYShapeAnnotationTests(String name) { 83 super(name); 84 } 85 86 89 public void testEquals() { 90 91 XYShapeAnnotation a1 = new XYShapeAnnotation( 92 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 93 new BasicStroke (1.2f), Color.red, Color.blue); 94 XYShapeAnnotation a2 = new XYShapeAnnotation( 95 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 96 new BasicStroke (1.2f), Color.red, Color.blue); 97 assertTrue(a1.equals(a2)); 98 assertTrue(a2.equals(a1)); 99 100 a1 = new XYShapeAnnotation( 102 new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 103 new BasicStroke (1.2f), Color.red, Color.blue); 104 assertFalse(a1.equals(a2)); 105 a2 = new XYShapeAnnotation( 106 new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 107 new BasicStroke (1.2f), Color.red, Color.blue); 108 assertTrue(a1.equals(a2)); 109 110 a1 = new XYShapeAnnotation( 112 new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 113 new BasicStroke (2.3f), Color.red, Color.blue); 114 assertFalse(a1.equals(a2)); 115 a2 = new XYShapeAnnotation( 116 new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 117 new BasicStroke (2.3f), Color.red, Color.blue); 118 assertTrue(a1.equals(a2)); 119 120 GradientPaint gp1a = new GradientPaint (1.0f, 2.0f, Color.blue, 121 3.0f, 4.0f, Color.red); 122 GradientPaint gp1b = new GradientPaint (1.0f, 2.0f, Color.blue, 123 3.0f, 4.0f, Color.red); 124 GradientPaint gp2a = new GradientPaint (5.0f, 6.0f, Color.pink, 125 7.0f, 8.0f, Color.white); 126 GradientPaint gp2b = new GradientPaint (5.0f, 6.0f, Color.pink, 127 7.0f, 8.0f, Color.white); 128 129 a1 = new XYShapeAnnotation( 131 new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 132 new BasicStroke (2.3f), gp1a, Color.blue); 133 assertFalse(a1.equals(a2)); 134 a2 = new XYShapeAnnotation( 135 new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 136 new BasicStroke (2.3f), gp1b, Color.blue); 137 assertTrue(a1.equals(a2)); 138 139 a1 = new XYShapeAnnotation( 141 new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 142 new BasicStroke (2.3f), gp1a, gp2a); 143 assertFalse(a1.equals(a2)); 144 a2 = new XYShapeAnnotation( 145 new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 146 new BasicStroke (2.3f), gp1b, gp2b); 147 assertTrue(a1.equals(a2)); 148 } 149 150 153 public void testHashCode() { 154 XYShapeAnnotation a1 = new XYShapeAnnotation( 155 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 156 new BasicStroke (1.2f), Color.red, Color.blue); 157 XYShapeAnnotation a2 = new XYShapeAnnotation( 158 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 159 new BasicStroke (1.2f), Color.red, Color.blue); 160 assertTrue(a1.equals(a2)); 161 int h1 = a1.hashCode(); 162 int h2 = a2.hashCode(); 163 assertEquals(h1, h2); 164 } 165 166 169 public void testCloning() { 170 171 XYShapeAnnotation a1 = new XYShapeAnnotation( 172 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 173 new BasicStroke (1.2f), Color.red, Color.blue); 174 XYShapeAnnotation a2 = null; 175 try { 176 a2 = (XYShapeAnnotation) a1.clone(); 177 } 178 catch (CloneNotSupportedException e) { 179 e.printStackTrace(); 180 } 181 assertTrue(a1 != a2); 182 assertTrue(a1.getClass() == a2.getClass()); 183 assertTrue(a1.equals(a2)); 184 } 185 186 189 public void testSerialization() { 190 XYShapeAnnotation a1 = new XYShapeAnnotation( 191 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 192 new BasicStroke (1.2f), Color.red, Color.blue); 193 XYShapeAnnotation a2 = null; 194 195 try { 196 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 197 ObjectOutput out = new ObjectOutputStream (buffer); 198 out.writeObject(a1); 199 out.close(); 200 201 ObjectInput in = new ObjectInputStream ( 202 new ByteArrayInputStream (buffer.toByteArray())); 203 a2 = (XYShapeAnnotation) in.readObject(); 204 in.close(); 205 } 206 catch (Exception e) { 207 e.printStackTrace(); 208 } 209 assertEquals(a1, a2); 210 } 211 212 } 213 | Popular Tags |