Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions base/src/main/java/io/spine/base/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
public final class Environment {

private static final ImmutableList<EnvironmentType> BASE_TYPES =
ImmutableList.of(Tests.type(), Production.type());
ImmutableList.of(Tests.ENVIRONMENT, Production.ENVIRONMENT);

private static final Environment INSTANCE = new Environment();

Expand Down Expand Up @@ -224,7 +224,7 @@ private void determineCurrentType() {
*/
@Deprecated
public boolean isTests() {
return is(Tests.type());
return is(Tests.ENVIRONMENT);
}

/**
Expand Down Expand Up @@ -271,7 +271,7 @@ public void setTo(EnvironmentType type) {
@Deprecated
@VisibleForTesting
public void setToTests() {
this.currentEnvType = Tests.type();
this.currentEnvType = Tests.ENVIRONMENT;
Tests.enable();
}

Expand All @@ -285,7 +285,7 @@ public void setToTests() {
@Deprecated
@VisibleForTesting
public void setToProduction() {
this.currentEnvType = Production.type();
this.currentEnvType = Production.ENVIRONMENT;
Tests.clearTestingEnvVariable();
}

Expand Down
30 changes: 4 additions & 26 deletions base/src/main/java/io/spine/base/EnvironmentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

package io.spine.base;

import com.google.common.base.Objects;
import com.google.errorprone.annotations.Immutable;

/**
* A type of environment.
Expand All @@ -31,7 +31,8 @@
* that their API is consistent with the env types provided by the {@code base} library:
* {@link Production}, {@link Tests}.
*/
public abstract class EnvironmentType {
@Immutable
public interface EnvironmentType {

/**
* Returns {@code true} if the underlying system is currently in this environment type.
Expand All @@ -40,28 +41,5 @@ 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.
*/
protected abstract boolean enabled();

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

/**
* @inheritDoc
*
* <p>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());
}
boolean enabled();
}
30 changes: 5 additions & 25 deletions base/src/main/java/io/spine/base/Production.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,12 @@
* <p>If the system is not in the {@link Tests} environment, it is in the production environment.
*/
@Immutable
public final class Production extends EnvironmentType {
public enum Production implements EnvironmentType {

@Override
protected boolean enabled() {
return !Tests.type()
.enabled();
}

/**
* Returns the singleton instance.
*/
public static Production type() {
return Singleton.INSTANCE.production;
}
ENVIRONMENT;

private enum Singleton {

INSTANCE;

@SuppressWarnings({
"NonSerializableFieldInSerializableClass",
"PMD.SingularField" /* this field cannot be local */})
private final Production production;

Singleton() {
this.production = new Production();
}
@Override
public boolean enabled() {
return !Tests.ENVIRONMENT.enabled();
}
}
27 changes: 4 additions & 23 deletions base/src/main/java/io/spine/base/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
*/
@Immutable
@SuppressWarnings("AccessOfSystemProperties" /* is necessary for this class to function */)
public final class Tests extends EnvironmentType {
public enum Tests implements EnvironmentType {

ENVIRONMENT;

/**
* The key name of the system property which tells if a code runs under a testing framework.
Expand Down Expand Up @@ -71,7 +73,7 @@ public final class Tests extends EnvironmentType {
* the system property explicitly.
*/
@Override
protected boolean enabled() {
public boolean enabled() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a design purpose to prohibit invoking this method from outside the package. This is why they were not enums.

String testProp = System.getProperty(ENV_KEY_TESTS);
if (testProp != null) {
testProp = TEST_PROP_PATTERN.matcher(testProp)
Expand Down Expand Up @@ -99,25 +101,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();
}
}
}
Loading