/** A test for bug report 1161329. */ public void test1161329() { TimePeriodValues tpv = new TimePeriodValues("Test"); RegularTimePeriod t = new Day(); tpv.add(t, 1.0); t = t.next(); tpv.add(t, 2.0); tpv.delete(0, 1); assertEquals(0, tpv.getItemCount()); tpv.add(t, 2.0); assertEquals(1, tpv.getItemCount()); }
/** Set up a quarter equal to Q1 1900. Request the previous quarter, it should be null. */ public void testClone() { TimePeriodValues series = new TimePeriodValues("Test Series"); RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002); try { series.add(jan1st2002, new Integer(42)); } catch (SeriesException e) { System.err.println("Problem adding to collection."); } TimePeriodValues clone = null; try { clone = (TimePeriodValues) series.clone(); clone.setKey("Clone Series"); try { clone.update(0, new Integer(10)); } catch (SeriesException e) { System.err.println("Problem updating series."); } } catch (CloneNotSupportedException e) { assertTrue(false); } int seriesValue = series.getValue(0).intValue(); int cloneValue = clone.getValue(0).intValue(); assertEquals(42, seriesValue); assertEquals(10, cloneValue); assertEquals("Test Series", series.getKey()); assertEquals("Clone Series", clone.getKey()); }
/** Some checks for the add() methods. */ public void testAdd() { TimePeriodValues tpv = new TimePeriodValues("Test"); MySeriesChangeListener listener = new MySeriesChangeListener(); tpv.addChangeListener(listener); tpv.add(new TimePeriodValue(new SimpleTimePeriod(new Date(1L), new Date(3L)), 99.0)); assertEquals(99.0, tpv.getValue(0).doubleValue(), EPSILON); assertEquals(tpv, listener.getLastEvent().getSource()); // a null item should throw an IllegalArgumentException boolean pass = false; try { tpv.add((TimePeriodValue) null); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); }
/** Tests the equals method. */ public void testEquals() { TimePeriodValues s1 = new TimePeriodValues("Time Series 1"); TimePeriodValues s2 = new TimePeriodValues("Time Series 2"); boolean b1 = s1.equals(s2); assertFalse("b1", b1); s2.setKey("Time Series 1"); boolean b2 = s1.equals(s2); assertTrue("b2", b2); // domain description s1.setDomainDescription("XYZ"); assertFalse(s1.equals(s2)); s2.setDomainDescription("XYZ"); assertTrue(s1.equals(s2)); // domain description - null s1.setDomainDescription(null); assertFalse(s1.equals(s2)); s2.setDomainDescription(null); assertTrue(s1.equals(s2)); // range description s1.setRangeDescription("XYZ"); assertFalse(s1.equals(s2)); s2.setRangeDescription("XYZ"); assertTrue(s1.equals(s2)); // range description - null s1.setRangeDescription(null); assertFalse(s1.equals(s2)); s2.setRangeDescription(null); assertTrue(s1.equals(s2)); RegularTimePeriod p1 = new Day(); RegularTimePeriod p2 = p1.next(); s1.add(p1, 100.0); s1.add(p2, 200.0); boolean b3 = s1.equals(s2); assertFalse("b3", b3); s2.add(p1, 100.0); s2.add(p2, 200.0); boolean b4 = s1.equals(s2); assertTrue("b4", b4); }
/** Add a value to series A for 1999. It should be added at index 0. */ public void testAddValue() { TimePeriodValues tpvs = new TimePeriodValues("Test"); try { tpvs.add(new Year(1999), new Integer(1)); } catch (SeriesException e) { System.err.println("Problem adding to series."); } int value = tpvs.getValue(0).intValue(); assertEquals(1, value); }
/** Serialize an instance, restore it, and check for equality. */ public void testSerialization() { TimePeriodValues s1 = new TimePeriodValues("A test"); s1.add(new Year(2000), 13.75); s1.add(new Year(2001), 11.90); s1.add(new Year(2002), null); s1.add(new Year(2005), 19.32); s1.add(new Year(2007), 16.89); TimePeriodValues s2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(s1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); s2 = (TimePeriodValues) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertTrue(s1.equals(s2)); }
/** * Creates a new instance by copying a subset of the data in this collection. * * @param start the index of the first item to copy. * @param end the index of the last item to copy. * @return A copy of a subset of the items. * @throws CloneNotSupportedException if there is a cloning problem. */ public TimePeriodValues createCopy(int start, int end) throws CloneNotSupportedException { TimePeriodValues copy = (TimePeriodValues) super.clone(); copy.data = new ArrayList(); if (this.data.size() > 0) { for (int index = start; index <= end; index++) { TimePeriodValue item = (TimePeriodValue) this.data.get(index); TimePeriodValue clone = (TimePeriodValue) item.clone(); try { copy.add(clone); } catch (SeriesException e) { System.err.println("Failed to add cloned item."); } } } return copy; }
/** * Adds a new data item to the series and sends a {@link SeriesChangeEvent} to all registered * listeners. * * @param period the time period (<code>null</code> not permitted). * @param value the value (<code>null</code> permitted). */ public void add(TimePeriod period, Number value) { TimePeriodValue item = new TimePeriodValue(period, value); add(item); }
/** * Adds a new data item to the series and sends a {@link SeriesChangeEvent} to all registered * listeners. * * @param period the time period (<code>null</code> not permitted). * @param value the value. * @see #add(TimePeriod, Number) */ public void add(TimePeriod period, double value) { TimePeriodValue item = new TimePeriodValue(period, value); add(item); }