View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.time;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertSame;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  
28  import java.util.Date;
29  import java.util.Locale;
30  import java.util.TimeZone;
31  
32  import org.apache.commons.lang3.AbstractLangTest;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests for {@link ImmutableTimeZone}.
38   */
39  class ImmutableTimeZoneTest extends AbstractLangTest {
40  
41      private static final TimeZone NEW_YORK = TimeZone.getTimeZone("America/New_York");
42      private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
43      private ImmutableTimeZone newYorkImmutable;
44      private ImmutableTimeZone utcImmutable;
45  
46      @BeforeEach
47      void setUp() {
48          utcImmutable = new ImmutableTimeZone(UTC);
49          newYorkImmutable = new ImmutableTimeZone(NEW_YORK);
50      }
51  
52      @Test
53      void testCloneReturnsSameInstance() {
54          // Immutable singleton: clone() must return 'this'
55          assertSame(utcImmutable, utcImmutable.clone());
56          assertSame(newYorkImmutable, newYorkImmutable.clone());
57      }
58  
59      @Test
60      void testConstructorNullThrowsNullPointerException() {
61          assertThrows(NullPointerException.class, () -> new ImmutableTimeZone(null));
62      }
63  
64      @Test
65      void testEquals() {
66          final ImmutableTimeZone anotherUtc = new ImmutableTimeZone((TimeZone) UTC.clone());
67          assertEquals(utcImmutable, utcImmutable);
68          assertEquals(utcImmutable, anotherUtc);
69          assertEquals(anotherUtc, utcImmutable);
70          assertNotEquals(utcImmutable, newYorkImmutable);
71          assertEquals(utcImmutable, UTC);
72          assertEquals(utcImmutable, UTC.clone());
73          assertNotEquals(utcImmutable, NEW_YORK);
74          assertFalse(utcImmutable.equals(null));
75          assertFalse(utcImmutable.equals("UTC"));
76      }
77  
78      @Test
79      void testGetDisplayNameDelegatesToWrappedTimeZone() {
80          assertEquals(UTC.getDisplayName(false, TimeZone.LONG, Locale.US), utcImmutable.getDisplayName(false, TimeZone.LONG, Locale.US));
81          assertEquals(NEW_YORK.getDisplayName(true, TimeZone.SHORT, Locale.US), newYorkImmutable.getDisplayName(true, TimeZone.SHORT, Locale.US));
82      }
83  
84      @Test
85      void testGetDSTSavingsDelegatesToWrappedTimeZone() {
86          assertEquals(UTC.getDSTSavings(), utcImmutable.getDSTSavings());
87          assertEquals(NEW_YORK.getDSTSavings(), newYorkImmutable.getDSTSavings());
88      }
89  
90      @Test
91      void testGetIDDelegatesToWrappedTimeZone() {
92          assertEquals(UTC.getID(), utcImmutable.getID());
93          assertEquals(NEW_YORK.getID(), newYorkImmutable.getID());
94      }
95  
96      @Test
97      void testGetOffsetEraYearMonthDayDelegatesToWrappedTimeZone() {
98          // era=1 (AD), year=2024, month=0 (Jan), day=1, dayOfWeek=2 (Mon), milliseconds=0
99          assertEquals(UTC.getOffset(1, 2024, 0, 1, 2, 0), utcImmutable.getOffset(1, 2024, 0, 1, 2, 0));
100         assertEquals(NEW_YORK.getOffset(1, 2024, 0, 1, 2, 0), newYorkImmutable.getOffset(1, 2024, 0, 1, 2, 0));
101     }
102 
103     @Test
104     void testGetOffsetLongDelegatesToWrappedTimeZone() {
105         final long now = System.currentTimeMillis();
106         assertEquals(UTC.getOffset(now), utcImmutable.getOffset(now));
107         assertEquals(NEW_YORK.getOffset(now), newYorkImmutable.getOffset(now));
108     }
109 
110     @Test
111     void testGetRawOffsetDelegatesToWrappedTimeZone() {
112         assertEquals(UTC.getRawOffset(), utcImmutable.getRawOffset());
113         assertEquals(NEW_YORK.getRawOffset(), newYorkImmutable.getRawOffset());
114     }
115 
116     @Test
117     void testHashCode() {
118         final ImmutableTimeZone anotherUtc = new ImmutableTimeZone((TimeZone) UTC.clone());
119         assertEquals(UTC.hashCode(), utcImmutable.hashCode());
120         assertEquals(utcImmutable.hashCode(), anotherUtc.hashCode());
121     }
122 
123     @Test
124     void testHasSameRulesDelegatesToWrappedTimeZone() {
125         assertTrue(utcImmutable.hasSameRules(UTC));
126         assertTrue(newYorkImmutable.hasSameRules(NEW_YORK));
127     }
128 
129     @Test
130     void testInDaylightTimeDelegatesToWrappedTimeZone() {
131         final Date date = new Date();
132         assertEquals(UTC.inDaylightTime(date), utcImmutable.inDaylightTime(date));
133         assertEquals(NEW_YORK.inDaylightTime(date), newYorkImmutable.inDaylightTime(date));
134     }
135 
136     @Test
137     void testObservesDaylightTimeDelegatesToWrappedTimeZone() {
138         assertEquals(UTC.observesDaylightTime(), utcImmutable.observesDaylightTime());
139         assertEquals(NEW_YORK.observesDaylightTime(), newYorkImmutable.observesDaylightTime());
140     }
141 
142     @Test
143     void testSetIDThrowsUnsupportedOperationException() {
144         assertThrows(UnsupportedOperationException.class, () -> utcImmutable.setID("GMT"));
145     }
146 
147     @Test
148     void testSetRawOffsetThrowsUnsupportedOperationException() {
149         assertThrows(UnsupportedOperationException.class, () -> utcImmutable.setRawOffset(0));
150     }
151 
152     @Test
153     void testToStringDelegatesToWrappedTimeZone() {
154         assertEquals(UTC.toString(), utcImmutable.toString());
155         assertEquals(NEW_YORK.toString(), newYorkImmutable.toString());
156     }
157 
158     @Test
159     void testToZoneIdDelegatesToWrappedTimeZone() {
160         assertEquals(UTC.toZoneId(), utcImmutable.toZoneId());
161         assertEquals(NEW_YORK.toZoneId(), newYorkImmutable.toZoneId());
162     }
163 
164     @Test
165     void testToZoneIdNotNull() {
166         assertNotNull(utcImmutable.toZoneId());
167         assertNotNull(newYorkImmutable.toZoneId());
168     }
169 
170     @Test
171     void testUseDaylightTimeDelegatesToWrappedTimeZone() {
172         assertEquals(UTC.useDaylightTime(), utcImmutable.useDaylightTime());
173         assertEquals(NEW_YORK.useDaylightTime(), newYorkImmutable.useDaylightTime());
174     }
175 }