1 /* 2 * ==================================================================== The 3 * Apache Software License, Version 1.1 4 * 5 * Copyright (c) 2003 Digital Clash LLC. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The end-user documentation included with the redistribution, if any, 15 * must include the following acknowledgment: "This product includes software 16 * developed by the ChronicJ team (http://www.chronicj.org/)." Alternately, 17 * this acknowledgment may appear in the software itself, if and wherever such 18 * third-party acknowledgments normally appear. 19 * 4. The names "ChronicJ" and "Digital Clash" not be used to endorse or 20 * promote products derived from this software without prior written 21 * permission. For written permission, please contact info@digitalclash.com. 22 * 5. Products derived from this software may not be called "ChronicJ", 23 * "Digital Clash", nor may "ChronicJ" or "Digital Clash" appear in their name, 24 * without prior written permission of Digital Clash LLC. 25 * 26 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 28 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 29 * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * ==================================================================== This 37 * product includes software developed by the by the Apache Software Foundation 38 * (http://www.apache.org/). 39 * ==================================================================== 40 */ 41 package org.chronicj; 42 43 import org.chronicj.DatePrecision; 44 import org.chronicj.DateRange; 45 import org.chronicj.TimePoint; 46 47 import java.util.Date; 48 import java.util.GregorianCalendar; 49 50 51 /*** 52 * DateRange unit tests. 53 * 54 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a> 55 * 56 * @see <a href="http://junit.org">JUnit</a> 57 */ 58 public class DateRangeTest extends BaseTestCase { 59 public DateRangeTest(String arg0) { 60 super(arg0); 61 } 62 63 public void testAbuts() { 64 TimePoint start = new TimePoint(2003, 8, 28, 6, 0); 65 TimePoint end = start.addMinutes(15); 66 67 DateRange aRange = new DateRange(start, end); 68 69 //Minutes is the default precision - seconds do not count! 70 DateRange anAbuttingRange = new DateRange(end.addMinutes(1), 71 end.addMinutes(30)); 72 73 assertTrue(aRange.gap(anAbuttingRange).isEmpty()); 74 75 assertTrue(aRange.abuts(anAbuttingRange)); 76 } 77 78 public void testCompareTo() { 79 TimePoint aTimePoint = new TimePoint(2003, 7, 28, 2, 59); 80 81 TimePoint tp1 = aTimePoint.addDays(0); 82 83 TimePoint tp2 = aTimePoint.addDays(3); 84 85 DateRange aRange = new DateRange(tp1, tp2); 86 87 //This range should overlap 88 DateRange earlierRange = new DateRange(tp1.addDays(-1), tp2.addDays(-1)); 89 90 int result = aRange.compareTo(earlierRange); 91 92 assertTrue(aRange.compareTo(earlierRange) == 1); 93 94 assertTrue(earlierRange.compareTo(aRange) == -1); 95 96 assertTrue(aRange.compareTo(aRange) == 0); 97 } 98 99 public void testEnd() { 100 TimePoint start = new TimePoint(2003, 1, 1); 101 TimePoint end = new TimePoint(new Date(), DatePrecision.MONTH); 102 DateRange aRange = new DateRange(start, end); 103 TimePoint aTimePoint = new TimePoint(new GregorianCalendar(), 104 DatePrecision.MONTH); 105 assertTrue(aRange.end().equals(aTimePoint)); 106 } 107 108 public void testEquals() { 109 TimePoint start = new TimePoint(2003, 7, 28, 11, 59); 110 111 TimePoint highPrecision = start.addDays(1).addHours(3).addMinutes(5); 112 113 TimePoint lowPrecision = start.addDays(1); 114 115 DateRange aRange = new DateRange(start, highPrecision); 116 117 DateRange anotherRange = new DateRange(new TimePoint(2003, 7, 28, 11, 59), 118 highPrecision); 119 120 assertEquals(aRange, anotherRange); 121 122 DateRange lowPrecisionRange = new DateRange(new TimePoint(2003, 7, 28, 123 11, 59), lowPrecision); 124 125 assertTrue(!aRange.equals(lowPrecisionRange)); 126 } 127 128 public void testGap() { 129 TimePoint startingPoint = new TimePoint(2001, 7, 28, 10, 0); 130 131 DateRange aRange = new DateRange(startingPoint, 132 startingPoint.addMinutes(10)); 133 134 //same range starting 60 minutes later 135 DateRange anotherRange = new DateRange(startingPoint.addHours(1), 136 startingPoint.addHours(1).addMinutes(10)); 137 138 DateRange gap = aRange.gap(anotherRange); 139 140 DateRange expectedResult = new DateRange(startingPoint.addMinutes(11), 141 startingPoint.addHours(1).addMinutes(-1)); 142 143 assertEquals(gap, expectedResult); 144 } 145 146 public void testIncludes() { 147 DateRange aRange = new DateRange(new TimePoint(2003, 12, 12, 10, 0), 148 new TimePoint(2003, 12, 12, 13, 45)); 149 150 DateRange aSubRange = new DateRange(new TimePoint(2003, 12, 12, 10, 1), 151 new TimePoint(2003, 12, 12, 12, 15)); 152 153 assertTrue(aRange.includes(aSubRange)); 154 } 155 156 public void testIsEmpty() { 157 TimePoint start = new TimePoint(2003, 12, 12); 158 TimePoint end = new TimePoint(2003, 10, 12); 159 DateRange aRange = new DateRange(start, end); 160 assertTrue(aRange.isEmpty()); 161 } 162 163 public void testOverlaps() { 164 TimePoint startingPoint = new TimePoint(2004, 10, 29, 12, 12); 165 166 DateRange aRange = new DateRange(startingPoint, 167 startingPoint.addHours(10)); 168 169 DateRange anotherRange = new DateRange(startingPoint.addHours(-1), 170 startingPoint.addHours(1).addMinutes(10)); 171 172 assertTrue(aRange.overlaps(anotherRange)); 173 } 174 175 public void testPartitionedBy() { 176 TimePoint start = new TimePoint(2004, 10, 30, 12, 40); 177 TimePoint end = new TimePoint(2004, 11, 1, 12, 25); 178 179 //Time period 2004-10-30 12:40 thru 2004-11-1 2:25 180 DateRange aRange = new DateRange(start, end); 181 182 DateRange subRange1 = new DateRange(start, start.addDays(1)); 183 DateRange subRange2 = new DateRange(start.addDays(1).addMinutes(1), 184 start.addDays(1).addMinutes(11)); 185 DateRange subRange3 = new DateRange(start.addDays(1).addMinutes(12), end); 186 187 assertTrue(aRange.partitionedBy( 188 new DateRange[] { subRange1, subRange2, subRange3 })); 189 } 190 191 public void testStart() { 192 TimePoint start = new TimePoint(new Date(), DatePrecision.MINUTE); 193 TimePoint end = new TimePoint(2003, 1, 1); 194 DateRange aRange = new DateRange(start, end); 195 TimePoint aTimePoint = new TimePoint(new GregorianCalendar(), 196 DatePrecision.MINUTE); 197 assertTrue(aRange.start().equals(aTimePoint)); 198 } 199 200 /* 201 * @see TestCase#setUp() 202 */ 203 protected void setUp() throws Exception { 204 super.setUp(); 205 } 206 207 /* 208 * @see TestCase#tearDown() 209 */ 210 protected void tearDown() throws Exception { 211 super.tearDown(); 212 } 213 }

This page was automatically generated by Maven