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.util.Date ; 58 import java.util.TimeZone ; 59 60 import junit.framework.Test; 61 import junit.framework.TestCase; 62 import junit.framework.TestSuite; 63 64 import org.jfree.data.time.Month; 65 import org.jfree.data.time.TimePeriodFormatException; 66 import org.jfree.date.MonthConstants; 67 68 71 public class MonthTests extends TestCase { 72 73 74 private Month jan1900; 75 76 77 private Month feb1900; 78 79 80 private Month nov9999; 81 82 83 private Month dec9999; 84 85 90 public static Test suite() { 91 return new TestSuite(MonthTests.class); 92 } 93 94 99 public MonthTests(String name) { 100 super(name); 101 } 102 103 106 protected void setUp() { 107 this.jan1900 = new Month(MonthConstants.JANUARY, 1900); 108 this.feb1900 = new Month(MonthConstants.FEBRUARY, 1900); 109 this.nov9999 = new Month(MonthConstants.NOVEMBER, 9999); 110 this.dec9999 = new Month(MonthConstants.DECEMBER, 9999); 111 } 112 113 118 public void testEqualsSelf() { 119 Month month = new Month(); 120 assertTrue(month.equals(month)); 121 } 122 123 126 public void testEquals() { 127 Month m1 = new Month(MonthConstants.MAY, 2002); 128 Month m2 = new Month(MonthConstants.MAY, 2002); 129 assertTrue(m1.equals(m2)); 130 } 131 132 136 public void testDateConstructor1() { 137 138 TimeZone zone = TimeZone.getTimeZone("GMT"); 139 Month m1 = new Month(new Date (951868799999L), zone); 140 Month m2 = new Month(new Date (951868800000L), zone); 141 142 assertEquals(MonthConstants.FEBRUARY, m1.getMonth()); 143 assertEquals(951868799999L, m1.getLastMillisecond(zone)); 144 145 assertEquals(MonthConstants.MARCH, m2.getMonth()); 146 assertEquals(951868800000L, m2.getFirstMillisecond(zone)); 147 148 } 149 150 154 public void testDateConstructor2() { 155 156 TimeZone zone = TimeZone.getTimeZone("Pacific/Auckland"); 157 Month m1 = new Month(new Date (951821999999L), zone); 158 Month m2 = new Month(new Date (951822000000L), zone); 159 160 assertEquals(MonthConstants.FEBRUARY, m1.getMonth()); 161 assertEquals(951821999999L, m1.getLastMillisecond(zone)); 162 163 assertEquals(MonthConstants.MARCH, m2.getMonth()); 164 assertEquals(951822000000L, m2.getFirstMillisecond(zone)); 165 166 } 167 168 172 public void testJan1900Previous() { 173 Month previous = (Month) this.jan1900.previous(); 174 assertNull(previous); 175 } 176 177 181 public void testJan1900Next() { 182 Month next = (Month) this.jan1900.next(); 183 assertEquals(this.feb1900, next); 184 } 185 186 190 public void testDec9999Previous() { 191 Month previous = (Month) this.dec9999.previous(); 192 assertEquals(this.nov9999, previous); 193 } 194 195 199 public void testDec9999Next() { 200 Month next = (Month) this.dec9999.next(); 201 assertNull(next); 202 } 203 204 207 public void testParseMonth() { 208 209 Month month = null; 210 211 try { 213 month = Month.parseMonth("1990-01"); 214 } 215 catch (TimePeriodFormatException e) { 216 month = new Month(1, 1900); 217 } 218 assertEquals(1, month.getMonth()); 219 assertEquals(1990, month.getYear().getYear()); 220 221 try { 223 month = Month.parseMonth("02-1991"); 224 } 225 catch (TimePeriodFormatException e) { 226 month = new Month(1, 1900); 227 } 228 assertEquals(2, month.getMonth()); 229 assertEquals(1991, month.getYear().getYear()); 230 231 try { 233 month = Month.parseMonth("March 1993"); 234 } 235 catch (TimePeriodFormatException e) { 236 month = new Month(1, 1900); 237 } 238 assertEquals(3, month.getMonth()); 239 assertEquals(1993, month.getYear().getYear()); 240 241 } 242 243 246 public void testSerialization() { 247 248 Month m1 = new Month(12, 1999); 249 Month m2 = null; 250 251 try { 252 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 253 ObjectOutput out = new ObjectOutputStream (buffer); 254 out.writeObject(m1); 255 out.close(); 256 257 ObjectInput in = new ObjectInputStream ( 258 new ByteArrayInputStream (buffer.toByteArray()) 259 ); 260 m2 = (Month) in.readObject(); 261 in.close(); 262 } 263 catch (Exception e) { 264 System.out.println(e.toString()); 265 } 266 assertEquals(m1, m2); 267 268 } 269 270 273 public void testHashcode() { 274 Month m1 = new Month(2, 2003); 275 Month m2 = new Month(2, 2003); 276 assertTrue(m1.equals(m2)); 277 int h1 = m1.hashCode(); 278 int h2 = m2.hashCode(); 279 assertEquals(h1, h2); 280 } 281 282 285 public void testNotCloneable() { 286 Month m = new Month(2, 2003); 287 assertFalse(m instanceof Cloneable ); 288 } 289 290 } 291 | Popular Tags |