1 42 43 package org.jfree.data.time.junit; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 import java.util.Date ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.data.time.SimpleTimePeriod; 58 59 62 public class SimpleTimePeriodTests extends TestCase { 63 64 69 public static Test suite() { 70 return new TestSuite(SimpleTimePeriodTests.class); 71 } 72 73 78 public SimpleTimePeriodTests(String name) { 79 super(name); 80 } 81 82 85 protected void setUp() { 86 } 88 89 94 public void testEqualsSelf() { 95 SimpleTimePeriod p = new SimpleTimePeriod( 96 new Date (1000L), new Date (1001L) 97 ); 98 assertTrue(p.equals(p)); 99 } 100 101 104 public void testEquals() { 105 SimpleTimePeriod p1 = new SimpleTimePeriod( 106 new Date (1000L), new Date (1004L) 107 ); 108 SimpleTimePeriod p2 = new SimpleTimePeriod( 109 new Date (1000L), new Date (1004L) 110 ); 111 assertTrue(p1.equals(p2)); 112 assertTrue(p2.equals(p1)); 113 114 p1 = new SimpleTimePeriod(new Date (1002L), new Date (1004L)); 115 assertFalse(p1.equals(p2)); 116 p2 = new SimpleTimePeriod(new Date (1002L), new Date (1004L)); 117 assertTrue(p1.equals(p2)); 118 119 p1 = new SimpleTimePeriod(new Date (1002L), new Date (1003L)); 120 assertFalse(p1.equals(p2)); 121 p2 = new SimpleTimePeriod(new Date (1002L), new Date (1003L)); 122 assertTrue(p1.equals(p2)); 123 } 124 125 128 public void testSerialization() { 129 SimpleTimePeriod p1 = new SimpleTimePeriod( 130 new Date (1000L), new Date (1001L) 131 ); 132 SimpleTimePeriod p2 = null; 133 try { 134 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 135 ObjectOutput out = new ObjectOutputStream (buffer); 136 out.writeObject(p1); 137 out.close(); 138 ObjectInput in = new ObjectInputStream ( 139 new ByteArrayInputStream (buffer.toByteArray()) 140 ); 141 p2 = (SimpleTimePeriod) in.readObject(); 142 in.close(); 143 } 144 catch (Exception e) { 145 System.out.println(e.toString()); 146 } 147 assertEquals(p1, p2); 148 } 149 150 153 public void testHashcode() { 154 SimpleTimePeriod s1 = new SimpleTimePeriod( 155 new Date (10L), new Date (20L) 156 ); 157 SimpleTimePeriod s2 = new SimpleTimePeriod( 158 new Date (10L), new Date (20L) 159 ); 160 assertTrue(s1.equals(s2)); 161 int h1 = s1.hashCode(); 162 int h2 = s2.hashCode(); 163 assertEquals(h1, h2); 164 } 165 166 169 public void testClone() { 170 SimpleTimePeriod s1 = new SimpleTimePeriod( 171 new Date (10L), new Date (20L) 172 ); 173 assertFalse(s1 instanceof Cloneable ); 174 } 175 176 179 public void testCompareTo() { 180 SimpleTimePeriod s1 = new SimpleTimePeriod( 181 new Date (10L), new Date (20L) 182 ); 183 SimpleTimePeriod s2 = new SimpleTimePeriod( 184 new Date (10L), new Date (20L) 185 ); 186 assertEquals(0, s1.compareTo(s2)); 187 188 s1 = new SimpleTimePeriod(new Date (9L), new Date (21L)); 189 s2 = new SimpleTimePeriod(new Date (10L), new Date (20L)); 190 assertEquals(-1, s1.compareTo(s2)); 191 192 s1 = new SimpleTimePeriod(new Date (11L), new Date (19L)); 193 s2 = new SimpleTimePeriod(new Date (10L), new Date (20L)); 194 assertEquals(1, s1.compareTo(s2)); 195 196 s1 = new SimpleTimePeriod(new Date (9L), new Date (19L)); 197 s2 = new SimpleTimePeriod(new Date (10L), new Date (20L)); 198 assertEquals(-1, s1.compareTo(s2)); 199 200 s1 = new SimpleTimePeriod(new Date (11L), new Date (21)); 201 s2 = new SimpleTimePeriod(new Date (10L), new Date (20L)); 202 assertEquals(1, s1.compareTo(s2)); 203 204 s1 = new SimpleTimePeriod(new Date (10L), new Date (18)); 205 s2 = new SimpleTimePeriod(new Date (10L), new Date (20L)); 206 assertEquals(-1, s1.compareTo(s2)); 207 208 s1 = new SimpleTimePeriod(new Date (10L), new Date (22)); 209 s2 = new SimpleTimePeriod(new Date (10L), new Date (20L)); 210 assertEquals(1, s1.compareTo(s2)); 211 } 212 213 } 214 | Popular Tags |