From f56e3ca11a85c58c925b0873f471dd73b62e91b7 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Tue, 26 May 2020 12:23:24 +0300 Subject: [PATCH 01/37] Adjust the `EnvironmentTests` to no longer assume environments to be a boolean test/not-a-test. Instead, now it's an enum that the users can extend. --- .../java/io/spine/base/EnvironmentTest.java | 134 +++++++++++++++--- config | 2 +- 2 files changed, 114 insertions(+), 22 deletions(-) diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 8e1d905ddc..f2cf41449f 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -26,11 +26,16 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertFalse; +import static com.google.common.truth.Truth.assertThat; +import static io.spine.base.BaseEnvironmentType.ENV_KEY_TESTS; +import static io.spine.base.BaseEnvironmentType.PRODUCTION; +import static io.spine.base.BaseEnvironmentType.TESTS; +import static io.spine.base.EnvironmentTest.CustomEnvType.LOCAL; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; @DisplayName("Environment utility class should") @SuppressWarnings("AccessOfSystemProperties") @@ -81,54 +86,48 @@ void cleanUp() { @DisplayName("tell that we are under tests if env. variable set to true") void environmentVarTrue() { Environment.instance() - .setToTests(); + .setTo(TESTS); - assertTrue(environment.isTests()); - assertFalse(environment.isProduction()); + assertThat(environment.envType()).isSameInstanceAs(TESTS); } @Test @DisplayName("tell that we are under tests if env. variable set to 1") void environmentVar1() { - System.setProperty(Environment.ENV_KEY_TESTS, "1"); + System.setProperty(ENV_KEY_TESTS, "1"); - assertTrue(environment.isTests()); - assertFalse(environment.isProduction()); + assertThat(environment.envType()).isSameInstanceAs(TESTS); } @Test @DisplayName("tell that we are under tests if run under known framework") void underTestFramework() { // As we run this from under JUnit... - assertTrue(environment.isTests()); - assertFalse(environment.isProduction()); + assertThat(environment.envType()).isSameInstanceAs(TESTS); } @Test @DisplayName("tell that we are not under tests if env set to something else") - void environmentVarUknownValue() { - System.setProperty(Environment.ENV_KEY_TESTS, "neitherTrueNor1"); + void environmentVarUnknownValue() { + System.setProperty(ENV_KEY_TESTS, "neitherTrueNor1"); - assertFalse(environment.isTests()); - assertTrue(environment.isProduction()); + assertThat(environment.envType()).isSameInstanceAs(PRODUCTION); } @Test @DisplayName("turn tests mode on") void turnTestsOn() { - environment.setToTests(); + environment.setTo(TESTS); - assertTrue(environment.isTests()); - assertFalse(environment.isProduction()); + assertThat(environment.envType()).isSameInstanceAs(TESTS); } @Test @DisplayName("turn production mode on") void turnProductionOn() { - environment.setToProduction(); + environment.setTo(PRODUCTION); - assertFalse(environment.isTests()); - assertTrue(environment.isProduction()); + assertThat(environment.envType()).isSameInstanceAs(PRODUCTION); } @Test @@ -136,6 +135,99 @@ void turnProductionOn() { void clearOnReset() { environment.reset(); - assertNull(System.getProperty(Environment.ENV_KEY_TESTS)); + assertNull(System.getProperty(ENV_KEY_TESTS)); + } + + @Nested + @DisplayName("when assigning custom environment types") + class CustomEnvTypes { + + @Test + @DisplayName("allow to provide user defined environment types") + void mutateKnownEnvTypesOnce() { + Environment.registerCustom(CustomEnvType.class); + + // Now that `Environment` knows about `LOCAL`, it should use it as fallback. + assertThat(Environment.instance() + .envType()).isSameInstanceAs(LOCAL); + } + + @Test + @DisplayName("throw if a user attempts to create register the same environment twice") + void throwOnDoubleCreation() { + Environment.registerCustom(CustomEnvType.class); + assertThrows(IllegalStateException.class, + () -> Environment.registerCustom(CustomEnvType.class)); + } + + @Test + @DisplayName("fallback to the `TESTS` environment") + void fallBack() { + Environment.registerCustom(BuildServerEnvironment.class); + assertThat(Environment.instance() + .envType()) + .isSameInstanceAs(TESTS); + } + } + + enum CustomEnvType implements EnvironmentType { + + LOCAL { + @Override + public boolean currentlyOn() { + // `LOCAL` is the default custom env type. It should be used as a fallback. + return true; + } + + @Override + public void reset() { + // NOP. + } + + @Override + public void setTo() { + // NOP. + } + }, + STAGING { + @Override + public boolean currentlyOn() { + return System.getProperty(STAGING_ENV_TYPE_KEY) + .equalsIgnoreCase(String.valueOf(true)); + + } + + @Override + public void reset() { + System.clearProperty(STAGING_ENV_TYPE_KEY); + } + + @Override + public void setTo() { + System.setProperty(STAGING_ENV_TYPE_KEY, String.valueOf(true)); + } + }; + + static final String STAGING_ENV_TYPE_KEY = "io.spine.base.EnvironmentTest.is_staging"; + } + + enum BuildServerEnvironment implements EnvironmentType { + + TRAVIS { + @Override + public boolean currentlyOn() { + return false; + } + + @Override + public void reset() { + // NOP. + } + + @Override + public void setTo() { + // NOP. + } + } } } diff --git a/config b/config index 5ffcb1a8db..5bb6b9f046 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit 5ffcb1a8dbe4b9f14aacb9ecf663c5e639eac4b5 +Subproject commit 5bb6b9f04693af4c98bba41127257abcb8774b5e From 6e42cc5919c2ca49242b81b223c91cebfc04bc1f Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Tue, 26 May 2020 12:39:15 +0300 Subject: [PATCH 02/37] Introduce the environment-related API. Strip the `Environment` docs. --- .../io/spine/base/BaseEnvironmentType.java | 113 +++++++++++++ .../main/java/io/spine/base/Environment.java | 148 ++++++------------ .../java/io/spine/base/EnvironmentType.java | 56 +++++++ 3 files changed, 221 insertions(+), 96 deletions(-) create mode 100644 base/src/main/java/io/spine/base/BaseEnvironmentType.java create mode 100644 base/src/main/java/io/spine/base/EnvironmentType.java diff --git a/base/src/main/java/io/spine/base/BaseEnvironmentType.java b/base/src/main/java/io/spine/base/BaseEnvironmentType.java new file mode 100644 index 0000000000..229c03946c --- /dev/null +++ b/base/src/main/java/io/spine/base/BaseEnvironmentType.java @@ -0,0 +1,113 @@ +/* + * Copyright 2020, TeamDev. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package io.spine.base; + +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; + +import java.util.regex.Pattern; + +/** + * Environment types provided by the base library. + * + *

Provides a {@code TESTS} option, that looks into the stack trace to find mentions of + * known testing frameworks. It also looks at an environment variable. + * + *

Also provides a {@code PRODUCTION} option. System is considered to be in {@code PRODUCTION} + * if its not in {@code TESTS}, i.e. they are mutually exclusive. + */ +enum BaseEnvironmentType implements EnvironmentType { + + @SuppressWarnings("AccessOfSystemProperties" /* OK as we need system properties for this class. */) + TESTS { + /** + * Verifies if the code currently runs under a unit testing framework. + * + *

The method returns {@code true} if the following packages are discovered + * in the stacktrace: + *

+ * + * @return {@code true} if the code runs under a testing framework, {@code false} otherwise + */ + @Override + public boolean currentlyOn() { + // Check the environment variable. We may run under unknown testing framework or + // tests may require production-like mode, which they simulate by setting + // the property to `false`. + String testProp = System.getProperty(ENV_KEY_TESTS); + if (testProp != null) { + testProp = TEST_PROP_PATTERN.matcher(testProp) + .replaceAll(""); + return (String.valueOf(true) + .equalsIgnoreCase(testProp) + || "1".equals(testProp)); + } + + // Check stacktrace for known frameworks. + String stacktrace = Throwables.getStackTraceAsString(new RuntimeException("")); + return KNOWN_TESTING_FRAMEWORKS.stream() + .anyMatch(stacktrace::contains); + } + + @Override + public void reset() { + System.clearProperty(ENV_KEY_TESTS); + } + + @Override + public void setTo() { + System.setProperty(ENV_KEY_TESTS, String.valueOf(true)); + } + }, PRODUCTION { + @Override + public boolean currentlyOn() { + return !TESTS.currentlyOn(); + } + + @Override + public void reset() { + // NOP. + } + + @Override + public void setTo() { + // NOP. + } + }; + + /** + * The key name of the system property which tells if a code runs under a testing framework. + * + *

If your testing framework is not among the + * {@link BaseEnvironmentType#KNOWN_TESTING_FRAMEWORKS}, set this property to {@code true} + * before running tests. + */ + public static final String ENV_KEY_TESTS = "io.spine.tests"; + + @SuppressWarnings("DuplicateStringLiteralInspection" /* Used in another context. */) + public static final ImmutableList KNOWN_TESTING_FRAMEWORKS = + ImmutableList.of("org.junit", "org.testng"); + + private static final Pattern TEST_PROP_PATTERN = Pattern.compile("\"' "); +} diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 6c938b4a67..1d769acae8 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -21,40 +21,53 @@ package io.spine.base; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; import io.spine.annotation.SPI; import org.checkerframework.checker.nullness.qual.Nullable; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; +import static io.spine.base.BaseEnvironmentType.PRODUCTION; +import static io.spine.base.BaseEnvironmentType.TESTS; + /** * Provides information about the environment (current platform used, etc.). */ @SPI -@SuppressWarnings("AccessOfSystemProperties") // OK as we need system properties for this class. public final class Environment { - private static final Environment INSTANCE = new Environment(); + private static final ImmutableList BASE_TYPES = + ImmutableList.of(TESTS, PRODUCTION); - /** - * The key name of the system property which tells if a code runs under a testing framework. - * - *

If your testing framework is not among the supported by {@link #isTests()}, - * set this property to {@code true} before running tests. - */ - public static final String ENV_KEY_TESTS = "io.spine.tests"; + private static final Environment INSTANCE = new Environment(); - /** If set, tells if the code runs from a testing framework. */ - private @Nullable Boolean tests; + private ImmutableList knownEnvTypes; + private @Nullable EnvironmentType currentEnvType; - /** Prevents instantiation of this singleton class from outside. */ private Environment() { + this.knownEnvTypes = BASE_TYPES; } /** Creates a new instance with the copy of the state of the passed environment. */ private Environment(Environment copy) { - this.tests = copy.tests; + this.knownEnvTypes = copy.knownEnvTypes; + this.currentEnvType = copy.currentEnvType; + } + + public static void registerCustom(Class enumClass) { + checkNotNull(enumClass); + ImmutableList newTypes = ImmutableList.copyOf(enumClass.getEnumConstants()); + checkState(!INSTANCE.knownEnvTypes.containsAll(newTypes), + "Attempted to register the same custom env enum `%s` twice." + + "Please make sure to call `Environment.registerCustom(...) only once" + + "per enum.", enumClass.getSimpleName()); + INSTANCE.knownEnvTypes = ImmutableList + .builder() + .addAll(newTypes) + .addAll(BASE_TYPES) + .build(); } - /** Returns the singleton instance. */ public static Environment instance() { return INSTANCE; } @@ -68,6 +81,18 @@ public Environment createCopy() { return new Environment(this); } + public EnvironmentType envType() { + if (currentEnvType == null) { + for (EnvironmentType type : knownEnvTypes) { + if (type.currentlyOn()) { + this.currentEnvType = type; + return this.currentEnvType; + } + } + } + return currentEnvType; + } + /** * Restores the state from the instance created by {@link #createCopy()}. * @@ -76,94 +101,25 @@ public Environment createCopy() { @VisibleForTesting public void restoreFrom(Environment copy) { // Make sure this matches the set of fields copied in the copy constructor. - this.tests = copy.tests; - } - - /** - * Verifies if the code currently runs under a unit testing framework. - * - *

The method returns {@code true} if the following packages are discovered - * in the stacktrace: - *

- * - * @return {@code true} if the code runs under a testing framework, {@code false} otherwise - */ - @SuppressWarnings({ - "DynamicRegexReplaceableByCompiledPattern", // OK as we cache the result - "DuplicateStringLiteralInspection" // used in another context - }) - public boolean isTests() { - // If we cached the value before, return it. - if (tests != null) { - return tests; - } - - // Check the environment variable. We may run under unknown testing framework or - // tests may require production-like mode, which they simulate by setting - // the property to `false`. - String testProp = System.getProperty(ENV_KEY_TESTS); - if (testProp != null) { - testProp = testProp.replaceAll("\"' ", ""); - this.tests = (String.valueOf(true) - .equalsIgnoreCase(testProp) - || "1".equals(testProp)); - return this.tests; - } - - // Check stacktrace for known frameworks. - String stacktrace = Throwables.getStackTraceAsString(new RuntimeException("")); - if (stacktrace.contains("org.junit") - || stacktrace.contains("org.testng")) { - this.tests = true; - return true; + this.knownEnvTypes = copy.knownEnvTypes; + this.currentEnvType = copy.currentEnvType; + if (currentEnvType != null) { + currentEnvType.setTo(); } - - this.tests = false; - return false; - } - - /** - * Verifies if the code runs in the production mode. - * - *

This method is opposite to {@link #isTests()} - * - * @return {@code true} if the code runs in the production mode, {@code false} otherwise - */ - public boolean isProduction() { - return !isTests(); } - /** - * Turns the test mode on. - * - *

This method is opposite to {@link #setToProduction()}. - */ - @VisibleForTesting - public void setToTests() { - this.tests = true; - System.setProperty(ENV_KEY_TESTS, String.valueOf(true)); + public void setTo(EnvironmentType type) { + this.currentEnvType = type; + currentEnvType.setTo(); } - /** - * Turns the production mode on. - * - *

This method is opposite to {@link #setToTests()}. - */ - @VisibleForTesting - public void setToProduction() { - this.tests = false; - System.setProperty(ENV_KEY_TESTS, String.valueOf(false)); - } - /** - * Resets the instance and clears the {@link #ENV_KEY_TESTS} variable. - */ @VisibleForTesting public void reset() { - this.tests = null; - System.clearProperty(ENV_KEY_TESTS); + if (currentEnvType != null) { + currentEnvType.reset(); + } + this.currentEnvType = null; + this.knownEnvTypes = BASE_TYPES; } } diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java new file mode 100644 index 0000000000..c987194bf0 --- /dev/null +++ b/base/src/main/java/io/spine/base/EnvironmentType.java @@ -0,0 +1,56 @@ +/* + * Copyright 2020, TeamDev. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package io.spine.base; + +/** + * A type of an environment that knows whether it is enabled. + * + *

Useful examples may include determining whether the code is being run from the production, + * staging or local environment. By default, the base library provides {@link BaseEnvironmentType}. + */ +public interface EnvironmentType { + + /** + * Returns {@code true} if the underlying system is currently in this environment type. + * + *

For example, let's say that an application is deployed to a fleet of virtual machines. + * Let's say the cloud provider sets an environment variable for every virtual machine. + * An application developer may use this type of knowledge to determine the current environment. + */ + boolean currentlyOn(); + + /** + * Makes it so the underlying system no longer has this environment type. + * + *

Be careful, since this method may mutate the state that is outside the scope of the + * Java application, such as clear an environment variable, remove a file, etc. + */ + void reset(); + + /** + * Makes it so the underlying system has this environment type. + * + *

Be careful, since this method may mutate the state that is outside the scope of the + * Java application, such as set an environment variable, create or move a file, etc. + */ + void setTo(); +} + From fc5a8d8bb5595402cab543c207f293fde15ec549 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Tue, 26 May 2020 12:56:15 +0300 Subject: [PATCH 03/37] Update the `Environment` Javadocs and the license report. --- .../main/java/io/spine/base/Environment.java | 47 ++++++++++++++++++- license-report.md | 30 ++++++------ 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 1d769acae8..3f61e2300a 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -32,6 +32,15 @@ /** * Provides information about the environment (current platform used, etc.). + * + *

By default, knows only about {@link BaseEnvironmentType}. Library users may extend the list + * of known environment types by providing their environment {@code enum} to + * {@linkplain #registerCustom(Class)}. + * + *

When extending, please note that this class does not handle the situations when the + * two or more {@linkplain EnvironmentType environment types} return {@code true} on the + * {@link EnvironmentType#currentlyOn()}. As such, if two or more user-defined environment types + * think that they are currently on, the behaviour of {@link #envType()} is undefined. */ @SPI public final class Environment { @@ -54,6 +63,22 @@ private Environment(Environment copy) { this.currentEnvType = copy.currentEnvType; } + /** + * Registers all of th constants from the provided {@code EnvironmentType}-implementing + * {@code enum}. + * + *

If the specified {@code enum} has already been registered, throws + * an {@code IllegalStateException}. + * + *

Note that the {@linkplain BaseEnvironmentType default types} are still present. + * When trying to {@linkplain #envType() determine which environment type} is currently on, + * the user defined types are checked first. + * + * @param enumClass + * an enum class that specifies the environment types + * @param + * a type that defines possible environment types + */ public static void registerCustom(Class enumClass) { checkNotNull(enumClass); ImmutableList newTypes = ImmutableList.copyOf(enumClass.getEnumConstants()); @@ -68,6 +93,7 @@ public static void registerCustom(Class en .build(); } + /** Returns the singleton instance. */ public static Environment instance() { return INSTANCE; } @@ -81,6 +107,18 @@ public Environment createCopy() { return new Environment(this); } + /** + * Determines the current environment type. + * + *

If {@linkplain #registerCustom(Class) user has defined custom env types}, goes through + * them in an undefined order fist. Then, checks the {@linkplain BaseEnvironmentType base env + * types}. + * + *

Note that if all of the {@link EnvironmentType#currentlyOn()} checks have returned + * {@code false}, this method falls back on {@link BaseEnvironmentType#PRODUCTION}. + * + * @return the current environment type. + */ public EnvironmentType envType() { if (currentEnvType == null) { for (EnvironmentType type : knownEnvTypes) { @@ -108,12 +146,19 @@ public void restoreFrom(Environment copy) { } } + /** + * Forces the specified environment type to be the current one. + */ + @VisibleForTesting public void setTo(EnvironmentType type) { this.currentEnvType = type; currentEnvType.setTo(); } - + /** + * Resets the instance and performs {@linkplain EnvironmentType#reset() the + * environment-specific reset}. + */ @VisibleForTesting public void reset() { if (currentEnvType != null) { diff --git a/license-report.md b/license-report.md index cac8d088b0..9a2967223c 100644 --- a/license-report.md +++ b/license-report.md @@ -328,7 +328,7 @@ The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:11 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:18 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -773,7 +773,7 @@ This report was generated on **Mon May 18 17:02:11 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:16 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:19 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1156,7 +1156,7 @@ This report was generated on **Mon May 18 17:02:16 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:19 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:20 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1521,7 +1521,7 @@ This report was generated on **Mon May 18 17:02:19 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:23 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:20 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1902,7 +1902,7 @@ This report was generated on **Mon May 18 17:02:23 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:35 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:21 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -2281,7 +2281,7 @@ This report was generated on **Mon May 18 17:02:35 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:38 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:21 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -2646,7 +2646,7 @@ This report was generated on **Mon May 18 17:02:38 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:43 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3065,7 +3065,7 @@ This report was generated on **Mon May 18 17:02:43 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:49 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3430,7 +3430,7 @@ This report was generated on **Mon May 18 17:02:49 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:02:54 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3795,7 +3795,7 @@ This report was generated on **Mon May 18 17:02:54 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:03:04 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4120,7 +4120,7 @@ This report was generated on **Mon May 18 17:03:04 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:03:09 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4453,7 +4453,7 @@ This report was generated on **Mon May 18 17:03:09 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:03:09 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4832,7 +4832,7 @@ This report was generated on **Mon May 18 17:03:09 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:03:13 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:24 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -5165,7 +5165,7 @@ This report was generated on **Mon May 18 17:03:13 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:03:17 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Mon May 25 12:01:24 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -5498,4 +5498,4 @@ This report was generated on **Mon May 18 17:03:17 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 18 17:03:21 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file +This report was generated on **Mon May 25 12:01:24 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file From e1ab97e3f63fbe3c8e4be81fe43edd9efd53e741 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Tue, 26 May 2020 13:07:36 +0300 Subject: [PATCH 04/37] Add a method Javadoc sentence. --- base/src/main/java/io/spine/base/Environment.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 3f61e2300a..b7b669e9d3 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -158,6 +158,8 @@ public void setTo(EnvironmentType type) { /** * Resets the instance and performs {@linkplain EnvironmentType#reset() the * environment-specific reset}. + * + *

Also the resets the {@linkplain #registerCustom(Class) user-defined environment types}. */ @VisibleForTesting public void reset() { From 55daf089ff5111a04f7335d8c897e85d453f0a55 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Tue, 26 May 2020 13:09:31 +0300 Subject: [PATCH 05/37] Advance the version. Related PR: https://github.com/SpineEventEngine/base/pull/539. --- version.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.gradle.kts b/version.gradle.kts index d307d56558..211c55daa5 100644 --- a/version.gradle.kts +++ b/version.gradle.kts @@ -25,7 +25,7 @@ * as we want to manage the versions in a single source. */ -val SPINE_VERSION = "1.5.11" +val SPINE_VERSION = "1.5.12" project.extra.apply { this["spineVersion"] = SPINE_VERSION From 478b10e76af485985658424eb88d4c0ff80ab944 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Tue, 26 May 2020 13:30:18 +0300 Subject: [PATCH 06/37] Fix documentation errors. --- base/src/main/java/io/spine/base/Environment.java | 8 ++++---- base/src/main/java/io/spine/base/EnvironmentType.java | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index b7b669e9d3..2ec39ee9e1 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -37,8 +37,8 @@ * of known environment types by providing their environment {@code enum} to * {@linkplain #registerCustom(Class)}. * - *

When extending, please note that this class does not handle the situations when the - * two or more {@linkplain EnvironmentType environment types} return {@code true} on the + *

When extending, please note that this class does not handle the situations when two + * or more {@linkplain EnvironmentType environment types} return {@code true} on the * {@link EnvironmentType#currentlyOn()}. As such, if two or more user-defined environment types * think that they are currently on, the behaviour of {@link #envType()} is undefined. */ @@ -64,7 +64,7 @@ private Environment(Environment copy) { } /** - * Registers all of th constants from the provided {@code EnvironmentType}-implementing + * Registers all of the constants from the provided {@code EnvironmentType}-implementing * {@code enum}. * *

If the specified {@code enum} has already been registered, throws @@ -111,7 +111,7 @@ public Environment createCopy() { * Determines the current environment type. * *

If {@linkplain #registerCustom(Class) user has defined custom env types}, goes through - * them in an undefined order fist. Then, checks the {@linkplain BaseEnvironmentType base env + * them in an undefined order. Then, checks the {@linkplain BaseEnvironmentType base env * types}. * *

Note that if all of the {@link EnvironmentType#currentlyOn()} checks have returned diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java index c987194bf0..58fe5e57f6 100644 --- a/base/src/main/java/io/spine/base/EnvironmentType.java +++ b/base/src/main/java/io/spine/base/EnvironmentType.java @@ -23,8 +23,8 @@ /** * A type of an environment that knows whether it is enabled. * - *

Useful examples may include determining whether the code is being run from the production, - * staging or local environment. By default, the base library provides {@link BaseEnvironmentType}. + *

Useful examples may include distinguishable {@code STAGING} or {@code LOCAL} environments. + * The base library provides {@linkplain BaseEnvironmentType default environment types}. */ public interface EnvironmentType { @@ -32,7 +32,7 @@ public interface EnvironmentType { * Returns {@code true} if the underlying system is currently in this environment type. * *

For example, let's say that an application is deployed to a fleet of virtual machines. - * Let's say the cloud provider sets an environment variable for every virtual machine. + * Let's say an environment variable is set for every virtual machine. * An application developer may use this type of knowledge to determine the current environment. */ boolean currentlyOn(); From 53370c6309f59bf9e4ab381abcca66c97591dae0 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Tue, 26 May 2020 13:32:12 +0300 Subject: [PATCH 07/37] Remove unnecessary parens. --- base/src/main/java/io/spine/base/BaseEnvironmentType.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/base/src/main/java/io/spine/base/BaseEnvironmentType.java b/base/src/main/java/io/spine/base/BaseEnvironmentType.java index 229c03946c..426b5732b1 100644 --- a/base/src/main/java/io/spine/base/BaseEnvironmentType.java +++ b/base/src/main/java/io/spine/base/BaseEnvironmentType.java @@ -59,9 +59,8 @@ public boolean currentlyOn() { if (testProp != null) { testProp = TEST_PROP_PATTERN.matcher(testProp) .replaceAll(""); - return (String.valueOf(true) - .equalsIgnoreCase(testProp) - || "1".equals(testProp)); + return String.valueOf(true) + .equalsIgnoreCase(testProp) || "1".equals(testProp); } // Check stacktrace for known frameworks. From a32ff87cda135351e84d9ed1e0d98021a93f820e Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 27 May 2020 14:51:28 +0300 Subject: [PATCH 08/37] `@Internal`ize a method. --- base/src/main/java/io/spine/base/Environment.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 2ec39ee9e1..e089b8ba80 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -22,6 +22,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; +import io.spine.annotation.Internal; import io.spine.annotation.SPI; import org.checkerframework.checker.nullness.qual.Nullable; @@ -79,6 +80,7 @@ private Environment(Environment copy) { * @param * a type that defines possible environment types */ + @Internal public static void registerCustom(Class enumClass) { checkNotNull(enumClass); ImmutableList newTypes = ImmutableList.copyOf(enumClass.getEnumConstants()); From 1cf44c2114bf268683420cbae96b755e4d1ae6dd Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 27 May 2020 15:31:21 +0300 Subject: [PATCH 09/37] Cut the `EnvironmentType` - it no longer can set the system to itself. Correct some docs errors. --- .../io/spine/base/BaseEnvironmentType.java | 60 ++++++++----------- .../main/java/io/spine/base/Environment.java | 13 +--- .../java/io/spine/base/EnvironmentType.java | 21 +------ .../java/io/spine/base/EnvironmentTest.java | 36 +---------- 4 files changed, 33 insertions(+), 97 deletions(-) diff --git a/base/src/main/java/io/spine/base/BaseEnvironmentType.java b/base/src/main/java/io/spine/base/BaseEnvironmentType.java index 426b5732b1..58b1b1ccfc 100644 --- a/base/src/main/java/io/spine/base/BaseEnvironmentType.java +++ b/base/src/main/java/io/spine/base/BaseEnvironmentType.java @@ -26,16 +26,18 @@ import java.util.regex.Pattern; /** - * Environment types provided by the base library. - * - *

Provides a {@code TESTS} option, that looks into the stack trace to find mentions of - * known testing frameworks. It also looks at an environment variable. - * - *

Also provides a {@code PRODUCTION} option. System is considered to be in {@code PRODUCTION} - * if its not in {@code TESTS}, i.e. they are mutually exclusive. + * Environment types provided by the {@code base} library. */ enum BaseEnvironmentType implements EnvironmentType { + /** + * Testing environment. + * + *

Detected by checking stack trace for mentions of the known testing frameworks. + * + *

This option is mutually exclusive with {@link #PRODUCTION}, i.e. one of them is always + * enabled. + */ @SuppressWarnings("AccessOfSystemProperties" /* OK as we need system properties for this class. */) TESTS { /** @@ -49,12 +51,14 @@ enum BaseEnvironmentType implements EnvironmentType { * * * @return {@code true} if the code runs under a testing framework, {@code false} otherwise + * @implNote In addition to checking the stack trace, this method checks the environment + * variable value. If you wish to simulate not being in tests, the variable must be set + * to {@code false} explicitly. If your framework is not among the + * {@linkplain #KNOWN_TESTING_FRAMEWORKS known ones}, make sure to set the system property + * explicitly. */ @Override - public boolean currentlyOn() { - // Check the environment variable. We may run under unknown testing framework or - // tests may require production-like mode, which they simulate by setting - // the property to `false`. + public boolean enabled() { String testProp = System.getProperty(ENV_KEY_TESTS); if (testProp != null) { testProp = TEST_PROP_PATTERN.matcher(testProp) @@ -63,35 +67,21 @@ public boolean currentlyOn() { .equalsIgnoreCase(testProp) || "1".equals(testProp); } - // Check stacktrace for known frameworks. String stacktrace = Throwables.getStackTraceAsString(new RuntimeException("")); return KNOWN_TESTING_FRAMEWORKS.stream() .anyMatch(stacktrace::contains); } - - @Override - public void reset() { - System.clearProperty(ENV_KEY_TESTS); - } - - @Override - public void setTo() { - System.setProperty(ENV_KEY_TESTS, String.valueOf(true)); - } - }, PRODUCTION { - @Override - public boolean currentlyOn() { - return !TESTS.currentlyOn(); - } - - @Override - public void reset() { - // NOP. - } - + }, + /** + * A non-testing environment. + * + *

If the system is not in the {@link #TESTS} environment, it is in the production + * environment. + */ + PRODUCTION { @Override - public void setTo() { - // NOP. + public boolean enabled() { + return !TESTS.enabled(); } }; diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index e089b8ba80..c70c9d1dd9 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -40,7 +40,7 @@ * *

When extending, please note that this class does not handle the situations when two * or more {@linkplain EnvironmentType environment types} return {@code true} on the - * {@link EnvironmentType#currentlyOn()}. As such, if two or more user-defined environment types + * {@link EnvironmentType#enabled()}. As such, if two or more user-defined environment types * think that they are currently on, the behaviour of {@link #envType()} is undefined. */ @SPI @@ -116,7 +116,7 @@ public Environment createCopy() { * them in an undefined order. Then, checks the {@linkplain BaseEnvironmentType base env * types}. * - *

Note that if all of the {@link EnvironmentType#currentlyOn()} checks have returned + *

Note that if all of the {@link EnvironmentType#enabled()} checks have returned * {@code false}, this method falls back on {@link BaseEnvironmentType#PRODUCTION}. * * @return the current environment type. @@ -124,7 +124,7 @@ public Environment createCopy() { public EnvironmentType envType() { if (currentEnvType == null) { for (EnvironmentType type : knownEnvTypes) { - if (type.currentlyOn()) { + if (type.enabled()) { this.currentEnvType = type; return this.currentEnvType; } @@ -143,9 +143,6 @@ public void restoreFrom(Environment copy) { // Make sure this matches the set of fields copied in the copy constructor. this.knownEnvTypes = copy.knownEnvTypes; this.currentEnvType = copy.currentEnvType; - if (currentEnvType != null) { - currentEnvType.setTo(); - } } /** @@ -154,7 +151,6 @@ public void restoreFrom(Environment copy) { @VisibleForTesting public void setTo(EnvironmentType type) { this.currentEnvType = type; - currentEnvType.setTo(); } /** @@ -165,9 +161,6 @@ public void setTo(EnvironmentType type) { */ @VisibleForTesting public void reset() { - if (currentEnvType != null) { - currentEnvType.reset(); - } this.currentEnvType = null; this.knownEnvTypes = BASE_TYPES; } diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java index 58fe5e57f6..37e2c91c2d 100644 --- a/base/src/main/java/io/spine/base/EnvironmentType.java +++ b/base/src/main/java/io/spine/base/EnvironmentType.java @@ -21,7 +21,7 @@ package io.spine.base; /** - * A type of an environment that knows whether it is enabled. + * A type of environment that knows whether it is enabled. * *

Useful examples may include distinguishable {@code STAGING} or {@code LOCAL} environments. * The base library provides {@linkplain BaseEnvironmentType default environment types}. @@ -35,22 +35,5 @@ public interface EnvironmentType { * Let's say an environment variable is set for every virtual machine. * An application developer may use this type of knowledge to determine the current environment. */ - boolean currentlyOn(); - - /** - * Makes it so the underlying system no longer has this environment type. - * - *

Be careful, since this method may mutate the state that is outside the scope of the - * Java application, such as clear an environment variable, remove a file, etc. - */ - void reset(); - - /** - * Makes it so the underlying system has this environment type. - * - *

Be careful, since this method may mutate the state that is outside the scope of the - * Java application, such as set an environment variable, create or move a file, etc. - */ - void setTo(); + boolean enabled(); } - diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index f2cf41449f..f48bd0955d 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -174,38 +174,18 @@ enum CustomEnvType implements EnvironmentType { LOCAL { @Override - public boolean currentlyOn() { + public boolean enabled() { // `LOCAL` is the default custom env type. It should be used as a fallback. return true; } - - @Override - public void reset() { - // NOP. - } - - @Override - public void setTo() { - // NOP. - } }, STAGING { @Override - public boolean currentlyOn() { + public boolean enabled() { return System.getProperty(STAGING_ENV_TYPE_KEY) .equalsIgnoreCase(String.valueOf(true)); } - - @Override - public void reset() { - System.clearProperty(STAGING_ENV_TYPE_KEY); - } - - @Override - public void setTo() { - System.setProperty(STAGING_ENV_TYPE_KEY, String.valueOf(true)); - } }; static final String STAGING_ENV_TYPE_KEY = "io.spine.base.EnvironmentTest.is_staging"; @@ -215,19 +195,9 @@ enum BuildServerEnvironment implements EnvironmentType { TRAVIS { @Override - public boolean currentlyOn() { + public boolean enabled() { return false; } - - @Override - public void reset() { - // NOP. - } - - @Override - public void setTo() { - // NOP. - } } } } From a7131d8f6eb9e5ce088abb223d257ebd80df7232 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 27 May 2020 16:51:16 +0300 Subject: [PATCH 10/37] Register environment types even if they are not enums. Also update config. --- .../io/spine/base/BaseEnvironmentType.java | 2 +- .../main/java/io/spine/base/Environment.java | 45 +++++++------------ .../java/io/spine/base/EnvironmentTest.java | 26 +++++++---- buildSrc/build.gradle.kts | 8 ++++ config | 2 +- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/base/src/main/java/io/spine/base/BaseEnvironmentType.java b/base/src/main/java/io/spine/base/BaseEnvironmentType.java index 58b1b1ccfc..7a5468c2fc 100644 --- a/base/src/main/java/io/spine/base/BaseEnvironmentType.java +++ b/base/src/main/java/io/spine/base/BaseEnvironmentType.java @@ -28,7 +28,7 @@ /** * Environment types provided by the {@code base} library. */ -enum BaseEnvironmentType implements EnvironmentType { +public enum BaseEnvironmentType implements EnvironmentType { /** * Testing environment. diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index c70c9d1dd9..10e998c228 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -26,7 +26,6 @@ import io.spine.annotation.SPI; import org.checkerframework.checker.nullness.qual.Nullable; -import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static io.spine.base.BaseEnvironmentType.PRODUCTION; import static io.spine.base.BaseEnvironmentType.TESTS; @@ -34,10 +33,6 @@ /** * Provides information about the environment (current platform used, etc.). * - *

By default, knows only about {@link BaseEnvironmentType}. Library users may extend the list - * of known environment types by providing their environment {@code enum} to - * {@linkplain #registerCustom(Class)}. - * *

When extending, please note that this class does not handle the situations when two * or more {@linkplain EnvironmentType environment types} return {@code true} on the * {@link EnvironmentType#enabled()}. As such, if two or more user-defined environment types @@ -65,33 +60,30 @@ private Environment(Environment copy) { } /** - * Registers all of the constants from the provided {@code EnvironmentType}-implementing - * {@code enum}. + * Remembers the specified environment type, allowing {@linkplain #envType()} + * to determine whether it's enabled} later. * - *

If the specified {@code enum} has already been registered, throws - * an {@code IllegalStateException}. + *

If the specified environment type has already been registered, throws an + * {@code IllegalStateException}. * *

Note that the {@linkplain BaseEnvironmentType default types} are still present. * When trying to {@linkplain #envType() determine which environment type} is currently on, * the user defined types are checked first. * - * @param enumClass - * an enum class that specifies the environment types - * @param - * a type that defines possible environment types + * @param environmentType + * a user-defined environment type */ @Internal - public static void registerCustom(Class enumClass) { - checkNotNull(enumClass); - ImmutableList newTypes = ImmutableList.copyOf(enumClass.getEnumConstants()); - checkState(!INSTANCE.knownEnvTypes.containsAll(newTypes), - "Attempted to register the same custom env enum `%s` twice." + + public static void registerCustom(EnvironmentType environmentType) { + checkState(!INSTANCE.knownEnvTypes.contains(environmentType), + "Attempted to register the same custom env type `%s` twice." + "Please make sure to call `Environment.registerCustom(...) only once" + - "per enum.", enumClass.getSimpleName()); + "per environment type.", environmentType.getClass() + .getSimpleName()); INSTANCE.knownEnvTypes = ImmutableList .builder() - .addAll(newTypes) - .addAll(BASE_TYPES) + .add(environmentType) + .addAll(INSTANCE.knownEnvTypes) .build(); } @@ -112,9 +104,9 @@ public Environment createCopy() { /** * Determines the current environment type. * - *

If {@linkplain #registerCustom(Class) user has defined custom env types}, goes through - * them in an undefined order. Then, checks the {@linkplain BaseEnvironmentType base env - * types}. + *

If {@linkplain #registerCustom(EnvironmentType) custom env types have been defined}, + * goes through them in an undefined order. Then, checks the {@linkplain BaseEnvironmentType + * base env types}. * *

Note that if all of the {@link EnvironmentType#enabled()} checks have returned * {@code false}, this method falls back on {@link BaseEnvironmentType#PRODUCTION}. @@ -154,10 +146,7 @@ public void setTo(EnvironmentType type) { } /** - * Resets the instance and performs {@linkplain EnvironmentType#reset() the - * environment-specific reset}. - * - *

Also the resets the {@linkplain #registerCustom(Class) user-defined environment types}. + * Resets the instance. */ @VisibleForTesting public void reset() { diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index f48bd0955d..0f450e779e 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -33,6 +33,7 @@ import static io.spine.base.BaseEnvironmentType.ENV_KEY_TESTS; import static io.spine.base.BaseEnvironmentType.PRODUCTION; import static io.spine.base.BaseEnvironmentType.TESTS; +import static io.spine.base.EnvironmentTest.BuildServerEnvironment.TRAVIS; import static io.spine.base.EnvironmentTest.CustomEnvType.LOCAL; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -80,6 +81,7 @@ void setUp() { void cleanUp() { Environment.instance() .reset(); + System.clearProperty(ENV_KEY_TESTS); } @Test @@ -103,7 +105,9 @@ void environmentVar1() { @DisplayName("tell that we are under tests if run under known framework") void underTestFramework() { // As we run this from under JUnit... - assertThat(environment.envType()).isSameInstanceAs(TESTS); + EnvironmentType type = environment.envType(); + BaseEnvironmentType expected = TESTS; + assertThat(type).isSameInstanceAs(expected); } @Test @@ -145,7 +149,7 @@ class CustomEnvTypes { @Test @DisplayName("allow to provide user defined environment types") void mutateKnownEnvTypesOnce() { - Environment.registerCustom(CustomEnvType.class); + registerEnum(CustomEnvType.class); // Now that `Environment` knows about `LOCAL`, it should use it as fallback. assertThat(Environment.instance() @@ -155,21 +159,27 @@ void mutateKnownEnvTypesOnce() { @Test @DisplayName("throw if a user attempts to create register the same environment twice") void throwOnDoubleCreation() { - Environment.registerCustom(CustomEnvType.class); + Environment.registerCustom(LOCAL); assertThrows(IllegalStateException.class, - () -> Environment.registerCustom(CustomEnvType.class)); + () -> Environment.registerCustom(LOCAL)); } @Test @DisplayName("fallback to the `TESTS` environment") void fallBack() { - Environment.registerCustom(BuildServerEnvironment.class); + Environment.registerCustom(TRAVIS); assertThat(Environment.instance() .envType()) .isSameInstanceAs(TESTS); } } + private static void registerEnum(Class envTypeClass) { + for (E envType : envTypeClass.getEnumConstants()) { + Environment.registerCustom(envType); + } + } + enum CustomEnvType implements EnvironmentType { LOCAL { @@ -182,15 +192,15 @@ public boolean enabled() { STAGING { @Override public boolean enabled() { - return System.getProperty(STAGING_ENV_TYPE_KEY) - .equalsIgnoreCase(String.valueOf(true)); - + return String.valueOf(true) + .equalsIgnoreCase(System.getProperty(STAGING_ENV_TYPE_KEY)); } }; static final String STAGING_ENV_TYPE_KEY = "io.spine.base.EnvironmentTest.is_staging"; } + @SuppressWarnings("unused" /* The only variant is used. */) enum BuildServerEnvironment implements EnvironmentType { TRAVIS { diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index bdfd32a6d6..7dc929afca 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -19,6 +19,8 @@ */ plugins { + // Use Kotlin for `buildSrc`. + // https://kotlinlang.org/docs/reference/using-gradle.html#targeting-the-jvm kotlin("jvm").version("1.3.72") } @@ -26,3 +28,9 @@ repositories { mavenLocal() jcenter() } + +val jacksonVersion = "2.11.0" + +dependencies { + implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:$jacksonVersion") +} diff --git a/config b/config index 5bb6b9f046..d9223f98f5 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit 5bb6b9f04693af4c98bba41127257abcb8774b5e +Subproject commit d9223f98f58cca6a6c91d3339f0fa6c366e3a00c From 927978ef719fa489deff700cc546d37551033969 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 27 May 2020 17:27:01 +0300 Subject: [PATCH 11/37] Bump the version, regenerate the license report. --- license-report.md | 30 +++++++++++++++--------------- version.gradle.kts | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/license-report.md b/license-report.md index 9a2967223c..23cf4733ef 100644 --- a/license-report.md +++ b/license-report.md @@ -328,7 +328,7 @@ The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:18 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:47 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -773,7 +773,7 @@ This report was generated on **Mon May 25 12:01:18 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:19 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:47 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1156,7 +1156,7 @@ This report was generated on **Mon May 25 12:01:19 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:20 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:48 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1521,7 +1521,7 @@ This report was generated on **Mon May 25 12:01:20 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:20 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1902,7 +1902,7 @@ This report was generated on **Mon May 25 12:01:20 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:21 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -2281,7 +2281,7 @@ This report was generated on **Mon May 25 12:01:21 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:21 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -2646,7 +2646,7 @@ This report was generated on **Mon May 25 12:01:21 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3065,7 +3065,7 @@ This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3430,7 +3430,7 @@ This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3795,7 +3795,7 @@ This report was generated on **Mon May 25 12:01:22 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4120,7 +4120,7 @@ This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4453,7 +4453,7 @@ This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4832,7 +4832,7 @@ This report was generated on **Mon May 25 12:01:23 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:24 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:52 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -5165,7 +5165,7 @@ This report was generated on **Mon May 25 12:01:24 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:24 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Tue May 26 13:03:52 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -5498,4 +5498,4 @@ This report was generated on **Mon May 25 12:01:24 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Mon May 25 12:01:24 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file +This report was generated on **Tue May 26 13:03:52 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file diff --git a/version.gradle.kts b/version.gradle.kts index 211c55daa5..8e3a0f75fd 100644 --- a/version.gradle.kts +++ b/version.gradle.kts @@ -25,7 +25,7 @@ * as we want to manage the versions in a single source. */ -val SPINE_VERSION = "1.5.12" +val SPINE_VERSION = "1.5.13" project.extra.apply { this["spineVersion"] = SPINE_VERSION From 6897c5e2bec9d0889a89093401c079d0bedb05bf Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 27 May 2020 17:37:18 +0300 Subject: [PATCH 12/37] Fix method naming and documentation wording errors. --- .../src/main/java/io/spine/base/Environment.java | 11 ++++++----- .../main/java/io/spine/base/EnvironmentType.java | 8 ++++---- .../test/java/io/spine/base/EnvironmentTest.java | 16 ++++++++-------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 10e998c228..7e059ab3b9 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -36,7 +36,8 @@ *

When extending, please note that this class does not handle the situations when two * or more {@linkplain EnvironmentType environment types} return {@code true} on the * {@link EnvironmentType#enabled()}. As such, if two or more user-defined environment types - * think that they are currently on, the behaviour of {@link #envType()} is undefined. + * think that they are currently enabled, the behaviour of {@link #currentType()} is + * undefined. */ @SPI public final class Environment { @@ -60,14 +61,14 @@ private Environment(Environment copy) { } /** - * Remembers the specified environment type, allowing {@linkplain #envType()} + * Remembers the specified environment type, allowing {@linkplain #currentType()} * to determine whether it's enabled} later. * *

If the specified environment type has already been registered, throws an * {@code IllegalStateException}. * *

Note that the {@linkplain BaseEnvironmentType default types} are still present. - * When trying to {@linkplain #envType() determine which environment type} is currently on, + * When trying to {@linkplain #currentType() determine which environment type} is enabled, * the user defined types are checked first. * * @param environmentType @@ -108,12 +109,12 @@ public Environment createCopy() { * goes through them in an undefined order. Then, checks the {@linkplain BaseEnvironmentType * base env types}. * - *

Note that if all of the {@link EnvironmentType#enabled()} checks have returned + *

Note that if all of the {@link EnvironmentType#enabled()} checks have returned * {@code false}, this method falls back on {@link BaseEnvironmentType#PRODUCTION}. * * @return the current environment type. */ - public EnvironmentType envType() { + public EnvironmentType currentType() { if (currentEnvType == null) { for (EnvironmentType type : knownEnvTypes) { if (type.enabled()) { diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java index 37e2c91c2d..b4d075f86a 100644 --- a/base/src/main/java/io/spine/base/EnvironmentType.java +++ b/base/src/main/java/io/spine/base/EnvironmentType.java @@ -24,16 +24,16 @@ * A type of environment that knows whether it is enabled. * *

Useful examples may include distinguishable {@code STAGING} or {@code LOCAL} environments. - * The base library provides {@linkplain BaseEnvironmentType default environment types}. + * {@code base} library provides {@linkplain BaseEnvironmentType default environment types}. */ public interface EnvironmentType { /** * Returns {@code true} if the underlying system is currently in this environment type. * - *

For example, let's say that an application is deployed to a fleet of virtual machines. - * Let's say an environment variable is set for every virtual machine. - * An application developer may use this type of knowledge to determine the current environment. + *

For example, if an application is deployed to a fleet of virtual machines, an environment + * variable may be set for every virtual machine. + * Application developer may use this type of knowledge to determine the current environment. */ boolean enabled(); } diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 0f450e779e..2804167e79 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -90,7 +90,7 @@ void environmentVarTrue() { Environment.instance() .setTo(TESTS); - assertThat(environment.envType()).isSameInstanceAs(TESTS); + assertThat(environment.currentType()).isSameInstanceAs(TESTS); } @Test @@ -98,14 +98,14 @@ void environmentVarTrue() { void environmentVar1() { System.setProperty(ENV_KEY_TESTS, "1"); - assertThat(environment.envType()).isSameInstanceAs(TESTS); + assertThat(environment.currentType()).isSameInstanceAs(TESTS); } @Test @DisplayName("tell that we are under tests if run under known framework") void underTestFramework() { // As we run this from under JUnit... - EnvironmentType type = environment.envType(); + EnvironmentType type = environment.currentType(); BaseEnvironmentType expected = TESTS; assertThat(type).isSameInstanceAs(expected); } @@ -115,7 +115,7 @@ void underTestFramework() { void environmentVarUnknownValue() { System.setProperty(ENV_KEY_TESTS, "neitherTrueNor1"); - assertThat(environment.envType()).isSameInstanceAs(PRODUCTION); + assertThat(environment.currentType()).isSameInstanceAs(PRODUCTION); } @Test @@ -123,7 +123,7 @@ void environmentVarUnknownValue() { void turnTestsOn() { environment.setTo(TESTS); - assertThat(environment.envType()).isSameInstanceAs(TESTS); + assertThat(environment.currentType()).isSameInstanceAs(TESTS); } @Test @@ -131,7 +131,7 @@ void turnTestsOn() { void turnProductionOn() { environment.setTo(PRODUCTION); - assertThat(environment.envType()).isSameInstanceAs(PRODUCTION); + assertThat(environment.currentType()).isSameInstanceAs(PRODUCTION); } @Test @@ -153,7 +153,7 @@ void mutateKnownEnvTypesOnce() { // Now that `Environment` knows about `LOCAL`, it should use it as fallback. assertThat(Environment.instance() - .envType()).isSameInstanceAs(LOCAL); + .currentType()).isSameInstanceAs(LOCAL); } @Test @@ -169,7 +169,7 @@ void throwOnDoubleCreation() { void fallBack() { Environment.registerCustom(TRAVIS); assertThat(Environment.instance() - .envType()) + .currentType()) .isSameInstanceAs(TESTS); } } From f17fe9c79edf8fa53169502fc914c5aaf4d84789 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 27 May 2020 17:37:34 +0300 Subject: [PATCH 13/37] Fix formatting. --- base/src/main/java/io/spine/base/EnvironmentType.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java index b4d075f86a..c8ae440b6d 100644 --- a/base/src/main/java/io/spine/base/EnvironmentType.java +++ b/base/src/main/java/io/spine/base/EnvironmentType.java @@ -32,8 +32,8 @@ public interface EnvironmentType { * Returns {@code true} if the underlying system is currently in this environment type. * *

For example, if an application is deployed to a fleet of virtual machines, an environment - * variable may be set for every virtual machine. - * Application developer may use this type of knowledge to determine the current environment. + * variable may be set for every virtual machine. Application developer may use this type of + * knowledge to determine the current environment. */ boolean enabled(); } From 0333a6909020edbe4af8aa574aac12db1e545d91 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 27 May 2020 17:54:59 +0300 Subject: [PATCH 14/37] `license-report.md` and `pom.xml`. --- license-report.md | 60 +++++++++++++++++++++++------------------------ pom.xml | 4 ++-- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/license-report.md b/license-report.md index 23cf4733ef..6250287f85 100644 --- a/license-report.md +++ b/license-report.md @@ -1,6 +1,6 @@ -# Dependencies of `io.spine:spine-base:1.5.11` +# Dependencies of `io.spine:spine-base:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -328,12 +328,12 @@ The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:47 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:38:54 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-errorprone-checks:1.5.11` +# Dependencies of `io.spine.tools:spine-errorprone-checks:1.5.13` ## Runtime 1. **Group:** com.github.ben-manes.caffeine **Name:** caffeine **Version:** 2.7.0 @@ -773,12 +773,12 @@ This report was generated on **Tue May 26 13:03:47 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:47 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:04 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-javadoc-filter:1.5.11` +# Dependencies of `io.spine.tools:spine-javadoc-filter:1.5.13` ## Runtime 1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4 @@ -1156,12 +1156,12 @@ This report was generated on **Tue May 26 13:03:47 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:48 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:07 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-javadoc-prettifier:1.5.11` +# Dependencies of `io.spine.tools:spine-javadoc-prettifier:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -1521,12 +1521,12 @@ This report was generated on **Tue May 26 13:03:48 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:12 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-model-compiler:1.5.11` +# Dependencies of `io.spine.tools:spine-model-compiler:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -1902,12 +1902,12 @@ This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:29 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-mute-logging:1.5.11` +# Dependencies of `io.spine.tools:spine-mute-logging:1.5.13` ## Runtime 1. **Group:** com.google.auto.value **Name:** auto-value-annotations **Version:** 1.6.3 @@ -2281,12 +2281,12 @@ This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:31 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-plugin-base:1.5.11` +# Dependencies of `io.spine.tools:spine-plugin-base:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -2646,12 +2646,12 @@ This report was generated on **Tue May 26 13:03:49 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:34 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-plugin-testlib:1.5.11` +# Dependencies of `io.spine.tools:spine-plugin-testlib:1.5.13` ## Runtime 1. **Group:** com.google.auto.value **Name:** auto-value-annotations **Version:** 1.6.3 @@ -3065,12 +3065,12 @@ This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:37 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-proto-dart-plugin:1.5.11` +# Dependencies of `io.spine.tools:spine-proto-dart-plugin:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -3430,12 +3430,12 @@ This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:42 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-proto-js-plugin:1.5.11` +# Dependencies of `io.spine.tools:spine-proto-js-plugin:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -3795,12 +3795,12 @@ This report was generated on **Tue May 26 13:03:50 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:51 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-protoc-api:1.5.11` +# Dependencies of `io.spine.tools:spine-protoc-api:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -4120,12 +4120,12 @@ This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:55 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-protoc-plugin:1.5.11` +# Dependencies of `io.spine.tools:spine-protoc-plugin:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -4453,12 +4453,12 @@ This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:55 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine:spine-testlib:1.5.11` +# Dependencies of `io.spine:spine-testlib:1.5.13` ## Runtime 1. **Group:** com.google.auto.value **Name:** auto-value-annotations **Version:** 1.6.3 @@ -4832,12 +4832,12 @@ This report was generated on **Tue May 26 13:03:51 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:52 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:39:58 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-tool-base:1.5.11` +# Dependencies of `io.spine.tools:spine-tool-base:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -5165,12 +5165,12 @@ This report was generated on **Tue May 26 13:03:52 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:52 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Wed May 27 17:40:02 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:spine-validation-generator:1.5.11` +# Dependencies of `io.spine.tools:spine-validation-generator:1.5.13` ## Runtime 1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2 @@ -5498,4 +5498,4 @@ This report was generated on **Tue May 26 13:03:52 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue May 26 13:03:52 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file +This report was generated on **Wed May 27 17:40:04 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file diff --git a/pom.xml b/pom.xml index f780e4d77d..9774fdb358 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ all modules and does not describe the project structure per-subproject. io.spine spine-base -1.5.12 +1.5.13 2015 @@ -154,7 +154,7 @@ all modules and does not describe the project structure per-subproject. io.spine.tools spine-protoc-plugin - 1.5.12 + 1.5.13 test From 27de43d4ece23a120629366c5155f7aa0225859a Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Fri, 29 May 2020 12:36:54 +0300 Subject: [PATCH 15/37] Fix documentation and wording errors. --- .../io/spine/base/BaseEnvironmentType.java | 1 + .../main/java/io/spine/base/Environment.java | 23 ++++++++----------- .../java/io/spine/base/EnvironmentType.java | 7 +++--- .../java/io/spine/base/EnvironmentTest.java | 8 +++---- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/base/src/main/java/io/spine/base/BaseEnvironmentType.java b/base/src/main/java/io/spine/base/BaseEnvironmentType.java index 7a5468c2fc..f7f6d6aae1 100644 --- a/base/src/main/java/io/spine/base/BaseEnvironmentType.java +++ b/base/src/main/java/io/spine/base/BaseEnvironmentType.java @@ -72,6 +72,7 @@ public boolean enabled() { .anyMatch(stacktrace::contains); } }, + /** * A non-testing environment. * diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 7e059ab3b9..6b89a42e42 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -26,24 +26,21 @@ import io.spine.annotation.SPI; import org.checkerframework.checker.nullness.qual.Nullable; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; -import static io.spine.base.BaseEnvironmentType.PRODUCTION; -import static io.spine.base.BaseEnvironmentType.TESTS; /** * Provides information about the environment (current platform used, etc.). * - *

When extending, please note that this class does not handle the situations when two - * or more {@linkplain EnvironmentType environment types} return {@code true} on the - * {@link EnvironmentType#enabled()}. As such, if two or more user-defined environment types - * think that they are currently enabled, the behaviour of {@link #currentType()} is - * undefined. + *

When extending, please ensure the mutual exclusivity on your {@code EnvironmentType}s. + * If two or more environment types {@linkplain EnvironmentType#enabled() consider themselves + * enabled} at the same time, the behaviour of {@link #currentType()} is undefined. */ @SPI public final class Environment { private static final ImmutableList BASE_TYPES = - ImmutableList.of(TESTS, PRODUCTION); + ImmutableList.copyOf(BaseEnvironmentType.values()); private static final Environment INSTANCE = new Environment(); @@ -69,16 +66,16 @@ private Environment(Environment copy) { * *

Note that the {@linkplain BaseEnvironmentType default types} are still present. * When trying to {@linkplain #currentType() determine which environment type} is enabled, - * the user defined types are checked first. + * the user-defined types are checked first. * * @param environmentType * a user-defined environment type */ @Internal - public static void registerCustom(EnvironmentType environmentType) { + public static void register(EnvironmentType environmentType) { checkState(!INSTANCE.knownEnvTypes.contains(environmentType), "Attempted to register the same custom env type `%s` twice." + - "Please make sure to call `Environment.registerCustom(...) only once" + + "Please make sure to call `Environment.register(...) only once" + "per environment type.", environmentType.getClass() .getSimpleName()); INSTANCE.knownEnvTypes = ImmutableList @@ -105,7 +102,7 @@ public Environment createCopy() { /** * Determines the current environment type. * - *

If {@linkplain #registerCustom(EnvironmentType) custom env types have been defined}, + *

If {@linkplain #register(EnvironmentType) custom env types have been defined}, * goes through them in an undefined order. Then, checks the {@linkplain BaseEnvironmentType * base env types}. * @@ -143,7 +140,7 @@ public void restoreFrom(Environment copy) { */ @VisibleForTesting public void setTo(EnvironmentType type) { - this.currentEnvType = type; + this.currentEnvType = checkNotNull(type); } /** diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java index c8ae440b6d..5feeb8bcfa 100644 --- a/base/src/main/java/io/spine/base/EnvironmentType.java +++ b/base/src/main/java/io/spine/base/EnvironmentType.java @@ -21,10 +21,11 @@ package io.spine.base; /** - * A type of environment that knows whether it is enabled. + * A type of environment. * - *

Useful examples may include distinguishable {@code STAGING} or {@code LOCAL} environments. - * {@code base} library provides {@linkplain BaseEnvironmentType default environment types}. + *

Some examples may be {@code STAGING} or {@code LOCAL} environments. + * + * @see BaseEnvironmentType */ public interface EnvironmentType { diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 2804167e79..9e13ebb574 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -159,15 +159,15 @@ void mutateKnownEnvTypesOnce() { @Test @DisplayName("throw if a user attempts to create register the same environment twice") void throwOnDoubleCreation() { - Environment.registerCustom(LOCAL); + Environment.register(LOCAL); assertThrows(IllegalStateException.class, - () -> Environment.registerCustom(LOCAL)); + () -> Environment.register(LOCAL)); } @Test @DisplayName("fallback to the `TESTS` environment") void fallBack() { - Environment.registerCustom(TRAVIS); + Environment.register(TRAVIS); assertThat(Environment.instance() .currentType()) .isSameInstanceAs(TESTS); @@ -176,7 +176,7 @@ void fallBack() { private static void registerEnum(Class envTypeClass) { for (E envType : envTypeClass.getEnumConstants()) { - Environment.registerCustom(envType); + Environment.register(envType); } } From 5976b4df4e3c69611e63332d6948e549e12f1a18 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Fri, 29 May 2020 13:10:10 +0300 Subject: [PATCH 16/37] Clear the env variable, specify env type checking order. --- base/src/main/java/io/spine/base/Environment.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 6b89a42e42..7b50699c0c 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -103,8 +103,8 @@ public Environment createCopy() { * Determines the current environment type. * *

If {@linkplain #register(EnvironmentType) custom env types have been defined}, - * goes through them in an undefined order. Then, checks the {@linkplain BaseEnvironmentType - * base env types}. + * goes through them in the latest-registered to earliest-registered order. + * Then, checks the {@linkplain BaseEnvironmentType base env types}. * *

Note that if all of the {@link EnvironmentType#enabled()} checks have returned * {@code false}, this method falls back on {@link BaseEnvironmentType#PRODUCTION}. @@ -144,11 +144,16 @@ public void setTo(EnvironmentType type) { } /** - * Resets the instance. + * Resets the instance and clears the {@link BaseEnvironmentType#ENV_KEY_TESTS} variable. */ @VisibleForTesting public void reset() { this.currentEnvType = null; this.knownEnvTypes = BASE_TYPES; + clearTestingEnvVariable(); + } + + private static void clearTestingEnvVariable() { + System.clearProperty(BaseEnvironmentType.ENV_KEY_TESTS); } } From 8c1865654d3447412967a24aa4a31f36bbeaceca Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Sat, 30 May 2020 19:57:30 +0300 Subject: [PATCH 17/37] Reword a doc paragraph. --- base/src/main/java/io/spine/base/Environment.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 7b50699c0c..7eebcb082a 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -32,9 +32,10 @@ /** * Provides information about the environment (current platform used, etc.). * - *

When extending, please ensure the mutual exclusivity on your {@code EnvironmentType}s. - * If two or more environment types {@linkplain EnvironmentType#enabled() consider themselves - * enabled} at the same time, the behaviour of {@link #currentType()} is undefined. + *

When {@link #register(EnvironmentType) registering custom types}, please ensure + * their mutual exclusivity. If two or more environment types {@linkplain EnvironmentType#enabled() + * consider themselves enabled} at the same time, the behaviour of {@link #currentType()} is + * undefined. */ @SPI public final class Environment { From 7e07c10720341bbaeb1f51bd20ac36e00b27ba87 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Sat, 30 May 2020 19:59:13 +0300 Subject: [PATCH 18/37] Fix formatting and a test `@DisplayName`. --- base/src/main/java/io/spine/base/BaseEnvironmentType.java | 1 + base/src/test/java/io/spine/base/EnvironmentTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/base/src/main/java/io/spine/base/BaseEnvironmentType.java b/base/src/main/java/io/spine/base/BaseEnvironmentType.java index f7f6d6aae1..7a8278c315 100644 --- a/base/src/main/java/io/spine/base/BaseEnvironmentType.java +++ b/base/src/main/java/io/spine/base/BaseEnvironmentType.java @@ -40,6 +40,7 @@ public enum BaseEnvironmentType implements EnvironmentType { */ @SuppressWarnings("AccessOfSystemProperties" /* OK as we need system properties for this class. */) TESTS { + /** * Verifies if the code currently runs under a unit testing framework. * diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 9e13ebb574..dd76b8de6a 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -157,7 +157,7 @@ void mutateKnownEnvTypesOnce() { } @Test - @DisplayName("throw if a user attempts to create register the same environment twice") + @DisplayName("throw if a user attempts to register the same environment twice") void throwOnDoubleCreation() { Environment.register(LOCAL); assertThrows(IllegalStateException.class, From 340fd047881e008aa64313697c5cf92f254f67c4 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Sat, 30 May 2020 20:06:33 +0300 Subject: [PATCH 19/37] Fix formatting and documentation wording. --- base/src/main/java/io/spine/base/BaseEnvironmentType.java | 1 + base/src/main/java/io/spine/base/Environment.java | 6 +++--- base/src/test/java/io/spine/base/EnvironmentTest.java | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/base/src/main/java/io/spine/base/BaseEnvironmentType.java b/base/src/main/java/io/spine/base/BaseEnvironmentType.java index 7a8278c315..0da0e36830 100644 --- a/base/src/main/java/io/spine/base/BaseEnvironmentType.java +++ b/base/src/main/java/io/spine/base/BaseEnvironmentType.java @@ -81,6 +81,7 @@ public boolean enabled() { * environment. */ PRODUCTION { + @Override public boolean enabled() { return !TESTS.enabled(); diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 7eebcb082a..a10c7276a6 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -59,15 +59,15 @@ private Environment(Environment copy) { } /** - * Remembers the specified environment type, allowing {@linkplain #currentType()} + * Remembers the specified environment type, allowing {@linkplain #currentType() * to determine whether it's enabled} later. * *

If the specified environment type has already been registered, throws an * {@code IllegalStateException}. * *

Note that the {@linkplain BaseEnvironmentType default types} are still present. - * When trying to {@linkplain #currentType() determine which environment type} is enabled, - * the user-defined types are checked first. + * When trying to determine which environment type is enabled, the user-defined types are + * checked first, in the first-registered to last-registered order. * * @param environmentType * a user-defined environment type diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index dd76b8de6a..1ec58393d7 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -183,6 +183,7 @@ private static void registerEnum(Class env enum CustomEnvType implements EnvironmentType { LOCAL { + @Override public boolean enabled() { // `LOCAL` is the default custom env type. It should be used as a fallback. @@ -190,6 +191,7 @@ public boolean enabled() { } }, STAGING { + @Override public boolean enabled() { return String.valueOf(true) @@ -204,6 +206,7 @@ public boolean enabled() { enum BuildServerEnvironment implements EnvironmentType { TRAVIS { + @Override public boolean enabled() { return false; From 35f1262ee79275947c9ecea4d21999dcde0ac206 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Sat, 30 May 2020 20:07:52 +0300 Subject: [PATCH 20/37] Fix test methods naming. --- base/src/test/java/io/spine/base/EnvironmentTest.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 1ec58393d7..4a6c972acc 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -105,9 +105,7 @@ void environmentVar1() { @DisplayName("tell that we are under tests if run under known framework") void underTestFramework() { // As we run this from under JUnit... - EnvironmentType type = environment.currentType(); - BaseEnvironmentType expected = TESTS; - assertThat(type).isSameInstanceAs(expected); + assertThat(environment.currentType()).isSameInstanceAs(TESTS); } @Test @@ -148,7 +146,7 @@ class CustomEnvTypes { @Test @DisplayName("allow to provide user defined environment types") - void mutateKnownEnvTypesOnce() { + void provideCustomTypes() { registerEnum(CustomEnvType.class); // Now that `Environment` knows about `LOCAL`, it should use it as fallback. @@ -158,7 +156,7 @@ void mutateKnownEnvTypesOnce() { @Test @DisplayName("throw if a user attempts to register the same environment twice") - void throwOnDoubleCreation() { + void throwOnDoubleRegistration() { Environment.register(LOCAL); assertThrows(IllegalStateException.class, () -> Environment.register(LOCAL)); @@ -183,7 +181,6 @@ private static void registerEnum(Class env enum CustomEnvType implements EnvironmentType { LOCAL { - @Override public boolean enabled() { // `LOCAL` is the default custom env type. It should be used as a fallback. @@ -191,7 +188,6 @@ public boolean enabled() { } }, STAGING { - @Override public boolean enabled() { return String.valueOf(true) @@ -206,7 +202,6 @@ public boolean enabled() { enum BuildServerEnvironment implements EnvironmentType { TRAVIS { - @Override public boolean enabled() { return false; From a838155f55e48c36a79068811539948d7a0711d8 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Sat, 30 May 2020 20:11:14 +0300 Subject: [PATCH 21/37] Decrease test verbosity. --- base/src/test/java/io/spine/base/EnvironmentTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 4a6c972acc..746cadf95a 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -150,8 +150,7 @@ void provideCustomTypes() { registerEnum(CustomEnvType.class); // Now that `Environment` knows about `LOCAL`, it should use it as fallback. - assertThat(Environment.instance() - .currentType()).isSameInstanceAs(LOCAL); + assertThat(environment.currentType()).isSameInstanceAs(LOCAL); } @Test @@ -166,8 +165,7 @@ void throwOnDoubleRegistration() { @DisplayName("fallback to the `TESTS` environment") void fallBack() { Environment.register(TRAVIS); - assertThat(Environment.instance() - .currentType()) + assertThat(environment.currentType()) .isSameInstanceAs(TESTS); } } From aaddbd463d1ec23e350e25ee1ef2a102d0afe1d6 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Sun, 31 May 2020 17:23:30 +0300 Subject: [PATCH 22/37] Rephrase the `Environment` class-level doc. --- .../main/java/io/spine/base/Environment.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index a10c7276a6..5148dfad94 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -32,10 +32,19 @@ /** * Provides information about the environment (current platform used, etc.). * - *

When {@link #register(EnvironmentType) registering custom types}, please ensure - * their mutual exclusivity. If two or more environment types {@linkplain EnvironmentType#enabled() - * consider themselves enabled} at the same time, the behaviour of {@link #currentType()} is - * undefined. + *

Allows to determine the current environment type. {@linkplain BaseEnvironmentType + * 2 environment types} exist by default. + * + *

{@code Environment} is a singleton. + * + *

Custom environment types

+ * {@code Environment} allows to {@link #register(EnvironmentType) reguster custom types}. If done, + * {@code Environment} can then check whether the specified type is currently enabled. + *

When registering custom types, please ensure their mutual exclusivity. + * If two or more environment types {@linkplain EnvironmentType#enabled() consider themselves + * enabled} at the same time, the behaviour of {@link #currentType()} is undefined. + * + * @see EnvironmentType */ @SPI public final class Environment { From 8a042f66b8c912aabe469f93f2d2ab44412baf41 Mon Sep 17 00:00:00 2001 From: Alex Tymchenko Date: Mon, 1 Jun 2020 17:53:18 +0300 Subject: [PATCH 23/37] Document the new functionality of `Environment`. Shorten the `currentType()` to `type()` and un-mark the `register(type)` as `@Internal`. --- .../main/java/io/spine/base/Environment.java | 95 ++++++++++++++++--- .../io/spine/code/proto/FieldDeclaration.java | 2 + .../java/io/spine/base/EnvironmentTest.java | 26 ++--- 3 files changed, 96 insertions(+), 27 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 5148dfad94..e485daa82c 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -22,7 +22,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; -import io.spine.annotation.Internal; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import io.spine.annotation.SPI; import org.checkerframework.checker.nullness.qual.Nullable; @@ -32,17 +32,82 @@ /** * Provides information about the environment (current platform used, etc.). * - *

Allows to determine the current environment type. {@linkplain BaseEnvironmentType - * 2 environment types} exist by default. + *

Environment Type Detection

* - *

{@code Environment} is a singleton. + *

Current implementation allows to detect the type of the current environment. The framework + * brings two environment types out-of-the-box: + * + *

+ * + *

The framework users may define their custom settings depending on the {@linkplain}current + * environment type: + * + *

+ *
+ * public final class Application {
+ *
+ *     private final EmailSender sender;
+ *
+ *     private Application() {
+ *         EnvironmentType type = Environment.instance()
+ *                                           .type();
+ *         if(type == BaseEnvironmentType.TESTS) {
+ *             // Do not send out emails if in tests.
+ *             this.sender = new MockEmailSender();
+ *         } else {
+ *             this.sender = EmailSender.withConfig("email_gateway.yml");
+ *         }
+ *         //...
+ *     }
+ * }
+ * 
* *

Custom environment types

- * {@code Environment} allows to {@link #register(EnvironmentType) reguster custom types}. If done, - * {@code Environment} can then check whether the specified type is currently enabled. + * + * {@code Environment} allows to {@link #register(EnvironmentType) reguster custom types}. + * In this case the environment detection functionality iterates over all known types, starting + * with those registered by the framework user: + * + *
+ *
+ * public final class Application {
+ *
+ *     static {
+ *         Environment.instance()
+ *                    .register(StagingEnvironmentType.instance())
+ *                    .register(LoadTestingType.instance());
+ *     }
+ *
+ *     private final ConnectionPool pool;
+ *
+ *     private Application() {
+ *         EnvironmentType type = Environment.instance()
+ *                                           .type();
+ *         if (type == BaseEnvironmentType.TESTS) {
+ *              // Single connection is enough for tests.
+ *             this.pool = new ConnectionPoolImpl(PoolCapacity.of(1));
+ *         } else {
+ *             if(LoadTestingType.instance().enabled()) {
+ *                 this.pool =
+ *                         new ConnectionPoolImpl(PoolCapacity.fromConfig("load_tests.yml"));
+ *             } else {
+ *                 this.pool =
+ *                         new ConnectionPoolImpl(PoolCapacity.fromConfig("cloud_deployment.yml"));
+ *             }
+ *         }
+ *         //...
+ *     }
+ * }
+ * 
+ * *

When registering custom types, please ensure their mutual exclusivity. * If two or more environment types {@linkplain EnvironmentType#enabled() consider themselves - * enabled} at the same time, the behaviour of {@link #currentType()} is undefined. + * enabled} at the same time, the behaviour of {@link #type() type()} is undefined. * * @see EnvironmentType */ @@ -68,8 +133,8 @@ private Environment(Environment copy) { } /** - * Remembers the specified environment type, allowing {@linkplain #currentType() - * to determine whether it's enabled} later. + * Remembers the specified environment type, allowing {@linkplain #type() to determine + * whether it's enabled} later. * *

If the specified environment type has already been registered, throws an * {@code IllegalStateException}. @@ -80,19 +145,21 @@ private Environment(Environment copy) { * * @param environmentType * a user-defined environment type + * @return this instance of {@code Environment} */ - @Internal - public static void register(EnvironmentType environmentType) { - checkState(!INSTANCE.knownEnvTypes.contains(environmentType), + @CanIgnoreReturnValue + public Environment register(EnvironmentType environmentType) { + checkState(!knownEnvTypes.contains(environmentType), "Attempted to register the same custom env type `%s` twice." + "Please make sure to call `Environment.register(...) only once" + "per environment type.", environmentType.getClass() .getSimpleName()); - INSTANCE.knownEnvTypes = ImmutableList + knownEnvTypes = ImmutableList .builder() .add(environmentType) .addAll(INSTANCE.knownEnvTypes) .build(); + return this; } /** Returns the singleton instance. */ @@ -121,7 +188,7 @@ public Environment createCopy() { * * @return the current environment type. */ - public EnvironmentType currentType() { + public EnvironmentType type() { if (currentEnvType == null) { for (EnvironmentType type : knownEnvTypes) { if (type.enabled()) { diff --git a/base/src/main/java/io/spine/code/proto/FieldDeclaration.java b/base/src/main/java/io/spine/code/proto/FieldDeclaration.java index ebda370d8e..35a57ccfe6 100644 --- a/base/src/main/java/io/spine/code/proto/FieldDeclaration.java +++ b/base/src/main/java/io/spine/code/proto/FieldDeclaration.java @@ -74,6 +74,8 @@ public FieldDeclaration(FieldDescriptor field) { this.declaringMessage = new MessageType(field.getContainingType()); } + + /** * Creates a new instance which potentially can have leading comments. */ diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 746cadf95a..863998ea2b 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -90,7 +90,7 @@ void environmentVarTrue() { Environment.instance() .setTo(TESTS); - assertThat(environment.currentType()).isSameInstanceAs(TESTS); + assertThat(environment.type()).isSameInstanceAs(TESTS); } @Test @@ -98,14 +98,14 @@ void environmentVarTrue() { void environmentVar1() { System.setProperty(ENV_KEY_TESTS, "1"); - assertThat(environment.currentType()).isSameInstanceAs(TESTS); + assertThat(environment.type()).isSameInstanceAs(TESTS); } @Test @DisplayName("tell that we are under tests if run under known framework") void underTestFramework() { // As we run this from under JUnit... - assertThat(environment.currentType()).isSameInstanceAs(TESTS); + assertThat(environment.type()).isSameInstanceAs(TESTS); } @Test @@ -113,7 +113,7 @@ void underTestFramework() { void environmentVarUnknownValue() { System.setProperty(ENV_KEY_TESTS, "neitherTrueNor1"); - assertThat(environment.currentType()).isSameInstanceAs(PRODUCTION); + assertThat(environment.type()).isSameInstanceAs(PRODUCTION); } @Test @@ -121,7 +121,7 @@ void environmentVarUnknownValue() { void turnTestsOn() { environment.setTo(TESTS); - assertThat(environment.currentType()).isSameInstanceAs(TESTS); + assertThat(environment.type()).isSameInstanceAs(TESTS); } @Test @@ -129,7 +129,7 @@ void turnTestsOn() { void turnProductionOn() { environment.setTo(PRODUCTION); - assertThat(environment.currentType()).isSameInstanceAs(PRODUCTION); + assertThat(environment.type()).isSameInstanceAs(PRODUCTION); } @Test @@ -150,29 +150,29 @@ void provideCustomTypes() { registerEnum(CustomEnvType.class); // Now that `Environment` knows about `LOCAL`, it should use it as fallback. - assertThat(environment.currentType()).isSameInstanceAs(LOCAL); + assertThat(environment.type()).isSameInstanceAs(LOCAL); } @Test @DisplayName("throw if a user attempts to register the same environment twice") void throwOnDoubleRegistration() { - Environment.register(LOCAL); + Environment.instance().register(LOCAL); assertThrows(IllegalStateException.class, - () -> Environment.register(LOCAL)); + () -> Environment.instance().register(LOCAL)); } @Test @DisplayName("fallback to the `TESTS` environment") void fallBack() { - Environment.register(TRAVIS); - assertThat(environment.currentType()) + Environment.instance().register(TRAVIS); + assertThat(environment.type()) .isSameInstanceAs(TESTS); } } - private static void registerEnum(Class envTypeClass) { + private static & EnvironmentType> void registerEnum(Class envTypeClass) { for (E envType : envTypeClass.getEnumConstants()) { - Environment.register(envType); + Environment.instance().register(envType); } } From d97c603d5e855d0d211382f78916ad085ba04743 Mon Sep 17 00:00:00 2001 From: Alex Tymchenko Date: Mon, 1 Jun 2020 17:58:24 +0300 Subject: [PATCH 24/37] Remove the redundant empty lines. --- base/src/main/java/io/spine/code/proto/FieldDeclaration.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/base/src/main/java/io/spine/code/proto/FieldDeclaration.java b/base/src/main/java/io/spine/code/proto/FieldDeclaration.java index 35a57ccfe6..ebda370d8e 100644 --- a/base/src/main/java/io/spine/code/proto/FieldDeclaration.java +++ b/base/src/main/java/io/spine/code/proto/FieldDeclaration.java @@ -74,8 +74,6 @@ public FieldDeclaration(FieldDescriptor field) { this.declaringMessage = new MessageType(field.getContainingType()); } - - /** * Creates a new instance which potentially can have leading comments. */ From dc6705e8fab294e9a9a018ca249d69bea27bf37e Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 3 Jun 2020 16:09:13 +0300 Subject: [PATCH 25/37] Tweak the API of `Environment`: `is(EnvironmentType)` instead of `currentType()` to make it simpler for the callers. Make `EnvironmentType` into an `abstract class` to ensure a more strict access level of the `active` method. --- .../io/spine/base/BaseEnvironmentType.java | 105 ------------- .../main/java/io/spine/base/Environment.java | 67 ++++----- .../java/io/spine/base/EnvironmentType.java | 33 +++- .../main/java/io/spine/base/Production.java | 56 +++++++ base/src/main/java/io/spine/base/Tests.java | 115 ++++++++++++++ .../java/io/spine/base/EnvironmentTest.java | 142 ++++++++++++------ 6 files changed, 332 insertions(+), 186 deletions(-) delete mode 100644 base/src/main/java/io/spine/base/BaseEnvironmentType.java create mode 100644 base/src/main/java/io/spine/base/Production.java create mode 100644 base/src/main/java/io/spine/base/Tests.java diff --git a/base/src/main/java/io/spine/base/BaseEnvironmentType.java b/base/src/main/java/io/spine/base/BaseEnvironmentType.java deleted file mode 100644 index 0da0e36830..0000000000 --- a/base/src/main/java/io/spine/base/BaseEnvironmentType.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2020, TeamDev. All rights reserved. - * - * Redistribution and use in source and/or binary forms, with or without - * modification, must retain the above copyright notice and the following - * disclaimer. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package io.spine.base; - -import com.google.common.base.Throwables; -import com.google.common.collect.ImmutableList; - -import java.util.regex.Pattern; - -/** - * Environment types provided by the {@code base} library. - */ -public enum BaseEnvironmentType implements EnvironmentType { - - /** - * Testing environment. - * - *

Detected by checking stack trace for mentions of the known testing frameworks. - * - *

This option is mutually exclusive with {@link #PRODUCTION}, i.e. one of them is always - * enabled. - */ - @SuppressWarnings("AccessOfSystemProperties" /* OK as we need system properties for this class. */) - TESTS { - - /** - * Verifies if the code currently runs under a unit testing framework. - * - *

The method returns {@code true} if the following packages are discovered - * in the stacktrace: - *

- * - * @return {@code true} if the code runs under a testing framework, {@code false} otherwise - * @implNote In addition to checking the stack trace, this method checks the environment - * variable value. If you wish to simulate not being in tests, the variable must be set - * to {@code false} explicitly. If your framework is not among the - * {@linkplain #KNOWN_TESTING_FRAMEWORKS known ones}, make sure to set the system property - * explicitly. - */ - @Override - public boolean enabled() { - String testProp = System.getProperty(ENV_KEY_TESTS); - if (testProp != null) { - testProp = TEST_PROP_PATTERN.matcher(testProp) - .replaceAll(""); - return String.valueOf(true) - .equalsIgnoreCase(testProp) || "1".equals(testProp); - } - - String stacktrace = Throwables.getStackTraceAsString(new RuntimeException("")); - return KNOWN_TESTING_FRAMEWORKS.stream() - .anyMatch(stacktrace::contains); - } - }, - - /** - * A non-testing environment. - * - *

If the system is not in the {@link #TESTS} environment, it is in the production - * environment. - */ - PRODUCTION { - - @Override - public boolean enabled() { - return !TESTS.enabled(); - } - }; - - /** - * The key name of the system property which tells if a code runs under a testing framework. - * - *

If your testing framework is not among the - * {@link BaseEnvironmentType#KNOWN_TESTING_FRAMEWORKS}, set this property to {@code true} - * before running tests. - */ - public static final String ENV_KEY_TESTS = "io.spine.tests"; - - @SuppressWarnings("DuplicateStringLiteralInspection" /* Used in another context. */) - public static final ImmutableList KNOWN_TESTING_FRAMEWORKS = - ImmutableList.of("org.junit", "org.testng"); - - private static final Pattern TEST_PROP_PATTERN = Pattern.compile("\"' "); -} diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index e485daa82c..483e1d288a 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -38,14 +38,14 @@ * brings two environment types out-of-the-box: * *

* - *

The framework users may define their custom settings depending on the {@linkplain}current - * environment type: + *

The framework users may define their custom settings depending on the current environment + * type: * *

  *
@@ -54,9 +54,8 @@
  *     private final EmailSender sender;
  *
  *     private Application() {
- *         EnvironmentType type = Environment.instance()
- *                                           .type();
- *         if(type == BaseEnvironmentType.TESTS) {
+ *         Environment environment = Environment.instance();
+ *         if(environment.is(Tests.instance())) {
  *             // Do not send out emails if in tests.
  *             this.sender = new MockEmailSender();
  *         } else {
@@ -86,13 +85,12 @@
  *     private final ConnectionPool pool;
  *
  *     private Application() {
- *         EnvironmentType type = Environment.instance()
- *                                           .type();
- *         if (type == BaseEnvironmentType.TESTS) {
+ *         Environment environment = Environment.instance();
+ *         if (environment.is(Tests.instance()) {
  *              // Single connection is enough for tests.
  *             this.pool = new ConnectionPoolImpl(PoolCapacity.of(1));
  *         } else {
- *             if(LoadTestingType.instance().enabled()) {
+ *             if(environment.is(LoadTesting.instance()) {
  *                 this.pool =
  *                         new ConnectionPoolImpl(PoolCapacity.fromConfig("load_tests.yml"));
  *             } else {
@@ -107,7 +105,7 @@
  *
  * 

When registering custom types, please ensure their mutual exclusivity. * If two or more environment types {@linkplain EnvironmentType#enabled() consider themselves - * enabled} at the same time, the behaviour of {@link #type() type()} is undefined. + * enabled} at the same time, the behaviour of {@link #is(EnvironmentType)}} is undefined. * * @see EnvironmentType */ @@ -115,7 +113,7 @@ public final class Environment { private static final ImmutableList BASE_TYPES = - ImmutableList.copyOf(BaseEnvironmentType.values()); + ImmutableList.of(Tests.instance(), Production.instance()); private static final Environment INSTANCE = new Environment(); @@ -133,19 +131,21 @@ private Environment(Environment copy) { } /** - * Remembers the specified environment type, allowing {@linkplain #type() to determine - * whether it's enabled} later. + * Remembers the specified environment type, allowing {@linkplain #is(EnvironmentType) to + * determine whether it's enabled} later. * *

If the specified environment type has already been registered, throws an * {@code IllegalStateException}. * - *

Note that the {@linkplain BaseEnvironmentType default types} are still present. + *

Note that the default types are still present. * When trying to determine which environment type is enabled, the user-defined types are * checked first, in the first-registered to last-registered order. * * @param environmentType * a user-defined environment type * @return this instance of {@code Environment} + * @see Tests + * @see Production */ @CanIgnoreReturnValue public Environment register(EnvironmentType environmentType) { @@ -177,27 +177,28 @@ public Environment createCopy() { } /** - * Determines the current environment type. + * Determines whether the current environment is the same as the specified one. * *

If {@linkplain #register(EnvironmentType) custom env types have been defined}, * goes through them in the latest-registered to earliest-registered order. - * Then, checks the {@linkplain BaseEnvironmentType base env types}. - * - *

Note that if all of the {@link EnvironmentType#enabled()} checks have returned - * {@code false}, this method falls back on {@link BaseEnvironmentType#PRODUCTION}. + * Then, checks {@link Tests} and {@link Production}. * * @return the current environment type. */ - public EnvironmentType type() { + public boolean is(EnvironmentType type) { if (currentEnvType == null) { - for (EnvironmentType type : knownEnvTypes) { - if (type.enabled()) { - this.currentEnvType = type; - return this.currentEnvType; - } + determineCurrentType(); + } + return currentEnvType.equals(type); + } + + private void determineCurrentType() { + for (EnvironmentType type : knownEnvTypes) { + if (type.enabled()) { + this.currentEnvType = type; + return; } } - return currentEnvType; } /** @@ -221,16 +222,12 @@ public void setTo(EnvironmentType type) { } /** - * Resets the instance and clears the {@link BaseEnvironmentType#ENV_KEY_TESTS} variable. + * Resets the instance and clears the {@link Tests#ENV_KEY_TESTS} variable. */ @VisibleForTesting public void reset() { this.currentEnvType = null; this.knownEnvTypes = BASE_TYPES; - clearTestingEnvVariable(); - } - - private static void clearTestingEnvVariable() { - System.clearProperty(BaseEnvironmentType.ENV_KEY_TESTS); + Tests.clearTestingEnvVariable(); } } diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java index 5feeb8bcfa..828301ca0a 100644 --- a/base/src/main/java/io/spine/base/EnvironmentType.java +++ b/base/src/main/java/io/spine/base/EnvironmentType.java @@ -20,14 +20,18 @@ package io.spine.base; +import com.google.common.base.Objects; + /** * A type of environment. * *

Some examples may be {@code STAGING} or {@code LOCAL} environments. * - * @see BaseEnvironmentType + * @implNote developers are encouraged to make their environment types singletons, such + * that their API is consistent with the env types provided by the {@code base} library: + * {@link Production}, {@link Tests}. */ -public interface EnvironmentType { +public abstract class EnvironmentType { /** * Returns {@code true} if the underlying system is currently in this environment type. @@ -36,5 +40,28 @@ public interface EnvironmentType { * variable may be set for every virtual machine. Application developer may use this type of * knowledge to determine the current environment. */ - boolean enabled(); + abstract boolean enabled(); + + /** + * @inheritDoc + * + *

By default, environments types are compared based on their classes. + */ + @Override + public boolean equals(Object obj) { + return this.getClass() + .equals(obj.getClass()); + } + + /** + * @inheritDoc + * + *

By default, adheres to the {@code equals} and {@code hashCode} contract, assuming that + * the implementation of the {@code equals} is the {@linkplain EnvironmentType#equals(Object) + * default one}. + */ + @Override + public int hashCode() { + return Objects.hashCode(getClass()); + } } diff --git a/base/src/main/java/io/spine/base/Production.java b/base/src/main/java/io/spine/base/Production.java new file mode 100644 index 0000000000..8c0ee9d04f --- /dev/null +++ b/base/src/main/java/io/spine/base/Production.java @@ -0,0 +1,56 @@ +/* + * Copyright 2020, TeamDev. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package io.spine.base; + +import com.google.errorprone.annotations.Immutable; + +/** + * A non-testing environment. + * + *

If the system is not in the {@link Tests} environment, it is in the production environment. + */ +@Immutable +public final class Production extends EnvironmentType { + + @Override + boolean enabled() { + return !Tests.instance() + .enabled(); + } + + public static Production instance() { + return Singleton.INSTANCE.production; + } + + private enum Singleton { + + INSTANCE; + + @SuppressWarnings({ + "NonSerializableFieldInSerializableClass", + "PMD.SingularField" /* this field cannot be local */}) + private final Production production; + + Singleton() { + this.production = new Production(); + } + } +} diff --git a/base/src/main/java/io/spine/base/Tests.java b/base/src/main/java/io/spine/base/Tests.java new file mode 100644 index 0000000000..be122e1675 --- /dev/null +++ b/base/src/main/java/io/spine/base/Tests.java @@ -0,0 +1,115 @@ +/* + * Copyright 2020, TeamDev. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package io.spine.base; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; +import com.google.errorprone.annotations.Immutable; + +import java.util.regex.Pattern; + +/** + * Testing environment. + * + *

Detected by checking stack trace for mentions of the known testing frameworks. + * + *

This option is mutually exclusive with {@link Production}, i.e. one of them is always enabled. + */ +@Immutable +@SuppressWarnings("AccessOfSystemProperties" /* is necessary for this class to function */) +public final class Tests extends EnvironmentType { + + /** + * The key name of the system property which tells if a code runs under a testing framework. + * + *

If your testing framework is not among the {@link #KNOWN_TESTING_FRAMEWORKS}, set this + * property to {@code true} before running tests. + */ + @VisibleForTesting + static final String ENV_KEY_TESTS = "io.spine.tests"; + + @SuppressWarnings("DuplicateStringLiteralInspection" /* Used in another context. */) + private static final ImmutableList KNOWN_TESTING_FRAMEWORKS = + ImmutableList.of("org.junit", "org.testng"); + + private static final Pattern TEST_PROP_PATTERN = Pattern.compile("\"' "); + + /** + * Verifies if the code currently runs under a unit testing framework. + * + *

The method returns {@code true} if the following packages are discovered + * in the stacktrace: + *

    + *
  • {@code org.junit} + *
  • {@code org.testng} + *
+ * + * @return {@code true} if the code runs under a testing framework, {@code false} otherwise + * @implNote In addition to checking the stack trace, this method checks the + * environment variable value. If you wish to simulate not being in tests, the + * variable must be set to {@code false} explicitly. If your framework is not + * among the {@linkplain #KNOWN_TESTING_FRAMEWORKS known ones}, make sure to set + * the system property explicitly. + */ + @Override + public boolean enabled() { + String testProp = System.getProperty(ENV_KEY_TESTS); + if (testProp != null) { + testProp = TEST_PROP_PATTERN.matcher(testProp) + .replaceAll(""); + return String.valueOf(true) + .equalsIgnoreCase(testProp) || "1".equals(testProp); + } + + String stacktrace = Throwables.getStackTraceAsString(new RuntimeException("")); + return KNOWN_TESTING_FRAMEWORKS.stream() + .anyMatch(stacktrace::contains); + } + + /** + * Clears the {@linkplain #ENV_KEY_TESTS environment variable used for test detection}. + */ + static void clearTestingEnvVariable() { + System.clearProperty(ENV_KEY_TESTS); + } + + /** + * Returns the singleton instance of this class. + */ + public static Tests instance() { + return Singleton.INSTANCE.tests; + } + + private enum Singleton { + + INSTANCE; + + @SuppressWarnings({ + "NonSerializableFieldInSerializableClass", + "PMD.SingularField" /* this field cannot be local */}) + private final Tests tests; + + Singleton() { + this.tests = new Tests(); + } + } +} diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 863998ea2b..2432140dc0 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -30,11 +30,7 @@ import org.junit.jupiter.api.Test; import static com.google.common.truth.Truth.assertThat; -import static io.spine.base.BaseEnvironmentType.ENV_KEY_TESTS; -import static io.spine.base.BaseEnvironmentType.PRODUCTION; -import static io.spine.base.BaseEnvironmentType.TESTS; -import static io.spine.base.EnvironmentTest.BuildServerEnvironment.TRAVIS; -import static io.spine.base.EnvironmentTest.CustomEnvType.LOCAL; +import static io.spine.base.Tests.ENV_KEY_TESTS; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -87,10 +83,11 @@ void cleanUp() { @Test @DisplayName("tell that we are under tests if env. variable set to true") void environmentVarTrue() { + Tests tests = Tests.instance(); Environment.instance() - .setTo(TESTS); + .setTo(tests); - assertThat(environment.type()).isSameInstanceAs(TESTS); + assertThat(environment.is(Tests.instance())).isTrue(); } @Test @@ -98,14 +95,14 @@ void environmentVarTrue() { void environmentVar1() { System.setProperty(ENV_KEY_TESTS, "1"); - assertThat(environment.type()).isSameInstanceAs(TESTS); + assertThat(environment.is(Tests.instance())).isTrue(); } @Test @DisplayName("tell that we are under tests if run under known framework") void underTestFramework() { // As we run this from under JUnit... - assertThat(environment.type()).isSameInstanceAs(TESTS); + assertThat(environment.is(Tests.instance())).isTrue(); } @Test @@ -113,23 +110,23 @@ void underTestFramework() { void environmentVarUnknownValue() { System.setProperty(ENV_KEY_TESTS, "neitherTrueNor1"); - assertThat(environment.type()).isSameInstanceAs(PRODUCTION); + assertThat(environment.is(Production.instance())).isTrue(); } @Test @DisplayName("turn tests mode on") void turnTestsOn() { - environment.setTo(TESTS); + environment.setTo(Tests.instance()); - assertThat(environment.type()).isSameInstanceAs(TESTS); + assertThat(environment.is(Tests.instance())).isTrue(); } @Test @DisplayName("turn production mode on") void turnProductionOn() { - environment.setTo(PRODUCTION); + environment.setTo(Production.instance()); - assertThat(environment.type()).isSameInstanceAs(PRODUCTION); + assertThat(environment.is(Production.instance())).isTrue(); } @Test @@ -147,62 +144,121 @@ class CustomEnvTypes { @Test @DisplayName("allow to provide user defined environment types") void provideCustomTypes() { - registerEnum(CustomEnvType.class); + register(Staging.instance(), Local.instance()); // Now that `Environment` knows about `LOCAL`, it should use it as fallback. - assertThat(environment.type()).isSameInstanceAs(LOCAL); + assertThat(environment.is(Local.instance())).isTrue(); } @Test @DisplayName("throw if a user attempts to register the same environment twice") void throwOnDoubleRegistration() { - Environment.instance().register(LOCAL); + Environment.instance() + .register(Local.instance()); assertThrows(IllegalStateException.class, - () -> Environment.instance().register(LOCAL)); + () -> Environment.instance() + .register(Local.instance())); } @Test @DisplayName("fallback to the `TESTS` environment") void fallBack() { - Environment.instance().register(TRAVIS); - assertThat(environment.type()) - .isSameInstanceAs(TESTS); + Environment.instance() + .register(Travis.instance()); + assertThat(environment.is(Travis.instance())).isFalse(); + assertThat(environment.is(Tests.instance())).isTrue(); } } - private static & EnvironmentType> void registerEnum(Class envTypeClass) { - for (E envType : envTypeClass.getEnumConstants()) { - Environment.instance().register(envType); + private static void register(EnvironmentType... types) { + for (EnvironmentType type : types) { + Environment.instance() + .register(type); } } - enum CustomEnvType implements EnvironmentType { + static final class Local extends EnvironmentType { - LOCAL { - @Override - public boolean enabled() { - // `LOCAL` is the default custom env type. It should be used as a fallback. - return true; - } - }, - STAGING { - @Override - public boolean enabled() { - return String.valueOf(true) - .equalsIgnoreCase(System.getProperty(STAGING_ENV_TYPE_KEY)); + private Local() { + } + + @Override + public boolean enabled() { + // `LOCAL` is the default custom env type. It should be used as a fallback. + return true; + } + + public static Local instance() { + return Singleton.INSTANCE.local; + } + + private enum Singleton { + + INSTANCE; + + @SuppressWarnings("NonSerializableFieldInSerializableClass") + private final Local local; + + Singleton() { + this.local = new Local(); } - }; + } + } + + static final class Staging extends EnvironmentType { static final String STAGING_ENV_TYPE_KEY = "io.spine.base.EnvironmentTest.is_staging"; + + private Staging() { + } + + public static Staging instance() { + return Singleton.INSTANCE.staging; + } + + @Override + public boolean enabled() { + return String.valueOf(true) + .equalsIgnoreCase(System.getProperty(STAGING_ENV_TYPE_KEY)); + } + + private enum Singleton { + + INSTANCE; + + @SuppressWarnings("NonSerializableFieldInSerializableClass") + private final Staging staging; + + Singleton() { + this.staging = new Staging(); + } + } } @SuppressWarnings("unused" /* The only variant is used. */) - enum BuildServerEnvironment implements EnvironmentType { + static final class Travis extends EnvironmentType { + + private Travis() { + } + + @Override + public boolean enabled() { + return false; + } + + public static Travis instance() { + return Singleton.INSTANCE.travis; + } + + private enum Singleton { + + INSTANCE; + + @SuppressWarnings("NonSerializableFieldInSerializableClass") + private final Travis travis; - TRAVIS { - @Override - public boolean enabled() { - return false; + Singleton() { + this.travis = new Travis(); } } } From 2ac7fcbb4dba651ea3ad3cb9d8cfb5e1af748cce Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 3 Jun 2020 16:28:47 +0300 Subject: [PATCH 26/37] Fix documentation errors. --- base/src/main/java/io/spine/base/Environment.java | 3 +-- base/src/main/java/io/spine/base/Production.java | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 483e1d288a..61fc4ce112 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -34,8 +34,7 @@ * *

Environment Type Detection

* - *

Current implementation allows to detect the type of the current environment. The framework - * brings two environment types out-of-the-box: + *

Current implementation allows to {@linkplain #is(EnvironmentType) check} whether a * *

    *
  • {@link Tests} is detected if the current call stack has a reference to the unit diff --git a/base/src/main/java/io/spine/base/Production.java b/base/src/main/java/io/spine/base/Production.java index 8c0ee9d04f..b1fd050e89 100644 --- a/base/src/main/java/io/spine/base/Production.java +++ b/base/src/main/java/io/spine/base/Production.java @@ -36,6 +36,9 @@ boolean enabled() { .enabled(); } + /** + * Returns the singleton instance. + */ public static Production instance() { return Singleton.INSTANCE.production; } From bf9bc3faaaffc5c5f03de45154c2146b1e66d4f6 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 3 Jun 2020 17:29:32 +0300 Subject: [PATCH 27/37] `instance()` -> `type()` for default `EnvironmentType`s. Also license report. --- .../main/java/io/spine/base/Environment.java | 12 +++--- .../main/java/io/spine/base/Production.java | 4 +- base/src/main/java/io/spine/base/Tests.java | 2 +- .../java/io/spine/base/EnvironmentTest.java | 38 +++++++++---------- license-report.md | 30 +++++++-------- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 61fc4ce112..966cca5f50 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -54,7 +54,7 @@ * * private Application() { * Environment environment = Environment.instance(); - * if(environment.is(Tests.instance())) { + * if(environment.is(Tests.type())) { * // Do not send out emails if in tests. * this.sender = new MockEmailSender(); * } else { @@ -77,19 +77,19 @@ * * static { * Environment.instance() - * .register(StagingEnvironmentType.instance()) - * .register(LoadTestingType.instance()); + * .register(StagingEnvironmentType.type()) + * .register(LoadTestingType.type()); * } * * private final ConnectionPool pool; * * private Application() { * Environment environment = Environment.instance(); - * if (environment.is(Tests.instance()) { + * if (environment.is(Tests.type()) { * // Single connection is enough for tests. * this.pool = new ConnectionPoolImpl(PoolCapacity.of(1)); * } else { - * if(environment.is(LoadTesting.instance()) { + * if(environment.is(LoadTesting.type()) { * this.pool = * new ConnectionPoolImpl(PoolCapacity.fromConfig("load_tests.yml")); * } else { @@ -112,7 +112,7 @@ public final class Environment { private static final ImmutableList BASE_TYPES = - ImmutableList.of(Tests.instance(), Production.instance()); + ImmutableList.of(Tests.type(), Production.type()); private static final Environment INSTANCE = new Environment(); diff --git a/base/src/main/java/io/spine/base/Production.java b/base/src/main/java/io/spine/base/Production.java index b1fd050e89..d8f1ff6f90 100644 --- a/base/src/main/java/io/spine/base/Production.java +++ b/base/src/main/java/io/spine/base/Production.java @@ -32,14 +32,14 @@ public final class Production extends EnvironmentType { @Override boolean enabled() { - return !Tests.instance() + return !Tests.type() .enabled(); } /** * Returns the singleton instance. */ - public static Production instance() { + public static Production type() { return Singleton.INSTANCE.production; } diff --git a/base/src/main/java/io/spine/base/Tests.java b/base/src/main/java/io/spine/base/Tests.java index be122e1675..822b9d2b9b 100644 --- a/base/src/main/java/io/spine/base/Tests.java +++ b/base/src/main/java/io/spine/base/Tests.java @@ -95,7 +95,7 @@ static void clearTestingEnvVariable() { /** * Returns the singleton instance of this class. */ - public static Tests instance() { + public static Tests type() { return Singleton.INSTANCE.tests; } diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 2432140dc0..c78e9031d7 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -83,11 +83,11 @@ void cleanUp() { @Test @DisplayName("tell that we are under tests if env. variable set to true") void environmentVarTrue() { - Tests tests = Tests.instance(); + Tests tests = Tests.type(); Environment.instance() .setTo(tests); - assertThat(environment.is(Tests.instance())).isTrue(); + assertThat(environment.is(Tests.type())).isTrue(); } @Test @@ -95,14 +95,14 @@ void environmentVarTrue() { void environmentVar1() { System.setProperty(ENV_KEY_TESTS, "1"); - assertThat(environment.is(Tests.instance())).isTrue(); + assertThat(environment.is(Tests.type())).isTrue(); } @Test @DisplayName("tell that we are under tests if run under known framework") void underTestFramework() { // As we run this from under JUnit... - assertThat(environment.is(Tests.instance())).isTrue(); + assertThat(environment.is(Tests.type())).isTrue(); } @Test @@ -110,23 +110,23 @@ void underTestFramework() { void environmentVarUnknownValue() { System.setProperty(ENV_KEY_TESTS, "neitherTrueNor1"); - assertThat(environment.is(Production.instance())).isTrue(); + assertThat(environment.is(Production.type())).isTrue(); } @Test @DisplayName("turn tests mode on") void turnTestsOn() { - environment.setTo(Tests.instance()); + environment.setTo(Tests.type()); - assertThat(environment.is(Tests.instance())).isTrue(); + assertThat(environment.is(Tests.type())).isTrue(); } @Test @DisplayName("turn production mode on") void turnProductionOn() { - environment.setTo(Production.instance()); + environment.setTo(Production.type()); - assertThat(environment.is(Production.instance())).isTrue(); + assertThat(environment.is(Production.type())).isTrue(); } @Test @@ -144,29 +144,29 @@ class CustomEnvTypes { @Test @DisplayName("allow to provide user defined environment types") void provideCustomTypes() { - register(Staging.instance(), Local.instance()); + register(Staging.type(), Local.type()); // Now that `Environment` knows about `LOCAL`, it should use it as fallback. - assertThat(environment.is(Local.instance())).isTrue(); + assertThat(environment.is(Local.type())).isTrue(); } @Test @DisplayName("throw if a user attempts to register the same environment twice") void throwOnDoubleRegistration() { Environment.instance() - .register(Local.instance()); + .register(Local.type()); assertThrows(IllegalStateException.class, () -> Environment.instance() - .register(Local.instance())); + .register(Local.type())); } @Test @DisplayName("fallback to the `TESTS` environment") void fallBack() { Environment.instance() - .register(Travis.instance()); - assertThat(environment.is(Travis.instance())).isFalse(); - assertThat(environment.is(Tests.instance())).isTrue(); + .register(Travis.type()); + assertThat(environment.is(Travis.type())).isFalse(); + assertThat(environment.is(Tests.type())).isTrue(); } } @@ -188,7 +188,7 @@ public boolean enabled() { return true; } - public static Local instance() { + public static Local type() { return Singleton.INSTANCE.local; } @@ -212,7 +212,7 @@ static final class Staging extends EnvironmentType { private Staging() { } - public static Staging instance() { + public static Staging type() { return Singleton.INSTANCE.staging; } @@ -246,7 +246,7 @@ public boolean enabled() { return false; } - public static Travis instance() { + public static Travis type() { return Singleton.INSTANCE.travis; } diff --git a/license-report.md b/license-report.md index 6250287f85..4bf6107acc 100644 --- a/license-report.md +++ b/license-report.md @@ -328,7 +328,7 @@ The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:38:54 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:28 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -773,7 +773,7 @@ This report was generated on **Wed May 27 17:38:54 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:04 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:28 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1156,7 +1156,7 @@ This report was generated on **Wed May 27 17:39:04 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:07 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:29 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1521,7 +1521,7 @@ This report was generated on **Wed May 27 17:39:07 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:12 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:29 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1902,7 +1902,7 @@ This report was generated on **Wed May 27 17:39:12 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:29 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:30 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -2281,7 +2281,7 @@ This report was generated on **Wed May 27 17:39:29 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:31 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:30 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -2646,7 +2646,7 @@ This report was generated on **Wed May 27 17:39:31 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:34 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:30 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3065,7 +3065,7 @@ This report was generated on **Wed May 27 17:39:34 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:37 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:31 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3430,7 +3430,7 @@ This report was generated on **Wed May 27 17:39:37 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:42 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:31 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -3795,7 +3795,7 @@ This report was generated on **Wed May 27 17:39:42 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:51 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:31 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4120,7 +4120,7 @@ This report was generated on **Wed May 27 17:39:51 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:55 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:31 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4453,7 +4453,7 @@ This report was generated on **Wed May 27 17:39:55 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:55 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:32 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -4832,7 +4832,7 @@ This report was generated on **Wed May 27 17:39:55 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:39:58 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:32 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -5165,7 +5165,7 @@ This report was generated on **Wed May 27 17:39:58 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:40:02 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). +This report was generated on **Thu May 28 12:20:32 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -5498,4 +5498,4 @@ This report was generated on **Wed May 27 17:40:02 EEST 2020** using [Gradle-Lic The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Wed May 27 17:40:04 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file +This report was generated on **Thu May 28 12:20:33 EEST 2020** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file From 16f571aceec3effa90ca16e81353cbee9d29feb2 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 3 Jun 2020 17:32:57 +0300 Subject: [PATCH 28/37] Deprecate the old API. --- .../main/java/io/spine/base/Environment.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 966cca5f50..e2defaa1f5 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -200,6 +200,29 @@ private void determineCurrentType() { } } + /** + * Verifies if the code currently runs under a unit testing framework. + * + * @deprecated use {@code Environment.instance().is(Tests.type)} + */ + @Deprecated + public boolean isTests() { + return is(Tests.type()); + } + + /** + * Verifies if the code runs in the production mode. + * + *

    This method is opposite to {@link #isTests()} + * + * @return {@code true} if the code runs in the production mode, {@code false} otherwise + * @deprecated use {@code Environment.instance().is(Production.type())} + */ + @Deprecated + public boolean isProduction() { + return !isTests(); + } + /** * Restores the state from the instance created by {@link #createCopy()}. * From a45855e13db9792cd6424d1de36c4470b4357c59 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Wed, 3 Jun 2020 17:45:02 +0300 Subject: [PATCH 29/37] Correct documentation errors, test the `@Deprecated` methods. --- .../main/java/io/spine/base/Environment.java | 5 ++++- .../java/io/spine/base/EnvironmentTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index e2defaa1f5..70d666d8f9 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -34,7 +34,8 @@ * *

    Environment Type Detection

    * - *

    Current implementation allows to {@linkplain #is(EnvironmentType) check} whether a + *

    Current implementation allows to {@linkplain #is(EnvironmentType) check} whether a given + * environment is currently the active one. * *

      *
    • {@link Tests} is detected if the current call stack has a reference to the unit @@ -203,6 +204,7 @@ private void determineCurrentType() { /** * Verifies if the code currently runs under a unit testing framework. * + * @see Tests * @deprecated use {@code Environment.instance().is(Tests.type)} */ @Deprecated @@ -216,6 +218,7 @@ public boolean isTests() { *

      This method is opposite to {@link #isTests()} * * @return {@code true} if the code runs in the production mode, {@code false} otherwise + * @see Production * @deprecated use {@code Environment.instance().is(Production.type())} */ @Deprecated diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index c78e9031d7..411b39b27b 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -113,6 +113,24 @@ void environmentVarUnknownValue() { assertThat(environment.is(Production.type())).isTrue(); } + @Test + @DisplayName("tell that we are under tests when a deprecated method is used") + void underTestFrameworkDeprecated() { + @SuppressWarnings("deprecation") + boolean isTests = environment.isTests(); + assertThat(isTests).isTrue(); + } + + @Test + @DisplayName("tell that we are not under tests when a deprecated method is used") + void inProductionUsingDeprecatedMethod() { + System.setProperty(ENV_KEY_TESTS, "neitherTrueNor1"); + + @SuppressWarnings("deprecation") + boolean isProduction = environment.isProduction(); + assertThat(isProduction).isTrue(); + } + @Test @DisplayName("turn tests mode on") void turnTestsOn() { From b9e9bb4c8f1e4f49536c37b751802170943924c0 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Thu, 4 Jun 2020 17:28:13 +0300 Subject: [PATCH 30/37] Add a doc sentence. --- base/src/main/java/io/spine/base/Environment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 70d666d8f9..b66386ce37 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -35,7 +35,7 @@ *

      Environment Type Detection

      * *

      Current implementation allows to {@linkplain #is(EnvironmentType) check} whether a given - * environment is currently the active one. + * environment is currently the active one. Two environment types exist out of the box: * *

        *
      • {@link Tests} is detected if the current call stack has a reference to the unit From 62926e1acbbc79737795636111c5c19ce17a0872 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Thu, 4 Jun 2020 17:42:31 +0300 Subject: [PATCH 31/37] Deprecate the `setToTests` and `setToProduction`. --- .../main/java/io/spine/base/Environment.java | 34 +++++++++++++++++-- .../java/io/spine/base/EnvironmentType.java | 2 +- .../main/java/io/spine/base/Production.java | 2 +- base/src/main/java/io/spine/base/Tests.java | 8 +++++ .../java/io/spine/base/EnvironmentTest.java | 18 ++++++++++ 5 files changed, 59 insertions(+), 5 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index b66386ce37..cbb6cfe15e 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -38,10 +38,10 @@ * environment is currently the active one. Two environment types exist out of the box: * *
          - *
        • {@link Tests} is detected if the current call stack has a reference to the unit - * testing framework. + *
        • {@link Tests} is detected if the current call stack has a reference to the unit + * testing framework. * - *
        • {@link Production} is set in all other cases. + *
        • {@link Production} is set in all other cases. *
        * *

        The framework users may define their custom settings depending on the current environment @@ -246,6 +246,34 @@ public void setTo(EnvironmentType type) { this.currentEnvType = checkNotNull(type); } + /** + * Turns the test mode on. + * + *

        This method is opposite to {@link #setToProduction()}. + * + * @deprecated use {@link #setTo(EnvironmentType)} + */ + @Deprecated + @VisibleForTesting + public void setToTests() { + this.currentEnvType = Tests.type(); + Tests.enable(); + } + + /** + * Turns the production mode on. + * + *

        This method is opposite to {@link #setToTests()}. + * + * @deprecated use {@link #setTo(EnvironmentType)} + */ + @Deprecated + @VisibleForTesting + public void setToProduction() { + this.currentEnvType = Production.type(); + Tests.clearTestingEnvVariable(); + } + /** * Resets the instance and clears the {@link Tests#ENV_KEY_TESTS} variable. */ diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java index 828301ca0a..561b34a230 100644 --- a/base/src/main/java/io/spine/base/EnvironmentType.java +++ b/base/src/main/java/io/spine/base/EnvironmentType.java @@ -40,7 +40,7 @@ public abstract class EnvironmentType { * variable may be set for every virtual machine. Application developer may use this type of * knowledge to determine the current environment. */ - abstract boolean enabled(); + protected abstract boolean enabled(); /** * @inheritDoc diff --git a/base/src/main/java/io/spine/base/Production.java b/base/src/main/java/io/spine/base/Production.java index d8f1ff6f90..9f3ea652e9 100644 --- a/base/src/main/java/io/spine/base/Production.java +++ b/base/src/main/java/io/spine/base/Production.java @@ -31,7 +31,7 @@ public final class Production extends EnvironmentType { @Override - boolean enabled() { + protected boolean enabled() { return !Tests.type() .enabled(); } diff --git a/base/src/main/java/io/spine/base/Tests.java b/base/src/main/java/io/spine/base/Tests.java index 822b9d2b9b..7a3e83d7a0 100644 --- a/base/src/main/java/io/spine/base/Tests.java +++ b/base/src/main/java/io/spine/base/Tests.java @@ -92,6 +92,14 @@ static void clearTestingEnvVariable() { System.clearProperty(ENV_KEY_TESTS); } + /** + * Sets the {@linkplain #ENV_KEY_TESTS environment variable} such that the system is brought to + * the testing environment type. + */ + static void enable() { + System.setProperty(ENV_KEY_TESTS, String.valueOf(true)); + } + /** * Returns the singleton instance of this class. */ diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 411b39b27b..902316ec47 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -121,6 +121,15 @@ void underTestFrameworkDeprecated() { assertThat(isTests).isTrue(); } + @Test + @DisplayName("tell that we are under tests if explicitly set to tests using a deprecated method") + @SuppressWarnings("deprecation") + void explicitlySetTrue() { + environment.setToTests(); + + assertThat(environment.is(Tests.type())).isTrue(); + } + @Test @DisplayName("tell that we are not under tests when a deprecated method is used") void inProductionUsingDeprecatedMethod() { @@ -147,6 +156,15 @@ void turnProductionOn() { assertThat(environment.is(Production.type())).isTrue(); } + @Test + @DisplayName("turn production mode on using a deprecated method") + @SuppressWarnings("deprecation") + void turnProductionOnUsingDeprecatedMethod() { + environment.setToProduction(); + + assertThat(environment.is(Production.type())).isTrue(); + } + @Test @DisplayName("clear environment var on rest") void clearOnReset() { From e4cefb636a58844f4e96e7f9a20df3589a6b7cf8 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Thu, 4 Jun 2020 17:47:52 +0300 Subject: [PATCH 32/37] Introduce a `type` method to check the current env type without having a concrete instance in hands. --- .../main/java/io/spine/base/Environment.java | 21 ++++++++++++++++++- .../java/io/spine/base/EnvironmentTest.java | 14 +++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index cbb6cfe15e..3ed8abc4c5 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -185,11 +185,30 @@ public Environment createCopy() { * * @return the current environment type. */ + @SuppressWarnings("ConstantConditions"/* no NPE is ensured by the `ensureTypeIsSet` call. */) public boolean is(EnvironmentType type) { + ensureTypeIsSet(); + return currentEnvType.equals(type); + } + + /** + * Returns the current environment type. + * + *

        If {@linkplain #register(EnvironmentType) custom env types have been defined}, + * goes through them in the latest-registered to earliest-registered order. + * Then, checks {@link Tests} and {@link Production}. + * + * @return the current environment type + */ + public EnvironmentType type() { + ensureTypeIsSet(); + return currentEnvType; + } + + private void ensureTypeIsSet() { if (currentEnvType == null) { determineCurrentType(); } - return currentEnvType.equals(type); } private void determineCurrentType() { diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index 902316ec47..c197f89261 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -206,6 +206,20 @@ void fallBack() { } } + @Test + @DisplayName("detect the current environment correctly using the `type` method") + void determineUsingType() { + assertThat(environment.type()).isSameInstanceAs(Tests.type()); + } + + @Test + @DisplayName("detect the current custom environment in presence of custom types") + void determineUsingTypeInPresenceOfCustom() { + register(Local.type()); + + assertThat(environment.type()).isSameInstanceAs(Local.type()); + } + private static void register(EnvironmentType... types) { for (EnvironmentType type : types) { Environment.instance() From 2de30486944da50bbc2fbe1d9279d1f4862415e2 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Thu, 4 Jun 2020 17:48:58 +0300 Subject: [PATCH 33/37] Ensure correct access level. --- base/src/main/java/io/spine/base/Tests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/main/java/io/spine/base/Tests.java b/base/src/main/java/io/spine/base/Tests.java index 7a3e83d7a0..91de532c1b 100644 --- a/base/src/main/java/io/spine/base/Tests.java +++ b/base/src/main/java/io/spine/base/Tests.java @@ -71,7 +71,7 @@ public final class Tests extends EnvironmentType { * the system property explicitly. */ @Override - public boolean enabled() { + protected boolean enabled() { String testProp = System.getProperty(ENV_KEY_TESTS); if (testProp != null) { testProp = TEST_PROP_PATTERN.matcher(testProp) From 117a4e9cd2e19b1e1d410867bf91628a7e06cfbb Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Thu, 4 Jun 2020 18:49:14 +0300 Subject: [PATCH 34/37] Add a test. --- .../base/environment/EnvironmentTest.java | 61 ++++++++++++++++++ .../io/spine/base/environment/Staging.java | 64 +++++++++++++++++++ .../spine/base/environment/package-info.java | 32 ++++++++++ 3 files changed, 157 insertions(+) create mode 100644 base/src/test/java/io/spine/base/environment/EnvironmentTest.java create mode 100644 base/src/test/java/io/spine/base/environment/Staging.java create mode 100644 base/src/test/java/io/spine/base/environment/package-info.java diff --git a/base/src/test/java/io/spine/base/environment/EnvironmentTest.java b/base/src/test/java/io/spine/base/environment/EnvironmentTest.java new file mode 100644 index 0000000000..299e6d4ad9 --- /dev/null +++ b/base/src/test/java/io/spine/base/environment/EnvironmentTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2020, TeamDev. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package io.spine.base.environment; + +import io.spine.base.Environment; +import io.spine.base.Tests; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static com.google.common.truth.Truth.assertThat; + +@DisplayName("Environment should") +class EnvironmentTest { + + @BeforeEach + void reset() { + Environment.instance() + .reset(); + } + + @Test + @DisplayName("allow a custom user type") + void allowCustomType() { + Environment.instance() + .register(Staging.type()); + + Staging.enable(); + assertThat(Environment.instance() + .is(Staging.type())).isTrue(); + } + + @Test + @DisplayName("fallback to the default type") + void fallbackToCustomType() { + Environment.instance().register(Staging.type()); + + Staging.disable(); + + assertThat(Environment.instance().is(Tests.type())).isTrue(); + } + +} diff --git a/base/src/test/java/io/spine/base/environment/Staging.java b/base/src/test/java/io/spine/base/environment/Staging.java new file mode 100644 index 0000000000..f436b35cdb --- /dev/null +++ b/base/src/test/java/io/spine/base/environment/Staging.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020, TeamDev. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package io.spine.base.environment; + +import io.spine.base.EnvironmentType; + +/** + * An environment type that mimics production but receives less traffic and is suitable for testing + * out new features. + * + *

        This implementations relies on a static {@code boolean} flag for detection. + */ +public final class Staging extends EnvironmentType { + + @Override + protected boolean enabled() { + return Singleton.INSTANCE.enabled; + } + + public static Staging type() { + return Singleton.INSTANCE.staging; + } + + /** + * Brings the underlying system into the staging environment. + */ + static void enable() { + Singleton.INSTANCE.enabled = true; + } + + /** + * Brings the underlying system out of the staging environment. + */ + static void disable() { + Singleton.INSTANCE.enabled = false; + } + + public enum Singleton { + + INSTANCE; + + @SuppressWarnings("NonSerializableFieldInSerializableClass") + private final Staging staging = new Staging(); + private boolean enabled; + } +} diff --git a/base/src/test/java/io/spine/base/environment/package-info.java b/base/src/test/java/io/spine/base/environment/package-info.java new file mode 100644 index 0000000000..61e9fdfd99 --- /dev/null +++ b/base/src/test/java/io/spine/base/environment/package-info.java @@ -0,0 +1,32 @@ +/* + * Copyright 2020, TeamDev. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * This package contains tests for {@link io.spine.base.Environment} with custom + * {@linkplain io.spine.base.EnvironmentType environment types} using them how they would be used + * from a client API, i.e. from a package different from {@code io.spine.base}. + */ +@ParametersAreNonnullByDefault +@CheckReturnValue +package io.spine.base.environment; + +import com.google.errorprone.annotations.CheckReturnValue; + +import javax.annotation.ParametersAreNonnullByDefault; From 1d8891e47a08ac5bb0e2846311d5ed0635b16a7e Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Thu, 4 Jun 2020 18:54:53 +0300 Subject: [PATCH 35/37] Fix doc errors. --- base/src/main/java/io/spine/base/Environment.java | 5 ++++- base/src/main/java/io/spine/base/Tests.java | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 3ed8abc4c5..70d362bc97 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -35,7 +35,8 @@ *

        Environment Type Detection

        * *

        Current implementation allows to {@linkplain #is(EnvironmentType) check} whether a given - * environment is currently the active one. Two environment types exist out of the box: + * environment is currently the active one and {@linkplain #type() get an instance of the current + * environment type}. Two environment types exist out of the box: * *

          *
        • {@link Tests} is detected if the current call stack has a reference to the unit @@ -108,6 +109,8 @@ * enabled} at the same time, the behaviour of {@link #is(EnvironmentType)}} is undefined. * * @see EnvironmentType + * @see Tests + * @see Production */ @SPI public final class Environment { diff --git a/base/src/main/java/io/spine/base/Tests.java b/base/src/main/java/io/spine/base/Tests.java index 91de532c1b..2d55191c64 100644 --- a/base/src/main/java/io/spine/base/Tests.java +++ b/base/src/main/java/io/spine/base/Tests.java @@ -59,8 +59,8 @@ public final class Tests extends EnvironmentType { *

          The method returns {@code true} if the following packages are discovered * in the stacktrace: *

            - *
          • {@code org.junit} - *
          • {@code org.testng} + *
          • {@code org.junit} + *
          • {@code org.testng} *
          * * @return {@code true} if the code runs under a testing framework, {@code false} otherwise From 9858b0d5544bad6e9afa4bfc99f0aa0f15b22c72 Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Thu, 4 Jun 2020 20:01:51 +0300 Subject: [PATCH 36/37] Allow to register the same environment twice. --- .../main/java/io/spine/base/Environment.java | 18 +++++++----------- .../java/io/spine/base/EnvironmentTest.java | 10 ---------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 70d362bc97..5b4320c9e6 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -27,7 +27,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; /** * Provides information about the environment (current platform used, etc.). @@ -152,16 +151,13 @@ private Environment(Environment copy) { */ @CanIgnoreReturnValue public Environment register(EnvironmentType environmentType) { - checkState(!knownEnvTypes.contains(environmentType), - "Attempted to register the same custom env type `%s` twice." + - "Please make sure to call `Environment.register(...) only once" + - "per environment type.", environmentType.getClass() - .getSimpleName()); - knownEnvTypes = ImmutableList - .builder() - .add(environmentType) - .addAll(INSTANCE.knownEnvTypes) - .build(); + if (!knownEnvTypes.contains(environmentType)) { + knownEnvTypes = ImmutableList + .builder() + .add(environmentType) + .addAll(INSTANCE.knownEnvTypes) + .build(); + } return this; } diff --git a/base/src/test/java/io/spine/base/EnvironmentTest.java b/base/src/test/java/io/spine/base/EnvironmentTest.java index c197f89261..9e6422eb88 100644 --- a/base/src/test/java/io/spine/base/EnvironmentTest.java +++ b/base/src/test/java/io/spine/base/EnvironmentTest.java @@ -186,16 +186,6 @@ void provideCustomTypes() { assertThat(environment.is(Local.type())).isTrue(); } - @Test - @DisplayName("throw if a user attempts to register the same environment twice") - void throwOnDoubleRegistration() { - Environment.instance() - .register(Local.type()); - assertThrows(IllegalStateException.class, - () -> Environment.instance() - .register(Local.type())); - } - @Test @DisplayName("fallback to the `TESTS` environment") void fallBack() { From 5cf0c03722d8c28c8140c2431e45912843fb2b9c Mon Sep 17 00:00:00 2001 From: "serhii.lekariev" Date: Thu, 4 Jun 2020 20:02:20 +0300 Subject: [PATCH 37/37] Correct a documentation error. --- base/src/main/java/io/spine/base/Environment.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index 5b4320c9e6..f1a1ffd077 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -136,9 +136,6 @@ private Environment(Environment copy) { * Remembers the specified environment type, allowing {@linkplain #is(EnvironmentType) to * determine whether it's enabled} later. * - *

          If the specified environment type has already been registered, throws an - * {@code IllegalStateException}. - * *

          Note that the default types are still present. * When trying to determine which environment type is enabled, the user-defined types are * checked first, in the first-registered to last-registered order.