KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > tz > TestCompiler


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 JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 import org.joda.time.DateTimeZone;
28
29 /**
30  * Test cases for ZoneInfoCompiler.
31  *
32  * @author Brian S O'Neill
33  */

34 public class TestCompiler extends TestCase {
35     public static void main(String JavaDoc[] args) {
36         junit.textui.TestRunner.run(suite());
37     }
38
39     public static TestSuite suite() {
40         return new TestSuite(TestCompiler.class);
41     }
42
43     static final String JavaDoc AMERICA_LOS_ANGELES_FILE =
44         "# Rules for building just America/Los_Angeles time zone.\n" +
45         "\n" +
46         "Rule US 1918 1919 - Mar lastSun 2:00 1:00 D\n" +
47         "Rule US 1918 1919 - Oct lastSun 2:00 0 S\n" +
48         "Rule US 1942 only - Feb 9 2:00 1:00 W # War\n" +
49         "Rule US 1945 only - Aug 14 23:00u 1:00 P # Peace\n" +
50         "Rule US 1945 only - Sep 30 2:00 0 S\n" +
51         "Rule US 1967 max - Oct lastSun 2:00 0 S\n" +
52         "Rule US 1967 1973 - Apr lastSun 2:00 1:00 D\n" +
53         "Rule US 1974 only - Jan 6 2:00 1:00 D\n" +
54         "Rule US 1975 only - Feb 23 2:00 1:00 D\n" +
55         "Rule US 1976 1986 - Apr lastSun 2:00 1:00 D\n" +
56         "Rule US 1987 max - Apr Sun>=1 2:00 1:00 D\n" +
57         "\n" +
58         "Rule CA 1948 only - Mar 14 2:00 1:00 D\n" +
59         "Rule CA 1949 only - Jan 1 2:00 0 S\n" +
60         "Rule CA 1950 1966 - Apr lastSun 2:00 1:00 D\n" +
61         "Rule CA 1950 1961 - Sep lastSun 2:00 0 S\n" +
62         "Rule CA 1962 1966 - Oct lastSun 2:00 0 S\n" +
63         "\n" +
64         "Zone America/Los_Angeles -7:52:58 - LMT 1883 Nov 18 12:00\n" +
65         " -8:00 US P%sT 1946\n" +
66         " -8:00 CA P%sT 1967\n" +
67         " -8:00 US P%sT";
68
69     private DateTimeZone originalDateTimeZone = null;
70
71     public TestCompiler(String JavaDoc name) {
72         super(name);
73     }
74
75     protected void setUp() throws Exception JavaDoc {
76         originalDateTimeZone = DateTimeZone.getDefault();
77         DateTimeZone.setDefault(DateTimeZone.UTC);
78     }
79
80     protected void tearDown() throws Exception JavaDoc {
81         DateTimeZone.setDefault(originalDateTimeZone);
82     }
83
84     public void testCompile() throws Exception JavaDoc {
85         Provider provider = compileAndLoad(AMERICA_LOS_ANGELES_FILE);
86         DateTimeZone tz = provider.getZone("America/Los_Angeles");
87
88         assertEquals("America/Los_Angeles", tz.getID());
89         assertEquals(false, tz.isFixed());
90         TestBuilder.testForwardTransitions(tz, TestBuilder.AMERICA_LOS_ANGELES_DATA);
91         TestBuilder.testReverseTransitions(tz, TestBuilder.AMERICA_LOS_ANGELES_DATA);
92     }
93
94     private Provider compileAndLoad(String JavaDoc data) throws Exception JavaDoc {
95         File JavaDoc tempDir = createDataFile(data);
96         File JavaDoc destDir = makeTempDir();
97
98         ZoneInfoCompiler.main(new String JavaDoc[] {
99             "-src", tempDir.getAbsolutePath(),
100             "-dst", destDir.getAbsolutePath(),
101             "tzdata"
102         });
103
104         // Mark all files to be deleted on exit.
105
deleteOnExit(destDir);
106
107         return new ZoneInfoProvider(destDir);
108     }
109
110     private File JavaDoc createDataFile(String JavaDoc data) throws IOException JavaDoc {
111         File JavaDoc tempDir = makeTempDir();
112
113         File JavaDoc tempFile = new File JavaDoc(tempDir, "tzdata");
114         tempFile.deleteOnExit();
115
116         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(data.getBytes("UTF-8"));
117
118         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(tempFile);
119         byte[] buf = new byte[1000];
120         int amt;
121         while ((amt = in.read(buf)) > 0) {
122             out.write(buf, 0, amt);
123         }
124         out.close();
125         in.close();
126
127         return tempDir;
128     }
129
130     private File JavaDoc makeTempDir() {
131         File JavaDoc tempDir = new File JavaDoc(System.getProperty("java.io.tmpdir"));
132         tempDir = new File JavaDoc(tempDir, "joda-test-" + (new java.util.Random JavaDoc().nextInt() & 0xffffff));
133         tempDir.mkdirs();
134         tempDir.deleteOnExit();
135         return tempDir;
136     }
137
138     private void deleteOnExit(File JavaDoc tempFile) {
139         tempFile.deleteOnExit();
140         if (tempFile.isDirectory()) {
141             File JavaDoc[] files = tempFile.listFiles();
142             for (int i=0; i<files.length; i++) {
143                 deleteOnExit(files[i]);
144             }
145         }
146     }
147 }
148
Popular Tags