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.util.Date; 019 import java.util.TimeZone; 020 021 import junit.framework.TestCase; 022 import junit.framework.TestSuite; 023 024 import org.joda.time.DateTimeZone; 025 026 /** 027 * Test cases for FixedDateTimeZone. 028 * 029 * @author Stephen Colebourne 030 */ 031 public class TestFixedDateTimeZone extends TestCase { 032 033 public static void main(String[] args) { 034 junit.textui.TestRunner.run(suite()); 035 } 036 037 public static TestSuite suite() { 038 return new TestSuite(TestFixedDateTimeZone.class); 039 } 040 041 private DateTimeZone originalDateTimeZone = null; 042 043 public TestFixedDateTimeZone(String name) { 044 super(name); 045 } 046 047 protected void setUp() throws Exception { 048 originalDateTimeZone = DateTimeZone.getDefault(); 049 DateTimeZone.setDefault(DateTimeZone.UTC); 050 } 051 052 protected void tearDown() throws Exception { 053 DateTimeZone.setDefault(originalDateTimeZone); 054 } 055 056 public void testEquals() throws Exception { 057 FixedDateTimeZone zone1 = new FixedDateTimeZone("A", "B", 1, 5); 058 FixedDateTimeZone zone1b = new FixedDateTimeZone("A", "B", 1, 5); 059 FixedDateTimeZone zone2 = new FixedDateTimeZone("A", "C", 1, 5); 060 FixedDateTimeZone zone3 = new FixedDateTimeZone("A", "B", 2, 5); 061 FixedDateTimeZone zone4 = new FixedDateTimeZone("A", "B", 1, 6); 062 063 assertEquals(true, zone1.equals(zone1)); 064 assertEquals(true, zone1.equals(zone1b)); 065 assertEquals(true, zone1.equals(zone2)); // second arg ignored 066 assertEquals(false, zone1.equals(zone3)); 067 assertEquals(false, zone1.equals(zone4)); 068 } 069 070 public void testHashCode() throws Exception { 071 FixedDateTimeZone zone1 = new FixedDateTimeZone("A", "B", 1, 5); 072 FixedDateTimeZone zone1b = new FixedDateTimeZone("A", "B", 1, 5); 073 FixedDateTimeZone zone2 = new FixedDateTimeZone("A", "C", 1, 5); 074 FixedDateTimeZone zone3 = new FixedDateTimeZone("A", "B", 2, 5); 075 FixedDateTimeZone zone4 = new FixedDateTimeZone("A", "B", 1, 6); 076 077 assertEquals(true, zone1.hashCode() == zone1.hashCode()); 078 assertEquals(true, zone1.hashCode() == zone1b.hashCode()); 079 assertEquals(true, zone1.hashCode() == zone2.hashCode()); // second arg ignored 080 assertEquals(false, zone1.hashCode() == zone3.hashCode()); 081 assertEquals(false, zone1.hashCode() == zone4.hashCode()); 082 } 083 084 public void testToTimeZone1() throws Exception { 085 FixedDateTimeZone zone = new FixedDateTimeZone("+00:01", "+00:01", 60000, 60000); 086 java.util.TimeZone tz = zone.toTimeZone(); 087 088 assertEquals(60000, tz.getRawOffset()); 089 assertEquals(60000, getOffset(tz, 1167638400000L)); 090 assertEquals(60000, getOffset(tz, 1185951600000L)); 091 } 092 093 public void testToTimeZone2() throws Exception { 094 FixedDateTimeZone zone = new FixedDateTimeZone("A", "B", 1, 5); 095 java.util.TimeZone tz = zone.toTimeZone(); 096 097 assertEquals(1, tz.getRawOffset()); 098 assertEquals(1, getOffset(tz, 1167638400000L)); 099 assertEquals(1, getOffset(tz, 1185951600000L)); 100 } 101 102 /** Make test compile on JDK 1.3. */ 103 private int getOffset(TimeZone zone, long millis) { 104 Date date = new Date(millis); 105 if (zone.inDaylightTime(date)) { 106 return zone.getRawOffset() + 3600000; 107 } 108 return zone.getRawOffset(); 109 } 110 111 }