1 42 43 package org.jfree.chart.annotations.junit; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jfree.chart.annotations.CategoryTextAnnotation; 57 import org.jfree.chart.axis.CategoryAnchor; 58 59 62 public class CategoryTextAnnotationTests extends TestCase { 63 64 69 public static Test suite() { 70 return new TestSuite(CategoryTextAnnotationTests.class); 71 } 72 73 78 public CategoryTextAnnotationTests(String name) { 79 super(name); 80 } 81 82 85 public void testEquals() { 86 87 CategoryTextAnnotation a1 = new CategoryTextAnnotation( 88 "Test", "Category", 1.0 89 ); 90 CategoryTextAnnotation a2 = new CategoryTextAnnotation( 91 "Test", "Category", 1.0 92 ); 93 assertTrue(a1.equals(a2)); 94 95 a1.setCategory("Category 2"); 97 assertFalse(a1.equals(a2)); 98 a2.setCategory("Category 2"); 99 assertTrue(a1.equals(a2)); 100 101 a1.setCategoryAnchor(CategoryAnchor.START); 103 assertFalse(a1.equals(a2)); 104 a2.setCategoryAnchor(CategoryAnchor.START); 105 assertTrue(a1.equals(a2)); 106 107 a1.setValue(0.15); 109 assertFalse(a1.equals(a2)); 110 a2.setValue(0.15); 111 assertTrue(a1.equals(a2)); 112 113 } 114 115 118 public void testHashcode() { 119 CategoryTextAnnotation a1 = new CategoryTextAnnotation( 120 "Test", "Category", 1.0 121 ); 122 CategoryTextAnnotation a2 = new CategoryTextAnnotation( 123 "Test", "Category", 1.0 124 ); 125 assertTrue(a1.equals(a2)); 126 int h1 = a1.hashCode(); 127 int h2 = a2.hashCode(); 128 assertEquals(h1, h2); 129 } 130 131 134 public void testCloning() { 135 CategoryTextAnnotation a1 = new CategoryTextAnnotation( 136 "Test", "Category", 1.0 137 ); 138 CategoryTextAnnotation a2 = null; 139 try { 140 a2 = (CategoryTextAnnotation) a1.clone(); 141 } 142 catch (CloneNotSupportedException e) { 143 System.err.println("Failed to clone."); 144 } 145 assertTrue(a1 != a2); 146 assertTrue(a1.getClass() == a2.getClass()); 147 assertTrue(a1.equals(a2)); 148 } 149 150 153 public void testSerialization() { 154 155 CategoryTextAnnotation a1 = new CategoryTextAnnotation( 156 "Test", "Category", 1.0 157 ); 158 CategoryTextAnnotation a2 = null; 159 160 try { 161 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 162 ObjectOutput out = new ObjectOutputStream (buffer); 163 out.writeObject(a1); 164 out.close(); 165 166 ObjectInput in = new ObjectInputStream ( 167 new ByteArrayInputStream (buffer.toByteArray()) 168 ); 169 a2 = (CategoryTextAnnotation) in.readObject(); 170 in.close(); 171 } 172 catch (Exception e) { 173 System.out.println(e.toString()); 174 } 175 assertEquals(a1, a2); 176 177 } 178 179 } 180 | Popular Tags |