diff --git a/base/src/main/java/io/spine/base/Environment.java b/base/src/main/java/io/spine/base/Environment.java index f1a1ffd077..bfa8702caa 100644 --- a/base/src/main/java/io/spine/base/Environment.java +++ b/base/src/main/java/io/spine/base/Environment.java @@ -27,15 +27,16 @@ import org.checkerframework.checker.nullness.qual.Nullable; import static com.google.common.base.Preconditions.checkNotNull; +import static io.spine.util.Exceptions.newIllegalStateException; /** * Provides information about the environment (current platform used, etc.). * *
Current implementation allows to {@linkplain #is(EnvironmentType) check} whether a given - * 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: + *
Current implementation allows to {@linkplain #is(Class) check} the type of the current + * environment, or {@linkplain #type() get the instance of the current environment}. + * Two environment types exist out of the box: * *
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 #is(EnvironmentType)}} is undefined.
+ * enabled} at the same time, the behaviour of {@link #is(Class)}} is undefined.
*
* @see EnvironmentType
* @see Tests
@@ -115,7 +116,7 @@
public final class Environment {
private static final ImmutableList Note that the default types are still present.
@@ -179,52 +180,46 @@ public Environment createCopy() {
* goes through them in the latest-registered to earliest-registered order.
* Then, checks {@link Tests} and {@link Production}.
*
- * @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.
+ * Please note that {@code is} follows assigment-compatibility:
+ * 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}.
+ * final class AppEngineStandard extends AppEngine {
+ * ...
+ * }
*
- * @return the current environment type
+ * Environment environment = Environment.instance();
+ *
+ * // Assuming we are under App Engine Standard
+ * assertThat(environment.is(AppEngine.class)).isTrue();
+ *
+ * Some examples may be {@code STAGING} or {@code LOCAL} environments.
*
- * @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}.
+ * @implNote Not an {@code interface} to limit the access level of {@link #enabled()}
*/
public abstract class EnvironmentType {
@@ -41,27 +37,4 @@ public abstract class EnvironmentType {
* knowledge to determine the current environment.
*/
protected 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
index 9f3ea652e9..a512549049 100644
--- a/base/src/main/java/io/spine/base/Production.java
+++ b/base/src/main/java/io/spine/base/Production.java
@@ -20,40 +20,25 @@
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
- protected boolean enabled() {
- return !Tests.type()
- .enabled();
- }
-
/**
- * Returns the singleton instance.
+ * Creates a new instance.
+ *
+ * All {@code Production} instances are immutable and equivalent.
*/
- public static Production type() {
- return Singleton.INSTANCE.production;
+ public Production() {
+ super();
}
- private enum Singleton {
-
- INSTANCE;
-
- @SuppressWarnings({
- "NonSerializableFieldInSerializableClass",
- "PMD.SingularField" /* this field cannot be local */})
- private final Production production;
-
- Singleton() {
- this.production = new Production();
- }
+ @Override
+ protected boolean enabled() {
+ boolean tests = new Tests().enabled();
+ return !tests;
}
}
diff --git a/base/src/main/java/io/spine/base/Tests.java b/base/src/main/java/io/spine/base/Tests.java
index 2d55191c64..c9494e88f6 100644
--- a/base/src/main/java/io/spine/base/Tests.java
+++ b/base/src/main/java/io/spine/base/Tests.java
@@ -23,7 +23,6 @@
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;
@@ -34,7 +33,6 @@
*
* 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 {
@@ -53,14 +51,23 @@ public final class Tests extends EnvironmentType {
private static final Pattern TEST_PROP_PATTERN = Pattern.compile("\"' ");
+ /**
+ * Creates a new instance.
+ *
+ * All {@code Tests} instances are immutable and equivalent.
+ */
+ public Tests() {
+ super();
+ }
+
/**
* 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:
* Leaves the implementation of {@link #enabled()} to subclasses.
+ */
+public abstract class AppEngine extends EnvironmentType {
+}
diff --git a/base/src/test/java/io/spine/base/given/AppEngineStandard.java b/base/src/test/java/io/spine/base/given/AppEngineStandard.java
new file mode 100644
index 0000000000..8e2aed4dc9
--- /dev/null
+++ b/base/src/test/java/io/spine/base/given/AppEngineStandard.java
@@ -0,0 +1,54 @@
+/*
+ * 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.given;
+
+/**
+ * Determines whether the system is running under Google App Engine Standard environment.
+ */
+@SuppressWarnings("AccessOfSystemProperties")
+public class AppEngineStandard extends AppEngine {
+
+ private static final String ENV_KEY = "io.spine.base.test.is_appengine";
+
+ @Override
+ protected boolean enabled() {
+ String propertyValue = System.getProperty(ENV_KEY);
+ return activeValue().equalsIgnoreCase(propertyValue);
+ }
+
+ /**
+ * Enables the App Engine Standard environment.
+ */
+ public static void enable() {
+ System.setProperty(ENV_KEY, activeValue());
+ }
+
+ /**
+ * Disables teh App Engine Standard environment.
+ */
+ public static void clear() {
+ System.clearProperty(ENV_KEY);
+ }
+
+ private static String activeValue() {
+ return String.valueOf(true);
+ }
+}
diff --git a/license-report.md b/license-report.md
index f940bd01bc..e2e407bb25 100644
--- a/license-report.md
+++ b/license-report.md
@@ -1,6 +1,6 @@
-# Dependencies of `io.spine:spine-base:1.5.14`
+# Dependencies of `io.spine:spine-base:1.5.15`
## 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 **Sat Jun 06 13:18: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 **Tue Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-errorprone-checks:1.5.14`
+# Dependencies of `io.spine.tools:spine-errorprone-checks:1.5.15`
## Runtime
1. **Group:** com.github.ben-manes.caffeine **Name:** caffeine **Version:** 2.7.0
@@ -773,12 +773,12 @@ This report was generated on **Sat Jun 06 13:18: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 **Sat Jun 06 13:18:59 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 Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-javadoc-filter:1.5.14`
+# Dependencies of `io.spine.tools:spine-javadoc-filter:1.5.15`
## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
@@ -1156,12 +1156,12 @@ This report was generated on **Sat Jun 06 13:18:59 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19:01 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 Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-javadoc-prettifier:1.5.14`
+# Dependencies of `io.spine.tools:spine-javadoc-prettifier:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -1521,12 +1521,12 @@ This report was generated on **Sat Jun 06 13:19:01 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19:01 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 Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-model-compiler:1.5.14`
+# Dependencies of `io.spine.tools:spine-model-compiler:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -1902,12 +1902,12 @@ This report was generated on **Sat Jun 06 13:19:01 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19:01 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 Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-mute-logging:1.5.14`
+# Dependencies of `io.spine.tools:spine-mute-logging:1.5.15`
## Runtime
1. **Group:** com.google.auto.value **Name:** auto-value-annotations **Version:** 1.6.3
@@ -2281,12 +2281,12 @@ This report was generated on **Sat Jun 06 13:19:01 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19: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 **Tue Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-plugin-base:1.5.14`
+# Dependencies of `io.spine.tools:spine-plugin-base:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -2646,12 +2646,12 @@ This report was generated on **Sat Jun 06 13:19: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 **Sat Jun 06 13:19: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 **Tue Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-plugin-testlib:1.5.14`
+# Dependencies of `io.spine.tools:spine-plugin-testlib:1.5.15`
## Runtime
1. **Group:** com.google.auto.value **Name:** auto-value-annotations **Version:** 1.6.3
@@ -3065,12 +3065,12 @@ This report was generated on **Sat Jun 06 13:19: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 **Sat Jun 06 13:19:03 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 Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-proto-dart-plugin:1.5.14`
+# Dependencies of `io.spine.tools:spine-proto-dart-plugin:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -3430,12 +3430,12 @@ This report was generated on **Sat Jun 06 13:19:03 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19:03 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 Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-proto-js-plugin:1.5.14`
+# Dependencies of `io.spine.tools:spine-proto-js-plugin:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -3795,12 +3795,12 @@ This report was generated on **Sat Jun 06 13:19:03 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19: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 **Tue Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-protoc-api:1.5.14`
+# Dependencies of `io.spine.tools:spine-protoc-api:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -4120,12 +4120,12 @@ This report was generated on **Sat Jun 06 13:19: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 **Sat Jun 06 13:19: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 **Tue Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-protoc-plugin:1.5.14`
+# Dependencies of `io.spine.tools:spine-protoc-plugin:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -4453,12 +4453,12 @@ This report was generated on **Sat Jun 06 13:19: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 **Sat Jun 06 13:19:05 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 Jun 09 13:39: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).
-# Dependencies of `io.spine:spine-testlib:1.5.14`
+# Dependencies of `io.spine:spine-testlib:1.5.15`
## Runtime
1. **Group:** com.google.auto.value **Name:** auto-value-annotations **Version:** 1.6.3
@@ -4832,12 +4832,12 @@ This report was generated on **Sat Jun 06 13:19:05 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19:05 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 Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-tool-base:1.5.14`
+# Dependencies of `io.spine.tools:spine-tool-base:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -5165,12 +5165,12 @@ This report was generated on **Sat Jun 06 13:19:05 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19:06 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 Jun 09 13:39: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).
-# Dependencies of `io.spine.tools:spine-validation-generator:1.5.14`
+# Dependencies of `io.spine.tools:spine-validation-generator:1.5.15`
## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
@@ -5498,4 +5498,4 @@ This report was generated on **Sat Jun 06 13:19:06 EEST 2020** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.
-This report was generated on **Sat Jun 06 13:19:06 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 Jun 09 13:39: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
diff --git a/pom.xml b/pom.xml
index 8bde29d651..35f1bdbfa2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -12,7 +12,7 @@ all modules and does not describe the project structure per-subproject.
+ * abstract class AppEngine extends EnvironmentType {
+ * ...
+ * }
*
- *
+ *
+ * @return whether the current environment type matches the specified one
*/
- public EnvironmentType type() {
- ensureTypeIsSet();
- return currentEnvType;
- }
-
- private void ensureTypeIsSet() {
- if (currentEnvType == null) {
- determineCurrentType();
- }
+ public boolean is(Class extends EnvironmentType> type) {
+ EnvironmentType currentEnv = cachedOrCalculated();
+ boolean result = type.isInstance(currentEnv);
+ return result;
}
- private void determineCurrentType() {
- for (EnvironmentType type : knownEnvTypes) {
- if (type.enabled()) {
- this.currentEnvType = type;
- return;
- }
- }
+ /** Returns the instance of the current environment. */
+ public EnvironmentType type() {
+ EnvironmentType currentEnv = cachedOrCalculated();
+ return currentEnv;
}
/**
* Verifies if the code currently runs under a unit testing framework.
*
* @see Tests
- * @deprecated use {@code Environment.instance().is(Tests.type)}
+ * @deprecated use {@code Environment.instance().is(Tests.class)}
*/
@Deprecated
public boolean isTests() {
- return is(Tests.type());
+ return is(Tests.class);
}
/**
@@ -234,7 +229,7 @@ public boolean 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 use {@code Environment.instance().is(Production.class)}
*/
@Deprecated
public boolean isProduction() {
@@ -271,7 +266,7 @@ public void setTo(EnvironmentType type) {
@Deprecated
@VisibleForTesting
public void setToTests() {
- this.currentEnvType = Tests.type();
+ this.currentEnvType = new Tests();
Tests.enable();
}
@@ -285,7 +280,7 @@ public void setToTests() {
@Deprecated
@VisibleForTesting
public void setToProduction() {
- this.currentEnvType = Production.type();
+ this.currentEnvType = new Production();
Tests.clearTestingEnvVariable();
}
@@ -298,4 +293,21 @@ public void reset() {
this.knownEnvTypes = BASE_TYPES;
Tests.clearTestingEnvVariable();
}
+
+ private EnvironmentType cachedOrCalculated() {
+ EnvironmentType result = currentEnvType != null
+ ? currentEnvType
+ : currentType();
+ return result;
+ }
+
+ private EnvironmentType currentType() {
+ for (EnvironmentType type : knownEnvTypes) {
+ if (type.enabled()) {
+ return type;
+ }
+ }
+
+ throw newIllegalStateException("`Environment` could not find an active environment type.");
+ }
}
diff --git a/base/src/main/java/io/spine/base/EnvironmentType.java b/base/src/main/java/io/spine/base/EnvironmentType.java
index 561b34a230..0c2719dd00 100644
--- a/base/src/main/java/io/spine/base/EnvironmentType.java
+++ b/base/src/main/java/io/spine/base/EnvironmentType.java
@@ -20,16 +20,12 @@
package io.spine.base;
-import com.google.common.base.Objects;
-
/**
* A type of environment.
*
*
- *
*
* @return {@code true} if the code runs under a testing framework, {@code false} otherwise
@@ -99,25 +106,4 @@ static void clearTestingEnvVariable() {
static void enable() {
System.setProperty(ENV_KEY_TESTS, String.valueOf(true));
}
-
- /**
- * Returns the singleton instance of this class.
- */
- public static Tests type() {
- 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 9e6422eb88..d6e27579f5 100644
--- a/base/src/test/java/io/spine/base/EnvironmentTest.java
+++ b/base/src/test/java/io/spine/base/EnvironmentTest.java
@@ -20,6 +20,9 @@
package io.spine.base;
+import com.google.errorprone.annotations.Immutable;
+import io.spine.base.given.AppEngine;
+import io.spine.base.given.AppEngineStandard;
import io.spine.testing.UtilityClassTest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
@@ -32,7 +35,6 @@
import static com.google.common.truth.Truth.assertThat;
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;
@DisplayName("Environment utility class should")
@SuppressWarnings("AccessOfSystemProperties")
@@ -83,11 +85,11 @@ void cleanUp() {
@Test
@DisplayName("tell that we are under tests if env. variable set to true")
void environmentVarTrue() {
- Tests tests = Tests.type();
+ Tests tests = new Tests();
Environment.instance()
.setTo(tests);
- assertThat(environment.is(Tests.type())).isTrue();
+ assertThat(environment.is(Tests.class)).isTrue();
}
@Test
@@ -95,14 +97,14 @@ void environmentVarTrue() {
void environmentVar1() {
System.setProperty(ENV_KEY_TESTS, "1");
- assertThat(environment.is(Tests.type())).isTrue();
+ assertThat(environment.is(Tests.class)).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.type())).isTrue();
+ assertThat(environment.is(Tests.class)).isTrue();
}
@Test
@@ -110,7 +112,7 @@ void underTestFramework() {
void environmentVarUnknownValue() {
System.setProperty(ENV_KEY_TESTS, "neitherTrueNor1");
- assertThat(environment.is(Production.type())).isTrue();
+ assertThat(environment.is(Production.class)).isTrue();
}
@Test
@@ -127,7 +129,7 @@ void underTestFrameworkDeprecated() {
void explicitlySetTrue() {
environment.setToTests();
- assertThat(environment.is(Tests.type())).isTrue();
+ assertThat(environment.is(Tests.class)).isTrue();
}
@Test
@@ -143,17 +145,17 @@ void inProductionUsingDeprecatedMethod() {
@Test
@DisplayName("turn tests mode on")
void turnTestsOn() {
- environment.setTo(Tests.type());
+ environment.setTo(new Tests());
- assertThat(environment.is(Tests.type())).isTrue();
+ assertThat(environment.is(Tests.class)).isTrue();
}
@Test
@DisplayName("turn production mode on")
void turnProductionOn() {
- environment.setTo(Production.type());
+ environment.setTo(new Production());
- assertThat(environment.is(Production.type())).isTrue();
+ assertThat(environment.is(Production.class)).isTrue();
}
@Test
@@ -162,7 +164,7 @@ void turnProductionOn() {
void turnProductionOnUsingDeprecatedMethod() {
environment.setToProduction();
- assertThat(environment.is(Production.type())).isTrue();
+ assertThat(environment.is(Production.class)).isTrue();
}
@Test
@@ -180,34 +182,58 @@ class CustomEnvTypes {
@Test
@DisplayName("allow to provide user defined environment types")
void provideCustomTypes() {
- register(Staging.type(), Local.type());
+ register(new Staging(), new Local());
// Now that `Environment` knows about `LOCAL`, it should use it as fallback.
- assertThat(environment.is(Local.type())).isTrue();
+ assertThat(environment.is(Local.class)).isTrue();
}
@Test
@DisplayName("fallback to the `TESTS` environment")
void fallBack() {
Environment.instance()
- .register(Travis.type());
- assertThat(environment.is(Travis.type())).isFalse();
- assertThat(environment.is(Tests.type())).isTrue();
+ .register(new Travis());
+ assertThat(environment.is(Travis.class)).isFalse();
+ assertThat(environment.is(Tests.class)).isTrue();
}
}
+ @Test
+ @DisplayName("follow assignment-compatibility when determining the type")
+ void polymorphicEnv() {
+ register(new AppEngineStandard());
+
+ AppEngineStandard.enable();
+ assertThat(environment.is(AppEngine.class)).isTrue();
+ AppEngineStandard.clear();
+ }
+
@Test
@DisplayName("detect the current environment correctly using the `type` method")
void determineUsingType() {
- assertThat(environment.type()).isSameInstanceAs(Tests.type());
+ assertThat(environment.is(Tests.class)).isTrue();
}
@Test
@DisplayName("detect the current custom environment in presence of custom types")
void determineUsingTypeInPresenceOfCustom() {
- register(Local.type());
+ register(new Staging(), new Local());
+
+ assertThat(environment.is(Local.class)).isTrue();
+ }
- assertThat(environment.type()).isSameInstanceAs(Local.type());
+ @Test
+ @DisplayName("return the instance of the default environment type")
+ void returnInstance() {
+ assertThat(environment.type()).isInstanceOf(Tests.class);
+ }
+
+ @Test
+ @DisplayName("return the instance of a custom environment type")
+ void returnCustomInstnace() {
+ register(new Local(), new Staging());
+
+ assertThat(environment.type()).isInstanceOf(Local.class);
}
private static void register(EnvironmentType... types) {
@@ -219,87 +245,31 @@ private static void register(EnvironmentType... types) {
static final class Local extends EnvironmentType {
- 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 type() {
- 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 type() {
- 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();
- }
- }
}
+ @Immutable
@SuppressWarnings("unused" /* The only variant is used. */)
static final class Travis extends EnvironmentType {
- private Travis() {
- }
-
@Override
public boolean enabled() {
return false;
}
-
- public static Travis type() {
- return Singleton.INSTANCE.travis;
- }
-
- private enum Singleton {
-
- INSTANCE;
-
- @SuppressWarnings("NonSerializableFieldInSerializableClass")
- private final Travis travis;
-
- Singleton() {
- this.travis = new Travis();
- }
- }
}
}
diff --git a/base/src/test/java/io/spine/base/environment/EnvironmentTest.java b/base/src/test/java/io/spine/base/environment/EnvironmentTest.java
index 299e6d4ad9..68ad0445c5 100644
--- a/base/src/test/java/io/spine/base/environment/EnvironmentTest.java
+++ b/base/src/test/java/io/spine/base/environment/EnvironmentTest.java
@@ -41,21 +41,20 @@ void reset() {
@DisplayName("allow a custom user type")
void allowCustomType() {
Environment.instance()
- .register(Staging.type());
+ .register(new Staging());
Staging.enable();
assertThat(Environment.instance()
- .is(Staging.type())).isTrue();
+ .is(Staging.class)).isTrue();
}
@Test
@DisplayName("fallback to the default type")
void fallbackToCustomType() {
- Environment.instance().register(Staging.type());
+ Environment.instance().register(new Staging());
Staging.disable();
- assertThat(Environment.instance().is(Tests.type())).isTrue();
+ assertThat(Environment.instance().is(Tests.class)).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
index f436b35cdb..b49ea07486 100644
--- a/base/src/test/java/io/spine/base/environment/Staging.java
+++ b/base/src/test/java/io/spine/base/environment/Staging.java
@@ -30,35 +30,28 @@
*/
public final class Staging extends EnvironmentType {
- @Override
- protected boolean enabled() {
- return Singleton.INSTANCE.enabled;
+ private static boolean enabled = false;
+
+ Staging() {
+ super();
}
- public static Staging type() {
- return Singleton.INSTANCE.staging;
+ @Override
+ protected boolean enabled() {
+ return enabled;
}
/**
* Brings the underlying system into the staging environment.
*/
static void enable() {
- Singleton.INSTANCE.enabled = true;
+ 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;
+ enabled = false;
}
}
diff --git a/base/src/test/java/io/spine/base/given/AppEngine.java b/base/src/test/java/io/spine/base/given/AppEngine.java
new file mode 100644
index 0000000000..3c049d31ba
--- /dev/null
+++ b/base/src/test/java/io/spine/base/given/AppEngine.java
@@ -0,0 +1,31 @@
+/*
+ * 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.given;
+
+import io.spine.base.EnvironmentType;
+
+/**
+ * An environment that denotes that the system is running under Google App Engine.
+ *
+ *