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.Date ; 55 import java.util.TimeZone ; 56 57 import junit.framework.Test; 58 import junit.framework.TestCase; 59 import junit.framework.TestSuite; 60 61 import org.jfree.data.time.Day; 62 import org.jfree.data.time.Hour; 63 import org.jfree.date.MonthConstants; 64 65 68 public class HourTests extends TestCase { 69 70 75 public static Test suite() { 76 return new TestSuite(HourTests.class); 77 } 78 79 84 public HourTests(String name) { 85 super(name); 86 } 87 88 91 protected void setUp() { 92 } 94 95 100 public void testEqualsSelf() { 101 Hour hour = new Hour(); 102 assertTrue(hour.equals(hour)); 103 } 104 105 108 public void testEquals() { 109 Hour hour1 = new Hour(15, new Day(29, MonthConstants.MARCH, 2002)); 110 Hour hour2 = new Hour(15, new Day(29, MonthConstants.MARCH, 2002)); 111 assertTrue(hour1.equals(hour2)); 112 } 113 114 118 public void testDateConstructor1() { 119 120 TimeZone zone = TimeZone.getTimeZone("GMT"); 121 Hour h1 = new Hour(new Date (1014307199999L), zone); 122 Hour h2 = new Hour(new Date (1014307200000L), zone); 123 124 assertEquals(15, h1.getHour()); 125 assertEquals(1014307199999L, h1.getLastMillisecond(zone)); 126 127 assertEquals(16, h2.getHour()); 128 assertEquals(1014307200000L, h2.getFirstMillisecond(zone)); 129 130 } 131 132 136 public void testDateConstructor2() { 137 138 TimeZone zone = TimeZone.getTimeZone("Australia/Sydney"); 139 Hour h1 = new Hour(new Date (1014267599999L), zone); 140 Hour h2 = new Hour (new Date (1014267600000L), zone); 141 142 assertEquals(15, h1.getHour()); 143 assertEquals(1014267599999L, h1.getLastMillisecond(zone)); 144 145 assertEquals(16, h2.getHour()); 146 assertEquals(1014267600000L, h2.getFirstMillisecond(zone)); 147 148 } 149 150 154 public void testFirstHourPrevious() { 155 156 Hour first = new Hour(0, new Day(1, MonthConstants.JANUARY, 1900)); 157 Hour previous = (Hour) first.previous(); 158 assertNull(previous); 159 160 } 161 162 166 public void testFirstHourNext() { 167 Hour first = new Hour(0, new Day(1, MonthConstants.JANUARY, 1900)); 168 Hour next = (Hour) first.next(); 169 assertEquals(1, next.getHour()); 170 assertEquals(1900, next.getYear()); 171 } 172 173 177 public void testLastHourPrevious() { 178 179 Hour last = new Hour(23, new Day(31, MonthConstants.DECEMBER, 9999)); 180 Hour previous = (Hour) last.previous(); 181 assertEquals(22, previous.getHour()); 182 assertEquals(9999, previous.getYear()); 183 184 } 185 186 190 public void testLastHourNext() { 191 192 Hour last = new Hour(23, new Day(31, MonthConstants.DECEMBER, 9999)); 193 Hour next = (Hour) last.next(); 194 assertNull(next); 195 196 } 197 198 201 public void testParseHour() { 202 203 Hour h = Hour.parseHour("2002-01-29 13"); 205 assertEquals(13, h.getHour()); 206 207 } 208 209 212 public void testSerialization() { 213 Hour h1 = new Hour(); 214 Hour h2 = null; 215 216 try { 217 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 218 ObjectOutput out = new ObjectOutputStream (buffer); 219 out.writeObject(h1); 220 out.close(); 221 222 ObjectInput in = new ObjectInputStream ( 223 new ByteArrayInputStream (buffer.toByteArray()) 224 ); 225 h2 = (Hour) in.readObject(); 226 in.close(); 227 } 228 catch (Exception e) { 229 System.out.println(e.toString()); 230 } 231 assertEquals(h1, h2); 232 233 } 234 235 238 public void testHashcode() { 239 Hour h1 = new Hour(7, 9, 10, 1999); 240 Hour h2 = new Hour(7, 9, 10, 1999); 241 assertTrue(h1.equals(h2)); 242 int hash1 = h1.hashCode(); 243 int hash2 = h2.hashCode(); 244 assertEquals(hash1, hash2); 245 } 246 247 250 public void testNotCloneable() { 251 Hour h = new Hour(7, 9, 10, 1999); 252 assertFalse(h instanceof Cloneable ); 253 } 254 255 } 256 | Popular Tags |