001 /*
002 * Copyright 2001-2012 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.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022
023 import junit.framework.TestCase;
024 import junit.framework.TestSuite;
025
026 import org.joda.time.DateTimeZone;
027
028 /**
029 * Test cases for FixedDateTimeZone.
030 *
031 * @author Stephen Colebourne
032 */
033 public class TestCachedDateTimeZone extends TestCase {
034
035 public static void main(String[] args) {
036 junit.textui.TestRunner.run(suite());
037 }
038
039 public static TestSuite suite() {
040 return new TestSuite(TestCachedDateTimeZone.class);
041 }
042
043 private DateTimeZone originalDateTimeZone = null;
044
045 public TestCachedDateTimeZone(String name) {
046 super(name);
047 }
048
049 protected void setUp() throws Exception {
050 originalDateTimeZone = DateTimeZone.getDefault();
051 DateTimeZone.setDefault(DateTimeZone.UTC);
052 }
053
054 protected void tearDown() throws Exception {
055 DateTimeZone.setDefault(originalDateTimeZone);
056 }
057
058 public void test_caching() throws Exception {
059 CachedDateTimeZone zone1 = CachedDateTimeZone.forZone(DateTimeZone.forID("Europe/Paris"));
060 CachedDateTimeZone zone2 = CachedDateTimeZone.forZone(DateTimeZone.forID("Europe/Paris"));
061 assertSame(zone1, zone2);
062 }
063
064 //-----------------------------------------------------------------------
065 public void testSerialization() throws Exception {
066 CachedDateTimeZone test = CachedDateTimeZone.forZone(DateTimeZone.forID("Europe/Paris"));
067
068 ByteArrayOutputStream baos = new ByteArrayOutputStream();
069 ObjectOutputStream oos = new ObjectOutputStream(baos);
070 oos.writeObject(test);
071 byte[] bytes = baos.toByteArray();
072 oos.close();
073
074 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
075 ObjectInputStream ois = new ObjectInputStream(bais);
076 CachedDateTimeZone result = (CachedDateTimeZone) ois.readObject();
077 ois.close();
078
079 assertEquals(test, result);
080 }
081
082 }