001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.tz;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import org.joda.time.DateTimeZone;
028:
029: /**
030: * Test cases for ZoneInfoCompiler.
031: *
032: * @author Brian S O'Neill
033: */
034: public class TestCompiler extends TestCase {
035: public static void main(String[] args) {
036: junit.textui.TestRunner.run(suite());
037: }
038:
039: public static TestSuite suite() {
040: return new TestSuite(TestCompiler.class);
041: }
042:
043: static final String AMERICA_LOS_ANGELES_FILE = "# Rules for building just America/Los_Angeles time zone.\n"
044: + "\n"
045: + "Rule US 1918 1919 - Mar lastSun 2:00 1:00 D\n"
046: + "Rule US 1918 1919 - Oct lastSun 2:00 0 S\n"
047: + "Rule US 1942 only - Feb 9 2:00 1:00 W # War\n"
048: + "Rule US 1945 only - Aug 14 23:00u 1:00 P # Peace\n"
049: + "Rule US 1945 only - Sep 30 2:00 0 S\n"
050: + "Rule US 1967 max - Oct lastSun 2:00 0 S\n"
051: + "Rule US 1967 1973 - Apr lastSun 2:00 1:00 D\n"
052: + "Rule US 1974 only - Jan 6 2:00 1:00 D\n"
053: + "Rule US 1975 only - Feb 23 2:00 1:00 D\n"
054: + "Rule US 1976 1986 - Apr lastSun 2:00 1:00 D\n"
055: + "Rule US 1987 max - Apr Sun>=1 2:00 1:00 D\n"
056: + "\n"
057: + "Rule CA 1948 only - Mar 14 2:00 1:00 D\n"
058: + "Rule CA 1949 only - Jan 1 2:00 0 S\n"
059: + "Rule CA 1950 1966 - Apr lastSun 2:00 1:00 D\n"
060: + "Rule CA 1950 1961 - Sep lastSun 2:00 0 S\n"
061: + "Rule CA 1962 1966 - Oct lastSun 2:00 0 S\n"
062: + "\n"
063: + "Zone America/Los_Angeles -7:52:58 - LMT 1883 Nov 18 12:00\n"
064: + " -8:00 US P%sT 1946\n"
065: + " -8:00 CA P%sT 1967\n"
066: + " -8:00 US P%sT";
067:
068: private DateTimeZone originalDateTimeZone = null;
069:
070: public TestCompiler(String name) {
071: super (name);
072: }
073:
074: protected void setUp() throws Exception {
075: originalDateTimeZone = DateTimeZone.getDefault();
076: DateTimeZone.setDefault(DateTimeZone.UTC);
077: }
078:
079: protected void tearDown() throws Exception {
080: DateTimeZone.setDefault(originalDateTimeZone);
081: }
082:
083: public void testCompile() throws Exception {
084: Provider provider = compileAndLoad(AMERICA_LOS_ANGELES_FILE);
085: DateTimeZone tz = provider.getZone("America/Los_Angeles");
086:
087: assertEquals("America/Los_Angeles", tz.getID());
088: assertEquals(false, tz.isFixed());
089: TestBuilder.testForwardTransitions(tz,
090: TestBuilder.AMERICA_LOS_ANGELES_DATA);
091: TestBuilder.testReverseTransitions(tz,
092: TestBuilder.AMERICA_LOS_ANGELES_DATA);
093: }
094:
095: private Provider compileAndLoad(String data) throws Exception {
096: File tempDir = createDataFile(data);
097: File destDir = makeTempDir();
098:
099: ZoneInfoCompiler.main(new String[] { "-src",
100: tempDir.getAbsolutePath(), "-dst",
101: destDir.getAbsolutePath(), "tzdata" });
102:
103: // Mark all files to be deleted on exit.
104: deleteOnExit(destDir);
105:
106: return new ZoneInfoProvider(destDir);
107: }
108:
109: private File createDataFile(String data) throws IOException {
110: File tempDir = makeTempDir();
111:
112: File tempFile = new File(tempDir, "tzdata");
113: tempFile.deleteOnExit();
114:
115: InputStream in = new ByteArrayInputStream(data
116: .getBytes("UTF-8"));
117:
118: FileOutputStream out = new FileOutputStream(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 makeTempDir() {
131: File tempDir = new File(System.getProperty("java.io.tmpdir"));
132: tempDir = new File(tempDir, "joda-test-"
133: + (new java.util.Random().nextInt() & 0xffffff));
134: tempDir.mkdirs();
135: tempDir.deleteOnExit();
136: return tempDir;
137: }
138:
139: private void deleteOnExit(File tempFile) {
140: tempFile.deleteOnExit();
141: if (tempFile.isDirectory()) {
142: File[] files = tempFile.listFiles();
143: for (int i = 0; i < files.length; i++) {
144: deleteOnExit(files[i]);
145: }
146: }
147: }
148: }
|