Source Code Cross Referenced for SegmentedTimelineTests.java in  » Chart » jfreechart » org » jfree » chart » axis » 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.chart.axis.junit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* ===========================================================
0002:         * JFreeChart : a free chart library for the Java(tm) platform
0003:         * ===========================================================
0004:         *
0005:         * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
0006:         *
0007:         * Project Info:  http://www.jfree.org/jfreechart/index.html
0008:         *
0009:         * This library is free software; you can redistribute it and/or modify it 
0010:         * under the terms of the GNU Lesser General Public License as published by 
0011:         * the Free Software Foundation; either version 2.1 of the License, or 
0012:         * (at your option) any later version.
0013:         *
0014:         * This library is distributed in the hope that it will be useful, but 
0015:         * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
0016:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
0017:         * License for more details.
0018:         *
0019:         * You should have received a copy of the GNU Lesser General Public
0020:         * License along with this library; if not, write to the Free Software
0021:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
0022:         * USA.  
0023:         *
0024:         * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
0025:         * in the United States and other countries.]
0026:         *
0027:         * ----------------------------
0028:         * SegmentedTimelineTests.java
0029:         * ----------------------------
0030:         * (C) Copyright 2003-2007, by Bill Kelemen and Contributors.
0031:         *
0032:         * Original Author:  Bill Kelemen;
0033:         * Contributor(s):   ;
0034:         *
0035:         * $Id: SegmentedTimelineTests.java,v 1.1.2.3 2007/02/02 15:10:21 mungady Exp $
0036:         *
0037:         * Changes
0038:         * -------
0039:         * 24-May-2003 : Version 1 (BK);
0040:         * 07-Jan-2005 : Added test for hashCode() method (DG);
0041:         * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
0042:         *
0043:         */
0044:
0045:        package org.jfree.chart.axis.junit;
0046:
0047:        import java.io.ByteArrayInputStream;
0048:        import java.io.ByteArrayOutputStream;
0049:        import java.io.ObjectInput;
0050:        import java.io.ObjectInputStream;
0051:        import java.io.ObjectOutput;
0052:        import java.io.ObjectOutputStream;
0053:        import java.text.Format;
0054:        import java.text.NumberFormat;
0055:        import java.text.ParseException;
0056:        import java.text.SimpleDateFormat;
0057:        import java.util.ArrayList;
0058:        import java.util.Calendar;
0059:        import java.util.GregorianCalendar;
0060:        import java.util.Iterator;
0061:
0062:        import junit.framework.Test;
0063:        import junit.framework.TestCase;
0064:        import junit.framework.TestSuite;
0065:
0066:        import org.jfree.chart.axis.SegmentedTimeline;
0067:
0068:        /**
0069:         * JUnit Tests for the {@link SegmentedTimeline} class.
0070:         */
0071:        public class SegmentedTimelineTests extends TestCase {
0072:
0073:            /** These constants control test cycles in the validateXXXX methods. */
0074:            private static final int TEST_CYCLE_START = 0;
0075:
0076:            /** These constants control test cycles in the validateXXXX methods. */
0077:            private static final int TEST_CYCLE_END = 1000;
0078:
0079:            /** These constants control test cycles in the validateXXXX methods. */
0080:            private static final int TEST_CYCLE_INC = 55;
0081:
0082:            /** Number of ms in five years */
0083:            private static final long FIVE_YEARS = 5 * 365 * SegmentedTimeline.DAY_SEGMENT_SIZE;
0084:
0085:            /** Number format object for ms tests. */
0086:            private static final NumberFormat NUMBER_FORMAT = NumberFormat
0087:                    .getNumberInstance();
0088:
0089:            /** Date format object for Monday through Friday tests. */
0090:            private static final SimpleDateFormat DATE_FORMAT;
0091:
0092:            /** Date format object 9:00 AM to 4:00 PM tests. */
0093:            private static final SimpleDateFormat DATE_TIME_FORMAT;
0094:
0095:            /** Some ms exceptions for ms testing. */
0096:            private static final String[] MS_EXCEPTIONS = { "0", "2", "4",
0097:                    "10", "15", "16", "17", "18", "19", "20", "21", "22", "23",
0098:                    "24", "47", "58", "100", "101" };
0099:
0100:            /** Some ms4 exceptions for ms testing. */
0101:            private static final String[] MS2_BASE_TIMELINE_EXCEPTIONS = { "0",
0102:                    "8", "16", "24", "32", "40", "48", "56", "64", "72", "80",
0103:                    "88", "96", "104", "112", "120", "128", "136" };
0104:
0105:            /** US non-trading dates in 2000 through 2002 to test exceptions. */
0106:            private static final String[] US_HOLIDAYS = { "2000-01-17",
0107:                    "2000-02-21", "2000-04-21", "2000-05-29", "2000-07-04",
0108:                    "2000-09-04", "2000-11-23", "2000-12-25", "2001-01-01",
0109:                    "2001-01-15", "2001-02-19", "2001-04-13", "2001-05-28",
0110:                    "2001-07-04", "2001-09-03", "2001-09-11", "2001-09-12",
0111:                    "2001-09-13", "2001-09-14", "2001-11-22", "2001-12-25",
0112:                    "2002-01-01", "2002-01-21", "2002-02-18", "2002-03-29",
0113:                    "2002-05-27", "2002-07-04", "2002-09-02", "2002-11-28",
0114:                    "2002-12-25" };
0115:
0116:            /** Some test exceptions for the fifteen min timeline. */
0117:            private static final String[] FIFTEEN_MIN_EXCEPTIONS = {
0118:                    "2000-01-10 09:00:00", "2000-01-10 09:15:00",
0119:                    "2000-01-10 09:30:00", "2000-01-10 09:45:00",
0120:                    "2000-01-10 10:00:00", "2000-01-10 10:15:00",
0121:                    "2000-02-15 09:00:00", "2000-02-15 09:15:00",
0122:                    "2000-02-15 09:30:00", "2000-02-15 09:45:00",
0123:                    "2000-02-15 10:00:00", "2000-02-15 10:15:00",
0124:                    "2000-02-16 11:00:00", "2000-02-16 11:15:00",
0125:                    "2000-02-16 11:30:00", "2000-02-16 11:45:00",
0126:                    "2000-02-16 12:00:00", "2000-02-16 12:15:00",
0127:                    "2000-02-16 12:30:00", "2000-02-16 12:45:00",
0128:                    "2000-02-16 01:00:00", "2000-02-16 01:15:00",
0129:                    "2000-02-16 01:30:00", "2000-02-16 01:45:00",
0130:                    "2000-05-17 11:45:00", "2000-05-17 12:00:00",
0131:                    "2000-05-17 12:15:00", "2000-05-17 12:30:00",
0132:                    "2000-05-17 12:45:00", "2000-05-17 01:00:00",
0133:                    "2000-05-17 01:15:00", "2000-05-17 01:30:00",
0134:                    "2000-05-17 01:45:00", "2000-05-17 02:00:00",
0135:                    "2000-05-17 02:15:00", "2000-05-17 02:30:00",
0136:                    "2000-05-17 02:45:00", "2000-05-17 03:00:00",
0137:                    "2000-05-17 03:15:00", "2000-05-17 03:30:00",
0138:                    "2000-05-17 03:45:00", "2000-05-17 04:00:00" };
0139:
0140:            /** Our 1-ms test timeline using 5 included and 2 excluded segments. */
0141:            private SegmentedTimeline msTimeline;
0142:
0143:            /** 
0144:             * Our 1-ms test timeline (with baseTimeline) using 2 included and 2 
0145:             * excluded segments. 
0146:             */
0147:            private SegmentedTimeline ms2Timeline;
0148:
0149:            /** 
0150:             * Our 4-ms test base timeline for ms2Timeline using 1 included and 1 
0151:             * excluded segments 
0152:             */
0153:            private SegmentedTimeline ms2BaseTimeline;
0154:
0155:            /** Our test Monday through Friday test timeline. */
0156:            private SegmentedTimeline mondayFridayTimeline;
0157:
0158:            /** Our 9:00 AM to 4:00 PM fifteen minute timeline. */
0159:            private SegmentedTimeline fifteenMinTimeline;
0160:
0161:            /** ms from 1970-01-01 to first monday after 2001-01-01. */
0162:            private Calendar monday;
0163:
0164:            /** ms from 1970-01-01 to 9 am first monday after 2001-01-01. */
0165:            private Calendar monday9am;
0166:
0167:            /** Static initialization block. */
0168:            static {
0169:                DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
0170:                DATE_FORMAT.setTimeZone(SegmentedTimeline.NO_DST_TIME_ZONE);
0171:
0172:                DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
0173:                DATE_TIME_FORMAT
0174:                        .setTimeZone(SegmentedTimeline.NO_DST_TIME_ZONE);
0175:            }
0176:
0177:            /**
0178:             * Returns the tests as a test suite.
0179:             *
0180:             * @return The test suite.
0181:             */
0182:            public static Test suite() {
0183:                return new TestSuite(SegmentedTimelineTests.class);
0184:            }
0185:
0186:            /**
0187:             * Constructs a new set of tests.
0188:             *
0189:             * @param name  the name of the tests.
0190:             */
0191:            public SegmentedTimelineTests(String name) {
0192:                super (name);
0193:            }
0194:
0195:            /**
0196:             * Sets up the fixture, for example, open a network connection.
0197:             * This method is called before a test is executed.
0198:             * 
0199:             * @throws Exception if there is a problem.
0200:             */
0201:            protected void setUp() throws Exception {
0202:                // setup our test timelines
0203:                //
0204:                // Legend for comments below:
0205:                // <spaces> = Segments included in the final timeline
0206:                // EE       = Excluded segments via timeline rules
0207:                // xx       = Exception segments inherited from base timeline exclusions
0208:
0209:                // 1-ms test timeline using 5 included and 2 excluded segments.
0210:                //
0211:                // timeline start time = 0
0212:                //   |
0213:                //   v
0214:                //   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 ..
0215:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+..
0216:                // |  |  |  |  |  |EE|EE|  |  |  |  |  |EE|EE|  |  |  |  |  |  |EE|EE|    <-- msTimeline
0217:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+..
0218:                //  \_________  ________/            \_/
0219:                //            \/                      |
0220:                //       segment group         segment size = 1 ms
0221:                //
0222:                this .msTimeline = new SegmentedTimeline(1, 5, 2);
0223:                this .msTimeline.setStartTime(0);
0224:
0225:                // 4-ms test base timeline for ms2Timeline using 1 included and 1
0226:                // excluded segments
0227:                //
0228:                // timeline start time = 0
0229:                //   |
0230:                //   v
0231:                //   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 ...
0232:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
0233:                // |  |  |  |  |EE|EE|EE|EE|  |  |  |  |EE|EE|EE|EE|  |  |  |  |    <-- ms2BaseTimeline
0234:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
0235:                //  \__________  _________/            \____  _____/
0236:                //             \/                           \/
0237:                //        segment group              segment size = 4 ms
0238:                //
0239:                this .ms2BaseTimeline = new SegmentedTimeline(4, 1, 1);
0240:                this .ms2BaseTimeline.setStartTime(0);
0241:
0242:                // 1-ms test timeline (with a baseTimeline) using 2 included and 2 
0243:                // excluded segments centered inside each base segment
0244:                //
0245:                // The ms2Timeline without a base would look like this:
0246:                //
0247:                //    timeline start time = 1
0248:                //      |
0249:                //      v
0250:                //   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 ...
0251:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
0252:                // |EE|  |  |EE|EE|  |  |EE|EE|  |  |EE|EE|  |  |EE|EE|  |  |EE|    <-- ms2Timeline
0253:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
0254:                //    \____  _____/            \_/
0255:                //         \/                   |
0256:                //    segment group      segment size = 1 ms
0257:                //
0258:                // With the base timeline some originally included segments are now 
0259:                // removed (see "xx" below):
0260:                //
0261:                //   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 ...
0262:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
0263:                // |EE|  |  |EE|EE|xx|xx|EE|EE|  |  |EE|EE|xx|xx|EE|EE|  |  |EE|    <-- ms2Timeline
0264:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
0265:                // |  |  |  |  |EE|EE|EE|EE|  |  |  |  |EE|EE|EE|EE|  |  |  |  |    <-- ms2BaseTimeline
0266:                // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
0267:                //
0268:                this .ms2Timeline = new SegmentedTimeline(1, 2, 2);
0269:                this .ms2Timeline.setStartTime(1);
0270:                this .ms2Timeline.setBaseTimeline(this .ms2BaseTimeline);
0271:
0272:                // test monday though friday timeline
0273:                this .mondayFridayTimeline = SegmentedTimeline
0274:                        .newMondayThroughFridayTimeline();
0275:
0276:                // test 9am-4pm Monday through Friday timeline
0277:                this .fifteenMinTimeline = SegmentedTimeline
0278:                        .newFifteenMinuteTimeline();
0279:
0280:                // find first Monday after 2001-01-01
0281:                Calendar cal = new GregorianCalendar(
0282:                        SegmentedTimeline.NO_DST_TIME_ZONE);
0283:                cal.set(2001, 0, 1, 0, 0, 0);
0284:                cal.set(Calendar.MILLISECOND, 0);
0285:                while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
0286:                    cal.add(Calendar.DATE, 1);
0287:                }
0288:                this .monday = (Calendar) cal.clone();
0289:
0290:                // calculate 9am on the first Monday after 2001-01-01
0291:                cal.add(Calendar.HOUR, 9);
0292:                this .monday9am = (Calendar) cal.clone();
0293:            }
0294:
0295:            /**
0296:             * Tears down the fixture, for example, close a network connection.
0297:             * This method is called after a test is executed.
0298:             * 
0299:             * @throws Exception if there is a problem.
0300:             */
0301:            protected void tearDown() throws Exception {
0302:                // does nothing
0303:            }
0304:
0305:            //////////////////////////////////////////////////////////////////////////
0306:            // test construction process
0307:            //////////////////////////////////////////////////////////////////////////
0308:
0309:            /**
0310:             * Tests that the new method that created the msTimeline segmented
0311:             * timeline did so correctly.
0312:             */
0313:            public void testMsSegmentedTimeline() {
0314:                // verify attributes set during object construction
0315:                assertEquals(1, this .msTimeline.getSegmentSize());
0316:                assertEquals(0, this .msTimeline.getStartTime());
0317:                assertEquals(5, this .msTimeline.getSegmentsIncluded());
0318:                assertEquals(2, this .msTimeline.getSegmentsExcluded());
0319:            }
0320:
0321:            /**
0322:             * Tests that the new method that created the ms2Timeline segmented
0323:             * timeline did so correctly.
0324:             */
0325:            public void testMs2SegmentedTimeline() {
0326:                // verify attributes set during object construction
0327:                assertEquals(1, this .ms2Timeline.getSegmentSize());
0328:                assertEquals(1, this .ms2Timeline.getStartTime());
0329:                assertEquals(2, this .ms2Timeline.getSegmentsIncluded());
0330:                assertEquals(2, this .ms2Timeline.getSegmentsExcluded());
0331:                assertEquals(this .ms2BaseTimeline, this .ms2Timeline
0332:                        .getBaseTimeline());
0333:            }
0334:
0335:            /**
0336:             * Tests that the factory method that creates Monday through Friday 
0337:             * segmented timeline does so correctly.
0338:             */
0339:            public void testMondayThroughFridaySegmentedTimeline() {
0340:                // verify attributes set during object construction
0341:                assertEquals(SegmentedTimeline.DAY_SEGMENT_SIZE,
0342:                        this .mondayFridayTimeline.getSegmentSize());
0343:                assertEquals(SegmentedTimeline.FIRST_MONDAY_AFTER_1900,
0344:                        this .mondayFridayTimeline.getStartTime());
0345:                assertEquals(5, this .mondayFridayTimeline.getSegmentsIncluded());
0346:                assertEquals(2, this .mondayFridayTimeline.getSegmentsExcluded());
0347:            }
0348:
0349:            /**
0350:             * Tests that the factory method that creates a 15-min 9:00 AM  4:00 PM
0351:             * segmented axis does so correctly.
0352:             */
0353:            public void testFifteenMinSegmentedTimeline() {
0354:                assertEquals(SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE,
0355:                        this .fifteenMinTimeline.getSegmentSize());
0356:                assertEquals(SegmentedTimeline.FIRST_MONDAY_AFTER_1900 + 36
0357:                        * this .fifteenMinTimeline.getSegmentSize(),
0358:                        this .fifteenMinTimeline.getStartTime());
0359:                assertEquals(28, this .fifteenMinTimeline.getSegmentsIncluded());
0360:                assertEquals(68, this .fifteenMinTimeline.getSegmentsExcluded());
0361:            }
0362:
0363:            //////////////////////////////////////////////////////////////////////////
0364:            // test one-segment and adjacent segments
0365:            //////////////////////////////////////////////////////////////////////////
0366:
0367:            /**
0368:             * Tests one segment of the ms timeline. Internal indices
0369:             * inside one segment as well as adjacent segments are verified.
0370:             */
0371:            public void testMsSegment() {
0372:                verifyOneSegment(this .msTimeline);
0373:            }
0374:
0375:            /**
0376:             * Tests one segment of the ms timeline. Internal indices
0377:             * inside one segment as well as adjacent segments are verified.
0378:             */
0379:            public void testMs2Segment() {
0380:                verifyOneSegment(this .ms2Timeline);
0381:            }
0382:
0383:            /**
0384:             * Tests one segment of the Monday through Friday timeline. Internal indices
0385:             * inside one segment as well as adjacent segments are verified.
0386:             */
0387:            public void testMondayThroughFridaySegment() {
0388:                verifyOneSegment(this .mondayFridayTimeline);
0389:            }
0390:
0391:            /**
0392:             * Tests one segment of the Fifteen timeline. Internal indices
0393:             * inside one segment as well as adjacent segments are verified.
0394:             */
0395:            public void testFifteenMinSegment() {
0396:                verifyOneSegment(this .fifteenMinTimeline);
0397:            }
0398:
0399:            /**
0400:             * Tests one segment of the Monday through Friday timeline. Internal indices
0401:             * inside one segment as well as adjacent segments are verified.
0402:             * @param timeline the timeline to use for verifications.
0403:             */
0404:            public void verifyOneSegment(SegmentedTimeline timeline) {
0405:
0406:                for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END; testCycle += TEST_CYCLE_INC) {
0407:
0408:                    // get two consecutive segments for various tests
0409:                    SegmentedTimeline.Segment segment1 = timeline
0410:                            .getSegment(this .monday.getTime().getTime()
0411:                                    + testCycle);
0412:                    SegmentedTimeline.Segment segment2 = timeline
0413:                            .getSegment(segment1.getSegmentEnd() + 1);
0414:
0415:                    // verify segments are consecutive and correct
0416:                    assertEquals(segment1.getSegmentNumber() + 1, segment2
0417:                            .getSegmentNumber());
0418:                    assertEquals(segment1.getSegmentEnd() + 1, segment2
0419:                            .getSegmentStart());
0420:                    assertEquals(segment1.getSegmentStart()
0421:                            + timeline.getSegmentSize() - 1, segment1
0422:                            .getSegmentEnd());
0423:                    assertEquals(segment1.getSegmentStart()
0424:                            + timeline.getSegmentSize(), segment2
0425:                            .getSegmentStart());
0426:                    assertEquals(segment1.getSegmentEnd()
0427:                            + timeline.getSegmentSize(), segment2
0428:                            .getSegmentEnd());
0429:
0430:                    // verify various indices inside a segment are the same segment
0431:                    long delta;
0432:                    if (timeline.getSegmentSize() > 1000000) {
0433:                        delta = timeline.getSegmentSize() / 10000;
0434:                    } else if (timeline.getSegmentSize() > 100000) {
0435:                        delta = timeline.getSegmentSize() / 1000;
0436:                    } else if (timeline.getSegmentSize() > 10000) {
0437:                        delta = timeline.getSegmentSize() / 100;
0438:                    } else if (timeline.getSegmentSize() > 1000) {
0439:                        delta = timeline.getSegmentSize() / 10;
0440:                    } else if (timeline.getSegmentSize() > 100) {
0441:                        delta = timeline.getSegmentSize() / 5;
0442:                    } else {
0443:                        delta = 1;
0444:                    }
0445:
0446:                    long start = segment1.getSegmentStart() + delta;
0447:                    long end = segment1.getSegmentStart()
0448:                            + timeline.getSegmentSize() - 1;
0449:                    SegmentedTimeline.Segment lastSeg = timeline
0450:                            .getSegment(segment1.getSegmentStart());
0451:                    SegmentedTimeline.Segment seg;
0452:                    for (long i = start; i < end; i += delta) {
0453:                        seg = timeline.getSegment(i);
0454:                        assertEquals(lastSeg.getSegmentNumber(), seg
0455:                                .getSegmentNumber());
0456:                        assertEquals(lastSeg.getSegmentStart(), seg
0457:                                .getSegmentStart());
0458:                        assertEquals(lastSeg.getSegmentEnd(), seg
0459:                                .getSegmentEnd());
0460:                        assertTrue(lastSeg.getMillisecond() < seg
0461:                                .getMillisecond());
0462:                        lastSeg = seg;
0463:                    }
0464:
0465:                    // try next segment
0466:                    seg = timeline.getSegment(end + 1);
0467:                    assertEquals(segment2.getSegmentNumber(), seg
0468:                            .getSegmentNumber());
0469:                    assertEquals(segment2.getSegmentStart(), seg
0470:                            .getSegmentStart());
0471:                    assertEquals(segment2.getSegmentEnd(), seg.getSegmentEnd());
0472:                }
0473:            }
0474:
0475:            //////////////////////////////////////////////////////////////////////////
0476:            // test inc methods
0477:            //////////////////////////////////////////////////////////////////////////
0478:
0479:            /**
0480:             * Tests the inc methods on the msTimeline.
0481:             */
0482:            public void testMsInc() {
0483:                verifyInc(this .msTimeline);
0484:            }
0485:
0486:            /**
0487:             * Tests the inc methods on the msTimeline.
0488:             */
0489:            public void testMs2Inc() {
0490:                verifyInc(this .ms2Timeline);
0491:            }
0492:
0493:            /**
0494:             * Tests the inc methods on the Monday through Friday timeline.
0495:             */
0496:            public void testMondayThroughFridayInc() {
0497:                verifyInc(this .mondayFridayTimeline);
0498:            }
0499:
0500:            /**
0501:             * Tests the inc methods on the Fifteen minute timeline.
0502:             */
0503:            public void testFifteenMinInc() {
0504:                verifyInc(this .fifteenMinTimeline);
0505:            }
0506:
0507:            /**
0508:             * Tests the inc methods.
0509:             * @param timeline the timeline to use for verifications.
0510:             */
0511:            public void verifyInc(SegmentedTimeline timeline) {
0512:                for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END; testCycle += TEST_CYCLE_INC) {
0513:
0514:                    long m = timeline.getSegmentSize();
0515:                    SegmentedTimeline.Segment segment = timeline
0516:                            .getSegment(testCycle);
0517:                    SegmentedTimeline.Segment seg1 = segment.copy();
0518:                    for (int i = 0; i < 1000; i++) {
0519:
0520:                        // test inc() method
0521:                        SegmentedTimeline.Segment seg2 = seg1.copy();
0522:                        seg2.inc();
0523:
0524:                        if ((seg1.getSegmentEnd() + 1) != seg2
0525:                                .getSegmentStart()) {
0526:                            // logically consecutive segments non-physically consecutive
0527:                            // (with non-contained time in between)
0528:                            assertTrue(!timeline.containsDomainRange(seg1
0529:                                    .getSegmentEnd() + 1, seg2
0530:                                    .getSegmentStart() - 1));
0531:                            assertEquals(0, (seg2.getSegmentStart() - seg1
0532:                                    .getSegmentStart())
0533:                                    % m);
0534:                            assertEquals(0, (seg2.getSegmentEnd() - seg1
0535:                                    .getSegmentEnd())
0536:                                    % m);
0537:                            assertEquals(0, (seg2.getMillisecond() - seg1
0538:                                    .getMillisecond())
0539:                                    % m);
0540:                        } else {
0541:                            // physically consecutive
0542:                            assertEquals(seg1.getSegmentStart() + m, seg2
0543:                                    .getSegmentStart());
0544:                            assertEquals(seg1.getSegmentEnd() + m, seg2
0545:                                    .getSegmentEnd());
0546:                            assertEquals(seg1.getMillisecond() + m, seg2
0547:                                    .getMillisecond());
0548:                        }
0549:
0550:                        // test inc(n) method
0551:                        SegmentedTimeline.Segment seg3 = seg1.copy();
0552:                        SegmentedTimeline.Segment seg4 = seg1.copy();
0553:
0554:                        for (int j = 0; j < i; j++) {
0555:                            seg3.inc();
0556:                        }
0557:                        seg4.inc(i);
0558:
0559:                        assertEquals(seg3.getSegmentStart(), seg4
0560:                                .getSegmentStart());
0561:                        assertEquals(seg3.getSegmentEnd(), seg4.getSegmentEnd());
0562:                        assertEquals(seg3.getMillisecond(), seg4
0563:                                .getMillisecond());
0564:
0565:                        // go to another segment to continue test
0566:                        seg1.inc();
0567:                    }
0568:                }
0569:            }
0570:
0571:            //////////////////////////////////////////////////////////////////////////
0572:            // main include and excluded segments
0573:            //////////////////////////////////////////////////////////////////////////
0574:
0575:            /**
0576:             * Tests that the msTimeline's included and excluded
0577:             * segments are being calculated correctly.
0578:             */
0579:            public void testMsIncludedAndExcludedSegments() {
0580:                verifyIncludedAndExcludedSegments(this .msTimeline, 0);
0581:            }
0582:
0583:            /**
0584:             * Tests that the ms2Timeline's included and excluded
0585:             * segments are being calculated correctly.
0586:             */
0587:            public void testMs2IncludedAndExcludedSegments() {
0588:                verifyIncludedAndExcludedSegments(this .ms2Timeline, 1);
0589:            }
0590:
0591:            /**
0592:             * Tests that the Monday through Friday timeline's included and excluded
0593:             * segments are being calculated correctly. The test is performed starting
0594:             * on the first monday after 1/1/2000 and for five years.
0595:             */
0596:            public void testMondayThroughFridayIncludedAndExcludedSegments() {
0597:                verifyIncludedAndExcludedSegments(this .mondayFridayTimeline,
0598:                        this .monday.getTime().getTime());
0599:            }
0600:
0601:            /**
0602:             * Tests that the Fifteen-Min timeline's included and excluded
0603:             * segments are being calculated correctly. The test is performed starting
0604:             * on the first monday after 1/1/2000 and for five years.
0605:             */
0606:            public void testFifteenMinIncludedAndExcludedSegments() {
0607:                verifyIncludedAndExcludedSegments(this .fifteenMinTimeline,
0608:                        this .monday9am.getTime().getTime());
0609:            }
0610:
0611:            /**
0612:             * Tests that a timeline's included and excluded segments are being 
0613:             * calculated correctly.
0614:             * 
0615:             * @param timeline the timeline to verify
0616:             * @param n the first segment number to start verifying
0617:             */
0618:            public void verifyIncludedAndExcludedSegments(
0619:                    SegmentedTimeline timeline, long n) {
0620:                // clear any exceptions in this timeline
0621:                timeline.setExceptionSegments(new java.util.ArrayList());
0622:
0623:                // test some included and excluded segments
0624:                SegmentedTimeline.Segment segment = timeline.getSegment(n);
0625:                for (int i = 0; i < 1000; i++) {
0626:                    int d = (i % timeline.getGroupSegmentCount());
0627:                    if (d < timeline.getSegmentsIncluded()) {
0628:                        // should be an included segment
0629:                        assertTrue(segment.inIncludeSegments());
0630:                        assertTrue(!segment.inExcludeSegments());
0631:                        assertTrue(!segment.inExceptionSegments());
0632:                    } else {
0633:                        // should be an excluded segment
0634:                        assertTrue(!segment.inIncludeSegments());
0635:                        assertTrue(segment.inExcludeSegments());
0636:                        assertTrue(!segment.inExceptionSegments());
0637:                    }
0638:                    segment.inc();
0639:                }
0640:            }
0641:
0642:            //////////////////////////////////////////////////////////////////////////
0643:            // test exception segments
0644:            //////////////////////////////////////////////////////////////////////////
0645:
0646:            /**
0647:             * Tests methods related to exceptions methods in the msTimeline.
0648:             * 
0649:             * @throws ParseException if there is a parsing error.
0650:             */
0651:            public void testMsExceptionSegments() throws ParseException {
0652:                verifyExceptionSegments(this .msTimeline, MS_EXCEPTIONS,
0653:                        NUMBER_FORMAT);
0654:            }
0655:
0656:            /**
0657:             * Tests methods related to exceptions methods in the ms2BaseTimeline.
0658:             * 
0659:             * @throws ParseException if there is a parsing error.
0660:             */
0661:            public void testMs2BaseTimelineExceptionSegments()
0662:                    throws ParseException {
0663:                verifyExceptionSegments(this .ms2BaseTimeline,
0664:                        MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
0665:            }
0666:
0667:            /**
0668:             * Tests methods related to exceptions methods in the mondayFridayTimeline.
0669:             * 
0670:             * @throws ParseException if there is a parsing error.
0671:             */
0672:            public void testMondayThoughFridayExceptionSegments()
0673:                    throws ParseException {
0674:                verifyExceptionSegments(this .mondayFridayTimeline, US_HOLIDAYS,
0675:                        DATE_FORMAT);
0676:            }
0677:
0678:            /**
0679:             * Tests methods related to exceptions methods in the fifteenMinTimeline.
0680:             * 
0681:             * @throws ParseException if there is a parsing error.
0682:             */
0683:            public void testFifteenMinExceptionSegments() throws ParseException {
0684:                verifyExceptionSegments(this .fifteenMinTimeline,
0685:                        FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT);
0686:            }
0687:
0688:            /**
0689:             * Tests methods related to adding exceptions.
0690:             * 
0691:             * @param timeline the timeline to verify
0692:             * @param exceptionString array of Strings that represent the exceptions
0693:             * @param fmt Format object that can parse the exceptionString strings
0694:             * 
0695:             * @throws ParseException if there is a parsing error.
0696:             */
0697:            public void verifyExceptionSegments(SegmentedTimeline timeline,
0698:                    String[] exceptionString, Format fmt) throws ParseException {
0699:
0700:                // fill in the exceptions
0701:                long[] exception = verifyFillInExceptions(timeline,
0702:                        exceptionString, fmt);
0703:
0704:                int m = exception.length;
0705:
0706:                // verify list of exceptions
0707:                assertEquals(exception.length, timeline.getExceptionSegments()
0708:                        .size());
0709:                SegmentedTimeline.Segment lastSegment = timeline
0710:                        .getSegment(exception[m - 1]);
0711:                for (int i = 0; i < m; i++) {
0712:                    SegmentedTimeline.Segment segment = timeline
0713:                            .getSegment(exception[i]);
0714:                    assertTrue(segment.inExceptionSegments());
0715:                    // include current exception and last one
0716:                    assertEquals(m - i, timeline.getExceptionSegmentCount(
0717:                            segment.getSegmentStart(), lastSegment
0718:                                    .getSegmentEnd()));
0719:                    // exclude current exception and last one
0720:                    assertEquals(Math.max(0, m - i - 2), timeline
0721:                            .getExceptionSegmentCount(exception[i] + 1,
0722:                                    exception[m - 1] - 1));
0723:                }
0724:
0725:            }
0726:
0727:            //////////////////////////////////////////////////////////////////////////
0728:            // test timeline translations
0729:            //////////////////////////////////////////////////////////////////////////
0730:
0731:            /**
0732:             * Tests translations for 1-ms timeline
0733:             * 
0734:             * @throws ParseException if there is a parsing error.
0735:             */
0736:            public void testMsTranslations() throws ParseException {
0737:                verifyFillInExceptions(this .msTimeline, MS_EXCEPTIONS,
0738:                        NUMBER_FORMAT);
0739:                verifyTranslations(this .msTimeline, 0);
0740:            }
0741:
0742:            /**
0743:             * Tests translations for the base timeline used for the ms2Timeline
0744:             * 
0745:             * @throws ParseException if there is a parsing error.
0746:             */
0747:            public void testMs2BaseTimelineTranslations() throws ParseException {
0748:                verifyFillInExceptions(this .ms2BaseTimeline,
0749:                        MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
0750:                verifyTranslations(this .ms2BaseTimeline, 0);
0751:            }
0752:
0753:            /**
0754:             * Tests translations for the Monday through Friday timeline
0755:             * 
0756:             * @throws ParseException if there is a parsing error.
0757:             */
0758:            public void testMs2Translations() throws ParseException {
0759:                fillInBaseTimelineExceptions(this .ms2Timeline,
0760:                        MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
0761:                fillInBaseTimelineExclusionsAsExceptions(this .ms2Timeline, 0,
0762:                        5000);
0763:                verifyTranslations(this .ms2Timeline, 1);
0764:            }
0765:
0766:            /**
0767:             * Tests translations for the Monday through Friday timeline
0768:             * 
0769:             * @throws ParseException if there is a parsing error.
0770:             */
0771:            public void testMondayThroughFridayTranslations()
0772:                    throws ParseException {
0773:                verifyFillInExceptions(this .mondayFridayTimeline, US_HOLIDAYS,
0774:                        DATE_FORMAT);
0775:                verifyTranslations(this .mondayFridayTimeline, this .monday
0776:                        .getTime().getTime());
0777:            }
0778:
0779:            /**
0780:             * Tests translations for the Fifteen Min timeline
0781:             * 
0782:             * @throws ParseException if there is a parsing error.
0783:             */
0784:            public void testFifteenMinTranslations() throws ParseException {
0785:                verifyFillInExceptions(this .fifteenMinTimeline,
0786:                        FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT);
0787:                fillInBaseTimelineExceptions(this .fifteenMinTimeline,
0788:                        US_HOLIDAYS, DATE_FORMAT);
0789:                fillInBaseTimelineExclusionsAsExceptions(
0790:                        this .fifteenMinTimeline, this .monday9am.getTime()
0791:                                .getTime(), this .monday9am.getTime().getTime()
0792:                                + FIVE_YEARS);
0793:                verifyTranslations(this .fifteenMinTimeline, this .monday9am
0794:                        .getTime().getTime());
0795:            }
0796:
0797:            /**
0798:             * Tests translations between timelines.
0799:             * 
0800:             * @param timeline the timeline to use for verifications.
0801:             * @param startTest  ??.
0802:             */
0803:            public void verifyTranslations(SegmentedTimeline timeline,
0804:                    long startTest) {
0805:                for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END; testCycle += TEST_CYCLE_INC) {
0806:
0807:                    long millisecond = startTest + testCycle
0808:                            * timeline.getSegmentSize();
0809:                    SegmentedTimeline.Segment segment = timeline
0810:                            .getSegment(millisecond);
0811:
0812:                    for (int i = 0; i < 1000; i++) {
0813:                        long translatedValue = timeline.toTimelineValue(segment
0814:                                .getMillisecond());
0815:                        long newValue = timeline.toMillisecond(translatedValue);
0816:
0817:                        if (segment.inExcludeSegments()
0818:                                || segment.inExceptionSegments()) {
0819:                            // the reverse transformed value will be in the start of the
0820:                            // next non-excluded and non-exception segment
0821:                            SegmentedTimeline.Segment tempSegment = segment
0822:                                    .copy();
0823:                            tempSegment.moveIndexToStart();
0824:                            do {
0825:                                tempSegment.inc();
0826:                            } while (!tempSegment.inIncludeSegments());
0827:                            assertEquals(tempSegment.getMillisecond(), newValue);
0828:                        }
0829:
0830:                        else {
0831:                            assertEquals(segment.getMillisecond(), newValue);
0832:                        }
0833:                        segment.inc();
0834:                    }
0835:                }
0836:            }
0837:
0838:            //////////////////////////////////////////////////////////////////////////
0839:            // test serialization
0840:            //////////////////////////////////////////////////////////////////////////
0841:
0842:            /**
0843:             * Serialize an instance, restore it, and check for equality.
0844:             */
0845:            public void testSerialization() {
0846:                verifySerialization(this .msTimeline);
0847:                verifySerialization(this .ms2Timeline);
0848:                verifySerialization(this .ms2BaseTimeline);
0849:                verifySerialization(SegmentedTimeline
0850:                        .newMondayThroughFridayTimeline());
0851:                verifySerialization(SegmentedTimeline
0852:                        .newFifteenMinuteTimeline());
0853:            }
0854:
0855:            /**
0856:             * Tests serialization of an instance.
0857:             * @param a1 The timeline to verify the serialization
0858:             */
0859:            private void verifySerialization(SegmentedTimeline a1) {
0860:                SegmentedTimeline a2 = null;
0861:
0862:                try {
0863:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
0864:                    ObjectOutput out = new ObjectOutputStream(buffer);
0865:                    out.writeObject(a1);
0866:                    out.close();
0867:
0868:                    ObjectInput in = new ObjectInputStream(
0869:                            new ByteArrayInputStream(buffer.toByteArray()));
0870:                    a2 = (SegmentedTimeline) in.readObject();
0871:                    in.close();
0872:                } catch (Exception e) {
0873:                    System.out.println(e.toString());
0874:                }
0875:                assertEquals(a1, a2);
0876:            }
0877:
0878:            /**
0879:             * Adds an array of exceptions to the timeline. The timeline exception list
0880:             * is first cleared.
0881:             * @param timeline The timeline where the exceptions will be stored
0882:             * @param exceptionString The exceptions to load
0883:             * @param fmt The date formatter to use to parse each exceptions[i] value
0884:             * @throws ParseException If there is any exception parsing each 
0885:             *         exceptions[i] value.
0886:             * @return An array of Dates[] containing each exception date.
0887:             */
0888:            private long[] verifyFillInExceptions(SegmentedTimeline timeline,
0889:                    String[] exceptionString, Format fmt) throws ParseException {
0890:                // make sure there are no exceptions
0891:                timeline.setExceptionSegments(new java.util.ArrayList());
0892:                assertEquals(0, timeline.getExceptionSegments().size());
0893:
0894:                // add our exceptions and store locally in ArrayList of Longs
0895:                ArrayList exceptionList = new ArrayList();
0896:                for (int i = 0; i < exceptionString.length; i++) {
0897:                    long e;
0898:                    if (fmt instanceof  NumberFormat) {
0899:                        e = ((NumberFormat) fmt).parse(exceptionString[i])
0900:                                .longValue();
0901:                    } else {
0902:                        e = timeline.getTime(((SimpleDateFormat) fmt)
0903:                                .parse(exceptionString[i]));
0904:                    }
0905:                    // only add an exception if it is currently an included segment
0906:                    SegmentedTimeline.Segment segment = timeline.getSegment(e);
0907:                    if (segment.inIncludeSegments()) {
0908:                        timeline.addException(e);
0909:                        exceptionList.add(new Long(e));
0910:                        assertEquals(exceptionList.size(), timeline
0911:                                .getExceptionSegments().size());
0912:                        assertTrue(segment.inExceptionSegments());
0913:                    }
0914:                }
0915:
0916:                // make array of exceptions
0917:                long[] exception = new long[exceptionList.size()];
0918:                int i = 0;
0919:                for (Iterator iter = exceptionList.iterator(); iter.hasNext();) {
0920:                    Long l = (Long) iter.next();
0921:                    exception[i++] = l.longValue();
0922:                }
0923:
0924:                return (exception);
0925:
0926:            }
0927:
0928:            /**
0929:             * Adds an array of exceptions relative to the base timeline.
0930:             *
0931:             * @param timeline The timeline where the exceptions will be stored
0932:             * @param exceptionString The exceptions to load
0933:             * @param fmt The date formatter to use to parse each exceptions[i] value
0934:             * @throws ParseException If there is any exception parsing each 
0935:             *                        exceptions[i] value.
0936:             */
0937:            private void fillInBaseTimelineExceptions(
0938:                    SegmentedTimeline timeline, String[] exceptionString,
0939:                    Format fmt) throws ParseException {
0940:                SegmentedTimeline baseTimeline = timeline.getBaseTimeline();
0941:                for (int i = 0; i < exceptionString.length; i++) {
0942:                    long e;
0943:                    if (fmt instanceof  NumberFormat) {
0944:                        e = ((NumberFormat) fmt).parse(exceptionString[i])
0945:                                .longValue();
0946:                    } else {
0947:                        e = timeline.getTime(((SimpleDateFormat) fmt)
0948:                                .parse(exceptionString[i]));
0949:                    }
0950:                    timeline.addBaseTimelineException(e);
0951:
0952:                    // verify all timeline segments included in the 
0953:                    // baseTimeline.segment are now exceptions
0954:                    SegmentedTimeline.Segment segment1 = baseTimeline
0955:                            .getSegment(e);
0956:                    for (SegmentedTimeline.Segment segment2 = timeline
0957:                            .getSegment(segment1.getSegmentStart()); segment2
0958:                            .getSegmentStart() <= segment1.getSegmentEnd(); segment2
0959:                            .inc()) {
0960:                        if (!segment2.inExcludeSegments()) {
0961:                            assertTrue(segment2.inExceptionSegments());
0962:                        }
0963:                    }
0964:
0965:                }
0966:            }
0967:
0968:            /**
0969:             * Adds new exceptions to a timeline. The exceptions are the excluded 
0970:             * segments from its base timeline.
0971:             *
0972:             * @param timeline  the timeline.
0973:             * @param from  the start.
0974:             * @param to  the end.
0975:             */
0976:            private void fillInBaseTimelineExclusionsAsExceptions(
0977:                    SegmentedTimeline timeline, long from, long to) {
0978:
0979:                // add the base timeline exclusions as timeline's esceptions
0980:                timeline.addBaseTimelineExclusions(from, to);
0981:
0982:                // validate base timeline exclusions added as timeline's esceptions
0983:                for (SegmentedTimeline.Segment segment1 = timeline
0984:                        .getBaseTimeline().getSegment(from); segment1
0985:                        .getSegmentStart() <= to; segment1.inc()) {
0986:
0987:                    if (segment1.inExcludeSegments()) {
0988:
0989:                        // verify all timeline segments included in the 
0990:                        // baseTimeline.segment are now exceptions
0991:                        for (SegmentedTimeline.Segment segment2 = timeline
0992:                                .getSegment(segment1.getSegmentStart()); segment2
0993:                                .getSegmentStart() <= segment1.getSegmentEnd(); segment2
0994:                                .inc()) {
0995:                            if (!segment2.inExcludeSegments()) {
0996:                                assertTrue(segment2.inExceptionSegments());
0997:                            }
0998:                        }
0999:                    }
1000:                }
1001:            }
1002:
1003:            /**
1004:             * Confirm that cloning works.
1005:             */
1006:            public void testCloning() {
1007:                SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
1008:                SegmentedTimeline l2 = null;
1009:                try {
1010:                    l2 = (SegmentedTimeline) l1.clone();
1011:                } catch (CloneNotSupportedException e) {
1012:                    System.err.println("Failed to clone.");
1013:                }
1014:                assertTrue(l1 != l2);
1015:                assertTrue(l1.getClass() == l2.getClass());
1016:                assertTrue(l1.equals(l2));
1017:            }
1018:
1019:            /**
1020:             * Confirm that the equals method can distinguish all the required fields.
1021:             */
1022:            public void testEquals() {
1023:
1024:                SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
1025:                SegmentedTimeline l2 = new SegmentedTimeline(1000, 5, 2);
1026:                assertTrue(l1.equals(l2));
1027:
1028:                l1 = new SegmentedTimeline(1000, 5, 2);
1029:                l2 = new SegmentedTimeline(1001, 5, 2);
1030:                assertFalse(l1.equals(l2));
1031:
1032:                l1 = new SegmentedTimeline(1000, 5, 2);
1033:                l2 = new SegmentedTimeline(1000, 4, 2);
1034:                assertFalse(l1.equals(l2));
1035:
1036:                l1 = new SegmentedTimeline(1000, 5, 2);
1037:                l2 = new SegmentedTimeline(1000, 5, 1);
1038:                assertFalse(l1.equals(l2));
1039:
1040:                l1 = new SegmentedTimeline(1000, 5, 2);
1041:                l2 = new SegmentedTimeline(1000, 5, 2);
1042:
1043:                // start time...
1044:                l1.setStartTime(1234L);
1045:                assertFalse(l1.equals(l2));
1046:                l2.setStartTime(1234L);
1047:                assertTrue(l1.equals(l2));
1048:
1049:            }
1050:
1051:            /**
1052:             * Two objects that are equal are required to return the same hashCode. 
1053:             */
1054:            public void testHashCode() {
1055:                SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
1056:                SegmentedTimeline l2 = new SegmentedTimeline(1000, 5, 2);
1057:                assertTrue(l1.equals(l2));
1058:                int h1 = l1.hashCode();
1059:                int h2 = l2.hashCode();
1060:                assertEquals(h1, h2);
1061:            }
1062:
1063:            /**
1064:             * Serialize an instance, restore it, and check for equality.
1065:             */
1066:            public void testSerialization2() {
1067:
1068:                SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
1069:                SegmentedTimeline l2 = null;
1070:
1071:                try {
1072:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
1073:                    ObjectOutput out = new ObjectOutputStream(buffer);
1074:                    out.writeObject(l1);
1075:                    out.close();
1076:
1077:                    ObjectInput in = new ObjectInputStream(
1078:                            new ByteArrayInputStream(buffer.toByteArray()));
1079:                    l2 = (SegmentedTimeline) in.readObject();
1080:                    in.close();
1081:                } catch (Exception e) {
1082:                    System.out.println(e.toString());
1083:                }
1084:                boolean b = l1.equals(l2);
1085:                assertTrue(b);
1086:
1087:            }
1088:
1089:            //////////////////////////////////////////////////////////////////////////
1090:            // utility methods
1091:            //////////////////////////////////////////////////////////////////////////
1092:
1093:            /**
1094:             * Tests a basic segmented timeline.
1095:             */
1096:            public void testBasicSegmentedTimeline() {
1097:                SegmentedTimeline stl = new SegmentedTimeline(10, 2, 3);
1098:                stl.setStartTime(946684800000L); // 1-Jan-2000
1099:                assertFalse(stl.containsDomainValue(946684799999L));
1100:                assertTrue(stl.containsDomainValue(946684800000L));
1101:                assertTrue(stl.containsDomainValue(946684800019L));
1102:                assertFalse(stl.containsDomainValue(946684800020L));
1103:                assertFalse(stl.containsDomainValue(946684800049L));
1104:                assertTrue(stl.containsDomainValue(946684800050L));
1105:                assertTrue(stl.containsDomainValue(946684800069L));
1106:                assertFalse(stl.containsDomainValue(946684800070L));
1107:                assertFalse(stl.containsDomainValue(946684800099L));
1108:                assertTrue(stl.containsDomainValue(946684800100L));
1109:
1110:                assertEquals(0, stl.toTimelineValue(946684800000L));
1111:                assertEquals(19, stl.toTimelineValue(946684800019L));
1112:                assertEquals(20, stl.toTimelineValue(946684800020L));
1113:                assertEquals(20, stl.toTimelineValue(946684800049L));
1114:                assertEquals(20, stl.toTimelineValue(946684800050L));
1115:                assertEquals(39, stl.toTimelineValue(946684800069L));
1116:                assertEquals(40, stl.toTimelineValue(946684800070L));
1117:                assertEquals(40, stl.toTimelineValue(946684800099L));
1118:                assertEquals(40, stl.toTimelineValue(946684800100L));
1119:
1120:                assertEquals(946684800000L, stl.toMillisecond(0));
1121:                assertEquals(946684800019L, stl.toMillisecond(19));
1122:                assertEquals(946684800050L, stl.toMillisecond(20));
1123:                assertEquals(946684800069L, stl.toMillisecond(39));
1124:                assertEquals(946684800100L, stl.toMillisecond(40));
1125:
1126:            }
1127:
1128:            /**
1129:             * Tests a basic time line with one exception.
1130:             */
1131:            public void testSegmentedTimelineWithException1() {
1132:                SegmentedTimeline stl = new SegmentedTimeline(10, 2, 3);
1133:                stl.setStartTime(946684800000L); // 1-Jan-2000
1134:                stl.addException(946684800050L);
1135:                assertFalse(stl.containsDomainValue(946684799999L));
1136:                assertTrue(stl.containsDomainValue(946684800000L));
1137:                assertTrue(stl.containsDomainValue(946684800019L));
1138:                assertFalse(stl.containsDomainValue(946684800020L));
1139:                assertFalse(stl.containsDomainValue(946684800049L));
1140:                assertFalse(stl.containsDomainValue(946684800050L));
1141:                assertFalse(stl.containsDomainValue(946684800059L));
1142:                assertTrue(stl.containsDomainValue(946684800060L));
1143:                assertTrue(stl.containsDomainValue(946684800069L));
1144:                assertFalse(stl.containsDomainValue(946684800070L));
1145:                assertFalse(stl.containsDomainValue(946684800099L));
1146:                assertTrue(stl.containsDomainValue(946684800100L));
1147:
1148:                //long v = stl.toTimelineValue(946684800020L);
1149:                assertEquals(0, stl.toTimelineValue(946684800000L));
1150:                assertEquals(19, stl.toTimelineValue(946684800019L));
1151:                assertEquals(20, stl.toTimelineValue(946684800020L));
1152:                assertEquals(20, stl.toTimelineValue(946684800049L));
1153:                assertEquals(20, stl.toTimelineValue(946684800050L));
1154:                assertEquals(29, stl.toTimelineValue(946684800069L));
1155:                assertEquals(30, stl.toTimelineValue(946684800070L));
1156:                assertEquals(30, stl.toTimelineValue(946684800099L));
1157:                assertEquals(30, stl.toTimelineValue(946684800100L));
1158:
1159:                assertEquals(946684800000L, stl.toMillisecond(0));
1160:                assertEquals(946684800019L, stl.toMillisecond(19));
1161:                assertEquals(946684800060L, stl.toMillisecond(20));
1162:                assertEquals(946684800069L, stl.toMillisecond(29));
1163:                assertEquals(946684800100L, stl.toMillisecond(30));
1164:
1165:            }
1166:
1167:            //////////////////////////////////////////////////////////////////////////
1168:            // main method only for debug
1169:            //////////////////////////////////////////////////////////////////////////
1170:
1171:            /**
1172:             * Only use to debug JUnit suite.
1173:             * 
1174:             * @param args  ignored.
1175:             * 
1176:             * @throws Exception if there is some problem.
1177:             */
1178:            public static void main(String[] args) throws Exception {
1179:                SegmentedTimelineTests test = new SegmentedTimelineTests("Test");
1180:                test.setUp();
1181:                test.testMondayThoughFridayExceptionSegments();
1182:                test.tearDown();
1183:            }
1184:
1185:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.