1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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 }