1 46 47 package org.jfree.date.junit; 48 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 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.date.MonthConstants; 61 import org.jfree.date.SerialDate; 62 63 66 public class SerialDateTests extends TestCase { 67 68 69 private SerialDate nov9Y2001; 70 71 76 public SerialDateTests(final String name) { 77 super(name); 78 } 79 80 85 public static Test suite() { 86 return new TestSuite(SerialDateTests.class); 87 } 88 89 92 protected void setUp() { 93 this.nov9Y2001 = SerialDate.createInstance(9, MonthConstants.NOVEMBER, 2001); 94 } 95 96 99 public void testAddMonthsTo9Nov2001() { 100 final SerialDate jan9Y2002 = SerialDate.addMonths(2, this.nov9Y2001); 101 final SerialDate answer = SerialDate.createInstance(9, 1, 2002); 102 assertEquals(answer, jan9Y2002); 103 } 104 105 108 public void testAddMonthsTo5Oct2003() { 109 final SerialDate d1 = SerialDate.createInstance(5, MonthConstants.OCTOBER, 2003); 110 final SerialDate d2 = SerialDate.addMonths(2, d1); 111 assertEquals(d2, SerialDate.createInstance(5, MonthConstants.DECEMBER, 2003)); 112 } 113 114 117 public void testAddMonthsTo1Jan2003() { 118 final SerialDate d1 = SerialDate.createInstance(1, MonthConstants.JANUARY, 2003); 119 final SerialDate d2 = SerialDate.addMonths(0, d1); 120 assertEquals(d2, d1); 121 } 122 123 126 public void testMondayPrecedingFriday9Nov2001() { 127 SerialDate mondayBefore = SerialDate.getPreviousDayOfWeek( 128 SerialDate.MONDAY, this.nov9Y2001 129 ); 130 assertEquals(5, mondayBefore.getDayOfMonth()); 131 } 132 133 136 public void testMondayFollowingFriday9Nov2001() { 137 SerialDate mondayAfter = SerialDate.getFollowingDayOfWeek( 138 SerialDate.MONDAY, this.nov9Y2001 139 ); 140 assertEquals(12, mondayAfter.getDayOfMonth()); 141 } 142 143 146 public void testMondayNearestFriday9Nov2001() { 147 SerialDate mondayNearest = SerialDate.getNearestDayOfWeek( 148 SerialDate.MONDAY, this.nov9Y2001 149 ); 150 assertEquals(12, mondayNearest.getDayOfMonth()); 151 } 152 153 156 public void testMondayNearest22Jan1970() { 157 SerialDate jan22Y1970 = SerialDate.createInstance(22, MonthConstants.JANUARY, 1970); 158 SerialDate mondayNearest = SerialDate.getNearestDayOfWeek(SerialDate.MONDAY, jan22Y1970); 159 assertEquals(19, mondayNearest.getDayOfMonth()); 160 } 161 162 166 public void testWeekdayCodeToString() { 167 168 final String test = SerialDate.weekdayCodeToString(SerialDate.SATURDAY); 169 assertEquals("Saturday", test); 170 171 } 172 173 177 public void testStringToWeekday() { 178 179 int weekday = SerialDate.stringToWeekdayCode("Wednesday"); 180 assertEquals(SerialDate.WEDNESDAY, weekday); 181 182 weekday = SerialDate.stringToWeekdayCode(" Wednesday "); 183 assertEquals(SerialDate.WEDNESDAY, weekday); 184 185 weekday = SerialDate.stringToWeekdayCode("Wed"); 186 assertEquals(SerialDate.WEDNESDAY, weekday); 187 188 } 189 190 194 public void testStringToMonthCode() { 195 196 int m = SerialDate.stringToMonthCode("January"); 197 assertEquals(MonthConstants.JANUARY, m); 198 199 m = SerialDate.stringToMonthCode(" January "); 200 assertEquals(MonthConstants.JANUARY, m); 201 202 m = SerialDate.stringToMonthCode("Jan"); 203 assertEquals(MonthConstants.JANUARY, m); 204 205 } 206 207 210 public void testMonthCodeToStringCode() { 211 212 final String test = SerialDate.monthCodeToString(MonthConstants.DECEMBER); 213 assertEquals("December", test); 214 215 } 216 217 220 public void testIsNotLeapYear1900() { 221 assertTrue(!SerialDate.isLeapYear(1900)); 222 } 223 224 227 public void testIsLeapYear2000() { 228 assertTrue(SerialDate.isLeapYear(2000)); 229 } 230 231 234 public void testLeapYearCount1899() { 235 assertEquals(SerialDate.leapYearCount(1899), 0); 236 } 237 238 241 public void testLeapYearCount1903() { 242 assertEquals(SerialDate.leapYearCount(1903), 0); 243 } 244 245 248 public void testLeapYearCount1904() { 249 assertEquals(SerialDate.leapYearCount(1904), 1); 250 } 251 252 255 public void testLeapYearCount1999() { 256 assertEquals(SerialDate.leapYearCount(1999), 24); 257 } 258 259 262 public void testLeapYearCount2000() { 263 assertEquals(SerialDate.leapYearCount(2000), 25); 264 } 265 266 269 public void testSerialization() { 270 271 SerialDate d1 = SerialDate.createInstance(15, 4, 2000); 272 SerialDate d2 = null; 273 274 try { 275 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 276 ObjectOutput out = new ObjectOutputStream (buffer); 277 out.writeObject(d1); 278 out.close(); 279 280 ObjectInput in = new ObjectInputStream (new ByteArrayInputStream (buffer.toByteArray())); 281 d2 = (SerialDate) in.readObject(); 282 in.close(); 283 } 284 catch (Exception e) { 285 System.out.println(e.toString()); 286 } 287 assertEquals(d1, d2); 288 289 } 290 291 294 public void test1096282() { 295 SerialDate d = SerialDate.createInstance(29, 2, 2004); 296 d = SerialDate.addYears(1, d); 297 SerialDate expected = SerialDate.createInstance(28, 2, 2005); 298 assertTrue(d.isOn(expected)); 299 } 300 301 304 public void testAddMonths() { 305 SerialDate d1 = SerialDate.createInstance(31, 5, 2004); 306 307 SerialDate d2 = SerialDate.addMonths(1, d1); 308 assertEquals(30, d2.getDayOfMonth()); 309 assertEquals(6, d2.getMonth()); 310 assertEquals(2004, d2.getYYYY()); 311 312 SerialDate d3 = SerialDate.addMonths(2, d1); 313 assertEquals(31, d3.getDayOfMonth()); 314 assertEquals(7, d3.getMonth()); 315 assertEquals(2004, d3.getYYYY()); 316 317 SerialDate d4 = SerialDate.addMonths(1, SerialDate.addMonths(1, d1)); 318 assertEquals(30, d4.getDayOfMonth()); 319 assertEquals(7, d4.getMonth()); 320 assertEquals(2004, d4.getYYYY()); 321 } 322 } 323 | Popular Tags |