2
* Copyright 2001-2005 Stephen Colebourne
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package org.joda.time;
18
import junit.framework.TestCase;
19
import junit.framework.TestSuite;
21
import org.joda.time.chrono.BuddhistChronology;
22
import org.joda.time.chrono.CopticChronology;
23
import org.joda.time.chrono.ISOChronology;
26
* This class is a Junit unit test for Partial.
28
* @author Stephen Colebourne
30
public class TestPartial_Match extends TestCase {
32
private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
33
private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
34
private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
35
private static final int OFFSET = 1;
36
private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
37
private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON);
38
private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO);
39
private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC();
40
private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
41
private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON);
42
private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO);
43
private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
44
private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS);
45
private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON);
46
private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO);
47
private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC();
49
private long TEST_TIME_NOW =
50
10L * DateTimeConstants.MILLIS_PER_HOUR
51
+ 20L * DateTimeConstants.MILLIS_PER_MINUTE
52
+ 30L * DateTimeConstants.MILLIS_PER_SECOND
55
private long TEST_TIME1 =
56
1L * DateTimeConstants.MILLIS_PER_HOUR
57
+ 2L * DateTimeConstants.MILLIS_PER_MINUTE
58
+ 3L * DateTimeConstants.MILLIS_PER_SECOND
61
private long TEST_TIME2 =
62
1L * DateTimeConstants.MILLIS_PER_DAY
63
+ 5L * DateTimeConstants.MILLIS_PER_HOUR
64
+ 6L * DateTimeConstants.MILLIS_PER_MINUTE
65
+ 7L * DateTimeConstants.MILLIS_PER_SECOND
68
private DateTimeZone zone = null;
70
public static void main(String[] args) {
71
junit.textui.TestRunner.run(suite());
74
public static TestSuite suite() {
75
return new TestSuite(TestPartial_Match.class);
78
public TestPartial_Match(String name) {
82
protected void setUp() throws Exception {
83
DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
84
zone = DateTimeZone.getDefault();
85
DateTimeZone.setDefault(LONDON);
88
protected void tearDown() throws Exception {
89
DateTimeUtils.setCurrentMillisSystem();
90
DateTimeZone.setDefault(zone);
94
//-----------------------------------------------------------------------
95
public void testIsMatch_Instant() {
96
// Year=2005, Month=7 (July), DayOfWeek=2 (Tuesday)
97
Partial test = createYMDwPartial(ISO_UTC, 2005, 7, 2);
98
DateTime instant = new DateTime(2005, 7, 5, 0, 0, 0, 0);
99
assertEquals(true, test.isMatch(instant));
101
instant = new DateTime(2005, 7, 4, 0, 0, 0, 0);
102
assertEquals(false, test.isMatch(instant));
104
instant = new DateTime(2005, 7, 6, 0, 0, 0, 0);
105
assertEquals(false, test.isMatch(instant));
107
instant = new DateTime(2005, 7, 12, 0, 0, 0, 0);
108
assertEquals(true, test.isMatch(instant));
110
instant = new DateTime(2005, 7, 19, 0, 0, 0, 0);
111
assertEquals(true, test.isMatch(instant));
113
instant = new DateTime(2005, 7, 26, 0, 0, 0, 0);
114
assertEquals(true, test.isMatch(instant));
116
instant = new DateTime(2005, 8, 2, 0, 0, 0, 0);
117
assertEquals(false, test.isMatch(instant));
119
instant = new DateTime(2006, 7, 5, 0, 0, 0, 0);
120
assertEquals(false, test.isMatch(instant));
122
instant = new DateTime(2005, 6, 5, 0, 0, 0, 0);
123
assertEquals(false, test.isMatch(instant));
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));
133
partial = new LocalDate(2005, 7, 4);
134
assertEquals(false, test.isMatch(partial));
136
partial = new LocalDate(2005, 7, 6);
137
assertEquals(false, test.isMatch(partial));
139
partial = new LocalDate(2005, 7, 12);
140
assertEquals(true, test.isMatch(partial));
142
partial = new LocalDate(2005, 7, 19);
143
assertEquals(true, test.isMatch(partial));
145
partial = new LocalDate(2005, 7, 26);
146
assertEquals(true, test.isMatch(partial));
148
partial = new LocalDate(2005, 8, 2);
149
assertEquals(false, test.isMatch(partial));
151
partial = new LocalDate(2006, 7, 5);
152
assertEquals(false, test.isMatch(partial));
154
partial = new LocalDate(2005, 6, 5);
155
assertEquals(false, test.isMatch(partial));
158
//-----------------------------------------------------------------------
159
private Partial createYMDwPartial(Chronology chrono, int year, int month, int dow) {
161
new DateTimeFieldType[] {
162
DateTimeFieldType.year(),
163
DateTimeFieldType.monthOfYear(),
164
DateTimeFieldType.dayOfWeek()},
165
new int[] {year, month, dow},