1 42 43 package org.jfree.chart.annotations.junit; 44 45 import java.awt.Color ; 46 import java.awt.Font ; 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.XYTextAnnotation; 60 import org.jfree.ui.TextAnchor; 61 62 65 public class XYTextAnnotationTests extends TestCase { 66 67 72 public static Test suite() { 73 return new TestSuite(XYTextAnnotationTests.class); 74 } 75 76 81 public XYTextAnnotationTests(String name) { 82 super(name); 83 } 84 85 88 public void testEquals() { 89 XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0); 90 XYTextAnnotation a2 = new XYTextAnnotation("Text", 10.0, 20.0); 91 assertTrue(a1.equals(a2)); 92 93 a1 = new XYTextAnnotation("ABC", 10.0, 20.0); 95 assertFalse(a1.equals(a2)); 96 a2 = new XYTextAnnotation("ABC", 10.0, 20.0); 97 assertTrue(a1.equals(a2)); 98 99 a1.setFont(new Font ("Serif", Font.PLAIN, 23)); 101 assertFalse(a1.equals(a2)); 102 a2.setFont(new Font ("Serif", Font.PLAIN, 23)); 103 assertTrue(a1.equals(a2)); 104 105 GradientPaint gp1 = new GradientPaint ( 107 1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow 108 ); 109 GradientPaint gp2 = new GradientPaint ( 110 1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow 111 ); 112 a1.setPaint(gp1); 113 assertFalse(a1.equals(a2)); 114 a2.setPaint(gp2); 115 assertTrue(a1.equals(a2)); 116 117 a1.setRotationAnchor(TextAnchor.BASELINE_RIGHT); 119 assertFalse(a1.equals(a2)); 120 a2.setRotationAnchor(TextAnchor.BASELINE_RIGHT); 121 assertTrue(a1.equals(a2)); 122 123 a1.setRotationAngle(12.3); 125 assertFalse(a1.equals(a2)); 126 a2.setRotationAngle(12.3); 127 assertTrue(a1.equals(a2)); 128 129 a1.setTextAnchor(TextAnchor.BASELINE_RIGHT); 131 assertFalse(a1.equals(a2)); 132 a2.setTextAnchor(TextAnchor.BASELINE_RIGHT); 133 assertTrue(a1.equals(a2)); 134 } 135 136 139 public void testHashCode() { 140 XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0); 141 XYTextAnnotation a2 = new XYTextAnnotation("Text", 10.0, 20.0); 142 assertTrue(a1.equals(a2)); 143 int h1 = a1.hashCode(); 144 int h2 = a2.hashCode(); 145 assertEquals(h1, h2); 146 } 147 148 151 public void testCloning() { 152 XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0); 153 XYTextAnnotation a2 = null; 154 try { 155 a2 = (XYTextAnnotation) a1.clone(); 156 } 157 catch (CloneNotSupportedException e) { 158 System.err.println("Failed to clone."); 159 } 160 assertTrue(a1 != a2); 161 assertTrue(a1.getClass() == a2.getClass()); 162 assertTrue(a1.equals(a2)); 163 } 164 165 168 public void testSerialization() { 169 170 XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0); 171 XYTextAnnotation a2 = null; 172 173 try { 174 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 175 ObjectOutput out = new ObjectOutputStream (buffer); 176 out.writeObject(a1); 177 out.close(); 178 179 ObjectInput in = new ObjectInputStream ( 180 new ByteArrayInputStream (buffer.toByteArray()) 181 ); 182 a2 = (XYTextAnnotation) in.readObject(); 183 in.close(); 184 } 185 catch (Exception e) { 186 System.out.println(e.toString()); 187 } 188 assertEquals(a1, a2); 189 190 } 191 192 } 193 | Popular Tags |