1 43 44 package org.jfree.chart.axis.junit; 45 46 import java.awt.BasicStroke ; 47 import java.awt.Color ; 48 import java.awt.Stroke ; 49 import java.io.ByteArrayInputStream ; 50 import java.io.ByteArrayOutputStream ; 51 import java.io.ObjectInput ; 52 import java.io.ObjectInputStream ; 53 import java.io.ObjectOutput ; 54 import java.io.ObjectOutputStream ; 55 import java.text.SimpleDateFormat ; 56 import java.util.SimpleTimeZone ; 57 import java.util.TimeZone ; 58 59 import junit.framework.Test; 60 import junit.framework.TestCase; 61 import junit.framework.TestSuite; 62 63 import org.jfree.chart.axis.PeriodAxis; 64 import org.jfree.chart.axis.PeriodAxisLabelInfo; 65 import org.jfree.data.time.Day; 66 import org.jfree.data.time.Minute; 67 import org.jfree.data.time.Month; 68 import org.jfree.data.time.Quarter; 69 import org.jfree.data.time.Second; 70 import org.jfree.data.time.Year; 71 72 75 public class PeriodAxisTests extends TestCase { 76 77 82 public static Test suite() { 83 return new TestSuite(PeriodAxisTests.class); 84 } 85 86 91 public PeriodAxisTests(String name) { 92 super(name); 93 } 94 95 98 public void testEquals() { 99 100 PeriodAxis a1 = new PeriodAxis("Test"); 101 PeriodAxis a2 = new PeriodAxis("Test"); 102 assertTrue(a1.equals(a2)); 103 assertTrue(a2.equals(a1)); 104 105 a1.setFirst(new Year(2000)); 106 assertFalse(a1.equals(a2)); 107 a2.setFirst(new Year(2000)); 108 assertTrue(a1.equals(a2)); 109 110 a1.setLast(new Year(2004)); 111 assertFalse(a1.equals(a2)); 112 a2.setLast(new Year(2004)); 113 assertTrue(a1.equals(a2)); 114 115 a1.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland")); 116 assertFalse(a1.equals(a2)); 117 a2.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland")); 118 assertTrue(a1.equals(a2)); 119 120 a1.setAutoRangeTimePeriodClass(Quarter.class); 121 assertFalse(a1.equals(a2)); 122 a2.setAutoRangeTimePeriodClass(Quarter.class); 123 assertTrue(a1.equals(a2)); 124 125 PeriodAxisLabelInfo info[] = new PeriodAxisLabelInfo[1]; 126 info[0] = new PeriodAxisLabelInfo( 127 Month.class, new SimpleDateFormat ("MMM") 128 ); 129 130 a1.setLabelInfo(info); 131 assertFalse(a1.equals(a2)); 132 a2.setLabelInfo(info); 133 assertTrue(a1.equals(a2)); 134 135 a1.setMajorTickTimePeriodClass(Minute.class); 136 assertFalse(a1.equals(a2)); 137 a2.setMajorTickTimePeriodClass(Minute.class); 138 assertTrue(a1.equals(a2)); 139 140 a1.setMinorTickMarksVisible(!a1.isMinorTickMarksVisible()); 141 assertFalse(a1.equals(a2)); 142 a2.setMinorTickMarksVisible(a1.isMinorTickMarksVisible()); 143 assertTrue(a1.equals(a2)); 144 145 a1.setMinorTickTimePeriodClass(Minute.class); 146 assertFalse(a1.equals(a2)); 147 a2.setMinorTickTimePeriodClass(Minute.class); 148 assertTrue(a1.equals(a2)); 149 150 Stroke s = new BasicStroke (1.23f); 151 a1.setMinorTickMarkStroke(s); 152 assertFalse(a1.equals(a2)); 153 a2.setMinorTickMarkStroke(s); 154 assertTrue(a1.equals(a2)); 155 156 a1.setMinorTickMarkPaint(Color.blue); 157 assertFalse(a1.equals(a2)); 158 a2.setMinorTickMarkPaint(Color.blue); 159 assertTrue(a1.equals(a2)); 160 161 } 162 163 166 public void testHashCode() { 167 PeriodAxis a1 = new PeriodAxis("Test"); 168 PeriodAxis a2 = new PeriodAxis("Test"); 169 assertTrue(a1.equals(a2)); 170 int h1 = a1.hashCode(); 171 int h2 = a2.hashCode(); 172 assertEquals(h1, h2); 173 } 174 175 178 public void testCloning() { 179 PeriodAxis a1 = new PeriodAxis("Test"); 180 PeriodAxis a2 = null; 181 try { 182 a2 = (PeriodAxis) a1.clone(); 183 } 184 catch (CloneNotSupportedException e) { 185 System.err.println("Failed to clone."); 186 } 187 assertTrue(a1 != a2); 188 assertTrue(a1.getClass() == a2.getClass()); 189 assertTrue(a1.equals(a2)); 190 191 a1.setLabel("New Label"); 193 assertFalse(a1.equals(a2)); 194 a2.setLabel("New Label"); 195 assertTrue(a1.equals(a2)); 196 197 a1.setFirst(new Year(1920)); 198 assertFalse(a1.equals(a2)); 199 a2.setFirst(new Year(1920)); 200 assertTrue(a1.equals(a2)); 201 202 a1.setLast(new Year(2020)); 203 assertFalse(a1.equals(a2)); 204 a2.setLast(new Year(2020)); 205 assertTrue(a1.equals(a2)); 206 207 PeriodAxisLabelInfo[] info = new PeriodAxisLabelInfo[2]; 208 info[0] = new PeriodAxisLabelInfo(Day.class, new SimpleDateFormat ("d")); 209 info[1] = new PeriodAxisLabelInfo( 210 Year.class, new SimpleDateFormat ("yyyy") 211 ); 212 a1.setLabelInfo(info); 213 assertFalse(a1.equals(a2)); 214 a2.setLabelInfo(info); 215 assertTrue(a1.equals(a2)); 216 217 a1.setAutoRangeTimePeriodClass(Second.class); 218 assertFalse(a1.equals(a2)); 219 a2.setAutoRangeTimePeriodClass(Second.class); 220 assertTrue(a1.equals(a2)); 221 222 a1.setTimeZone(new SimpleTimeZone (123, "Bogus")); 223 assertFalse(a1.equals(a2)); 224 a2.setTimeZone(new SimpleTimeZone (123, "Bogus")); 225 assertTrue(a1.equals(a2)); 226 227 } 228 229 232 public void testSerialization() { 233 PeriodAxis a1 = new PeriodAxis("Test Axis"); 234 PeriodAxis a2 = null; 235 try { 236 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 237 ObjectOutput out = new ObjectOutputStream (buffer); 238 out.writeObject(a1); 239 out.close(); 240 241 ObjectInput in = new ObjectInputStream ( 242 new ByteArrayInputStream (buffer.toByteArray()) 243 ); 244 a2 = (PeriodAxis) in.readObject(); 245 in.close(); 246 } 247 catch (Exception e) { 248 System.out.println(e.toString()); 249 } 250 boolean b = a1.equals(a2); 251 assertTrue(b); 252 } 253 254 } 255 | Popular Tags |