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.forID("Europe/Paris");
033 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
034 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
035 private static final int OFFSET = 1;
036 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
037 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON);
038 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO);
039 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC();
040 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
041 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON);
042 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO);
043 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
044 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS);
045 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON);
046 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO);
047 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC();
048
049 private long TEST_TIME_NOW =
050 10L * DateTimeConstants.MILLIS_PER_HOUR
051 + 20L * DateTimeConstants.MILLIS_PER_MINUTE
052 + 30L * DateTimeConstants.MILLIS_PER_SECOND
053 + 40L;
054
055 private long TEST_TIME1 =
056 1L * DateTimeConstants.MILLIS_PER_HOUR
057 + 2L * DateTimeConstants.MILLIS_PER_MINUTE
058 + 3L * DateTimeConstants.MILLIS_PER_SECOND
059 + 4L;
060
061 private long TEST_TIME2 =
062 1L * DateTimeConstants.MILLIS_PER_DAY
063 + 5L * DateTimeConstants.MILLIS_PER_HOUR
064 + 6L * DateTimeConstants.MILLIS_PER_MINUTE
065 + 7L * DateTimeConstants.MILLIS_PER_SECOND
066 + 8L;
067
068 private DateTimeZone zone = null;
069
070 public static void main(String[] args) {
071 junit.textui.TestRunner.run(suite());
072 }
073
074 public static TestSuite suite() {
075 return new TestSuite(TestPartial_Match.class);
076 }
077
078 public TestPartial_Match(String name) {
079 super(name);
080 }
081
082 protected void setUp() throws Exception {
083 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
084 zone = DateTimeZone.getDefault();
085 DateTimeZone.setDefault(LONDON);
086 }
087
088 protected void tearDown() throws Exception {
089 DateTimeUtils.setCurrentMillisSystem();
090 DateTimeZone.setDefault(zone);
091 zone = null;
092 }
093
094 //-----------------------------------------------------------------------
095 public void testIsMatch_Instant() {
096 // Year=2005, Month=7 (July), DayOfWeek=2 (Tuesday)
097 Partial test = createYMDwPartial(ISO_UTC, 2005, 7, 2);
098 DateTime instant = new DateTime(2005, 7, 5, 0, 0, 0, 0);
099 assertEquals(true, test.isMatch(instant));
100
101 instant = new DateTime(2005, 7, 4, 0, 0, 0, 0);
102 assertEquals(false, test.isMatch(instant));
103
104 instant = new DateTime(2005, 7, 6, 0, 0, 0, 0);
105 assertEquals(false, test.isMatch(instant));
106
107 instant = new DateTime(2005, 7, 12, 0, 0, 0, 0);
108 assertEquals(true, test.isMatch(instant));
109
110 instant = new DateTime(2005, 7, 19, 0, 0, 0, 0);
111 assertEquals(true, test.isMatch(instant));
112
113 instant = new DateTime(2005, 7, 26, 0, 0, 0, 0);
114 assertEquals(true, test.isMatch(instant));
115
116 instant = new DateTime(2005, 8, 2, 0, 0, 0, 0);
117 assertEquals(false, test.isMatch(instant));
118
119 instant = new DateTime(2006, 7, 5, 0, 0, 0, 0);
120 assertEquals(false, test.isMatch(instant));
121
122 instant = new DateTime(2005, 6, 5, 0, 0, 0, 0);
123 assertEquals(false, test.isMatch(instant));
124 }
125
126 //-----------------------------------------------------------------------
127 public void testIsMatch_Partial() {
128 // Year=2005, Month=7 (July), DayOfWeek=2 (Tuesday)
129 Partial test = createYMDwPartial(ISO_UTC, 2005, 7, 2);
130 LocalDate partial = new LocalDate(2005, 7, 5);
131 assertEquals(true, test.isMatch(partial));
132
133 partial = new LocalDate(2005, 7, 4);
134 assertEquals(false, test.isMatch(partial));
135
136 partial = new LocalDate(2005, 7, 6);
137 assertEquals(false, test.isMatch(partial));
138
139 partial = new LocalDate(2005, 7, 12);
140 assertEquals(true, test.isMatch(partial));
141
142 partial = new LocalDate(2005, 7, 19);
143 assertEquals(true, test.isMatch(partial));
144
145 partial = new LocalDate(2005, 7, 26);
146 assertEquals(true, test.isMatch(partial));
147
148 partial = new LocalDate(2005, 8, 2);
149 assertEquals(false, test.isMatch(partial));
150
151 partial = new LocalDate(2006, 7, 5);
152 assertEquals(false, test.isMatch(partial));
153
154 partial = new LocalDate(2005, 6, 5);
155 assertEquals(false, test.isMatch(partial));
156
157 try {
158 test.isMatch((ReadablePartial) null);
159 fail();
160 } catch (IllegalArgumentException ex) {}
161 }
162
163 //-----------------------------------------------------------------------
164 private Partial createYMDwPartial(Chronology chrono, int year, int month, int dow) {
165 return new Partial(
166 new DateTimeFieldType[] {
167 DateTimeFieldType.year(),
168 DateTimeFieldType.monthOfYear(),
169 DateTimeFieldType.dayOfWeek()},
170 new int[] {year, month, dow},
171 chrono);
172 }
173
174 }