1 43 44 package org.jfree.chart.annotations.junit; 45 46 import java.awt.Graphics2D ; 47 import java.awt.geom.Rectangle2D ; 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 import java.io.Serializable ; 55 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.chart.annotations.XYDrawableAnnotation; 61 import org.jfree.ui.Drawable; 62 63 66 public class XYDrawableAnnotationTests extends TestCase { 67 68 static class TestDrawable implements Drawable, Cloneable , Serializable { 69 72 public TestDrawable() { 73 } 74 79 public void draw(Graphics2D g2, Rectangle2D area) { 80 } 82 87 public boolean equals(Object obj) { 88 if (obj == this) { 89 return true; 90 } 91 if (!(obj instanceof TestDrawable)) { 92 return false; 93 } 94 return true; 95 } 96 } 97 98 103 public static Test suite() { 104 return new TestSuite(XYDrawableAnnotationTests.class); 105 } 106 107 112 public XYDrawableAnnotationTests(String name) { 113 super(name); 114 } 115 116 119 public void testEquals() { 120 XYDrawableAnnotation a1 = new XYDrawableAnnotation( 121 10.0, 20.0, 100.0, 200.0, new TestDrawable() 122 ); 123 XYDrawableAnnotation a2 = new XYDrawableAnnotation( 124 10.0, 20.0, 100.0, 200.0, new TestDrawable() 125 ); 126 assertTrue(a1.equals(a2)); 127 } 128 129 132 public void testHashCode() { 133 XYDrawableAnnotation a1 = new XYDrawableAnnotation( 134 10.0, 20.0, 100.0, 200.0, new TestDrawable() 135 ); 136 XYDrawableAnnotation a2 = new XYDrawableAnnotation( 137 10.0, 20.0, 100.0, 200.0, new TestDrawable() 138 ); 139 assertTrue(a1.equals(a2)); 140 int h1 = a1.hashCode(); 141 int h2 = a2.hashCode(); 142 assertEquals(h1, h2); 143 } 144 145 148 public void testCloning() { 149 XYDrawableAnnotation a1 = new XYDrawableAnnotation( 150 10.0, 20.0, 100.0, 200.0, new TestDrawable() 151 ); 152 XYDrawableAnnotation a2 = null; 153 try { 154 a2 = (XYDrawableAnnotation) a1.clone(); 155 } 156 catch (CloneNotSupportedException e) { 157 System.err.println("Failed to clone."); 158 } 159 assertTrue(a1 != a2); 160 assertTrue(a1.getClass() == a2.getClass()); 161 assertTrue(a1.equals(a2)); 162 } 163 164 167 public void testSerialization() { 168 169 XYDrawableAnnotation a1 = new XYDrawableAnnotation( 170 10.0, 20.0, 100.0, 200.0, new TestDrawable() 171 ); 172 XYDrawableAnnotation a2 = null; 173 174 try { 175 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 176 ObjectOutput out = new ObjectOutputStream (buffer); 177 out.writeObject(a1); 178 out.close(); 179 180 ObjectInput in = new ObjectInputStream ( 181 new ByteArrayInputStream (buffer.toByteArray()) 182 ); 183 a2 = (XYDrawableAnnotation) in.readObject(); 184 in.close(); 185 } 186 catch (Exception e) { 187 System.out.println(e.toString()); 188 } 189 assertEquals(a1, a2); 190 191 } 192 193 } 194 | Popular Tags |