1 42 43 package org.jfree.chart.axis.junit; 44 45 import java.awt.geom.Rectangle2D ; 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 import java.text.SimpleDateFormat ; 53 import java.util.Calendar ; 54 import java.util.Date ; 55 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.chart.axis.DateAxis; 61 import org.jfree.chart.axis.DateTickMarkPosition; 62 import org.jfree.chart.axis.DateTickUnit; 63 import org.jfree.chart.axis.SegmentedTimeline; 64 import org.jfree.data.time.DateRange; 65 import org.jfree.ui.RectangleEdge; 66 67 70 public class DateAxisTests extends TestCase { 71 72 77 public static Test suite() { 78 return new TestSuite(DateAxisTests.class); 79 } 80 81 86 public DateAxisTests(String name) { 87 super(name); 88 } 89 90 93 public void testEquals() { 94 95 DateAxis a1 = new DateAxis("Test"); 96 DateAxis a2 = new DateAxis("Test"); 97 assertTrue(a1.equals(a2)); 98 99 a1.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7)); 101 assertFalse(a1.equals(a2)); 102 a2.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7)); 103 assertTrue(a1.equals(a2)); 104 105 a1.setDateFormatOverride(new SimpleDateFormat ("yyyy")); 107 assertFalse(a1.equals(a2)); 108 a2.setDateFormatOverride(new SimpleDateFormat ("yyyy")); 109 assertTrue(a1.equals(a2)); 110 111 a1.setTickMarkPosition(DateTickMarkPosition.END); 113 assertFalse(a1.equals(a2)); 114 a2.setTickMarkPosition(DateTickMarkPosition.END); 115 assertTrue(a1.equals(a2)); 116 117 a1.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline()); 119 assertFalse(a1.equals(a2)); 120 a2.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline()); 121 assertTrue(a1.equals(a2)); 122 123 } 124 125 128 public void testHashCode() { 129 DateAxis a1 = new DateAxis("Test"); 130 DateAxis a2 = new DateAxis("Test"); 131 assertTrue(a1.equals(a2)); 132 int h1 = a1.hashCode(); 133 int h2 = a2.hashCode(); 134 assertEquals(h1, h2); 135 } 136 137 140 public void testCloning() { 141 DateAxis a1 = new DateAxis("Test"); 142 DateAxis a2 = null; 143 try { 144 a2 = (DateAxis) a1.clone(); 145 } 146 catch (CloneNotSupportedException e) { 147 System.err.println("Failed to clone."); 148 } 149 assertTrue(a1 != a2); 150 assertTrue(a1.getClass() == a2.getClass()); 151 assertTrue(a1.equals(a2)); 152 } 153 154 157 public void testSetRange() { 158 159 DateAxis axis = new DateAxis("Test Axis"); 160 Calendar calendar = Calendar.getInstance(); 161 calendar.set(1999, Calendar.JANUARY, 3); 162 Date d1 = calendar.getTime(); 163 calendar.set(1999, Calendar.JANUARY, 31); 164 Date d2 = calendar.getTime(); 165 axis.setRange(d1, d2); 166 167 DateRange range = (DateRange) axis.getRange(); 168 assertEquals(d1, range.getLowerDate()); 169 assertEquals(d2, range.getUpperDate()); 170 171 } 172 173 176 public void testSetMaximumDate() { 177 178 DateAxis axis = new DateAxis("Test Axis"); 179 Date date = new Date (); 180 axis.setMaximumDate(date); 181 assertEquals(date, axis.getMaximumDate()); 182 183 } 184 185 188 public void testSetMinimumDate() { 189 190 DateAxis axis = new DateAxis("Test Axis"); 191 Date d1 = new Date (); 192 Date d2 = new Date (d1.getTime() + 1); 193 axis.setMaximumDate(d2); 194 axis.setMinimumDate(d1); 195 assertEquals(d1, axis.getMinimumDate()); 196 197 } 198 199 208 private boolean same(double d1, double d2, double tolerance) { 209 return (Math.abs(d1 - d2) < tolerance); 210 } 211 212 215 public void testJava2DToValue() { 216 DateAxis axis = new DateAxis(); 217 axis.setRange(50.0, 100.0); 218 Rectangle2D dataArea = new Rectangle2D.Double (10.0, 50.0, 400.0, 300.0); 219 double y1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT); 220 assertTrue(same(y1, 95.8333333, 1.0)); 221 double y2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT); 222 assertTrue(same(y2, 95.8333333, 1.0)); 223 double x1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP); 224 assertTrue(same(x1, 58.125, 1.0)); 225 double x2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM); 226 assertTrue(same(x2, 58.125, 1.0)); 227 axis.setInverted(true); 228 double y3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT); 229 assertTrue(same(y3, 54.1666667, 1.0)); 230 double y4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT); 231 assertTrue(same(y4, 54.1666667, 1.0)); 232 double x3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP); 233 assertTrue(same(x3, 91.875, 1.0)); 234 double x4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM); 235 assertTrue(same(x4, 91.875, 1.0)); 236 } 237 238 241 public void testSerialization() { 242 243 DateAxis a1 = new DateAxis("Test Axis"); 244 DateAxis a2 = null; 245 246 try { 247 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 248 ObjectOutput out = new ObjectOutputStream (buffer); 249 out.writeObject(a1); 250 out.close(); 251 252 ObjectInput in = new ObjectInputStream ( 253 new ByteArrayInputStream (buffer.toByteArray()) 254 ); 255 a2 = (DateAxis) in.readObject(); 256 in.close(); 257 } 258 catch (Exception e) { 259 System.out.println(e.toString()); 260 } 261 boolean b = a1.equals(a2); 262 assertTrue(b); 263 264 } 265 266 } 267 | Popular Tags |