Skip to content

Commit

Permalink
Inherit default locale and timezone annotations (#456 / #615)
Browse files Browse the repository at this point in the history
`@DefaultLocale` and `@DefaultTimeZone` are now `@Inherited`.

Relates to: #456
PR: #615
  • Loading branch information
beatngu13 committed Apr 7, 2022
1 parent 19cdcb9 commit 9a90140
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/default-locale-timezone.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:page-description: Extends JUnit Jupiter with `@DefaultLocale`, `@DefaultTimeZone`, which change the values returned from Locale.getDefault() and TimeZone.getDefault()

The `@DefaultLocale` and `@DefaultTimeZone` annotations can be used to change the values returned from `Locale.getDefault()` and `TimeZone.getDefault()`.
Both annotations work on the test class level and on the test method level.
Both annotations work on the test class level and on the test method level, and are inherited from higher-level containers.
After the annotated element has been executed the initial default value is restored.

== `@DefaultLocale`
Expand Down
2 changes: 1 addition & 1 deletion docs/environment-variables.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:page-description: Extends JUnit Jupiter with `@ClearEnvironmentVariable`, `@SetEnvironmentVariable`, which clear and set the values of environment variables

The `@ClearEnvironmentVariable` and `@SetEnvironmentVariable` annotations can be used to clear, respectively, set the values of environment variables for a test execution.
Both annotations work on the test method and class level, are repeatable as well as combinable.
Both annotations work on the test method and class level, are repeatable, combinable, and inherited from higher-level containers.
After the annotated method has been executed, the variables mentioned in the annotation will be restored to their original value or will be cleared if they didn't have one before.
Other environment variables that are changed during the test, are *not* restored.

Expand Down
2 changes: 1 addition & 1 deletion docs/system-properties.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:page-description: Extends JUnit Jupiter with `@ClearSystemProperty`, `@SetSystemProperty`, which clear and set the values of system properties

The `@ClearSystemProperty` and `@SetSystemProperty` annotations can be used to clear, respectively, set the values of system properties for a test execution.
Both annotations work on the test method and class level, are repeatable as well as combinable.
Both annotations work on the test method and class level, are repeatable, combinable, and inherited from higher-level containers.
After the annotated method has been executed, the properties mentioned in the annotation will be restored to their original value or will be cleared if they didn't have one before.
Other system properties that are changed during the test, are *not* restored.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
* <p>The key of the environment variable to be cleared must be specified via {@link #key()}.
* After the annotated element has been executed, the initial default value is restored.</p>
*
* <p>{@code ClearEnvironmentVariable} is repeatable and can be used on the method and
* on the class level. If a class is annotated, the configured variable will be
* cleared before every test inside that class.</p>
* <p>{@code ClearEnvironmentVariable} can be used on the method and on the class level.
* It is repeatable and inherited from higher-level containers. If a class is
* annotated, the configured property will be cleared before every test inside that
* class.</p>
*
* <p>WARNING: Java considers environment variables to be immutable, so this extension
* uses reflection to change them. This requires that the {@link SecurityManager}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
* <p>The key of the system property to be cleared must be specified via {@link #key()}.
* After the annotated element has been executed, the initial default value is restored.</p>
*
* <p>{@code ClearSystemProperty} is repeatable and can be used on the method and
* on the class level. If a class is annotated, the configured property will be
* cleared before every test inside that class.</p>
* <p>{@code ClearSystemProperty} can be used on the method and on the class level.
* It is repeatable and inherited from higher-level containers. If a class is
* annotated, the configured property will be cleared before every test inside that
* class.</p>
*
* <p>During
* <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution" target="_top">parallel test execution</a>,
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/junitpioneer/jupiter/DefaultLocale.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.junitpioneer.jupiter;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand Down Expand Up @@ -39,9 +40,10 @@
* element has been executed, the default {@code Locale} will be restored to
* its original value.</p>
*
* <p>{@code @DefaultLocale} can be used on the method and on the class level.
* If a class is annotated, the configured {@code Locale} will be the default
* {@code Locale} for all tests inside that class. Any method level
* <p>{@code @DefaultLocale} can be used on the method and on the class level. It
* is inherited from higher-level containers, but can only be used once per method
* or class. If a class is annotated, the configured {@code Locale} will be the
* default {@code Locale} for all tests inside that class. Any method level
* configurations will override the class level default {@code Locale}.</p>
*
* <p>During
Expand All @@ -60,6 +62,7 @@
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@WritesDefaultLocale
@ExtendWith(DefaultLocaleExtension.class)
public @interface DefaultLocale {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/junitpioneer/jupiter/DefaultTimeZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.junitpioneer.jupiter;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -28,9 +29,11 @@
* original value.</p>
*
* <p>{@code @DefaultTimeZone} can be used on the method and on the class
* level. If a class is annotated, the configured {@code TimeZone} will be the
* default {@code TimeZone} for all tests inside that class. Any method level
* configurations will override the class level default {@code TimeZone}.</p>
* level. It is inherited from higher-level containers, but can only be used
* once per method or class. If a class is annotated, the configured
* {@code TimeZone} will be the default {@code TimeZone} for all tests inside
* that class. Any method level configurations will override the class level
* default {@code TimeZone}.</p>
*
* <p>During
* <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution" target="_top">parallel test execution</a>,
Expand All @@ -48,6 +51,7 @@
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@WritesDefaultTimeZone
@ExtendWith(DefaultTimeZoneExtension.class)
public @interface DefaultTimeZone {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/junitpioneer/jupiter/DisabledUntil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* method is disabled.
*
* <p>{@code @DisabledUntil} can be used on the method and class level. It can only be used once per method or class,
* but is inherited from higher level containers.</p>
* but is inherited from higher-level containers.</p>
*
* @see org.junit.jupiter.api.Disabled
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
* {@link #key()} and {@link #value()}. After the annotated method has been
* executed, the initial default value is restored.</p>
*
* <p>{@code SetEnvironmentVariable} is repeatable and can be used on the method and on
* the class level. If a class is annotated, the configured variable will be set
* before every test inside that class. Any method level configurations will
* override the class level configurations.</p>
* <p>{@code SetEnvironmentVariable} can be used on the method and on the class level.
* It is repeatable and inherited from higher-level containers. If a class is
* annotated, the configured property will be set before every test inside that
* class. Any method level configurations will override the class level
* configurations.</p>
*
* <p>WARNING: Java considers environment variables to be immutable, so this extension
* uses reflection to change them. This requires that the {@link SecurityManager}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/junitpioneer/jupiter/SetSystemProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
* {@link #key()} and {@link #value()}. After the annotated method has been
* executed, the initial default value is restored.</p>
*
* <p>{@code SetSystemProperty} is repeatable and can be used on the method and on
* the class level. If a class is annotated, the configured property will be set
* before every test inside that class. Any method level configurations will
* override the class level configurations.</p>
* <p>{@code SetSystemProperty} can be used on the method and on the class level.
* It is repeatable and inherited from higher-level containers. If a class is
* annotated, the configured property will be set before every test inside that
* class. Any method level configurations will override the class level
* configurations.</p>
*
* <p>During
* <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution" target="_top">parallel test execution</a>,
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/junitpioneer/jupiter/DefaultLocaleTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,21 @@ void shouldFail() {

}

@Nested
@DisplayName("used with inheritance")
class InheritanceTests extends InheritanceBaseTest {

@Test
@DisplayName("should inherit default locale annotation")
void shouldInheritClearAndSetProperty() {
assertThat(Locale.getDefault()).isEqualTo(new Locale("fr", "FR"));
}

}

@DefaultLocale(language = "fr", country = "FR")
static class InheritanceBaseTest {

}

}
17 changes: 17 additions & 0 deletions src/test/java/org/junitpioneer/jupiter/DefaultTimeZoneTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,21 @@ void badConfiguration() {

}

@Nested
@DisplayName("used with inheritance")
class InheritanceTests extends InheritanceBaseTest {

@Test
@DisplayName("should inherit default time zone annotation")
void shouldInheritClearAndSetProperty() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("GMT-8:00"));
}

}

@DefaultTimeZone("GMT-8:00")
static class InheritanceBaseTest {

}

}

0 comments on commit 9a90140

Please sign in to comment.