1 44 45 package org.jfree.data.time.junit; 46 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutput ; 52 import java.io.ObjectOutputStream ; 53 import java.util.Date ; 54 import java.util.TimeZone ; 55 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.data.time.Quarter; 61 import org.jfree.data.time.TimePeriodFormatException; 62 63 66 public class QuarterTests extends TestCase { 67 68 69 private Quarter q1Y1900; 70 71 72 private Quarter q2Y1900; 73 74 75 private Quarter q3Y9999; 76 77 78 private Quarter q4Y9999; 79 80 85 public static Test suite() { 86 return new TestSuite(QuarterTests.class); 87 } 88 89 94 public QuarterTests(String name) { 95 super(name); 96 } 97 98 101 protected void setUp() { 102 this.q1Y1900 = new Quarter(1, 1900); 103 this.q2Y1900 = new Quarter(2, 1900); 104 this.q3Y9999 = new Quarter(3, 9999); 105 this.q4Y9999 = new Quarter(4, 9999); 106 } 107 108 113 public void testEqualsSelf() { 114 Quarter quarter = new Quarter(); 115 assertTrue(quarter.equals(quarter)); 116 } 117 118 121 public void testEquals() { 122 Quarter q1 = new Quarter(2, 2002); 123 Quarter q2 = new Quarter(2, 2002); 124 assertTrue(q1.equals(q2)); 125 } 126 127 131 public void testDateConstructor1() { 132 133 TimeZone zone = TimeZone.getTimeZone("GMT"); 134 Quarter q1 = new Quarter(new Date (1017619199999L), zone); 135 Quarter q2 = new Quarter(new Date (1017619200000L), zone); 136 137 assertEquals(1, q1.getQuarter()); 138 assertEquals(1017619199999L, q1.getLastMillisecond(zone)); 139 140 assertEquals(2, q2.getQuarter()); 141 assertEquals(1017619200000L, q2.getFirstMillisecond(zone)); 142 143 } 144 145 149 public void testDateConstructor2() { 150 151 TimeZone zone = TimeZone.getTimeZone("Europe/Istanbul"); 152 Quarter q1 = new Quarter(new Date (1017608399999L), zone); 153 Quarter q2 = new Quarter(new Date (1017608400000L), zone); 154 155 assertEquals(1, q1.getQuarter()); 156 assertEquals(1017608399999L, q1.getLastMillisecond(zone)); 157 158 assertEquals(2, q2.getQuarter()); 159 assertEquals(1017608400000L, q2.getFirstMillisecond(zone)); 160 161 } 162 163 167 public void testQ1Y1900Previous() { 168 Quarter previous = (Quarter) this.q1Y1900.previous(); 169 assertNull(previous); 170 } 171 172 176 public void testQ1Y1900Next() { 177 Quarter next = (Quarter) this.q1Y1900.next(); 178 assertEquals(this.q2Y1900, next); 179 } 180 181 185 public void testQ4Y9999Previous() { 186 Quarter previous = (Quarter) this.q4Y9999.previous(); 187 assertEquals(this.q3Y9999, previous); 188 } 189 190 194 public void testQ4Y9999Next() { 195 Quarter next = (Quarter) this.q4Y9999.next(); 196 assertNull(next); 197 } 198 199 202 public void testParseQuarter() { 203 204 Quarter quarter = null; 205 206 try { 208 quarter = Quarter.parseQuarter("Q1-2000"); 209 } 210 catch (TimePeriodFormatException e) { 211 quarter = new Quarter(1, 1900); 212 } 213 assertEquals(1, quarter.getQuarter()); 214 assertEquals(2000, quarter.getYear().getYear()); 215 216 try { 218 quarter = Quarter.parseQuarter("2001-Q2"); 219 } 220 catch (TimePeriodFormatException e) { 221 quarter = new Quarter(1, 1900); 222 } 223 assertEquals(2, quarter.getQuarter()); 224 assertEquals(2001, quarter.getYear().getYear()); 225 226 try { 228 quarter = Quarter.parseQuarter("Q3, 2002"); 229 } 230 catch (TimePeriodFormatException e) { 231 quarter = new Quarter(1, 1900); 232 } 233 assertEquals(3, quarter.getQuarter()); 234 assertEquals(2002, quarter.getYear().getYear()); 235 236 } 237 238 241 public void testSerialization() { 242 243 Quarter q1 = new Quarter(4, 1999); 244 Quarter q2 = null; 245 246 try { 247 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 248 ObjectOutput out = new ObjectOutputStream (buffer); 249 out.writeObject(q1); 250 out.close(); 251 252 ObjectInput in = new ObjectInputStream ( 253 new ByteArrayInputStream (buffer.toByteArray()) 254 ); 255 q2 = (Quarter) in.readObject(); 256 in.close(); 257 } 258 catch (Exception e) { 259 System.out.println(e.toString()); 260 } 261 assertEquals(q1, q2); 262 263 } 264 265 268 public void testHashcode() { 269 Quarter q1 = new Quarter(2, 2003); 270 Quarter q2 = new Quarter(2, 2003); 271 assertTrue(q1.equals(q2)); 272 int h1 = q1.hashCode(); 273 int h2 = q2.hashCode(); 274 assertEquals(h1, h2); 275 } 276 277 281 public void testNotCloneable() { 282 Quarter q = new Quarter(2, 2003); 283 assertFalse(q instanceof Cloneable ); 284 } 285 286 } 287 | Popular Tags |