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.io.ByteArrayInputStream ; 49 import java.io.ByteArrayOutputStream ; 50 import java.io.ObjectInput ; 51 import java.io.ObjectInputStream ; 52 import java.io.ObjectOutput ; 53 import java.io.ObjectOutputStream ; 54 55 import junit.framework.Test; 56 import junit.framework.TestCase; 57 import junit.framework.TestSuite; 58 59 import org.jfree.chart.annotations.XYBoxAnnotation; 60 61 64 public class XYBoxAnnotationTests extends TestCase { 65 66 71 public static Test suite() { 72 return new TestSuite(XYBoxAnnotationTests.class); 73 } 74 75 80 public XYBoxAnnotationTests(String name) { 81 super(name); 82 } 83 84 87 public void testEquals() { 88 89 XYBoxAnnotation a1 = new XYBoxAnnotation( 90 1.0, 2.0, 3.0, 4.0, new BasicStroke (1.2f), Color.red, Color.blue 91 ); 92 XYBoxAnnotation a2 = new XYBoxAnnotation( 93 1.0, 2.0, 3.0, 4.0, new BasicStroke (1.2f), Color.red, Color.blue 94 ); 95 assertTrue(a1.equals(a2)); 96 assertTrue(a2.equals(a1)); 97 98 a1 = new XYBoxAnnotation( 100 2.0, 2.0, 3.0, 4.0, new BasicStroke (1.2f), Color.red, Color.blue 101 ); 102 assertFalse(a1.equals(a2)); 103 a2 = new XYBoxAnnotation( 104 2.0, 2.0, 3.0, 4.0, new BasicStroke (1.2f), Color.red, Color.blue 105 ); 106 assertTrue(a1.equals(a2)); 107 108 a1 = new XYBoxAnnotation( 110 1.0, 2.0, 3.0, 4.0, new BasicStroke (2.3f), Color.red, Color.blue 111 ); 112 assertFalse(a1.equals(a2)); 113 a2 = new XYBoxAnnotation( 114 1.0, 2.0, 3.0, 4.0, new BasicStroke (2.3f), Color.red, Color.blue 115 ); 116 assertTrue(a1.equals(a2)); 117 118 GradientPaint gp1a = new GradientPaint (1.0f, 2.0f, Color.blue, 119 3.0f, 4.0f, Color.red); 120 GradientPaint gp1b = new GradientPaint (1.0f, 2.0f, Color.blue, 121 3.0f, 4.0f, Color.red); 122 GradientPaint gp2a = new GradientPaint (5.0f, 6.0f, Color.pink, 123 7.0f, 8.0f, Color.white); 124 GradientPaint gp2b = new GradientPaint (5.0f, 6.0f, Color.pink, 125 7.0f, 8.0f, Color.white); 126 127 a1 = new XYBoxAnnotation( 129 1.0, 2.0, 3.0, 4.0, new BasicStroke (2.3f), gp1a, Color.blue 130 ); 131 assertFalse(a1.equals(a2)); 132 a2 = new XYBoxAnnotation( 133 1.0, 2.0, 3.0, 4.0, new BasicStroke (2.3f), gp1b, Color.blue 134 ); 135 assertTrue(a1.equals(a2)); 136 137 a1 = new XYBoxAnnotation( 139 1.0, 2.0, 3.0, 4.0, new BasicStroke (2.3f), gp1a, gp2a 140 ); 141 assertFalse(a1.equals(a2)); 142 a2 = new XYBoxAnnotation( 143 1.0, 2.0, 3.0, 4.0, new BasicStroke (2.3f), gp1b, gp2b 144 ); 145 assertTrue(a1.equals(a2)); 146 } 147 148 151 public void testHashCode() { 152 XYBoxAnnotation a1 = new XYBoxAnnotation( 153 1.0, 2.0, 3.0, 4.0, new BasicStroke (1.2f), Color.red, Color.blue 154 ); 155 XYBoxAnnotation a2 = new XYBoxAnnotation( 156 1.0, 2.0, 3.0, 4.0, new BasicStroke (1.2f), Color.red, Color.blue 157 ); 158 assertTrue(a1.equals(a2)); 159 int h1 = a1.hashCode(); 160 int h2 = a2.hashCode(); 161 assertEquals(h1, h2); 162 } 163 164 167 public void testCloning() { 168 169 XYBoxAnnotation a1 = new XYBoxAnnotation( 170 1.0, 2.0, 3.0, 4.0, new BasicStroke (1.2f), Color.red, Color.blue 171 ); 172 XYBoxAnnotation a2 = null; 173 try { 174 a2 = (XYBoxAnnotation) a1.clone(); 175 } 176 catch (CloneNotSupportedException e) { 177 System.err.println("Failed to clone."); 178 } 179 assertTrue(a1 != a2); 180 assertTrue(a1.getClass() == a2.getClass()); 181 assertTrue(a1.equals(a2)); 182 } 183 184 187 public void testSerialization() { 188 189 XYBoxAnnotation a1 = new XYBoxAnnotation( 190 1.0, 2.0, 3.0, 4.0, 191 new BasicStroke (1.2f), Color.red, Color.blue 192 ); 193 XYBoxAnnotation 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 ); 204 a2 = (XYBoxAnnotation) in.readObject(); 205 in.close(); 206 } 207 catch (Exception e) { 208 System.out.println(e.toString()); 209 } 210 assertEquals(a1, a2); 211 212 } 213 214 } 215 | Popular Tags |