View Javadoc

1   /*
2    *  Copyright 2001-2012 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.tz;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.joda.time.DateTimeZone;
27  
28  /**
29   * Test cases for FixedDateTimeZone.
30   *
31   * @author Stephen Colebourne
32   */
33  public class TestCachedDateTimeZone extends TestCase {
34  
35      public static void main(String[] args) {
36          junit.textui.TestRunner.run(suite());
37      }
38  
39      public static TestSuite suite() {
40          return new TestSuite(TestCachedDateTimeZone.class);
41      }
42  
43      private DateTimeZone originalDateTimeZone = null;
44  
45      public TestCachedDateTimeZone(String name) {
46          super(name);
47      }
48  
49      protected void setUp() throws Exception {
50          originalDateTimeZone = DateTimeZone.getDefault();
51          DateTimeZone.setDefault(DateTimeZone.UTC);
52      }
53  
54      protected void tearDown() throws Exception {
55          DateTimeZone.setDefault(originalDateTimeZone);
56      }
57  
58      public void test_caching() throws Exception {
59          CachedDateTimeZone zone1 = CachedDateTimeZone.forZone(DateTimeZone.forID("Europe/Paris"));
60          CachedDateTimeZone zone2 = CachedDateTimeZone.forZone(DateTimeZone.forID("Europe/Paris"));
61          assertSame(zone1, zone2);
62      }
63  
64      //-----------------------------------------------------------------------
65      public void testSerialization() throws Exception {
66          CachedDateTimeZone test = CachedDateTimeZone.forZone(DateTimeZone.forID("Europe/Paris"));
67          
68          ByteArrayOutputStream baos = new ByteArrayOutputStream();
69          ObjectOutputStream oos = new ObjectOutputStream(baos);
70          oos.writeObject(test);
71          byte[] bytes = baos.toByteArray();
72          oos.close();
73          
74          ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
75          ObjectInputStream ois = new ObjectInputStream(bais);
76          CachedDateTimeZone result = (CachedDateTimeZone) ois.readObject();
77          ois.close();
78          
79          assertEquals(test, result);
80      }
81  
82  }