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.ByteArrayOutputStream;
020: import java.io.IOException;
021:
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: import org.joda.time.DateTime;
026: import org.joda.time.DateTimeZone;
027: import org.joda.time.format.DateTimeFormatter;
028: import org.joda.time.format.DateTimeFormatterBuilder;
029:
030: /**
031: * Test cases for DateTimeZoneBuilder.
032: *
033: * @author Brian S O'Neill
034: */
035: public class TestBuilder extends TestCase {
036: public static void main(String[] args) {
037: junit.textui.TestRunner.run(suite());
038: }
039:
040: public static TestSuite suite() {
041: return new TestSuite(TestBuilder.class);
042: }
043:
044: static final DateTimeFormatter OFFSET_FORMATTER = new DateTimeFormatterBuilder()
045: .appendTimeZoneOffset(null, true, 2, 4).toFormatter();
046:
047: // Each row is {transition, nameKey, standardOffset, offset}
048: static final String[][] AMERICA_LOS_ANGELES_DATA = {
049: { null, "LMT", "-07:52:58", "-07:52:58" },
050: { "1883-11-18T19:52:58.000Z", "PST", "-08:00", "-08:00" },
051: { "1918-03-31T02:00:00.000-08:00", "PDT", "-08:00",
052: "-07:00" },
053: { "1918-10-27T02:00:00.000-07:00", "PST", "-08:00",
054: "-08:00" },
055: { "1919-03-30T02:00:00.000-08:00", "PDT", "-08:00",
056: "-07:00" },
057: { "1919-10-26T02:00:00.000-07:00", "PST", "-08:00",
058: "-08:00" },
059: { "1942-02-09T02:00:00.000-08:00", "PWT", "-08:00",
060: "-07:00" },
061: { "1945-08-14T23:00:00.000Z", "PPT", "-08:00", "-07:00" },
062: { "1945-09-30T02:00:00.000-07:00", "PST", "-08:00",
063: "-08:00" },
064: { "1948-03-14T02:00:00.000-08:00", "PDT", "-08:00",
065: "-07:00" },
066: { "1949-01-01T02:00:00.000-07:00", "PST", "-08:00",
067: "-08:00" },
068: { "1950-04-30T02:00:00.000-08:00", "PDT", "-08:00",
069: "-07:00" },
070: { "1950-09-24T02:00:00.000-07:00", "PST", "-08:00",
071: "-08:00" },
072: { "1951-04-29T02:00:00.000-08:00", "PDT", "-08:00",
073: "-07:00" },
074: { "1951-09-30T02:00:00.000-07:00", "PST", "-08:00",
075: "-08:00" },
076: { "1952-04-27T02:00:00.000-08:00", "PDT", "-08:00",
077: "-07:00" },
078: { "1952-09-28T02:00:00.000-07:00", "PST", "-08:00",
079: "-08:00" },
080: { "1953-04-26T02:00:00.000-08:00", "PDT", "-08:00",
081: "-07:00" },
082: { "1953-09-27T02:00:00.000-07:00", "PST", "-08:00",
083: "-08:00" },
084: { "1954-04-25T02:00:00.000-08:00", "PDT", "-08:00",
085: "-07:00" },
086: { "1954-09-26T02:00:00.000-07:00", "PST", "-08:00",
087: "-08:00" },
088: { "1955-04-24T02:00:00.000-08:00", "PDT", "-08:00",
089: "-07:00" },
090: { "1955-09-25T02:00:00.000-07:00", "PST", "-08:00",
091: "-08:00" },
092: { "1956-04-29T02:00:00.000-08:00", "PDT", "-08:00",
093: "-07:00" },
094: { "1956-09-30T02:00:00.000-07:00", "PST", "-08:00",
095: "-08:00" },
096: { "1957-04-28T02:00:00.000-08:00", "PDT", "-08:00",
097: "-07:00" },
098: { "1957-09-29T02:00:00.000-07:00", "PST", "-08:00",
099: "-08:00" },
100: { "1958-04-27T02:00:00.000-08:00", "PDT", "-08:00",
101: "-07:00" },
102: { "1958-09-28T02:00:00.000-07:00", "PST", "-08:00",
103: "-08:00" },
104: { "1959-04-26T02:00:00.000-08:00", "PDT", "-08:00",
105: "-07:00" },
106: { "1959-09-27T02:00:00.000-07:00", "PST", "-08:00",
107: "-08:00" },
108: { "1960-04-24T02:00:00.000-08:00", "PDT", "-08:00",
109: "-07:00" },
110: { "1960-09-25T02:00:00.000-07:00", "PST", "-08:00",
111: "-08:00" },
112: { "1961-04-30T02:00:00.000-08:00", "PDT", "-08:00",
113: "-07:00" },
114: { "1961-09-24T02:00:00.000-07:00", "PST", "-08:00",
115: "-08:00" },
116: { "1962-04-29T02:00:00.000-08:00", "PDT", "-08:00",
117: "-07:00" },
118: { "1962-10-28T02:00:00.000-07:00", "PST", "-08:00",
119: "-08:00" },
120: { "1963-04-28T02:00:00.000-08:00", "PDT", "-08:00",
121: "-07:00" },
122: { "1963-10-27T02:00:00.000-07:00", "PST", "-08:00",
123: "-08:00" },
124: { "1964-04-26T02:00:00.000-08:00", "PDT", "-08:00",
125: "-07:00" },
126: { "1964-10-25T02:00:00.000-07:00", "PST", "-08:00",
127: "-08:00" },
128: { "1965-04-25T02:00:00.000-08:00", "PDT", "-08:00",
129: "-07:00" },
130: { "1965-10-31T02:00:00.000-07:00", "PST", "-08:00",
131: "-08:00" },
132: { "1966-04-24T02:00:00.000-08:00", "PDT", "-08:00",
133: "-07:00" },
134: { "1966-10-30T02:00:00.000-07:00", "PST", "-08:00",
135: "-08:00" },
136: { "1967-04-30T02:00:00.000-08:00", "PDT", "-08:00",
137: "-07:00" },
138: { "1967-10-29T02:00:00.000-07:00", "PST", "-08:00",
139: "-08:00" },
140: { "1968-04-28T02:00:00.000-08:00", "PDT", "-08:00",
141: "-07:00" },
142: { "1968-10-27T02:00:00.000-07:00", "PST", "-08:00",
143: "-08:00" },
144: { "1969-04-27T02:00:00.000-08:00", "PDT", "-08:00",
145: "-07:00" },
146: { "1969-10-26T02:00:00.000-07:00", "PST", "-08:00",
147: "-08:00" },
148: { "1970-04-26T02:00:00.000-08:00", "PDT", "-08:00",
149: "-07:00" },
150: { "1970-10-25T02:00:00.000-07:00", "PST", "-08:00",
151: "-08:00" },
152: { "1971-04-25T02:00:00.000-08:00", "PDT", "-08:00",
153: "-07:00" },
154: { "1971-10-31T02:00:00.000-07:00", "PST", "-08:00",
155: "-08:00" },
156: { "1972-04-30T02:00:00.000-08:00", "PDT", "-08:00",
157: "-07:00" },
158: { "1972-10-29T02:00:00.000-07:00", "PST", "-08:00",
159: "-08:00" },
160: { "1973-04-29T02:00:00.000-08:00", "PDT", "-08:00",
161: "-07:00" },
162: { "1973-10-28T02:00:00.000-07:00", "PST", "-08:00",
163: "-08:00" },
164: { "1974-01-06T02:00:00.000-08:00", "PDT", "-08:00",
165: "-07:00" },
166: { "1974-10-27T02:00:00.000-07:00", "PST", "-08:00",
167: "-08:00" },
168: { "1975-02-23T02:00:00.000-08:00", "PDT", "-08:00",
169: "-07:00" },
170: { "1975-10-26T02:00:00.000-07:00", "PST", "-08:00",
171: "-08:00" },
172: { "1976-04-25T02:00:00.000-08:00", "PDT", "-08:00",
173: "-07:00" },
174: { "1976-10-31T02:00:00.000-07:00", "PST", "-08:00",
175: "-08:00" },
176: { "1977-04-24T02:00:00.000-08:00", "PDT", "-08:00",
177: "-07:00" },
178: { "1977-10-30T02:00:00.000-07:00", "PST", "-08:00",
179: "-08:00" },
180: { "1978-04-30T02:00:00.000-08:00", "PDT", "-08:00",
181: "-07:00" },
182: { "1978-10-29T02:00:00.000-07:00", "PST", "-08:00",
183: "-08:00" },
184: { "1979-04-29T02:00:00.000-08:00", "PDT", "-08:00",
185: "-07:00" },
186: { "1979-10-28T02:00:00.000-07:00", "PST", "-08:00",
187: "-08:00" },
188: { "1980-04-27T02:00:00.000-08:00", "PDT", "-08:00",
189: "-07:00" },
190: { "1980-10-26T02:00:00.000-07:00", "PST", "-08:00",
191: "-08:00" },
192: { "1981-04-26T02:00:00.000-08:00", "PDT", "-08:00",
193: "-07:00" },
194: { "1981-10-25T02:00:00.000-07:00", "PST", "-08:00",
195: "-08:00" },
196: { "1982-04-25T02:00:00.000-08:00", "PDT", "-08:00",
197: "-07:00" },
198: { "1982-10-31T02:00:00.000-07:00", "PST", "-08:00",
199: "-08:00" },
200: { "1983-04-24T02:00:00.000-08:00", "PDT", "-08:00",
201: "-07:00" },
202: { "1983-10-30T02:00:00.000-07:00", "PST", "-08:00",
203: "-08:00" },
204: { "1984-04-29T02:00:00.000-08:00", "PDT", "-08:00",
205: "-07:00" },
206: { "1984-10-28T02:00:00.000-07:00", "PST", "-08:00",
207: "-08:00" },
208: { "1985-04-28T02:00:00.000-08:00", "PDT", "-08:00",
209: "-07:00" },
210: { "1985-10-27T02:00:00.000-07:00", "PST", "-08:00",
211: "-08:00" },
212: { "1986-04-27T02:00:00.000-08:00", "PDT", "-08:00",
213: "-07:00" },
214: { "1986-10-26T02:00:00.000-07:00", "PST", "-08:00",
215: "-08:00" },
216: { "1987-04-05T02:00:00.000-08:00", "PDT", "-08:00",
217: "-07:00" },
218: { "1987-10-25T02:00:00.000-07:00", "PST", "-08:00",
219: "-08:00" },
220: { "1988-04-03T02:00:00.000-08:00", "PDT", "-08:00",
221: "-07:00" },
222: { "1988-10-30T02:00:00.000-07:00", "PST", "-08:00",
223: "-08:00" },
224: { "1989-04-02T02:00:00.000-08:00", "PDT", "-08:00",
225: "-07:00" },
226: { "1989-10-29T02:00:00.000-07:00", "PST", "-08:00",
227: "-08:00" },
228: { "1990-04-01T02:00:00.000-08:00", "PDT", "-08:00",
229: "-07:00" },
230: { "1990-10-28T02:00:00.000-07:00", "PST", "-08:00",
231: "-08:00" },
232: { "1991-04-07T02:00:00.000-08:00", "PDT", "-08:00",
233: "-07:00" },
234: { "1991-10-27T02:00:00.000-07:00", "PST", "-08:00",
235: "-08:00" },
236: { "1992-04-05T02:00:00.000-08:00", "PDT", "-08:00",
237: "-07:00" },
238: { "1992-10-25T02:00:00.000-07:00", "PST", "-08:00",
239: "-08:00" },
240: { "1993-04-04T02:00:00.000-08:00", "PDT", "-08:00",
241: "-07:00" },
242: { "1993-10-31T02:00:00.000-07:00", "PST", "-08:00",
243: "-08:00" },
244: { "1994-04-03T02:00:00.000-08:00", "PDT", "-08:00",
245: "-07:00" },
246: { "1994-10-30T02:00:00.000-07:00", "PST", "-08:00",
247: "-08:00" },
248: { "1995-04-02T02:00:00.000-08:00", "PDT", "-08:00",
249: "-07:00" },
250: { "1995-10-29T02:00:00.000-07:00", "PST", "-08:00",
251: "-08:00" },
252: { "1996-04-07T02:00:00.000-08:00", "PDT", "-08:00",
253: "-07:00" },
254: { "1996-10-27T02:00:00.000-07:00", "PST", "-08:00",
255: "-08:00" },
256: { "1997-04-06T02:00:00.000-08:00", "PDT", "-08:00",
257: "-07:00" },
258: { "1997-10-26T02:00:00.000-07:00", "PST", "-08:00",
259: "-08:00" },
260: { "1998-04-05T02:00:00.000-08:00", "PDT", "-08:00",
261: "-07:00" },
262: { "1998-10-25T02:00:00.000-07:00", "PST", "-08:00",
263: "-08:00" },
264: { "1999-04-04T02:00:00.000-08:00", "PDT", "-08:00",
265: "-07:00" },
266: { "1999-10-31T02:00:00.000-07:00", "PST", "-08:00",
267: "-08:00" },
268: { "2000-04-02T02:00:00.000-08:00", "PDT", "-08:00",
269: "-07:00" },
270: { "2000-10-29T02:00:00.000-07:00", "PST", "-08:00",
271: "-08:00" }, };
272:
273: static DateTimeZoneBuilder buildAmericaLosAngelesBuilder() {
274: return new DateTimeZoneBuilder().addCutover(-2147483648, 'w',
275: 1, 1, 0, false, 0).setStandardOffset(-28378000)
276: .setFixedSavings("LMT", 0).addCutover(1883, 'w', 11,
277: 18, 0, false, 43200000).setStandardOffset(
278: -28800000).addRecurringSavings("PDT", 3600000,
279: 1918, 1919, 'w', 3, -1, 7, false, 7200000)
280: .addRecurringSavings("PST", 0, 1918, 1919, 'w', 10, -1,
281: 7, false, 7200000).addRecurringSavings("PWT",
282: 3600000, 1942, 1942, 'w', 2, 9, 0, false,
283: 7200000).addRecurringSavings("PPT", 3600000,
284: 1945, 1945, 'u', 8, 14, 0, false, 82800000)
285: .addRecurringSavings("PST", 0, 1945, 1945, 'w', 9, 30,
286: 0, false, 7200000).addRecurringSavings("PDT",
287: 3600000, 1948, 1948, 'w', 3, 14, 0, false,
288: 7200000).addRecurringSavings("PST", 0, 1949,
289: 1949, 'w', 1, 1, 0, false, 7200000)
290: .addRecurringSavings("PDT", 3600000, 1950, 1966, 'w',
291: 4, -1, 7, false, 7200000).addRecurringSavings(
292: "PST", 0, 1950, 1961, 'w', 9, -1, 7, false,
293: 7200000).addRecurringSavings("PST", 0, 1962,
294: 1966, 'w', 10, -1, 7, false, 7200000)
295: .addRecurringSavings("PST", 0, 1967, 2147483647, 'w',
296: 10, -1, 7, false, 7200000).addRecurringSavings(
297: "PDT", 3600000, 1967, 1973, 'w', 4, -1, 7,
298: false, 7200000).addRecurringSavings("PDT",
299: 3600000, 1974, 1974, 'w', 1, 6, 0, false,
300: 7200000).addRecurringSavings("PDT", 3600000,
301: 1975, 1975, 'w', 2, 23, 0, false, 7200000)
302: .addRecurringSavings("PDT", 3600000, 1976, 1986, 'w',
303: 4, -1, 7, false, 7200000).addRecurringSavings(
304: "PDT", 3600000, 1987, 2147483647, 'w', 4, 1, 7,
305: true, 7200000);
306: }
307:
308: static DateTimeZone buildAmericaLosAngeles() {
309: return buildAmericaLosAngelesBuilder().toDateTimeZone(
310: "America/Los_Angeles", true);
311: }
312:
313: private DateTimeZone originalDateTimeZone = null;
314:
315: public TestBuilder(String name) {
316: super (name);
317: }
318:
319: protected void setUp() throws Exception {
320: originalDateTimeZone = DateTimeZone.getDefault();
321: DateTimeZone.setDefault(DateTimeZone.UTC);
322: }
323:
324: protected void tearDown() throws Exception {
325: DateTimeZone.setDefault(originalDateTimeZone);
326: }
327:
328: public void testID() {
329: DateTimeZone tz = buildAmericaLosAngeles();
330: assertEquals("America/Los_Angeles", tz.getID());
331: assertEquals(false, tz.isFixed());
332: }
333:
334: public void testForwardTransitions() {
335: DateTimeZone tz = buildAmericaLosAngeles();
336: testForwardTransitions(tz, AMERICA_LOS_ANGELES_DATA);
337: }
338:
339: static void testForwardTransitions(DateTimeZone tz, String[][] data) {
340: long instant = Long.MIN_VALUE;
341: for (int i = 0; i < data.length; i++) {
342: String[] row = data[i];
343: long expectedInstant = instant;
344: if (row[0] != null) {
345: instant = tz.nextTransition(instant);
346: expectedInstant = new DateTime(row[0]).getMillis();
347: }
348:
349: String expectedKey = row[1];
350: int expectedStandardOffset = -(int) OFFSET_FORMATTER
351: .parseMillis(row[2]);
352: int expectedOffset = -(int) OFFSET_FORMATTER
353: .parseMillis(row[3]);
354:
355: assertEquals(expectedInstant, instant);
356: assertEquals(expectedKey, tz.getNameKey(instant));
357: assertEquals(expectedStandardOffset, tz
358: .getStandardOffset(instant));
359: assertEquals(expectedOffset, tz.getOffset(instant));
360:
361: // Sample a few instants between transitions.
362: if (i < data.length - 1) {
363: long nextInstant = new DateTime(data[i + 1][0])
364: .getMillis();
365: long span = (nextInstant - instant) / 10;
366: for (int j = 1; j < 10; j++) {
367: long between = instant + j * span;
368: assertEquals(expectedKey, tz.getNameKey(between));
369: assertEquals(expectedStandardOffset, tz
370: .getStandardOffset(between));
371: assertEquals(expectedOffset, tz.getOffset(between));
372: }
373: }
374: }
375: }
376:
377: public void testReverseTransitions() {
378: DateTimeZone tz = buildAmericaLosAngeles();
379: testReverseTransitions(tz, AMERICA_LOS_ANGELES_DATA);
380: }
381:
382: static void testReverseTransitions(DateTimeZone tz, String[][] data) {
383: long instant = new DateTime(data[data.length - 1][0])
384: .getMillis();
385: for (int i = data.length; --i >= 1;) {
386: String[] row = data[i];
387: String[] prevRow = data[i - 1];
388: instant = tz.previousTransition(instant);
389:
390: long expectedInstant = new DateTime(row[0]).getMillis() - 1;
391: String expectedKey = prevRow[1];
392: int expectedStandardOffset = -(int) OFFSET_FORMATTER
393: .parseMillis(prevRow[2]);
394: int expectedOffset = -(int) OFFSET_FORMATTER
395: .parseMillis(prevRow[3]);
396:
397: assertEquals(expectedInstant, instant);
398: assertEquals(expectedKey, tz.getNameKey(instant));
399: assertEquals(expectedStandardOffset, tz
400: .getStandardOffset(instant));
401: assertEquals(expectedOffset, tz.getOffset(instant));
402: }
403: }
404:
405: public void testSerialization() throws IOException {
406: DateTimeZone tz = testSerialization(
407: buildAmericaLosAngelesBuilder(), "America/Los_Angeles");
408:
409: assertEquals(false, tz.isFixed());
410: testForwardTransitions(tz, AMERICA_LOS_ANGELES_DATA);
411: testReverseTransitions(tz, AMERICA_LOS_ANGELES_DATA);
412: }
413:
414: static DateTimeZone testSerialization(DateTimeZoneBuilder builder,
415: String id) throws IOException {
416: ByteArrayOutputStream out = new ByteArrayOutputStream();
417: builder.writeTo("America/Los_Angeles", out);
418: ByteArrayInputStream in = new ByteArrayInputStream(out
419: .toByteArray());
420: DateTimeZone tz = DateTimeZoneBuilder.readFrom(in, id);
421: assertEquals(id, tz.getID());
422: return tz;
423: }
424:
425: public void testFixed() throws IOException {
426: DateTimeZoneBuilder builder = new DateTimeZoneBuilder()
427: .setStandardOffset(3600000).setFixedSavings("LMT", 0);
428: DateTimeZone tz = builder.toDateTimeZone("Test", true);
429:
430: for (int i = 0; i < 2; i++) {
431: assertEquals("Test", tz.getID());
432: assertEquals(true, tz.isFixed());
433: assertEquals(3600000, tz.getOffset(0));
434: assertEquals(3600000, tz.getStandardOffset(0));
435: assertEquals(0, tz.nextTransition(0));
436: assertEquals(0, tz.previousTransition(0));
437:
438: tz = testSerialization(builder, "Test");
439: }
440: }
441: }
|