1 42 43 package org.jfree.chart.annotations.junit; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.GradientPaint ; 48 import java.awt.Stroke ; 49 import java.io.ByteArrayInputStream ; 50 import java.io.ByteArrayOutputStream ; 51 import java.io.ObjectInput ; 52 import java.io.ObjectInputStream ; 53 import java.io.ObjectOutput ; 54 import java.io.ObjectOutputStream ; 55 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.chart.annotations.XYPolygonAnnotation; 61 62 65 public class XYPolygonAnnotationTests extends TestCase { 66 67 72 public static Test suite() { 73 return new TestSuite(XYPolygonAnnotationTests.class); 74 } 75 76 81 public XYPolygonAnnotationTests(String name) { 82 super(name); 83 } 84 85 88 public void testEquals() { 89 Stroke stroke1 = new BasicStroke (2.0f); 90 Stroke stroke2 = new BasicStroke (2.5f); 91 XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0, 92 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue); 93 XYPolygonAnnotation a2 = new XYPolygonAnnotation(new double[] {1.0, 94 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue); 95 assertTrue(a1.equals(a2)); 96 assertTrue(a2.equals(a1)); 97 98 a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0, 99 6.0}, stroke1, Color.red, Color.blue); 100 assertFalse(a1.equals(a2)); 101 a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0, 102 6.0}, stroke1, Color.red, Color.blue); 103 assertTrue(a1.equals(a2)); 104 105 a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0, 106 6.0}, stroke2, Color.red, Color.blue); 107 assertFalse(a1.equals(a2)); 108 a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0, 109 6.0}, stroke2, Color.red, Color.blue); 110 assertTrue(a1.equals(a2)); 111 112 GradientPaint gp1 = new GradientPaint (1.0f, 2.0f, Color.yellow, 3.0f, 113 4.0f, Color.white); 114 GradientPaint gp2 = new GradientPaint (1.0f, 2.0f, Color.yellow, 3.0f, 115 4.0f, Color.white); 116 a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0, 117 6.0}, stroke2, gp1, Color.blue); 118 assertFalse(a1.equals(a2)); 119 a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0, 120 6.0}, stroke2, gp2, Color.blue); 121 assertTrue(a1.equals(a2)); 122 123 GradientPaint gp3 = new GradientPaint (1.0f, 2.0f, Color.green, 3.0f, 124 4.0f, Color.white); 125 GradientPaint gp4 = new GradientPaint (1.0f, 2.0f, Color.green, 3.0f, 126 4.0f, Color.white); 127 a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0, 128 6.0}, stroke2, gp1, gp3); 129 assertFalse(a1.equals(a2)); 130 a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0, 131 6.0}, stroke2, gp2, gp4); 132 assertTrue(a1.equals(a2)); 133 } 134 135 138 public void testHashCode() { 139 Stroke stroke = new BasicStroke (2.0f); 140 XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0, 141 2.0, 3.0, 4.0, 5.0, 6.0}, stroke, Color.red, Color.blue); 142 XYPolygonAnnotation a2 = new XYPolygonAnnotation(new double[] {1.0, 143 2.0, 3.0, 4.0, 5.0, 6.0}, stroke, Color.red, Color.blue); 144 assertTrue(a1.equals(a2)); 145 int h1 = a1.hashCode(); 146 int h2 = a2.hashCode(); 147 assertEquals(h1, h2); 148 } 149 150 153 public void testCloning() { 154 Stroke stroke1 = new BasicStroke (2.0f); 155 XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0, 156 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue); 157 XYPolygonAnnotation a2 = null; 158 try { 159 a2 = (XYPolygonAnnotation) a1.clone(); 160 } 161 catch (CloneNotSupportedException e) { 162 e.printStackTrace(); 163 } 164 assertTrue(a1 != a2); 165 assertTrue(a1.getClass() == a2.getClass()); 166 assertTrue(a1.equals(a2)); 167 } 168 169 172 public void testSerialization() { 173 174 Stroke stroke1 = new BasicStroke (2.0f); 175 XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0, 176 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue); 177 XYPolygonAnnotation a2 = null; 178 179 try { 180 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 181 ObjectOutput out = new ObjectOutputStream (buffer); 182 out.writeObject(a1); 183 out.close(); 184 185 ObjectInput in = new ObjectInputStream ( 186 new ByteArrayInputStream (buffer.toByteArray()) 187 ); 188 a2 = (XYPolygonAnnotation) in.readObject(); 189 in.close(); 190 } 191 catch (Exception e) { 192 e.printStackTrace(); 193 } 194 assertEquals(a1, a2); 195 196 } 197 198 } 199 | Popular Tags |