1 46 47 package org.jfree.data.time.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 import java.util.Date ; 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.TimePeriodFormatException; 63 import org.jfree.data.time.Year; 64 65 68 public class YearTests extends TestCase { 69 70 75 public static Test suite() { 76 return new TestSuite(YearTests.class); 77 } 78 79 84 public YearTests(String name) { 85 super(name); 86 } 87 88 91 protected void setUp() { 92 } 94 95 100 public void testEqualsSelf() { 101 Year year = new Year(); 102 assertTrue(year.equals(year)); 103 } 104 105 108 public void testEquals() { 109 Year year1 = new Year(2002); 110 Year year2 = new Year(2002); 111 assertTrue(year1.equals(year2)); 112 } 113 114 118 public void testDateConstructor1() { 119 120 TimeZone zone = TimeZone.getTimeZone("GMT"); 121 Date d1 = new Date (1009843199999L); 122 Date d2 = new Date (1009843200000L); 123 Year y1 = new Year(d1, zone); 124 Year y2 = new Year(d2, zone); 125 126 assertEquals(2001, y1.getYear()); 127 assertEquals(1009843199999L, y1.getLastMillisecond(zone)); 128 129 assertEquals(2002, y2.getYear()); 130 assertEquals(1009843200000L, y2.getFirstMillisecond(zone)); 131 132 } 133 134 138 public void testDateConstructor2() { 139 140 TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles"); 141 Year y1 = new Year(new Date (1009871999999L), zone); 142 Year y2 = new Year(new Date (1009872000000L), zone); 143 144 assertEquals(2001, y1.getYear()); 145 assertEquals(1009871999999L, y1.getLastMillisecond(zone)); 146 147 assertEquals(2002, y2.getYear()); 148 assertEquals(1009872000000L, y2.getFirstMillisecond(zone)); 149 150 } 151 152 156 public void test1900Previous() { 157 Year current = new Year(1900); 158 Year previous = (Year) current.previous(); 159 assertNull(previous); 160 } 161 162 165 public void test1900Next() { 166 Year current = new Year(1900); 167 Year next = (Year) current.next(); 168 assertEquals(1901, next.getYear()); 169 } 170 171 175 public void test9999Previous() { 176 Year current = new Year(9999); 177 Year previous = (Year) current.previous(); 178 assertEquals(9998, previous.getYear()); 179 } 180 181 184 public void test9999Next() { 185 Year current = new Year(9999); 186 Year next = (Year) current.next(); 187 assertNull(next); 188 } 189 190 193 public void testParseYear() { 194 195 Year year = null; 196 197 try { 199 year = Year.parseYear("2000"); 200 } 201 catch (TimePeriodFormatException e) { 202 year = new Year(1900); 203 } 204 assertEquals(2000, year.getYear()); 205 206 try { 208 year = Year.parseYear(" 2001 "); 209 } 210 catch (TimePeriodFormatException e) { 211 year = new Year(1900); 212 } 213 assertEquals(2001, year.getYear()); 214 215 try { 217 year = Year.parseYear("99"); 218 } 219 catch (TimePeriodFormatException e) { 220 year = new Year(1900); 221 } 222 assertEquals(1900, year.getYear()); 223 224 } 225 226 229 public void testSerialization() { 230 231 Year y1 = new Year(1999); 232 Year y2 = null; 233 234 try { 235 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 236 ObjectOutput out = new ObjectOutputStream (buffer); 237 out.writeObject(y1); 238 out.close(); 239 240 ObjectInput in = new ObjectInputStream ( 241 new ByteArrayInputStream (buffer.toByteArray()) 242 ); 243 y2 = (Year) in.readObject(); 244 in.close(); 245 } 246 catch (Exception e) { 247 System.out.println(e.toString()); 248 } 249 assertEquals(y1, y2); 250 251 } 252 253 256 public void testNotCloneable() { 257 Year y = new Year(1999); 258 assertFalse(y instanceof Cloneable ); 259 } 260 261 264 public void testHashcode() { 265 Year y1 = new Year(1988); 266 Year y2 = new Year(1988); 267 assertTrue(y1.equals(y2)); 268 int h1 = y1.hashCode(); 269 int h2 = y2.hashCode(); 270 assertEquals(h1, h2); 271 } 272 273 } 274 | Popular Tags |