Skip to content

Commit

Permalink
Test @DefaultTimeZone for misconfiguration (#164 / #272)
Browse files Browse the repository at this point in the history
Progresses: #164
PR: #272
  • Loading branch information
Michael1993 committed Jun 20, 2020
1 parent 0ca9f4c commit 4da75d5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
Expand All @@ -42,8 +43,21 @@ public void beforeEach(ExtensionContext context) {

private void setDefaultTimeZone(Store store, DefaultTimeZone annotation) {
storeDefaultTimeZone(store);
TimeZone configuredTimeZone = TimeZone.getTimeZone(annotation.value());
TimeZone.setDefault(configuredTimeZone);
String timeZoneId = annotation.value();
TimeZone.setDefault(getTimeZone(timeZoneId));
}

private TimeZone getTimeZone(String timeZoneId) {
TimeZone configuredTimeZone = TimeZone.getTimeZone(timeZoneId);
// TimeZone::getTimeZone returns with GMT as fallback if the given ID cannot be understood
if (configuredTimeZone.equals(TimeZone.getTimeZone("GMT")) && !timeZoneId.equals("GMT")) {
throw new ExtensionConfigurationException(String
.format("@DefaultTimeZone not configured correctly. "
+ "Could not find the specified time zone + '%s'. "
+ "Please use correct identifiers, e.g. \"GMT\" for Greenwich Mean Time.",
timeZoneId));
}
return configuredTimeZone;
}

private void storeDefaultTimeZone(Store store) {
Expand Down
72 changes: 71 additions & 1 deletion src/test/java/org/junitpioneer/jupiter/DefaultTimeZoneTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
package org.junitpioneer.jupiter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.junitpioneer.testkit.PioneerTestKit.executeTestClass;
import static org.junitpioneer.testkit.PioneerTestKit.executeTestMethod;

import java.util.TimeZone;

Expand All @@ -22,6 +24,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junitpioneer.testkit.ExecutionResults;

@DisplayName("DefaultTimeZone extension")
Expand Down Expand Up @@ -60,6 +63,25 @@ void doesNothingWhenAnnotationNotPresent() {
assertThat(TimeZone.getDefault()).isEqualTo(TEST_DEFAULT_TIMEZONE);
}

@Test
@DisplayName("throws exception on bad configuration")
void throwsWhenConfigurationIsBad() {
ExecutionResults results = executeTestMethod(BadMethodLevelConfigurationTestCase.class, "badConfiguration");

assertThat(results.numberOfFailedTests()).isEqualTo(1);
assertThat(results.firstFailuresThrowable()).isExactlyInstanceOf(ExtensionConfigurationException.class);
assertThat(results.firstFailuresThrowableMessage())
.doesNotContain("should never execute")
.contains("@DefaultTimeZone not configured correctly.");
}

@DefaultTimeZone("GMT")
@Test
@DisplayName("does not throw when explicitly set to GMT")
void doesNotThrowForExplicitGmt() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT"));
}

@DefaultTimeZone("CET")
@Test
@DisplayName("sets the default time zone using an abbreviation")
Expand All @@ -86,13 +108,31 @@ void setUp() {
}

@Test
@DisplayName("should execute tests with configured TimeZone")
@DisplayName("executes tests with configured TimeZone")
void shouldExecuteTestsWithConfiguredTimeZone() {
ExecutionResults results = executeTestClass(ClassLevelTestCase.class);

assertThat(results.numberOfSucceededTests()).isEqualTo(2);
}

@Test
@DisplayName("throws when configuration is bad")
void shouldThrowWithBadConfiguration() {
ExecutionResults results = executeTestClass(BadClassLevelConfigurationTestCase.class);

assertThat(results.numberOfStartedTests()).isEqualTo(0);
assertThat(results.firstFailuresThrowable()).isExactlyInstanceOf(ExtensionConfigurationException.class);
assertThat(results.firstFailuresThrowableMessage()).contains("@DefaultTimeZone not configured correctly.");
}

@Test
@DisplayName("does not throw when explicitly set to GMT")
void shouldNotThrowForExplicitGmt() {
ExecutionResults results = executeTestClass(ExplicitGmtClassLevelTestCase.class);

assertThat(results.numberOfSucceededTests()).isEqualTo(1);
}

@AfterEach
void tearDown() {
assertThat(TimeZone.getDefault()).isEqualTo(TEST_DEFAULT_TIMEZONE);
Expand Down Expand Up @@ -155,4 +195,34 @@ public void shouldSetTimeZoneFromMethodOfNestedClass() {

}

static class BadMethodLevelConfigurationTestCase {

@DefaultTimeZone("Gibberish")
@Test
void badConfiguration() {
fail("This test should never execute");
}

}

@DefaultTimeZone("Gibberish")
static class BadClassLevelConfigurationTestCase {

@Test
void badConfiguration() {
fail("This test should never execute");
}

}

@DefaultTimeZone("GMT")
static class ExplicitGmtClassLevelTestCase {

@Test
void explicitGmt() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT"));
}

}

}

0 comments on commit 4da75d5

Please sign in to comment.