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.data.time.Millisecond; 64 import org.jfree.data.time.Minute; 65 import org.jfree.data.time.Second; 66 import org.jfree.date.MonthConstants; 67 68 71 public class MillisecondTests extends TestCase { 72 73 78 public static Test suite() { 79 return new TestSuite(MillisecondTests.class); 80 } 81 82 87 public MillisecondTests(String name) { 88 super(name); 89 } 90 91 94 protected void setUp() { 95 } 97 98 103 public void testEqualsSelf() { 104 Millisecond millisecond = new Millisecond(); 105 assertTrue(millisecond.equals(millisecond)); 106 } 107 108 111 public void testEquals() { 112 Day day1 = new Day(29, MonthConstants.MARCH, 2002); 113 Hour hour1 = new Hour(15, day1); 114 Minute minute1 = new Minute(15, hour1); 115 Second second1 = new Second(34, minute1); 116 Millisecond milli1 = new Millisecond(999, second1); 117 Day day2 = new Day(29, MonthConstants.MARCH, 2002); 118 Hour hour2 = new Hour(15, day2); 119 Minute minute2 = new Minute(15, hour2); 120 Second second2 = new Second(34, minute2); 121 Millisecond milli2 = new Millisecond(999, second2); 122 assertTrue(milli1.equals(milli2)); 123 } 124 125 130 public void testDateConstructor1() { 131 132 TimeZone zone = TimeZone.getTimeZone("GMT"); 133 Millisecond m1 = new Millisecond(new Date (1016729759122L), zone); 134 Millisecond m2 = new Millisecond(new Date (1016729759123L), zone); 135 136 assertEquals(122, m1.getMillisecond()); 137 assertEquals(1016729759122L, m1.getLastMillisecond(zone)); 138 139 assertEquals(123, m2.getMillisecond()); 140 assertEquals(1016729759123L, m2.getFirstMillisecond(zone)); 141 142 } 143 144 149 public void testDateConstructor2() { 150 151 TimeZone zone = TimeZone.getTimeZone("Europe/Tallinn"); 152 Millisecond m1 = new Millisecond(new Date (1016722559122L), zone); 153 Millisecond m2 = new Millisecond(new Date (1016722559123L), zone); 154 155 assertEquals(122, m1.getMillisecond()); 156 assertEquals(1016722559122L, m1.getLastMillisecond(zone)); 157 158 assertEquals(123, m2.getMillisecond()); 159 assertEquals(1016722559123L, m2.getFirstMillisecond(zone)); 160 161 } 162 163 166 public void testSerialization() { 167 168 Millisecond m1 = new Millisecond(); 169 Millisecond m2 = null; 170 171 try { 172 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 173 ObjectOutput out = new ObjectOutputStream (buffer); 174 out.writeObject(m1); 175 out.close(); 176 177 ObjectInput in = new ObjectInputStream ( 178 new ByteArrayInputStream (buffer.toByteArray()) 179 ); 180 m2 = (Millisecond) in.readObject(); 181 in.close(); 182 } 183 catch (Exception e) { 184 System.out.println(e.toString()); 185 } 186 assertEquals(m1, m2); 187 188 } 189 190 193 public void testHashcode() { 194 Millisecond m1 = new Millisecond(599, 23, 45, 7, 9, 10, 2007); 195 Millisecond m2 = new Millisecond(599, 23, 45, 7, 9, 10, 2007); 196 assertTrue(m1.equals(m2)); 197 int hash1 = m1.hashCode(); 198 int hash2 = m2.hashCode(); 199 assertEquals(hash1, hash2); 200 } 201 202 206 public void test943985() { 207 Millisecond ms = new Millisecond(new java.util.Date (4)); 208 assertEquals(ms.getFirstMillisecond(), ms.getMiddleMillisecond()); 209 assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond()); 210 ms = new Millisecond(new java.util.Date (5)); 211 assertEquals(ms.getFirstMillisecond(), ms.getMiddleMillisecond()); 212 assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond()); 213 } 214 215 219 public void testNotCloneable() { 220 Millisecond m = new Millisecond(599, 23, 45, 7, 9, 10, 2007); 221 assertFalse(m instanceof Cloneable ); 222 } 223 224 } 225 | Popular Tags |