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 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jfree.data.time.Day; 57 import org.jfree.data.time.RegularTimePeriod; 58 import org.jfree.data.time.TimePeriodAnchor; 59 import org.jfree.data.time.TimeSeries; 60 import org.jfree.data.time.TimeSeriesCollection; 61 62 65 public class TimeSeriesCollectionTests extends TestCase { 66 67 72 public static Test suite() { 73 return new TestSuite(TimeSeriesCollectionTests.class); 74 } 75 76 81 public TimeSeriesCollectionTests(String name) { 82 super(name); 83 } 84 85 88 public void testEquals() { 89 90 TimeSeriesCollection c1 = new TimeSeriesCollection(); 91 TimeSeriesCollection c2 = new TimeSeriesCollection(); 92 93 TimeSeries s1 = new TimeSeries("Series 1"); 94 TimeSeries s2 = new TimeSeries("Series 2"); 95 96 boolean b1 = c1.equals(c2); 98 assertTrue("b1", b1); 99 100 c1.addSeries(s1); 102 c1.addSeries(s2); 103 boolean b2 = c1.equals(c2); 104 assertFalse("b2", b2); 105 106 c2.addSeries(s1); 108 c2.addSeries(s2); 109 boolean b3 = c1.equals(c2); 110 assertTrue("b3", b3); 111 112 c2.removeSeries(s2); 114 boolean b4 = c1.equals(c2); 115 assertFalse("b4", b4); 116 117 c1.removeSeries(s2); 119 boolean b5 = c1.equals(c2); 120 assertTrue("b5", b5); 121 } 122 123 126 public void testRemoveSeries() { 127 128 TimeSeriesCollection c1 = new TimeSeriesCollection(); 129 130 TimeSeries s1 = new TimeSeries("Series 1"); 131 TimeSeries s2 = new TimeSeries("Series 2"); 132 TimeSeries s3 = new TimeSeries("Series 3"); 133 TimeSeries s4 = new TimeSeries("Series 4"); 134 135 c1.addSeries(s1); 136 c1.addSeries(s2); 137 c1.addSeries(s3); 138 c1.addSeries(s4); 139 140 c1.removeSeries(s3); 141 142 TimeSeries s = c1.getSeries(2); 143 boolean b1 = s.equals(s4); 144 assertTrue(b1); 145 146 } 147 148 152 public void testGetSurroundingItems() { 153 154 TimeSeries series = new TimeSeries("Series 1", Day.class); 155 TimeSeriesCollection collection = new TimeSeriesCollection(series); 156 collection.setXPosition(TimePeriodAnchor.MIDDLE); 157 158 int[] result = collection.getSurroundingItems(0, 1000L); 160 assertTrue(result[0] == -1); 161 assertTrue(result[1] == -1); 162 163 Day today = new Day(); 165 long start1 = today.getFirstMillisecond(); 166 long middle1 = today.getMiddleMillisecond(); 167 long end1 = today.getLastMillisecond(); 168 169 series.add(today, 99.9); 170 result = collection.getSurroundingItems(0, start1); 171 assertTrue(result[0] == -1); 172 assertTrue(result[1] == 0); 173 174 result = collection.getSurroundingItems(0, middle1); 175 assertTrue(result[0] == 0); 176 assertTrue(result[1] == 0); 177 178 result = collection.getSurroundingItems(0, end1); 179 assertTrue(result[0] == 0); 180 assertTrue(result[1] == -1); 181 182 Day tomorrow = (Day) today.next(); 184 long start2 = tomorrow.getFirstMillisecond(); 185 long middle2 = tomorrow.getMiddleMillisecond(); 186 long end2 = tomorrow.getLastMillisecond(); 187 188 series.add(tomorrow, 199.9); 189 result = collection.getSurroundingItems(0, start2); 190 assertTrue(result[0] == 0); 191 assertTrue(result[1] == 1); 192 193 result = collection.getSurroundingItems(0, middle2); 194 assertTrue(result[0] == 1); 195 assertTrue(result[1] == 1); 196 197 result = collection.getSurroundingItems(0, end2); 198 assertTrue(result[0] == 1); 199 assertTrue(result[1] == -1); 200 201 Day yesterday = (Day) today.previous(); 203 long start3 = yesterday.getFirstMillisecond(); 204 long middle3 = yesterday.getMiddleMillisecond(); 205 long end3 = yesterday.getLastMillisecond(); 206 207 series.add(yesterday, 1.23); 208 result = collection.getSurroundingItems(0, start3); 209 assertTrue(result[0] == -1); 210 assertTrue(result[1] == 0); 211 212 result = collection.getSurroundingItems(0, middle3); 213 assertTrue(result[0] == 0); 214 assertTrue(result[1] == 0); 215 216 result = collection.getSurroundingItems(0, end3); 217 assertTrue(result[0] == 0); 218 assertTrue(result[1] == 1); 219 220 } 221 222 225 public void testSerialization() { 226 227 TimeSeriesCollection c1 = new TimeSeriesCollection(createSeries()); 228 TimeSeriesCollection c2 = null; 229 230 try { 231 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 232 ObjectOutput out = new ObjectOutputStream (buffer); 233 out.writeObject(c1); 234 out.close(); 235 236 ObjectInput in = new ObjectInputStream ( 237 new ByteArrayInputStream (buffer.toByteArray()) 238 ); 239 c2 = (TimeSeriesCollection) in.readObject(); 240 in.close(); 241 } 242 catch (Exception e) { 243 System.out.println(e.toString()); 244 } 245 assertEquals(c1, c2); 246 247 } 248 249 254 private TimeSeries createSeries() { 255 RegularTimePeriod t = new Day(); 256 TimeSeries series = new TimeSeries("Test"); 257 series.add(t, 1.0); 258 t = t.next(); 259 series.add(t, 2.0); 260 t = t.next(); 261 series.add(t, null); 262 t = t.next(); 263 series.add(t, 4.0); 264 return series; 265 } 266 267 270 public void test1170825() { 271 TimeSeries s1 = new TimeSeries("Series1"); 272 TimeSeriesCollection dataset = new TimeSeriesCollection(); 273 dataset.addSeries(s1); 274 try { 275 dataset.getSeries(1); 276 } 277 catch (IllegalArgumentException e) { 278 } 280 catch (IndexOutOfBoundsException e) { 281 assertTrue(false); } 283 } 284 285 286 } 287 | Popular Tags |