1 42 43 package org.jfree.chart.annotations.junit; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutput ; 52 import java.io.ObjectOutputStream ; 53 54 import junit.framework.Test; 55 import junit.framework.TestCase; 56 import junit.framework.TestSuite; 57 58 import org.jfree.chart.annotations.CategoryLineAnnotation; 59 60 63 public class CategoryLineAnnotationTests extends TestCase { 64 65 70 public static Test suite() { 71 return new TestSuite(CategoryLineAnnotationTests.class); 72 } 73 74 79 public CategoryLineAnnotationTests(String name) { 80 super(name); 81 } 82 83 86 public void testEquals() { 87 88 BasicStroke s1 = new BasicStroke (1.0f); 89 BasicStroke s2 = new BasicStroke (2.0f); 90 CategoryLineAnnotation a1 = new CategoryLineAnnotation( 91 "Category 1", 1.0, "Category 2", 2.0, Color.red, s1); 92 CategoryLineAnnotation a2 = new CategoryLineAnnotation( 93 "Category 1", 1.0, "Category 2", 2.0, Color.red, s1); 94 assertTrue(a1.equals(a2)); 95 assertTrue(a2.equals(a1)); 96 97 a1.setCategory1("Category A"); 99 assertFalse(a1.equals(a2)); 100 a2.setCategory1("Category A"); 101 assertTrue(a1.equals(a2)); 102 103 a1.setValue1(0.15); 105 assertFalse(a1.equals(a2)); 106 a2.setValue1(0.15); 107 assertTrue(a1.equals(a2)); 108 109 a1.setCategory2("Category B"); 111 assertFalse(a1.equals(a2)); 112 a2.setCategory2("Category B"); 113 assertTrue(a1.equals(a2)); 114 115 a1.setValue2(0.25); 117 assertFalse(a1.equals(a2)); 118 a2.setValue2(0.25); 119 assertTrue(a1.equals(a2)); 120 121 a1.setPaint(Color.yellow); 123 assertFalse(a1.equals(a2)); 124 a2.setPaint(Color.yellow); 125 assertTrue(a1.equals(a2)); 126 127 a1.setStroke(s2); 129 assertFalse(a1.equals(a2)); 130 a2.setStroke(s2); 131 assertTrue(a1.equals(a2)); 132 } 133 134 137 public void testHashcode() { 138 CategoryLineAnnotation a1 = new CategoryLineAnnotation( 139 "Category 1", 1.0, "Category 2", 2.0, Color.red, 140 new BasicStroke (1.0f)); 141 CategoryLineAnnotation a2 = new CategoryLineAnnotation( 142 "Category 1", 1.0, "Category 2", 2.0, Color.red, 143 new BasicStroke (1.0f)); 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 CategoryLineAnnotation a1 = new CategoryLineAnnotation( 155 "Category 1", 1.0, "Category 2", 2.0, Color.red, 156 new BasicStroke (1.0f)); 157 CategoryLineAnnotation a2 = null; 158 try { 159 a2 = (CategoryLineAnnotation) 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 CategoryLineAnnotation a1 = new CategoryLineAnnotation( 175 "Category 1", 1.0, "Category 2", 2.0, Color.red, 176 new BasicStroke (1.0f)); 177 CategoryLineAnnotation 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 = (CategoryLineAnnotation) 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 |