1 42 43 package org.jfree.chart.axis.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 import java.util.Date ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.chart.axis.DateTick; 58 import org.jfree.ui.TextAnchor; 59 60 63 public class DateTickTests extends TestCase { 64 65 70 public static Test suite() { 71 return new TestSuite(DateTickTests.class); 72 } 73 74 79 public DateTickTests(String name) { 80 super(name); 81 } 82 83 86 public void testEquals() { 87 88 Date d1 = new Date (0L); 89 Date d2 = new Date (1L); 90 String l1 = "Label 1"; 91 String l2 = "Label 2"; 92 TextAnchor ta1 = TextAnchor.CENTER; 93 TextAnchor ta2 = TextAnchor.BASELINE_LEFT; 94 95 DateTick t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0); 96 DateTick t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0); 97 assertTrue(t1.equals(t2)); 98 99 t1 = new DateTick(d2, l1, ta1, ta1, Math.PI / 2.0); 100 assertFalse(t1.equals(t2)); 101 t2 = new DateTick(d2, l1, ta1, ta1, Math.PI / 2.0); 102 assertTrue(t1.equals(t2)); 103 104 t1 = new DateTick(d1, l2, ta1, ta1, Math.PI / 2.0); 105 assertFalse(t1.equals(t2)); 106 t2 = new DateTick(d1, l2, ta1, ta1, Math.PI / 2.0); 107 assertTrue(t1.equals(t2)); 108 109 t1 = new DateTick(d1, l1, ta2, ta1, Math.PI / 2.0); 110 assertFalse(t1.equals(t2)); 111 t2 = new DateTick(d1, l1, ta2, ta1, Math.PI / 2.0); 112 assertTrue(t1.equals(t2)); 113 114 t1 = new DateTick(d1, l1, ta1, ta2, Math.PI / 2.0); 115 assertFalse(t1.equals(t2)); 116 t2 = new DateTick(d1, l1, ta1, ta2, Math.PI / 2.0); 117 assertTrue(t1.equals(t2)); 118 119 t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 3.0); 120 assertFalse(t1.equals(t2)); 121 t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 3.0); 122 assertTrue(t1.equals(t2)); 123 124 } 125 126 129 public void testHashCode() { 130 Date d1 = new Date (0L); 131 String l1 = "Label 1"; 132 TextAnchor ta1 = TextAnchor.CENTER; 133 134 DateTick t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0); 135 DateTick t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0); 136 assertTrue(t1.equals(t2)); 137 int h1 = t1.hashCode(); 138 int h2 = t2.hashCode(); 139 assertEquals(h1, h2); 140 } 141 142 145 public void testCloning() { 146 DateTick t1 = new DateTick( 147 new Date (0L), "Label", TextAnchor.CENTER, TextAnchor.CENTER, 10.0 148 ); 149 DateTick t2 = null; 150 try { 151 t2 = (DateTick) t1.clone(); 152 } 153 catch (CloneNotSupportedException e) { 154 System.err.println("Failed to clone."); 155 } 156 assertTrue(t1 != t2); 157 assertTrue(t1.getClass() == t2.getClass()); 158 assertTrue(t1.equals(t2)); 159 } 160 161 164 public void testSerialization() { 165 166 DateTick t1 = new DateTick( 167 new Date (0L), "Label", TextAnchor.CENTER, TextAnchor.CENTER, 10.0 168 ); 169 DateTick t2 = null; 170 171 try { 172 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 173 ObjectOutput out = new ObjectOutputStream (buffer); 174 out.writeObject(t1); 175 out.close(); 176 177 ObjectInput in = new ObjectInputStream ( 178 new ByteArrayInputStream (buffer.toByteArray()) 179 ); 180 t2 = (DateTick) in.readObject(); 181 in.close(); 182 } 183 catch (Exception e) { 184 System.out.println(e.toString()); 185 } 186 assertEquals(t1, t2); 187 188 } 189 190 } 191 | Popular Tags |