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.io.ByteArrayInputStream;
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.StringTokenizer;
24  
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  import org.joda.time.DateTime;
29  import org.joda.time.DateTimeZone;
30  import org.joda.time.tz.ZoneInfoCompiler.DateTimeOfYear;
31  
32  /**
33   * Test cases for ZoneInfoCompiler.
34   *
35   * @author Brian S O'Neill
36   */
37  public class TestCompiler extends TestCase {
38      public static void main(String[] args) {
39          junit.textui.TestRunner.run(suite());
40      }
41  
42      public static TestSuite suite() {
43          return new TestSuite(TestCompiler.class);
44      }
45  
46      static final String AMERICA_LOS_ANGELES_FILE =
47          "# Rules for building just America/Los_Angeles time zone.\n" + 
48          "\n" + 
49          "Rule    US  1918    1919    -   Mar lastSun 2:00    1:00    D\n" + 
50          "Rule    US  1918    1919    -   Oct lastSun 2:00    0   S\n" + 
51          "Rule    US  1942    only    -   Feb 9   2:00    1:00    W # War\n" + 
52          "Rule    US  1945    only    -   Aug 14  23:00u  1:00    P # Peace\n" + 
53          "Rule    US  1945    only    -   Sep 30  2:00    0   S\n" + 
54          "Rule    US  1967    max -   Oct lastSun 2:00    0   S\n" + 
55          "Rule    US  1967    1973    -   Apr lastSun 2:00    1:00    D\n" + 
56          "Rule    US  1974    only    -   Jan 6   2:00    1:00    D\n" + 
57          "Rule    US  1975    only    -   Feb 23  2:00    1:00    D\n" + 
58          "Rule    US  1976    1986    -   Apr lastSun 2:00    1:00    D\n" + 
59          "Rule    US  1987    max -   Apr Sun>=1  2:00    1:00    D\n" + 
60          "\n" + 
61          "Rule    CA  1948    only    -   Mar 14  2:00    1:00    D\n" + 
62          "Rule    CA  1949    only    -   Jan  1  2:00    0   S\n" + 
63          "Rule    CA  1950    1966    -   Apr lastSun 2:00    1:00    D\n" + 
64          "Rule    CA  1950    1961    -   Sep lastSun 2:00    0   S\n" + 
65          "Rule    CA  1962    1966    -   Oct lastSun 2:00    0   S\n" + 
66          "\n" + 
67          "Zone America/Los_Angeles -7:52:58 - LMT 1883 Nov 18 12:00\n" + 
68          "            -8:00   US  P%sT    1946\n" + 
69          "            -8:00   CA  P%sT    1967\n" + 
70          "            -8:00   US  P%sT";
71  
72      private DateTimeZone originalDateTimeZone = null;
73  
74      public TestCompiler(String name) {
75          super(name);
76      }
77  
78      protected void setUp() throws Exception {
79          originalDateTimeZone = DateTimeZone.getDefault();
80          DateTimeZone.setDefault(DateTimeZone.UTC);
81      }
82  
83      protected void tearDown() throws Exception {
84          DateTimeZone.setDefault(originalDateTimeZone);
85      }
86  
87      public void testCompile() throws Exception {
88          Provider provider = compileAndLoad(AMERICA_LOS_ANGELES_FILE);
89          DateTimeZone tz = provider.getZone("America/Los_Angeles");
90  
91          assertEquals("America/Los_Angeles", tz.getID());
92          assertEquals(false, tz.isFixed());
93          TestBuilder.testForwardTransitions(tz, TestBuilder.AMERICA_LOS_ANGELES_DATA);
94          TestBuilder.testReverseTransitions(tz, TestBuilder.AMERICA_LOS_ANGELES_DATA);
95      }
96  
97      private Provider compileAndLoad(String data) throws Exception {
98          File tempDir = createDataFile(data);
99          File destDir = makeTempDir();
100 
101         ZoneInfoCompiler.main(new String[] {
102             "-src", tempDir.getAbsolutePath(),
103             "-dst", destDir.getAbsolutePath(),
104             "tzdata"
105         });
106 
107         // Mark all files to be deleted on exit.
108         deleteOnExit(destDir);
109 
110         return new ZoneInfoProvider(destDir);
111     }
112 
113     private File createDataFile(String data) throws IOException {
114         File tempDir = makeTempDir();
115 
116         File tempFile = new File(tempDir, "tzdata");
117         tempFile.deleteOnExit();
118 
119         InputStream in = new ByteArrayInputStream(data.getBytes("UTF-8"));
120 
121         FileOutputStream out = new FileOutputStream(tempFile);
122         byte[] buf = new byte[1000];
123         int amt;
124         while ((amt = in.read(buf)) > 0) {
125             out.write(buf, 0, amt);
126         }
127         out.close();
128         in.close();
129 
130         return tempDir;
131     }
132 
133     private File makeTempDir() {
134         File tempDir = new File(System.getProperty("java.io.tmpdir"));
135         tempDir = new File(tempDir, "joda-test-" + (new java.util.Random().nextInt() & 0xffffff));
136         tempDir.mkdirs();
137         tempDir.deleteOnExit();
138         return tempDir;
139     }
140 
141     private void deleteOnExit(File tempFile) {
142         tempFile.deleteOnExit();
143         if (tempFile.isDirectory()) {
144             File[] files = tempFile.listFiles();
145             for (int i=0; i<files.length; i++) {
146                 deleteOnExit(files[i]);
147             }
148         }
149     }
150 
151     //-----------------------------------------------------------------------
152     public void test_2400_fromDay() {
153         StringTokenizer st = new StringTokenizer("Apr Sun>=1  24:00");
154         DateTimeOfYear test = new DateTimeOfYear(st);
155         assertEquals(4, test.iMonthOfYear);  // Apr
156         assertEquals(2, test.iDayOfMonth);   // 2nd
157         assertEquals(1, test.iDayOfWeek);    // Mon
158         assertEquals(0, test.iMillisOfDay);  // 00:00
159         assertEquals(true, test.iAdvanceDayOfWeek);
160     }
161 
162     public void test_2400_last() {
163         StringTokenizer st = new StringTokenizer("Mar lastSun 24:00");
164         DateTimeOfYear test = new DateTimeOfYear(st);
165         assertEquals(4, test.iMonthOfYear);  // Apr
166         assertEquals(1, test.iDayOfMonth);   // 1st
167         assertEquals(1, test.iDayOfWeek);    // Mon
168         assertEquals(0, test.iMillisOfDay);  // 00:00
169         assertEquals(false, test.iAdvanceDayOfWeek);
170     }
171 
172     public void test_Amman_2003() {
173         DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
174         DateTime dt = new DateTime(2003, 3, 1, 0, 0, zone);
175         long next = zone.nextTransition(dt.getMillis());
176         assertEquals(next, new DateTime(2003, 3, 28, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
177     }
178 
179     public void test_Amman_2004() {
180         DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
181         DateTime dt = new DateTime(2004, 3, 1, 0, 0, zone);
182         long next = zone.nextTransition(dt.getMillis());
183         assertEquals(next, new DateTime(2004, 3, 26, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
184     }
185 
186     public void test_Amman_2005() {
187         DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
188         DateTime dt = new DateTime(2005, 3, 1, 0, 0, zone);
189         long next = zone.nextTransition(dt.getMillis());
190         assertEquals(next, new DateTime(2005, 4, 1, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
191     }
192 
193     public void test_Amman_2006() {
194         DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
195         DateTime dt = new DateTime(2006, 3, 1, 0, 0, zone);
196         long next = zone.nextTransition(dt.getMillis());
197         assertEquals(next, new DateTime(2006, 3, 31, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
198     }
199 
200 }