001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time;
017:
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020:
021: import org.joda.time.chrono.BuddhistChronology;
022: import org.joda.time.chrono.CopticChronology;
023: import org.joda.time.chrono.ISOChronology;
024:
025: /**
026: * This class is a Junit unit test for Partial.
027: *
028: * @author Stephen Colebourne
029: */
030: public class TestPartial_Match extends TestCase {
031:
032: private static final DateTimeZone PARIS = DateTimeZone
033: .forID("Europe/Paris");
034: private static final DateTimeZone LONDON = DateTimeZone
035: .forID("Europe/London");
036: private static final DateTimeZone TOKYO = DateTimeZone
037: .forID("Asia/Tokyo");
038: private static final int OFFSET = 1;
039: private static final Chronology COPTIC_PARIS = CopticChronology
040: .getInstance(PARIS);
041: private static final Chronology COPTIC_LONDON = CopticChronology
042: .getInstance(LONDON);
043: private static final Chronology COPTIC_TOKYO = CopticChronology
044: .getInstance(TOKYO);
045: private static final Chronology COPTIC_UTC = CopticChronology
046: .getInstanceUTC();
047: private static final Chronology ISO_PARIS = ISOChronology
048: .getInstance(PARIS);
049: private static final Chronology ISO_LONDON = ISOChronology
050: .getInstance(LONDON);
051: private static final Chronology ISO_TOKYO = ISOChronology
052: .getInstance(TOKYO);
053: private static final Chronology ISO_UTC = ISOChronology
054: .getInstanceUTC();
055: private static final Chronology BUDDHIST_PARIS = BuddhistChronology
056: .getInstance(PARIS);
057: private static final Chronology BUDDHIST_LONDON = BuddhistChronology
058: .getInstance(LONDON);
059: private static final Chronology BUDDHIST_TOKYO = BuddhistChronology
060: .getInstance(TOKYO);
061: private static final Chronology BUDDHIST_UTC = BuddhistChronology
062: .getInstanceUTC();
063:
064: private long TEST_TIME_NOW = 10L
065: * DateTimeConstants.MILLIS_PER_HOUR + 20L
066: * DateTimeConstants.MILLIS_PER_MINUTE + 30L
067: * DateTimeConstants.MILLIS_PER_SECOND + 40L;
068:
069: private long TEST_TIME1 = 1L * DateTimeConstants.MILLIS_PER_HOUR
070: + 2L * DateTimeConstants.MILLIS_PER_MINUTE + 3L
071: * DateTimeConstants.MILLIS_PER_SECOND + 4L;
072:
073: private long TEST_TIME2 = 1L * DateTimeConstants.MILLIS_PER_DAY
074: + 5L * DateTimeConstants.MILLIS_PER_HOUR + 6L
075: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
076: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
077:
078: private DateTimeZone zone = null;
079:
080: public static void main(String[] args) {
081: junit.textui.TestRunner.run(suite());
082: }
083:
084: public static TestSuite suite() {
085: return new TestSuite(TestPartial_Match.class);
086: }
087:
088: public TestPartial_Match(String name) {
089: super (name);
090: }
091:
092: protected void setUp() throws Exception {
093: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
094: zone = DateTimeZone.getDefault();
095: DateTimeZone.setDefault(LONDON);
096: }
097:
098: protected void tearDown() throws Exception {
099: DateTimeUtils.setCurrentMillisSystem();
100: DateTimeZone.setDefault(zone);
101: zone = null;
102: }
103:
104: //-----------------------------------------------------------------------
105: public void testIsMatch_Instant() {
106: // Year=2005, Month=7 (July), DayOfWeek=2 (Tuesday)
107: Partial test = createYMDwPartial(ISO_UTC, 2005, 7, 2);
108: DateTime instant = new DateTime(2005, 7, 5, 0, 0, 0, 0);
109: assertEquals(true, test.isMatch(instant));
110:
111: instant = new DateTime(2005, 7, 4, 0, 0, 0, 0);
112: assertEquals(false, test.isMatch(instant));
113:
114: instant = new DateTime(2005, 7, 6, 0, 0, 0, 0);
115: assertEquals(false, test.isMatch(instant));
116:
117: instant = new DateTime(2005, 7, 12, 0, 0, 0, 0);
118: assertEquals(true, test.isMatch(instant));
119:
120: instant = new DateTime(2005, 7, 19, 0, 0, 0, 0);
121: assertEquals(true, test.isMatch(instant));
122:
123: instant = new DateTime(2005, 7, 26, 0, 0, 0, 0);
124: assertEquals(true, test.isMatch(instant));
125:
126: instant = new DateTime(2005, 8, 2, 0, 0, 0, 0);
127: assertEquals(false, test.isMatch(instant));
128:
129: instant = new DateTime(2006, 7, 5, 0, 0, 0, 0);
130: assertEquals(false, test.isMatch(instant));
131:
132: instant = new DateTime(2005, 6, 5, 0, 0, 0, 0);
133: assertEquals(false, test.isMatch(instant));
134: }
135:
136: //-----------------------------------------------------------------------
137: public void testIsMatch_Partial() {
138: // Year=2005, Month=7 (July), DayOfWeek=2 (Tuesday)
139: Partial test = createYMDwPartial(ISO_UTC, 2005, 7, 2);
140: LocalDate partial = new LocalDate(2005, 7, 5);
141: assertEquals(true, test.isMatch(partial));
142:
143: partial = new LocalDate(2005, 7, 4);
144: assertEquals(false, test.isMatch(partial));
145:
146: partial = new LocalDate(2005, 7, 6);
147: assertEquals(false, test.isMatch(partial));
148:
149: partial = new LocalDate(2005, 7, 12);
150: assertEquals(true, test.isMatch(partial));
151:
152: partial = new LocalDate(2005, 7, 19);
153: assertEquals(true, test.isMatch(partial));
154:
155: partial = new LocalDate(2005, 7, 26);
156: assertEquals(true, test.isMatch(partial));
157:
158: partial = new LocalDate(2005, 8, 2);
159: assertEquals(false, test.isMatch(partial));
160:
161: partial = new LocalDate(2006, 7, 5);
162: assertEquals(false, test.isMatch(partial));
163:
164: partial = new LocalDate(2005, 6, 5);
165: assertEquals(false, test.isMatch(partial));
166: }
167:
168: //-----------------------------------------------------------------------
169: private Partial createYMDwPartial(Chronology chrono, int year,
170: int month, int dow) {
171: return new Partial(new DateTimeFieldType[] {
172: DateTimeFieldType.year(),
173: DateTimeFieldType.monthOfYear(),
174: DateTimeFieldType.dayOfWeek() }, new int[] { year,
175: month, dow }, chrono);
176: }
177:
178: }
|