1 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 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 27 import org.joda.time.DateTimeZone; 28 29 34 public class TestCompiler extends TestCase { 35 public static void main(String [] 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 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 name) { 72 super(name); 73 } 74 75 protected void setUp() throws Exception { 76 originalDateTimeZone = DateTimeZone.getDefault(); 77 DateTimeZone.setDefault(DateTimeZone.UTC); 78 } 79 80 protected void tearDown() throws Exception { 81 DateTimeZone.setDefault(originalDateTimeZone); 82 } 83 84 public void testCompile() throws Exception { 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 data) throws Exception { 95 File tempDir = createDataFile(data); 96 File destDir = makeTempDir(); 97 98 ZoneInfoCompiler.main(new String [] { 99 "-src", tempDir.getAbsolutePath(), 100 "-dst", destDir.getAbsolutePath(), 101 "tzdata" 102 }); 103 104 deleteOnExit(destDir); 106 107 return new ZoneInfoProvider(destDir); 108 } 109 110 private File createDataFile(String data) throws IOException { 111 File tempDir = makeTempDir(); 112 113 File tempFile = new File (tempDir, "tzdata"); 114 tempFile.deleteOnExit(); 115 116 InputStream in = new ByteArrayInputStream (data.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-" + (new java.util.Random ().nextInt() & 0xffffff)); 133 tempDir.mkdirs(); 134 tempDir.deleteOnExit(); 135 return tempDir; 136 } 137 138 private void deleteOnExit(File tempFile) { 139 tempFile.deleteOnExit(); 140 if (tempFile.isDirectory()) { 141 File [] files = tempFile.listFiles(); 142 for (int i=0; i<files.length; i++) { 143 deleteOnExit(files[i]); 144 } 145 } 146 } 147 } 148 | Popular Tags |