1 43 44 package org.jfree.chart.axis.junit; 45 46 import java.io.ByteArrayInputStream ; 47 import java.io.ByteArrayOutputStream ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectInputStream ; 50 import java.io.ObjectOutput ; 51 import java.io.ObjectOutputStream ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.chart.axis.CategoryTick; 58 import org.jfree.text.TextBlock; 59 import org.jfree.text.TextBlockAnchor; 60 import org.jfree.text.TextLine; 61 import org.jfree.ui.TextAnchor; 62 63 66 public class CategoryTickTests extends TestCase { 67 68 73 public static Test suite() { 74 return new TestSuite(CategoryTickTests.class); 75 } 76 77 82 public CategoryTickTests(String name) { 83 super(name); 84 } 85 86 89 public void testEquals() { 90 91 Comparable c1 = "C1"; 92 Comparable c2 = "C2"; 93 TextBlock tb1 = new TextBlock(); 94 tb1.addLine(new TextLine("Block 1")); 95 TextBlock tb2 = new TextBlock(); 96 tb1.addLine(new TextLine("Block 2")); 97 TextBlockAnchor tba1 = TextBlockAnchor.CENTER; 98 TextBlockAnchor tba2 = TextBlockAnchor.BOTTOM_CENTER; 99 TextAnchor ta1 = TextAnchor.CENTER; 100 TextAnchor ta2 = TextAnchor.BASELINE_LEFT; 101 102 CategoryTick t1 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f); 103 CategoryTick t2 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f); 104 assertTrue(t1.equals(t2)); 105 106 t1 = new CategoryTick(c2, tb1, tba1, ta1, 1.0f); 107 assertFalse(t1.equals(t2)); 108 t2 = new CategoryTick(c2, tb1, tba1, ta1, 1.0f); 109 assertTrue(t1.equals(t2)); 110 111 t1 = new CategoryTick(c2, tb2, tba1, ta1, 1.0f); 112 assertFalse(t1.equals(t2)); 113 t2 = new CategoryTick(c2, tb2, tba1, ta1, 1.0f); 114 assertTrue(t1.equals(t2)); 115 116 t1 = new CategoryTick(c2, tb2, tba2, ta1, 1.0f); 117 assertFalse(t1.equals(t2)); 118 t2 = new CategoryTick(c2, tb2, tba2, ta1, 1.0f); 119 assertTrue(t1.equals(t2)); 120 121 t1 = new CategoryTick(c2, tb2, tba2, ta2, 1.0f); 122 assertFalse(t1.equals(t2)); 123 t2 = new CategoryTick(c2, tb2, tba2, ta2, 1.0f); 124 assertTrue(t1.equals(t2)); 125 126 t1 = new CategoryTick(c2, tb2, tba2, ta2, 2.0f); 127 assertFalse(t1.equals(t2)); 128 t2 = new CategoryTick(c2, tb2, tba2, ta2, 2.0f); 129 assertTrue(t1.equals(t2)); 130 131 } 132 133 136 public void testHashCode() { 137 Comparable c1 = "C1"; 138 TextBlock tb1 = new TextBlock(); 139 tb1.addLine(new TextLine("Block 1")); 140 tb1.addLine(new TextLine("Block 2")); 141 TextBlockAnchor tba1 = TextBlockAnchor.CENTER; 142 TextAnchor ta1 = TextAnchor.CENTER; 143 144 CategoryTick t1 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f); 145 CategoryTick t2 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f); 146 assertTrue(t1.equals(t2)); 147 int h1 = t1.hashCode(); 148 int h2 = t2.hashCode(); 149 assertEquals(h1, h2); 150 } 151 152 155 public void testCloning() { 156 CategoryTick t1 = new CategoryTick( 157 "C1", new TextBlock(), TextBlockAnchor.CENTER, 158 TextAnchor.CENTER, 1.5f 159 ); 160 CategoryTick t2 = null; 161 try { 162 t2 = (CategoryTick) t1.clone(); 163 } 164 catch (CloneNotSupportedException e) { 165 System.err.println("Failed to clone."); 166 } 167 assertTrue(t1 != t2); 168 assertTrue(t1.getClass() == t2.getClass()); 169 assertTrue(t1.equals(t2)); 170 } 171 172 175 public void testSerialization() { 176 177 CategoryTick t1 = new CategoryTick( 178 "C1", new TextBlock(), TextBlockAnchor.CENTER, 179 TextAnchor.CENTER, 1.5f 180 ); 181 CategoryTick t2 = null; 182 183 try { 184 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 185 ObjectOutput out = new ObjectOutputStream (buffer); 186 out.writeObject(t1); 187 out.close(); 188 189 ObjectInput in = new ObjectInputStream ( 190 new ByteArrayInputStream (buffer.toByteArray()) 191 ); 192 t2 = (CategoryTick) in.readObject(); 193 in.close(); 194 } 195 catch (Exception e) { 196 System.out.println(e.toString()); 197 } 198 assertEquals(t1, t2); 199 200 } 201 202 } 203 | Popular Tags |