View Javadoc

1   /*
2    *  Copyright 2001-2005 Stephen Colebourne
3    *
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
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package org.joda.time;
17  
18  import junit.framework.TestCase;
19  import junit.framework.TestSuite;
20  
21  import org.joda.time.chrono.BuddhistChronology;
22  import org.joda.time.chrono.CopticChronology;
23  import org.joda.time.chrono.ISOChronology;
24  
25  /**
26   * This class is a Junit unit test for Partial.
27   *
28   * @author Stephen Colebourne
29   */
30  public class TestPartial_Match extends TestCase {
31  
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();
48      
49      private long TEST_TIME_NOW =
50              10L * DateTimeConstants.MILLIS_PER_HOUR
51              + 20L * DateTimeConstants.MILLIS_PER_MINUTE
52              + 30L * DateTimeConstants.MILLIS_PER_SECOND
53              + 40L;
54              
55      private long TEST_TIME1 =
56          1L * DateTimeConstants.MILLIS_PER_HOUR
57          + 2L * DateTimeConstants.MILLIS_PER_MINUTE
58          + 3L * DateTimeConstants.MILLIS_PER_SECOND
59          + 4L;
60          
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
66          + 8L;
67          
68      private DateTimeZone zone = null;
69  
70      public static void main(String[] args) {
71          junit.textui.TestRunner.run(suite());
72      }
73  
74      public static TestSuite suite() {
75          return new TestSuite(TestPartial_Match.class);
76      }
77  
78      public TestPartial_Match(String name) {
79          super(name);
80      }
81  
82      protected void setUp() throws Exception {
83          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
84          zone = DateTimeZone.getDefault();
85          DateTimeZone.setDefault(LONDON);
86      }
87  
88      protected void tearDown() throws Exception {
89          DateTimeUtils.setCurrentMillisSystem();
90          DateTimeZone.setDefault(zone);
91          zone = null;
92      }
93  
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));
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 }