Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ExtensionConfigurationException to DefaultTimeZoneExtension #272

Merged
merged 4 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 Down Expand Up @@ -43,6 +44,11 @@ public void beforeEach(ExtensionContext context) {
private void setDefaultTimeZone(Store store, DefaultTimeZone annotation) {
storeDefaultTimeZone(store);
TimeZone configuredTimeZone = TimeZone.getTimeZone(annotation.value());
// TimeZone::getTimeZone returns with GMT as fallback if the given ID cannot be understood
if (configuredTimeZone.equals(TimeZone.getTimeZone("GMT")) && !annotation.value().equals("GMT")) {
Michael1993 marked this conversation as resolved.
Show resolved Hide resolved
throw new ExtensionConfigurationException(
"@DefaultTimeZone not configured correctly. Could not find the specified TimeZone.");
}
TimeZone.setDefault(configuredTimeZone);
}

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"));
}

}

}