Source Code Cross Referenced for TimeSeriesTests.java in  » Chart » jfreechart » org » jfree » data » time » junit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Chart » jfreechart » org.jfree.data.time.junit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ===========================================================
002:         * JFreeChart : a free chart library for the Java(tm) platform
003:         * ===========================================================
004:         *
005:         * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006:         *
007:         * Project Info:  http://www.jfree.org/jfreechart/index.html
008:         *
009:         * This library is free software; you can redistribute it and/or modify it 
010:         * under the terms of the GNU Lesser General Public License as published by 
011:         * the Free Software Foundation; either version 2.1 of the License, or 
012:         * (at your option) any later version.
013:         *
014:         * This library is distributed in the hope that it will be useful, but 
015:         * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017:         * License for more details.
018:         *
019:         * You should have received a copy of the GNU Lesser General Public
020:         * License along with this library; if not, write to the Free Software
021:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022:         * USA.  
023:         *
024:         * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025:         * in the United States and other countries.]
026:         *
027:         * --------------------
028:         * TimeSeriesTests.java
029:         * --------------------
030:         * (C) Copyright 2001-2006, by Object Refinery Limited.
031:         *
032:         * Original Author:  David Gilbert (for Object Refinery Limited);
033:         * Contributor(s):   -;
034:         *
035:         * $Id: TimeSeriesTests.java,v 1.1.2.1 2006/10/03 15:41:39 mungady Exp $
036:         *
037:         * Changes
038:         * -------
039:         * 16-Nov-2001 : Version 1 (DG);
040:         * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041:         * 13-Mar-2003 : Added serialization test (DG);
042:         * 15-Oct-2003 : Added test for setMaximumItemCount method (DG);
043:         * 23-Aug-2004 : Added test that highlights a bug where the addOrUpdate() 
044:         *               method can lead to more than maximumItemCount items in the 
045:         *               dataset (DG);
046:         * 24-May-2006 : Added new tests (DG);
047:         *
048:         */
049:
050:        package org.jfree.data.time.junit;
051:
052:        import java.io.ByteArrayInputStream;
053:        import java.io.ByteArrayOutputStream;
054:        import java.io.ObjectInput;
055:        import java.io.ObjectInputStream;
056:        import java.io.ObjectOutput;
057:        import java.io.ObjectOutputStream;
058:
059:        import junit.framework.Test;
060:        import junit.framework.TestCase;
061:        import junit.framework.TestSuite;
062:
063:        import org.jfree.data.general.SeriesChangeEvent;
064:        import org.jfree.data.general.SeriesChangeListener;
065:        import org.jfree.data.general.SeriesException;
066:        import org.jfree.data.time.Day;
067:        import org.jfree.data.time.FixedMillisecond;
068:        import org.jfree.data.time.Month;
069:        import org.jfree.data.time.RegularTimePeriod;
070:        import org.jfree.data.time.TimeSeries;
071:        import org.jfree.data.time.TimeSeriesDataItem;
072:        import org.jfree.data.time.Year;
073:        import org.jfree.date.MonthConstants;
074:
075:        /**
076:         * A collection of test cases for the {@link TimeSeries} class.
077:         */
078:        public class TimeSeriesTests extends TestCase implements 
079:                SeriesChangeListener {
080:
081:            /** A time series. */
082:            private TimeSeries seriesA;
083:
084:            /** A time series. */
085:            private TimeSeries seriesB;
086:
087:            /** A time series. */
088:            private TimeSeries seriesC;
089:
090:            /** A flag that indicates whether or not a change event was fired. */
091:            private boolean gotSeriesChangeEvent = false;
092:
093:            /**
094:             * Returns the tests as a test suite.
095:             *
096:             * @return The test suite.
097:             */
098:            public static Test suite() {
099:                return new TestSuite(TimeSeriesTests.class);
100:            }
101:
102:            /**
103:             * Constructs a new set of tests.
104:             *
105:             * @param name  the name of the tests.
106:             */
107:            public TimeSeriesTests(String name) {
108:                super (name);
109:            }
110:
111:            /**
112:             * Common test setup.
113:             */
114:            protected void setUp() {
115:
116:                this .seriesA = new TimeSeries("Series A", Year.class);
117:                try {
118:                    this .seriesA.add(new Year(2000), new Integer(102000));
119:                    this .seriesA.add(new Year(2001), new Integer(102001));
120:                    this .seriesA.add(new Year(2002), new Integer(102002));
121:                    this .seriesA.add(new Year(2003), new Integer(102003));
122:                    this .seriesA.add(new Year(2004), new Integer(102004));
123:                    this .seriesA.add(new Year(2005), new Integer(102005));
124:                } catch (SeriesException e) {
125:                    System.err.println("Problem creating series.");
126:                }
127:
128:                this .seriesB = new TimeSeries("Series B", Year.class);
129:                try {
130:                    this .seriesB.add(new Year(2006), new Integer(202006));
131:                    this .seriesB.add(new Year(2007), new Integer(202007));
132:                    this .seriesB.add(new Year(2008), new Integer(202008));
133:                } catch (SeriesException e) {
134:                    System.err.println("Problem creating series.");
135:                }
136:
137:                this .seriesC = new TimeSeries("Series C", Year.class);
138:                try {
139:                    this .seriesC.add(new Year(1999), new Integer(301999));
140:                    this .seriesC.add(new Year(2000), new Integer(302000));
141:                    this .seriesC.add(new Year(2002), new Integer(302002));
142:                } catch (SeriesException e) {
143:                    System.err.println("Problem creating series.");
144:                }
145:
146:            }
147:
148:            /**
149:             * Sets the flag to indicate that a {@link SeriesChangeEvent} has been 
150:             * received.
151:             * 
152:             * @param event  the event.
153:             */
154:            public void seriesChanged(SeriesChangeEvent event) {
155:                this .gotSeriesChangeEvent = true;
156:            }
157:
158:            /**
159:             * Check that cloning works.
160:             */
161:            public void testClone() {
162:
163:                TimeSeries series = new TimeSeries("Test Series");
164:
165:                RegularTimePeriod jan1st2002 = new Day(1,
166:                        MonthConstants.JANUARY, 2002);
167:                try {
168:                    series.add(jan1st2002, new Integer(42));
169:                } catch (SeriesException e) {
170:                    System.err.println("Problem adding to series.");
171:                }
172:
173:                TimeSeries clone = null;
174:                try {
175:                    clone = (TimeSeries) series.clone();
176:                    clone.setKey("Clone Series");
177:                    try {
178:                        clone.update(jan1st2002, new Integer(10));
179:                    } catch (SeriesException e) {
180:                        e.printStackTrace();
181:                    }
182:                } catch (CloneNotSupportedException e) {
183:                    assertTrue(false);
184:                }
185:
186:                int seriesValue = series.getValue(jan1st2002).intValue();
187:                int cloneValue = Integer.MAX_VALUE;
188:                if (clone != null) {
189:                    cloneValue = clone.getValue(jan1st2002).intValue();
190:                }
191:
192:                assertEquals(42, seriesValue);
193:                assertEquals(10, cloneValue);
194:                assertEquals("Test Series", series.getKey());
195:                if (clone != null) {
196:                    assertEquals("Clone Series", clone.getKey());
197:                } else {
198:                    assertTrue(false);
199:                }
200:
201:            }
202:
203:            /**
204:             * Add a value to series A for 1999.  It should be added at index 0.
205:             */
206:            public void testAddValue() {
207:
208:                try {
209:                    this .seriesA.add(new Year(1999), new Integer(1));
210:                } catch (SeriesException e) {
211:                    System.err.println("Problem adding to series.");
212:                }
213:
214:                int value = this .seriesA.getValue(0).intValue();
215:                assertEquals(1, value);
216:
217:            }
218:
219:            /**
220:             * Tests the retrieval of values.
221:             */
222:            public void testGetValue() {
223:
224:                Number value1 = this .seriesA.getValue(new Year(1999));
225:                assertNull(value1);
226:                int value2 = this .seriesA.getValue(new Year(2000)).intValue();
227:                assertEquals(102000, value2);
228:
229:            }
230:
231:            /**
232:             * Tests the deletion of values.
233:             */
234:            public void testDelete() {
235:                this .seriesA.delete(0, 0);
236:                assertEquals(5, this .seriesA.getItemCount());
237:                Number value = this .seriesA.getValue(new Year(2000));
238:                assertNull(value);
239:            }
240:
241:            /**
242:             * Basic tests for the delete() method.
243:             */
244:            public void testDelete2() {
245:                TimeSeries s1 = new TimeSeries("Series", Year.class);
246:                s1.add(new Year(2000), 13.75);
247:                s1.add(new Year(2001), 11.90);
248:                s1.add(new Year(2002), null);
249:                s1.addChangeListener(this );
250:                this .gotSeriesChangeEvent = false;
251:                s1.delete(new Year(2001));
252:                assertTrue(this .gotSeriesChangeEvent);
253:                assertEquals(2, s1.getItemCount());
254:                assertEquals(null, s1.getValue(new Year(2001)));
255:
256:                // try deleting a time period that doesn't exist...
257:                this .gotSeriesChangeEvent = false;
258:                s1.delete(new Year(2006));
259:                assertFalse(this .gotSeriesChangeEvent);
260:
261:                // try deleting null
262:                try {
263:                    s1.delete(null);
264:                    fail("Expected IllegalArgumentException.");
265:                } catch (IllegalArgumentException e) {
266:                    // expected
267:                }
268:            }
269:
270:            /**
271:             * Serialize an instance, restore it, and check for equality.
272:             */
273:            public void testSerialization() {
274:
275:                TimeSeries s1 = new TimeSeries("A test", Year.class);
276:                s1.add(new Year(2000), 13.75);
277:                s1.add(new Year(2001), 11.90);
278:                s1.add(new Year(2002), null);
279:                s1.add(new Year(2005), 19.32);
280:                s1.add(new Year(2007), 16.89);
281:                TimeSeries s2 = null;
282:
283:                try {
284:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
285:                    ObjectOutput out = new ObjectOutputStream(buffer);
286:                    out.writeObject(s1);
287:                    out.close();
288:
289:                    ObjectInput in = new ObjectInputStream(
290:                            new ByteArrayInputStream(buffer.toByteArray()));
291:                    s2 = (TimeSeries) in.readObject();
292:                    in.close();
293:                } catch (Exception e) {
294:                    System.out.println(e.toString());
295:                }
296:                assertTrue(s1.equals(s2));
297:
298:            }
299:
300:            /**
301:             * Tests the equals method.
302:             */
303:            public void testEquals() {
304:                TimeSeries s1 = new TimeSeries("Time Series 1");
305:                TimeSeries s2 = new TimeSeries("Time Series 2");
306:                boolean b1 = s1.equals(s2);
307:                assertFalse("b1", b1);
308:
309:                s2.setKey("Time Series 1");
310:                boolean b2 = s1.equals(s2);
311:                assertTrue("b2", b2);
312:
313:                RegularTimePeriod p1 = new Day();
314:                RegularTimePeriod p2 = p1.next();
315:                s1.add(p1, 100.0);
316:                s1.add(p2, 200.0);
317:                boolean b3 = s1.equals(s2);
318:                assertFalse("b3", b3);
319:
320:                s2.add(p1, 100.0);
321:                s2.add(p2, 200.0);
322:                boolean b4 = s1.equals(s2);
323:                assertTrue("b4", b4);
324:
325:                s1.setMaximumItemCount(100);
326:                boolean b5 = s1.equals(s2);
327:                assertFalse("b5", b5);
328:
329:                s2.setMaximumItemCount(100);
330:                boolean b6 = s1.equals(s2);
331:                assertTrue("b6", b6);
332:
333:                s1.setMaximumItemAge(100);
334:                boolean b7 = s1.equals(s2);
335:                assertFalse("b7", b7);
336:
337:                s2.setMaximumItemAge(100);
338:                boolean b8 = s1.equals(s2);
339:                assertTrue("b8", b8);
340:            }
341:
342:            /**
343:             * Tests a specific bug report where null arguments in the constructor 
344:             * cause the equals() method to fail.  Fixed for 0.9.21.
345:             */
346:            public void testEquals2() {
347:                TimeSeries s1 = new TimeSeries("Series", null, null, Day.class);
348:                TimeSeries s2 = new TimeSeries("Series", null, null, Day.class);
349:                assertTrue(s1.equals(s2));
350:            }
351:
352:            /**
353:             * Some tests to ensure that the createCopy(RegularTimePeriod, 
354:             * RegularTimePeriod) method is functioning correctly.
355:             */
356:            public void testCreateCopy1() {
357:
358:                TimeSeries series = new TimeSeries("Series", Month.class);
359:                series.add(new Month(MonthConstants.JANUARY, 2003), 45.0);
360:                series.add(new Month(MonthConstants.FEBRUARY, 2003), 55.0);
361:                series.add(new Month(MonthConstants.JUNE, 2003), 35.0);
362:                series.add(new Month(MonthConstants.NOVEMBER, 2003), 85.0);
363:                series.add(new Month(MonthConstants.DECEMBER, 2003), 75.0);
364:
365:                try {
366:                    // copy a range before the start of the series data...
367:                    TimeSeries result1 = series.createCopy(new Month(
368:                            MonthConstants.NOVEMBER, 2002), new Month(
369:                            MonthConstants.DECEMBER, 2002));
370:                    assertEquals(0, result1.getItemCount());
371:
372:                    // copy a range that includes only the first item in the series...
373:                    TimeSeries result2 = series.createCopy(new Month(
374:                            MonthConstants.NOVEMBER, 2002), new Month(
375:                            MonthConstants.JANUARY, 2003));
376:                    assertEquals(1, result2.getItemCount());
377:
378:                    // copy a range that begins before and ends in the middle of the 
379:                    // series...
380:                    TimeSeries result3 = series.createCopy(new Month(
381:                            MonthConstants.NOVEMBER, 2002), new Month(
382:                            MonthConstants.APRIL, 2003));
383:                    assertEquals(2, result3.getItemCount());
384:
385:                    TimeSeries result4 = series.createCopy(new Month(
386:                            MonthConstants.NOVEMBER, 2002), new Month(
387:                            MonthConstants.DECEMBER, 2003));
388:                    assertEquals(5, result4.getItemCount());
389:
390:                    TimeSeries result5 = series.createCopy(new Month(
391:                            MonthConstants.NOVEMBER, 2002), new Month(
392:                            MonthConstants.MARCH, 2004));
393:                    assertEquals(5, result5.getItemCount());
394:
395:                    TimeSeries result6 = series.createCopy(new Month(
396:                            MonthConstants.JANUARY, 2003), new Month(
397:                            MonthConstants.JANUARY, 2003));
398:                    assertEquals(1, result6.getItemCount());
399:
400:                    TimeSeries result7 = series.createCopy(new Month(
401:                            MonthConstants.JANUARY, 2003), new Month(
402:                            MonthConstants.APRIL, 2003));
403:                    assertEquals(2, result7.getItemCount());
404:
405:                    TimeSeries result8 = series.createCopy(new Month(
406:                            MonthConstants.JANUARY, 2003), new Month(
407:                            MonthConstants.DECEMBER, 2003));
408:                    assertEquals(5, result8.getItemCount());
409:
410:                    TimeSeries result9 = series.createCopy(new Month(
411:                            MonthConstants.JANUARY, 2003), new Month(
412:                            MonthConstants.MARCH, 2004));
413:                    assertEquals(5, result9.getItemCount());
414:
415:                    TimeSeries result10 = series.createCopy(new Month(
416:                            MonthConstants.MAY, 2003), new Month(
417:                            MonthConstants.DECEMBER, 2003));
418:                    assertEquals(3, result10.getItemCount());
419:
420:                    TimeSeries result11 = series.createCopy(new Month(
421:                            MonthConstants.MAY, 2003), new Month(
422:                            MonthConstants.MARCH, 2004));
423:                    assertEquals(3, result11.getItemCount());
424:
425:                    TimeSeries result12 = series.createCopy(new Month(
426:                            MonthConstants.DECEMBER, 2003), new Month(
427:                            MonthConstants.DECEMBER, 2003));
428:                    assertEquals(1, result12.getItemCount());
429:
430:                    TimeSeries result13 = series.createCopy(new Month(
431:                            MonthConstants.DECEMBER, 2003), new Month(
432:                            MonthConstants.MARCH, 2004));
433:                    assertEquals(1, result13.getItemCount());
434:
435:                    TimeSeries result14 = series.createCopy(new Month(
436:                            MonthConstants.JANUARY, 2004), new Month(
437:                            MonthConstants.MARCH, 2004));
438:                    assertEquals(0, result14.getItemCount());
439:                } catch (CloneNotSupportedException e) {
440:                    assertTrue(false);
441:                }
442:
443:            }
444:
445:            /**
446:             * Some tests to ensure that the createCopy(int, int) method is 
447:             * functioning correctly.
448:             */
449:            public void testCreateCopy2() {
450:
451:                TimeSeries series = new TimeSeries("Series", Month.class);
452:                series.add(new Month(MonthConstants.JANUARY, 2003), 45.0);
453:                series.add(new Month(MonthConstants.FEBRUARY, 2003), 55.0);
454:                series.add(new Month(MonthConstants.JUNE, 2003), 35.0);
455:                series.add(new Month(MonthConstants.NOVEMBER, 2003), 85.0);
456:                series.add(new Month(MonthConstants.DECEMBER, 2003), 75.0);
457:
458:                try {
459:                    // copy just the first item...
460:                    TimeSeries result1 = series.createCopy(0, 0);
461:                    assertEquals(new Month(1, 2003), result1.getTimePeriod(0));
462:
463:                    // copy the first two items...
464:                    result1 = series.createCopy(0, 1);
465:                    assertEquals(new Month(2, 2003), result1.getTimePeriod(1));
466:
467:                    // copy the middle three items...
468:                    result1 = series.createCopy(1, 3);
469:                    assertEquals(new Month(2, 2003), result1.getTimePeriod(0));
470:                    assertEquals(new Month(11, 2003), result1.getTimePeriod(2));
471:
472:                    // copy the last two items...
473:                    result1 = series.createCopy(3, 4);
474:                    assertEquals(new Month(11, 2003), result1.getTimePeriod(0));
475:                    assertEquals(new Month(12, 2003), result1.getTimePeriod(1));
476:
477:                    // copy the last item...
478:                    result1 = series.createCopy(4, 4);
479:                    assertEquals(new Month(12, 2003), result1.getTimePeriod(0));
480:                } catch (CloneNotSupportedException e) {
481:                    assertTrue(false);
482:                }
483:
484:                // check negative first argument
485:                boolean pass = false;
486:                try {
487:                    /* TimeSeries result = */series.createCopy(-1, 1);
488:                } catch (IllegalArgumentException e) {
489:                    pass = true;
490:                } catch (CloneNotSupportedException e) {
491:                    pass = false;
492:                }
493:                assertTrue(pass);
494:
495:                // check second argument less than first argument
496:                pass = false;
497:                try {
498:                    /* TimeSeries result = */series.createCopy(1, 0);
499:                } catch (IllegalArgumentException e) {
500:                    pass = true;
501:                } catch (CloneNotSupportedException e) {
502:                    pass = false;
503:                }
504:                assertTrue(pass);
505:
506:                TimeSeries series2 = new TimeSeries("Series 2");
507:                try {
508:                    TimeSeries series3 = series2.createCopy(99, 999);
509:                    assertEquals(0, series3.getItemCount());
510:                } catch (CloneNotSupportedException e) {
511:                    assertTrue(false);
512:                }
513:            }
514:
515:            /**
516:             * Test the setMaximumItemCount() method to ensure that it removes items 
517:             * from the series if necessary.
518:             */
519:            public void testSetMaximumItemCount() {
520:
521:                TimeSeries s1 = new TimeSeries("S1", Year.class);
522:                s1.add(new Year(2000), 13.75);
523:                s1.add(new Year(2001), 11.90);
524:                s1.add(new Year(2002), null);
525:                s1.add(new Year(2005), 19.32);
526:                s1.add(new Year(2007), 16.89);
527:
528:                assertTrue(s1.getItemCount() == 5);
529:                s1.setMaximumItemCount(3);
530:                assertTrue(s1.getItemCount() == 3);
531:                TimeSeriesDataItem item = s1.getDataItem(0);
532:                assertTrue(item.getPeriod().equals(new Year(2002)));
533:
534:            }
535:
536:            /**
537:             * Some checks for the addOrUpdate() method.
538:             */
539:            public void testAddOrUpdate() {
540:                TimeSeries s1 = new TimeSeries("S1", Year.class);
541:                s1.setMaximumItemCount(2);
542:                s1.addOrUpdate(new Year(2000), 100.0);
543:                assertEquals(1, s1.getItemCount());
544:                s1.addOrUpdate(new Year(2001), 101.0);
545:                assertEquals(2, s1.getItemCount());
546:                s1.addOrUpdate(new Year(2001), 102.0);
547:                assertEquals(2, s1.getItemCount());
548:                s1.addOrUpdate(new Year(2002), 103.0);
549:                assertEquals(2, s1.getItemCount());
550:            }
551:
552:            /**
553:             * A test for the bug report 1075255.
554:             */
555:            public void testBug1075255() {
556:                TimeSeries ts = new TimeSeries("dummy", FixedMillisecond.class);
557:                ts.add(new FixedMillisecond(0L), 0.0);
558:                TimeSeries ts2 = new TimeSeries("dummy2",
559:                        FixedMillisecond.class);
560:                ts2.add(new FixedMillisecond(0L), 1.0);
561:                try {
562:                    ts.addAndOrUpdate(ts2);
563:                } catch (Exception e) {
564:                    e.printStackTrace();
565:                    assertTrue(false);
566:                }
567:                assertEquals(1, ts.getItemCount());
568:            }
569:
570:            /**
571:             * Some checks for the getIndex() method.
572:             */
573:            public void testGetIndex() {
574:                TimeSeries series = new TimeSeries("Series", Month.class);
575:                assertEquals(-1, series.getIndex(new Month(1, 2003)));
576:
577:                series.add(new Month(1, 2003), 45.0);
578:                assertEquals(0, series.getIndex(new Month(1, 2003)));
579:                assertEquals(-1, series.getIndex(new Month(12, 2002)));
580:                assertEquals(-2, series.getIndex(new Month(2, 2003)));
581:
582:                series.add(new Month(3, 2003), 55.0);
583:                assertEquals(-1, series.getIndex(new Month(12, 2002)));
584:                assertEquals(0, series.getIndex(new Month(1, 2003)));
585:                assertEquals(-2, series.getIndex(new Month(2, 2003)));
586:                assertEquals(1, series.getIndex(new Month(3, 2003)));
587:                assertEquals(-3, series.getIndex(new Month(4, 2003)));
588:            }
589:
590:            /**
591:             * Some checks for the getDataItem(int) method.
592:             */
593:            public void testGetDataItem1() {
594:                TimeSeries series = new TimeSeries("S", Year.class);
595:
596:                // can't get anything yet...just an exception
597:                boolean pass = false;
598:                try {
599:                    /*TimeSeriesDataItem item =*/series.getDataItem(0);
600:                } catch (IndexOutOfBoundsException e) {
601:                    pass = true;
602:                }
603:                assertTrue(pass);
604:
605:                series.add(new Year(2006), 100.0);
606:                TimeSeriesDataItem item = series.getDataItem(0);
607:                assertEquals(new Year(2006), item.getPeriod());
608:                pass = false;
609:                try {
610:                    item = series.getDataItem(-1);
611:                } catch (IndexOutOfBoundsException e) {
612:                    pass = true;
613:                }
614:                assertTrue(pass);
615:
616:                pass = false;
617:                try {
618:                    item = series.getDataItem(1);
619:                } catch (IndexOutOfBoundsException e) {
620:                    pass = true;
621:                }
622:                assertTrue(pass);
623:            }
624:
625:            /**
626:             * Some checks for the getDataItem(RegularTimePeriod) method.
627:             */
628:            public void testGetDataItem2() {
629:                TimeSeries series = new TimeSeries("S", Year.class);
630:                assertNull(series.getDataItem(new Year(2006)));
631:
632:                // try a null argument
633:                boolean pass = false;
634:                try {
635:                    /* TimeSeriesDataItem item = */series.getDataItem(null);
636:                } catch (IllegalArgumentException e) {
637:                    pass = true;
638:                }
639:                assertTrue(pass);
640:            }
641:
642:            /**
643:             * Some checks for the removeAgedItems() method.
644:             */
645:            public void testRemoveAgedItems() {
646:                TimeSeries series = new TimeSeries("Test Series", Year.class);
647:                series.addChangeListener(this );
648:                assertEquals(Long.MAX_VALUE, series.getMaximumItemAge());
649:                assertEquals(Integer.MAX_VALUE, series.getMaximumItemCount());
650:                this .gotSeriesChangeEvent = false;
651:
652:                // test empty series
653:                series.removeAgedItems(true);
654:                assertEquals(0, series.getItemCount());
655:                assertFalse(this .gotSeriesChangeEvent);
656:
657:                // test series with one item
658:                series.add(new Year(1999), 1.0);
659:                series.setMaximumItemAge(0);
660:                this .gotSeriesChangeEvent = false;
661:                series.removeAgedItems(true);
662:                assertEquals(1, series.getItemCount());
663:                assertFalse(this .gotSeriesChangeEvent);
664:
665:                // test series with two items
666:                series.setMaximumItemAge(10);
667:                series.add(new Year(2001), 2.0);
668:                this .gotSeriesChangeEvent = false;
669:                series.setMaximumItemAge(2);
670:                assertEquals(2, series.getItemCount());
671:                assertEquals(0, series.getIndex(new Year(1999)));
672:                assertFalse(this .gotSeriesChangeEvent);
673:                series.setMaximumItemAge(1);
674:                assertEquals(1, series.getItemCount());
675:                assertEquals(0, series.getIndex(new Year(2001)));
676:                assertTrue(this .gotSeriesChangeEvent);
677:            }
678:
679:            /**
680:             * Some checks for the removeAgedItems(long, boolean) method.
681:             */
682:            public void testRemoveAgedItems2() {
683:                long y2006 = 1157087372534L; // milliseconds somewhere in 2006
684:                TimeSeries series = new TimeSeries("Test Series", Year.class);
685:                series.addChangeListener(this );
686:                assertEquals(Long.MAX_VALUE, series.getMaximumItemAge());
687:                assertEquals(Integer.MAX_VALUE, series.getMaximumItemCount());
688:                this .gotSeriesChangeEvent = false;
689:
690:                // test empty series
691:                series.removeAgedItems(y2006, true);
692:                assertEquals(0, series.getItemCount());
693:                assertFalse(this .gotSeriesChangeEvent);
694:
695:                // test a series with 1 item
696:                series.add(new Year(2004), 1.0);
697:                series.setMaximumItemAge(1);
698:                this .gotSeriesChangeEvent = false;
699:                series.removeAgedItems(new Year(2005).getMiddleMillisecond(),
700:                        true);
701:                assertEquals(1, series.getItemCount());
702:                assertFalse(this .gotSeriesChangeEvent);
703:                series.removeAgedItems(y2006, true);
704:                assertEquals(0, series.getItemCount());
705:                assertTrue(this .gotSeriesChangeEvent);
706:
707:                // test a series with two items
708:                series.setMaximumItemAge(2);
709:                series.add(new Year(2003), 1.0);
710:                series.add(new Year(2005), 2.0);
711:                assertEquals(2, series.getItemCount());
712:                this .gotSeriesChangeEvent = false;
713:                assertEquals(2, series.getItemCount());
714:
715:                series.removeAgedItems(new Year(2005).getMiddleMillisecond(),
716:                        true);
717:                assertEquals(2, series.getItemCount());
718:                assertFalse(this .gotSeriesChangeEvent);
719:                series.removeAgedItems(y2006, true);
720:                assertEquals(1, series.getItemCount());
721:                assertTrue(this.gotSeriesChangeEvent);
722:            }
723:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.