1   /*
2   	StatCvs - CVS statistics generation 
3   	Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
4   	http://statcvs.sf.net/
5       
6   	This library is free software; you can redistribute it and/or
7   	modify it under the terms of the GNU Lesser General Public
8   	License as published by the Free Software Foundation; either
9   	version 2.1 of the License, or (at your option) any later version.
10  
11  	This library is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  	Lesser General Public License for more details.
15  
16  	You should have received a copy of the GNU Lesser General Public
17  	License along with this library; if not, write to the Free Software
18  	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19      
20  	$RCSfile: TimeLineTest.java,v $
21  	$Date: 2008/04/02 11:22:16 $
22  */
23  package net.sf.statcvs.reportmodel;
24  
25  import java.util.Date;
26  import java.util.List;
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * Test cases for {@link TimeLine}
32   * 
33   * @author Richard Cyganiak <rcyg@gmx.de>
34   * @version $Id: TimeLineTest.java,v 1.2 2008/04/02 11:22:16 benoitx Exp $
35   */
36  public class TimeLineTest extends TestCase {
37      private final Date date1 = new Date(100000000);
38      private final Date date2 = new Date(200000000);
39      private final Date date3 = new Date(300000000);
40      private final Date date4 = new Date(400000000);
41      private final Date beforeDate1 = new Date(99999999);
42  
43      /**
44       * Constructor
45       * @param arg0 stuff
46       */
47      public TimeLineTest(final String arg0) {
48          super(arg0);
49      }
50  
51      protected void setUp() throws Exception {
52          super.setUp();
53      }
54  
55      /**
56       * Tests the {@link HelperTimePoint} helper class 
57       */
58      public void testHelperTimePoint() {
59          final HelperTimePoint p1 = HelperTimePoint.createAbsoluteValueTimePoint(date1, 100);
60          final HelperTimePoint p2 = HelperTimePoint.createAbsoluteValueTimePoint(date1, 200);
61          final HelperTimePoint p3 = HelperTimePoint.createDeltaTimePoint(date1, 50);
62          final HelperTimePoint p4 = HelperTimePoint.createDeltaTimePoint(date1, 20);
63          assertTrue(p1.isAbsolute());
64          assertTrue(!p3.isAbsolute());
65          assertEquals(100, p1.getValue());
66          assertEquals(50, p3.getValue());
67          assertEquals(date1, p1.getDate());
68          assertEquals(date1, p3.getDate());
69          final HelperTimePoint p1p2 = p1.join(p2);
70          final HelperTimePoint p1p3 = p1.join(p3);
71          final HelperTimePoint p3p4 = p3.join(p4);
72          assertTrue(p1p2.isAbsolute());
73          assertTrue(p1p3.isAbsolute());
74          assertTrue(!p3p4.isAbsolute());
75          assertTrue(p1p2.getValue() == 100 || p1p2.getValue() == 200);
76          assertTrue(p1p3.getValue() == 100 || p1p2.getValue() == 150);
77          assertEquals(70, p3p4.getValue());
78      }
79  
80      /**
81       * Tests an empty TimeLine
82       */
83      public void testCreation() {
84          final TimeLine tl = new TimeLine("title", "domain");
85          assertTrue(tl.isEmpty());
86          assertTrue(tl.getDataPoints().isEmpty());
87          assertEquals("title", tl.getTitle());
88          assertEquals("domain", tl.getRangeLabel());
89      }
90  
91      /**
92       * Tests a TimeLine with one data point
93       */
94      public void testOneDataPoint() {
95          final TimeLine tl = new TimeLine("title", "domain");
96          tl.addTimePoint(date1, 100);
97          assertTrue(tl.isEmpty());
98          assertEquals(1, tl.getDataPoints().size());
99          final TimePoint tp = (TimePoint) tl.getDataPoints().get(0);
100         assertEquals(date1, tp.getDate());
101         assertEquals(100, tp.getValue());
102     }
103 
104     /**
105      * Test if the time points will be sorted if added in a non-ascending order 
106      */
107     public void testSorting() {
108         final TimeLine tl = new TimeLine("title", "domain");
109         tl.addTimePoint(date2, 100);
110         tl.addTimePoint(date1, 110);
111         tl.addTimePoint(date4, 120);
112         tl.addTimePoint(date3, 130);
113         assertTrue(!tl.isEmpty());
114         assertEquals(4, tl.getDataPoints().size());
115     }
116 
117     /**
118      * Test a time line with only relative values
119      */
120     public void testDeltaTimeLine() {
121         final TimeLine t1 = new TimeLine("title", "domain");
122         t1.setInitialValue(100);
123         t1.addChange(date1, 10);
124         t1.addChange(date2, -5);
125         final List points = t1.getDataPoints();
126         assertEquals(3, points.size());
127         assertEquals(beforeDate1, ((TimePoint) points.get(0)).getDate());
128         assertEquals(100, ((TimePoint) points.get(0)).getValue());
129         assertEquals(date1, ((TimePoint) points.get(1)).getDate());
130         assertEquals(110, ((TimePoint) points.get(1)).getValue());
131         assertEquals(10, ((TimePoint) points.get(1)).getDelta());
132         assertEquals(date2, ((TimePoint) points.get(2)).getDate());
133         assertEquals(105, ((TimePoint) points.get(2)).getValue());
134         assertEquals(-5, ((TimePoint) points.get(2)).getDelta());
135     }
136 
137     /**
138      * Test a time line with only relative values and no initial value
139      */
140     public void testIllegalDeltaTimeLine() {
141         final TimeLine t1 = new TimeLine("title", "domain");
142         t1.addChange(date1, 10);
143         t1.addChange(date2, -5);
144         try {
145             t1.getDataPoints();
146             fail("expected IllegalStateException because of missing setInitialValue");
147         } catch (final IllegalStateException expected) {
148             // expected
149         }
150     }
151 
152     /**
153      * Test a time line with multiple relative values at the same time
154      */
155     public void testDeltaTimeLineMultipleValues() {
156         final TimeLine t1 = new TimeLine("title", "domain");
157         t1.setInitialValue(100);
158         t1.addChange(date1, 10);
159         t1.addChange(date2, -5);
160         t1.addChange(date1, 20);
161         final List points = t1.getDataPoints();
162         assertEquals(3, points.size());
163         assertEquals(beforeDate1, ((TimePoint) points.get(0)).getDate());
164         assertEquals(100, ((TimePoint) points.get(0)).getValue());
165         assertEquals(date1, ((TimePoint) points.get(1)).getDate());
166         assertEquals(130, ((TimePoint) points.get(1)).getValue());
167         assertEquals(30, ((TimePoint) points.get(1)).getDelta());
168         assertEquals(date2, ((TimePoint) points.get(2)).getDate());
169         assertEquals(125, ((TimePoint) points.get(2)).getValue());
170         assertEquals(-5, ((TimePoint) points.get(2)).getDelta());
171     }
172 }