1 41 42 package org.jfree.data.time.junit; 43 44 import java.io.ByteArrayInputStream ; 45 import java.io.ByteArrayOutputStream ; 46 import java.io.ObjectInput ; 47 import java.io.ObjectInputStream ; 48 import java.io.ObjectOutput ; 49 import java.io.ObjectOutputStream ; 50 51 import junit.framework.Test; 52 import junit.framework.TestCase; 53 import junit.framework.TestSuite; 54 55 import org.jfree.data.general.SeriesException; 56 import org.jfree.data.time.Day; 57 import org.jfree.data.time.RegularTimePeriod; 58 import org.jfree.data.time.TimePeriodValues; 59 import org.jfree.data.time.Year; 60 import org.jfree.date.MonthConstants; 61 62 65 public class TimePeriodValuesTests extends TestCase { 66 67 68 private TimePeriodValues seriesA; 69 70 71 private TimePeriodValues seriesB; 72 73 74 private TimePeriodValues seriesC; 75 76 81 public static Test suite() { 82 return new TestSuite(TimePeriodValuesTests.class); 83 } 84 85 90 public TimePeriodValuesTests(String name) { 91 super(name); 92 } 93 94 97 protected void setUp() { 98 99 this.seriesA = new TimePeriodValues("Series A"); 100 try { 101 this.seriesA.add(new Year(2000), new Integer (102000)); 102 this.seriesA.add(new Year(2001), new Integer (102001)); 103 this.seriesA.add(new Year(2002), new Integer (102002)); 104 this.seriesA.add(new Year(2003), new Integer (102003)); 105 this.seriesA.add(new Year(2004), new Integer (102004)); 106 this.seriesA.add(new Year(2005), new Integer (102005)); 107 } 108 catch (SeriesException e) { 109 System.err.println("Problem creating series."); 110 } 111 112 this.seriesB = new TimePeriodValues("Series B"); 113 try { 114 this.seriesB.add(new Year(2006), new Integer (202006)); 115 this.seriesB.add(new Year(2007), new Integer (202007)); 116 this.seriesB.add(new Year(2008), new Integer (202008)); 117 } 118 catch (SeriesException e) { 119 System.err.println("Problem creating series."); 120 } 121 122 this.seriesC = new TimePeriodValues("Series C"); 123 try { 124 this.seriesC.add(new Year(1999), new Integer (301999)); 125 this.seriesC.add(new Year(2000), new Integer (302000)); 126 this.seriesC.add(new Year(2002), new Integer (302002)); 127 } 128 catch (SeriesException e) { 129 System.err.println("Problem creating series."); 130 } 131 132 } 133 134 138 public void testClone() { 139 140 TimePeriodValues series = new TimePeriodValues("Test Series"); 141 142 RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002); 143 try { 144 series.add(jan1st2002, new Integer (42)); 145 } 146 catch (SeriesException e) { 147 System.err.println("Problem adding to collection."); 148 } 149 150 TimePeriodValues clone = null; 151 try { 152 clone = (TimePeriodValues) series.clone(); 153 clone.setKey("Clone Series"); 154 try { 155 clone.update(0, new Integer (10)); 156 } 157 catch (SeriesException e) { 158 System.err.println("Problem updating series."); 159 } 160 } 161 catch (CloneNotSupportedException e) { 162 assertTrue(false); 163 } 164 165 int seriesValue = series.getValue(0).intValue(); 166 int cloneValue = clone.getValue(0).intValue(); 167 168 assertEquals(42, seriesValue); 169 assertEquals(10, cloneValue); 170 assertEquals("Test Series", series.getKey()); 171 assertEquals("Clone Series", clone.getKey()); 172 173 } 174 175 178 public void testAddValue() { 179 180 TimePeriodValues tpvs = new TimePeriodValues("Test"); 181 try { 182 tpvs.add(new Year(1999), new Integer (1)); 183 } 184 catch (SeriesException e) { 185 System.err.println("Problem adding to series."); 186 } 187 188 int value = tpvs.getValue(0).intValue(); 189 assertEquals(1, value); 190 191 } 192 193 196 public void testSerialization() { 197 198 TimePeriodValues s1 = new TimePeriodValues("A test"); 199 s1.add(new Year(2000), 13.75); 200 s1.add(new Year(2001), 11.90); 201 s1.add(new Year(2002), null); 202 s1.add(new Year(2005), 19.32); 203 s1.add(new Year(2007), 16.89); 204 TimePeriodValues s2 = null; 205 206 try { 207 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 208 ObjectOutput out = new ObjectOutputStream (buffer); 209 out.writeObject(s1); 210 out.close(); 211 212 ObjectInput in = new ObjectInputStream ( 213 new ByteArrayInputStream (buffer.toByteArray()) 214 ); 215 s2 = (TimePeriodValues) in.readObject(); 216 in.close(); 217 } 218 catch (Exception e) { 219 System.out.println(e.toString()); 220 } 221 assertTrue(s1.equals(s2)); 222 223 } 224 225 228 public void testEquals() { 229 TimePeriodValues s1 = new TimePeriodValues("Time Series 1"); 230 TimePeriodValues s2 = new TimePeriodValues("Time Series 2"); 231 boolean b1 = s1.equals(s2); 232 assertFalse("b1", b1); 233 234 s2.setKey("Time Series 1"); 235 boolean b2 = s1.equals(s2); 236 assertTrue("b2", b2); 237 238 RegularTimePeriod p1 = new Day(); 239 RegularTimePeriod p2 = p1.next(); 240 s1.add(p1, 100.0); 241 s1.add(p2, 200.0); 242 boolean b3 = s1.equals(s2); 243 assertFalse("b3", b3); 244 245 s2.add(p1, 100.0); 246 s2.add(p2, 200.0); 247 boolean b4 = s1.equals(s2); 248 assertTrue("b4", b4); 249 250 } 251 252 255 public void test1161329() { 256 TimePeriodValues tpv = new TimePeriodValues("Test"); 257 RegularTimePeriod t = new Day(); 258 tpv.add(t, 1.0); 259 t = t.next(); 260 tpv.add(t, 2.0); 261 tpv.delete(0, 1); 262 assertEquals(0, tpv.getItemCount()); 263 tpv.add(t, 2.0); 264 assertEquals(1, tpv.getItemCount()); 265 } 266 267 } 268 | Popular Tags |