1 45 46 package org.jfree.data.time.junit; 47 48 import java.io.ByteArrayInputStream ; 49 import java.io.ByteArrayOutputStream ; 50 import java.io.ObjectInput ; 51 import java.io.ObjectInputStream ; 52 import java.io.ObjectOutput ; 53 import java.io.ObjectOutputStream ; 54 import java.util.Calendar ; 55 import java.util.Locale ; 56 import java.util.TimeZone ; 57 58 import junit.framework.Test; 59 import junit.framework.TestCase; 60 import junit.framework.TestSuite; 61 62 import org.jfree.data.time.Week; 63 64 67 public class WeekTests extends TestCase { 68 69 70 private Week w1Y1900; 71 72 73 private Week w2Y1900; 74 75 76 private Week w51Y9999; 77 78 79 private Week w52Y9999; 80 81 86 public static Test suite() { 87 return new TestSuite(WeekTests.class); 88 } 89 90 95 public WeekTests(String name) { 96 super(name); 97 } 98 99 102 protected void setUp() { 103 this.w1Y1900 = new Week(1, 1900); 104 this.w2Y1900 = new Week(2, 1900); 105 this.w51Y9999 = new Week(51, 9999); 106 this.w52Y9999 = new Week(52, 9999); 107 } 108 109 112 public void testEquals() { 113 Week w1 = new Week(1, 2002); 114 Week w2 = new Week(1, 2002); 115 assertTrue(w1.equals(w2)); 116 assertTrue(w2.equals(w1)); 117 118 w1 = new Week(2, 2002); 119 assertFalse(w1.equals(w2)); 120 w2 = new Week(2, 2002); 121 assertTrue(w1.equals(w2)); 122 123 w1 = new Week(2, 2003); 124 assertFalse(w1.equals(w2)); 125 w2 = new Week(2, 2003); 126 assertTrue(w1.equals(w2)); 127 } 128 129 132 public void testW1Y1900Previous() { 133 Week previous = (Week) this.w1Y1900.previous(); 134 assertNull(previous); 135 } 136 137 140 public void testW1Y1900Next() { 141 Week next = (Week) this.w1Y1900.next(); 142 assertEquals(this.w2Y1900, next); 143 } 144 145 148 public void testW52Y9999Previous() { 149 Week previous = (Week) this.w52Y9999.previous(); 150 assertEquals(this.w51Y9999, previous); 151 } 152 153 156 public void testW52Y9999Next() { 157 Week next = (Week) this.w52Y9999.next(); 158 assertNull(next); 159 } 160 161 164 public void testSerialization() { 165 166 Week w1 = new Week(24, 1999); 167 Week w2 = null; 168 169 try { 170 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 171 ObjectOutput out = new ObjectOutputStream (buffer); 172 out.writeObject(w1); 173 out.close(); 174 175 ObjectInput in = new ObjectInputStream ( 176 new ByteArrayInputStream (buffer.toByteArray()) 177 ); 178 w2 = (Week) in.readObject(); 179 in.close(); 180 } 181 catch (Exception e) { 182 System.out.println(e.toString()); 183 } 184 assertEquals(w1, w2); 185 186 } 187 188 191 public void testHashcode() { 192 Week w1 = new Week(2, 2003); 193 Week w2 = new Week(2, 2003); 194 assertTrue(w1.equals(w2)); 195 int h1 = w1.hashCode(); 196 int h2 = w2.hashCode(); 197 assertEquals(h1, h2); 198 } 199 200 203 public void testNotCloneable() { 204 Week w = new Week(1, 1999); 205 assertFalse(w instanceof Cloneable ); 206 } 207 208 228 public void testWeek12005() { 229 Week w1 = new Week(1, 2005); 230 Calendar c1 = Calendar.getInstance( 231 TimeZone.getTimeZone("Europe/London"), Locale.UK 232 ); 233 c1.setMinimalDaysInFirstWeek(4); assertEquals(1104710400000L, w1.getFirstMillisecond(c1)); 235 assertEquals(1105315199999L, w1.getLastMillisecond(c1)); 236 Calendar c2 = Calendar.getInstance( 237 TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE 238 ); 239 c2.setMinimalDaysInFirstWeek(4); assertEquals(1104706800000L, w1.getFirstMillisecond(c2)); 241 assertEquals(1105311599999L, w1.getLastMillisecond(c2)); 242 Calendar c3 = Calendar.getInstance( 243 TimeZone.getTimeZone("America/New_York"), Locale.US 244 ); 245 assertEquals(1104037200000L, w1.getFirstMillisecond(c3)); 246 assertEquals(1104641999999L, w1.getLastMillisecond(c3)); 247 } 248 249 266 public void testWeek532005() { 267 Week w1 = new Week(53, 2004); 268 Calendar c1 = Calendar.getInstance( 269 TimeZone.getTimeZone("Europe/London"), Locale.UK 270 ); 271 c1.setMinimalDaysInFirstWeek(4); assertEquals(1104105600000L, w1.getFirstMillisecond(c1)); 273 assertEquals(1104710399999L, w1.getLastMillisecond(c1)); 274 Calendar c2 = Calendar.getInstance( 275 TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE 276 ); 277 c2.setMinimalDaysInFirstWeek(4); assertEquals(1104102000000L, w1.getFirstMillisecond(c2)); 279 assertEquals(1104706799999L, w1.getLastMillisecond(c2)); 280 w1 = new Week(53, 2005); 281 Calendar c3 = Calendar.getInstance( 282 TimeZone.getTimeZone("America/New_York"), Locale.US 283 ); 284 assertEquals(1135486800000L, w1.getFirstMillisecond(c3)); 285 assertEquals(1136091599999L, w1.getLastMillisecond(c3)); 286 } 287 288 } 289 | Popular Tags |