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.Day; 61 import org.jfree.data.time.Hour; 62 import org.jfree.data.time.Minute; 63 import org.jfree.data.time.Second; 64 import org.jfree.date.MonthConstants; 65 66 69 public class SecondTests extends TestCase { 70 71 76 public static Test suite() { 77 return new TestSuite(SecondTests.class); 78 } 79 80 85 public SecondTests(String name) { 86 super(name); 87 } 88 89 92 protected void setUp() { 93 } 95 96 101 public void testEqualsSelf() { 102 Second second = new Second(); 103 assertTrue(second.equals(second)); 104 } 105 106 109 public void testEquals() { 110 Day day1 = new Day(29, MonthConstants.MARCH, 2002); 111 Hour hour1 = new Hour(15, day1); 112 Minute minute1 = new Minute(15, hour1); 113 Second second1 = new Second(34, minute1); 114 Day day2 = new Day(29, MonthConstants.MARCH, 2002); 115 Hour hour2 = new Hour(15, day2); 116 Minute minute2 = new Minute(15, hour2); 117 Second second2 = new Second(34, minute2); 118 assertTrue(second1.equals(second2)); 119 } 120 121 125 public void testDateConstructor1() { 126 127 TimeZone zone = TimeZone.getTimeZone("GMT"); 128 Second s1 = new Second(new Date (1016729758999L), zone); 129 Second s2 = new Second(new Date (1016729759000L), zone); 130 131 assertEquals(58, s1.getSecond()); 132 assertEquals(1016729758999L, s1.getLastMillisecond(zone)); 133 134 assertEquals(59, s2.getSecond()); 135 assertEquals(1016729759000L, s2.getFirstMillisecond(zone)); 136 137 } 138 139 143 public void testDateConstructor2() { 144 145 TimeZone zone = TimeZone.getTimeZone("America/Chicago"); 146 Second s1 = new Second(new Date (1016751358999L), zone); 147 Second s2 = new Second(new Date (1016751359000L), zone); 148 149 assertEquals(58, s1.getSecond()); 150 assertEquals(1016751358999L, s1.getLastMillisecond(zone)); 151 152 assertEquals(59, s2.getSecond()); 153 assertEquals(1016751359000L, s2.getFirstMillisecond(zone)); 154 155 } 156 157 160 public void testSerialization() { 161 162 Second s1 = new Second(); 163 Second s2 = null; 164 165 try { 166 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 167 ObjectOutput out = new ObjectOutputStream (buffer); 168 out.writeObject(s1); 169 out.close(); 170 171 ObjectInput in = new ObjectInputStream ( 172 new ByteArrayInputStream (buffer.toByteArray()) 173 ); 174 s2 = (Second) in.readObject(); 175 in.close(); 176 } 177 catch (Exception e) { 178 System.out.println(e.toString()); 179 } 180 assertEquals(s1, s2); 181 182 } 183 184 187 public void testHashcode() { 188 Second s1 = new Second(13, 45, 5, 1, 2, 2003); 189 Second s2 = new Second(13, 45, 5, 1, 2, 2003); 190 assertTrue(s1.equals(s2)); 191 int h1 = s1.hashCode(); 192 int h2 = s2.hashCode(); 193 assertEquals(h1, h2); 194 } 195 196 200 public void testNotCloneable() { 201 Second s = new Second(13, 45, 5, 1, 2, 2003); 202 assertFalse(s instanceof Cloneable ); 203 } 204 205 } 206 | Popular Tags |