1 48 49 package org.jfree.data.time.junit; 50 51 import java.io.ByteArrayInputStream ; 52 import java.io.ByteArrayOutputStream ; 53 import java.io.ObjectInput ; 54 import java.io.ObjectInputStream ; 55 import java.io.ObjectOutput ; 56 import java.io.ObjectOutputStream ; 57 import java.text.ParseException ; 58 import java.text.SimpleDateFormat ; 59 import java.util.Date ; 60 import java.util.GregorianCalendar ; 61 import java.util.TimeZone ; 62 63 import junit.framework.Test; 64 import junit.framework.TestCase; 65 import junit.framework.TestSuite; 66 67 import org.jfree.data.time.Day; 68 import org.jfree.date.MonthConstants; 69 70 73 public class DayTests extends TestCase { 74 75 80 public static Test suite() { 81 return new TestSuite(DayTests.class); 82 } 83 84 89 public DayTests(String name) { 90 super(name); 91 } 92 93 96 protected void setUp() { 97 } 99 100 105 public void testEqualsSelf() { 106 Day day = new Day(); 107 assertTrue(day.equals(day)); 108 } 109 110 113 public void testEquals() { 114 Day day1 = new Day(29, MonthConstants.MARCH, 2002); 115 Day day2 = new Day(29, MonthConstants.MARCH, 2002); 116 assertTrue(day1.equals(day2)); 117 } 118 119 123 public void testDateConstructor1() { 124 125 TimeZone zone = TimeZone.getTimeZone("GMT"); 126 Day d1 = new Day(new Date (1078099199999L), zone); 127 Day d2 = new Day(new Date (1078099200000L), zone); 128 129 assertEquals(MonthConstants.FEBRUARY, d1.getMonth()); 130 assertEquals(1078099199999L, d1.getLastMillisecond(zone)); 131 132 assertEquals(MonthConstants.MARCH, d2.getMonth()); 133 assertEquals(1078099200000L, d2.getFirstMillisecond(zone)); 134 135 } 136 137 142 public void testDateConstructor2() { 143 144 TimeZone zone = TimeZone.getTimeZone("Europe/Helsinki"); 145 Day d1 = new Day(new Date (1078091999999L), zone); 146 Day d2 = new Day(new Date (1078092000000L), zone); 147 148 assertEquals(MonthConstants.FEBRUARY, d1.getMonth()); 149 assertEquals(1078091999999L, d1.getLastMillisecond(zone)); 150 151 assertEquals(MonthConstants.MARCH, d2.getMonth()); 152 assertEquals(1078092000000L, d2.getFirstMillisecond(zone)); 153 154 } 155 156 160 public void test1Jan1900Previous() { 161 162 Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900); 163 Day previous = (Day) jan1st1900.previous(); 164 assertNull(previous); 165 166 } 167 168 172 public void test1Jan1900Next() { 173 174 Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900); 175 Day next = (Day) jan1st1900.next(); 176 assertEquals(2, next.getDayOfMonth()); 177 178 } 179 180 184 public void test31Dec9999Previous() { 185 186 Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999); 187 Day previous = (Day) dec31st9999.previous(); 188 assertEquals(30, previous.getDayOfMonth()); 189 190 } 191 192 196 public void test31Dec9999Next() { 197 198 Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999); 199 Day next = (Day) dec31st9999.next(); 200 assertNull(next); 201 202 } 203 204 213 public void testParseDay() throws ParseException { 214 215 GregorianCalendar gc = new GregorianCalendar (2001, 12, 31); 216 SimpleDateFormat format = new SimpleDateFormat ("dd/MM/yyyy"); 217 Date reference = format.parse("31/12/2001"); 218 if (reference.equals(gc.getTime())) { 219 Day d = Day.parseDay("31/12/2001"); 221 assertEquals(37256, d.getSerialDate().toSerial()); 222 } 223 224 Day d = Day.parseDay("2001-12-31"); 226 assertEquals(37256, d.getSerialDate().toSerial()); 227 228 } 229 230 233 public void testSerialization() { 234 235 Day d1 = new Day(15, 4, 2000); 236 Day d2 = null; 237 238 try { 239 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 240 ObjectOutput out = new ObjectOutputStream (buffer); 241 out.writeObject(d1); 242 out.close(); 243 244 ObjectInput in = new ObjectInputStream ( 245 new ByteArrayInputStream (buffer.toByteArray()) 246 ); 247 d2 = (Day) in.readObject(); 248 in.close(); 249 } 250 catch (Exception e) { 251 System.out.println(e.toString()); 252 } 253 assertEquals(d1, d2); 254 255 } 256 257 260 public void testHashcode() { 261 Day d1 = new Day(1, 2, 2003); 262 Day d2 = new Day(1, 2, 2003); 263 assertTrue(d1.equals(d2)); 264 int h1 = d1.hashCode(); 265 int h2 = d2.hashCode(); 266 assertEquals(h1, h2); 267 } 268 269 272 public void testNotCloneable() { 273 Day d = new Day(1, 2, 2003); 274 assertFalse(d instanceof Cloneable ); 275 } 276 277 } 278 | Popular Tags |