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.tz;
17  
18  import java.util.Date;
19  import java.util.TimeZone;
20  
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.joda.time.DateTimeZone;
25  
26  /**
27   * Test cases for FixedDateTimeZone.
28   *
29   * @author Stephen Colebourne
30   */
31  public class TestFixedDateTimeZone extends TestCase {
32  
33      public static void main(String[] args) {
34          junit.textui.TestRunner.run(suite());
35      }
36  
37      public static TestSuite suite() {
38          return new TestSuite(TestFixedDateTimeZone.class);
39      }
40  
41      private DateTimeZone originalDateTimeZone = null;
42  
43      public TestFixedDateTimeZone(String name) {
44          super(name);
45      }
46  
47      protected void setUp() throws Exception {
48          originalDateTimeZone = DateTimeZone.getDefault();
49          DateTimeZone.setDefault(DateTimeZone.UTC);
50      }
51  
52      protected void tearDown() throws Exception {
53          DateTimeZone.setDefault(originalDateTimeZone);
54      }
55  
56      public void testEquals() throws Exception {
57          FixedDateTimeZone zone1 = new FixedDateTimeZone("A", "B", 1, 5);
58          FixedDateTimeZone zone1b = new FixedDateTimeZone("A", "B", 1, 5);
59          FixedDateTimeZone zone2 = new FixedDateTimeZone("A", "C", 1, 5);
60          FixedDateTimeZone zone3 = new FixedDateTimeZone("A", "B", 2, 5);
61          FixedDateTimeZone zone4 = new FixedDateTimeZone("A", "B", 1, 6);
62          
63          assertEquals(true, zone1.equals(zone1));
64          assertEquals(true, zone1.equals(zone1b));
65          assertEquals(true, zone1.equals(zone2));  // second arg ignored
66          assertEquals(false, zone1.equals(zone3));
67          assertEquals(false, zone1.equals(zone4));
68      }
69  
70      public void testHashCode() throws Exception {
71          FixedDateTimeZone zone1 = new FixedDateTimeZone("A", "B", 1, 5);
72          FixedDateTimeZone zone1b = new FixedDateTimeZone("A", "B", 1, 5);
73          FixedDateTimeZone zone2 = new FixedDateTimeZone("A", "C", 1, 5);
74          FixedDateTimeZone zone3 = new FixedDateTimeZone("A", "B", 2, 5);
75          FixedDateTimeZone zone4 = new FixedDateTimeZone("A", "B", 1, 6);
76          
77          assertEquals(true, zone1.hashCode() == zone1.hashCode());
78          assertEquals(true, zone1.hashCode() == zone1b.hashCode());
79          assertEquals(true, zone1.hashCode() == zone2.hashCode());  // second arg ignored
80          assertEquals(false, zone1.hashCode() == zone3.hashCode());
81          assertEquals(false, zone1.hashCode() == zone4.hashCode());
82      }
83  
84      public void testToTimeZone1() throws Exception {
85          FixedDateTimeZone zone = new FixedDateTimeZone("+00:01", "+00:01", 60000, 60000);
86          java.util.TimeZone tz = zone.toTimeZone();
87          
88          assertEquals(60000, tz.getRawOffset());
89          assertEquals(60000, getOffset(tz, 1167638400000L));
90          assertEquals(60000, getOffset(tz, 1185951600000L));
91      }
92  
93      public void testToTimeZone2() throws Exception {
94          FixedDateTimeZone zone = new FixedDateTimeZone("A", "B", 1, 5);
95          java.util.TimeZone tz = zone.toTimeZone();
96          
97          assertEquals(1, tz.getRawOffset());
98          assertEquals(1, getOffset(tz, 1167638400000L));
99          assertEquals(1, getOffset(tz, 1185951600000L));
100     }
101 
102     /** Make test compile on JDK 1.3. */
103     private int getOffset(TimeZone zone, long millis) {
104         Date date = new Date(millis);
105         if (zone.inDaylightTime(date)) {
106             return zone.getRawOffset() + 3600000;
107         }
108         return zone.getRawOffset();
109     }
110 
111 }