-
Notifications
You must be signed in to change notification settings - Fork 73
/
DefaultTimeZoneTests.java
253 lines (202 loc) · 6.96 KB
/
DefaultTimeZoneTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright 2016-2021 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junitpioneer.jupiter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junitpioneer.testkit.PioneerTestKit.executeTestClass;
import static org.junitpioneer.testkit.PioneerTestKit.executeTestMethod;
import static org.junitpioneer.testkit.assertion.PioneerAssert.assertThat;
import java.util.TimeZone;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junitpioneer.testkit.ExecutionResults;
@DisplayName("DefaultTimeZone extension")
class DefaultTimeZoneTests {
private static TimeZone TEST_DEFAULT_TIMEZONE;
private static TimeZone DEFAULT_TIMEZONE_BEFORE_TEST;
@BeforeAll
static void globalSetUp() {
// we set UTC as test time zone unless it is already
// the system's time zone; in that case we use UTC+12
DEFAULT_TIMEZONE_BEFORE_TEST = TimeZone.getDefault();
TimeZone utc = TimeZone.getTimeZone("UTC");
TimeZone utcPlusTwelve = TimeZone.getTimeZone("GMT+12:00");
if (DEFAULT_TIMEZONE_BEFORE_TEST.equals(utc))
TimeZone.setDefault(utcPlusTwelve);
else
TimeZone.setDefault(utc);
TEST_DEFAULT_TIMEZONE = TimeZone.getDefault();
}
@AfterAll
static void globalTearDown() {
TimeZone.setDefault(DEFAULT_TIMEZONE_BEFORE_TEST);
}
@Nested
@DisplayName("when applied on the method level")
class MethodLevelTests {
@Test
@ReadsDefaultTimeZone
@DisplayName("does nothing when annotation is not present")
void doesNothingWhenAnnotationNotPresent() {
assertThat(TimeZone.getDefault()).isEqualTo(TEST_DEFAULT_TIMEZONE);
}
@Test
@DefaultTimeZone("GMT")
@DisplayName("does not throw when explicitly set to GMT")
void doesNotThrowForExplicitGmt() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT"));
}
@Test
@DefaultTimeZone("CET")
@DisplayName("sets the default time zone using an abbreviation")
void setsTimeZoneFromAbbreviation() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("CET"));
}
@Test
@DefaultTimeZone("America/Los_Angeles")
@DisplayName("sets the default time zone using a full name")
void setsTimeZoneFromFullName() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("America/Los_Angeles"));
}
}
@Nested
@DefaultTimeZone("GMT-8:00")
@DisplayName("when applied on the class level")
class ClassLevelTestCases {
@Test
@ReadsDefaultTimeZone
@DisplayName("sets the default time zone")
void shouldExecuteWithClassLevelTimeZone() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT-8:00"));
}
@Test
@DefaultTimeZone("GMT-12:00")
@DisplayName("gets overridden by annotation on the method level")
void shouldBeOverriddenWithMethodLevelTimeZone() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT-12:00"));
}
}
@Nested
@DefaultTimeZone("GMT")
@DisplayName("when explicitly set to GMT on the class level")
class ExplicitGmtClassLevelTestCases {
@Test
@DisplayName("does not throw and sets to GMT ")
void explicitGmt() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT"));
}
}
@Nested
@DefaultTimeZone("GMT-8:00")
@DisplayName("with nested classes")
class NestedTests {
@Nested
@DisplayName("without DefaultTimeZone annotation")
class NestedClass {
@Test
@ReadsDefaultTimeZone
@DisplayName("DefaultTimeZone should be set from enclosed class when it is not provided in nested")
public void shouldSetTimeZoneFromEnclosedClass() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT-8:00"));
}
}
@Nested
@DefaultTimeZone("GMT-12:00")
@DisplayName("with DefaultTimeZone annotation")
class AnnotatedNestedClass {
@Test
@ReadsDefaultTimeZone
@DisplayName("DefaultTimeZone should be set from nested class when it is provided")
public void shouldSetTimeZoneFromNestedClass() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT-12:00"));
}
@Test
@DefaultTimeZone("GMT-6:00")
@DisplayName("DefaultTimeZone should be set from method when it is provided")
public void shouldSetTimeZoneFromMethodOfNestedClass() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT-6:00"));
}
}
}
@Nested
@DefaultTimeZone("GMT-12:00")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ResettingDefaultTimeZoneTests {
@Nested
@DefaultTimeZone("GMT-3:00")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ResettingDefaultTimeZoneNestedTests {
@Test
@DefaultTimeZone("GMT+6:00")
void setForTestMethod() {
// only here to set the time zone, so another test can verify whether it was reset;
// still, better to assert the value was actually set
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT+6:00"));
}
@AfterAll
@ReadsDefaultTimeZone
void resetAfterTestMethodExecution() {
assertThat(TimeZone.getDefault()).isEqualTo(TEST_DEFAULT_TIMEZONE);
}
}
@AfterAll
@ReadsDefaultTimeZone
void resetAfterTestMethodExecution() {
assertThat(TimeZone.getDefault()).isEqualTo(TEST_DEFAULT_TIMEZONE);
}
}
@Nested
@DisplayName("when misconfigured")
class ConfigurationTests {
@Test
@ReadsDefaultTimeZone
@DisplayName("on method level, throws exception")
void throwsWhenConfigurationIsBad() {
ExecutionResults results = executeTestMethod(BadMethodLevelConfigurationTestCases.class,
"badConfiguration");
assertThat(results)
.hasSingleFailedTest()
.withExceptionInstanceOf(ExtensionConfigurationException.class)
.hasMessageNotContaining("should never execute")
.hasMessageContaining("@DefaultTimeZone not configured correctly.");
}
@Test
@ReadsDefaultTimeZone
@DisplayName("on class level, throws exception")
void shouldThrowWithBadConfiguration() {
ExecutionResults results = executeTestClass(BadClassLevelConfigurationTestCases.class);
assertThat(results)
.hasSingleFailedTest()
.withExceptionInstanceOf(ExtensionConfigurationException.class)
.hasMessageContaining("@DefaultTimeZone not configured correctly.");
}
@AfterEach
void verifyMisconfigurationSisNotChangeTimeZone() {
assertThat(TimeZone.getDefault()).isEqualTo(TEST_DEFAULT_TIMEZONE);
}
}
static class BadMethodLevelConfigurationTestCases {
@Test
@DefaultTimeZone("Gibberish")
void badConfiguration() {
}
}
@DefaultTimeZone("Gibberish")
static class BadClassLevelConfigurationTestCases {
@Test
void badConfiguration() {
}
}
}