Create the o.s.d.gemfire.tests.config.TestProperties class.
TestProperties allows a test class to load and use properties (e.g. from a test.properties classpath resource) specific to the test during test execution.
This commit is contained in:
@@ -0,0 +1,333 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.gemfire.tests.config;
|
||||
|
||||
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link TestProperties} used to configure the underlying resources in the Spring Test for Apache Geode Framework.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see java.lang.Iterable
|
||||
* @see java.util.Properties
|
||||
* @see org.springframework.core.io.Resource
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class TestProperties implements Iterable<String> {
|
||||
|
||||
public static final boolean NORMALIZE_SYSTEM_PROPERTY_NAMES = true;
|
||||
|
||||
public static final String SYSTEM_PROPERTY_PREFIX = "system.";
|
||||
public static final String TEST_PROPERTIES_PATH = "test.properties";
|
||||
|
||||
private static TestProperties testPropertiesReference;
|
||||
|
||||
/**
|
||||
* Factory method used to construct a new, and get the {@literal single} instance of, {@link TestProperties}
|
||||
* initialized withg a {@literal test.properties} resource in the application classpath if it exists.
|
||||
*
|
||||
* @return a new {@link TestProperties}.
|
||||
* @throws TestConfigurationException if the {@literal test.properties} classpath resource exists,
|
||||
* but could not be loaded.
|
||||
* @see #TestProperties()
|
||||
*/
|
||||
public static synchronized TestProperties getInstance() {
|
||||
|
||||
if (testPropertiesReference == null) {
|
||||
testPropertiesReference = new TestProperties();
|
||||
}
|
||||
|
||||
return testPropertiesReference;
|
||||
}
|
||||
|
||||
private final Properties testProperties;
|
||||
|
||||
/**
|
||||
* Constructs a new instance of {@link TestProperties} initialized with a {@literal test.properties} resource
|
||||
* in the application classpath if it exists.
|
||||
*
|
||||
* @throws TestConfigurationException if the {@literal test.properties} classpath resource exists,
|
||||
* but could not be loaded.
|
||||
*/
|
||||
public TestProperties() {
|
||||
this.testProperties = new Properties(resolveDefaultTestProperties());
|
||||
}
|
||||
|
||||
private @NonNull Properties resolveDefaultTestProperties() {
|
||||
|
||||
Properties defaultTestProperties = new Properties();
|
||||
|
||||
Resource defaultTestPropertiesResource = getTestPropertiesResource();
|
||||
|
||||
if (defaultTestPropertiesResource.exists()) {
|
||||
try (InputStream in = defaultTestPropertiesResource.getInputStream()) {
|
||||
defaultTestProperties.load(in);
|
||||
}
|
||||
catch (IOException cause) {
|
||||
|
||||
String message = String.format("Failed to load test configuration [%s]", defaultTestPropertiesResource);
|
||||
|
||||
throw new TestConfigurationException(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
return defaultTestProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a reference to the {@literal mutable} test {@link Properties} managed by {@literal this}
|
||||
* {@link TestProperties} instance.
|
||||
*
|
||||
* @return a reference to the {@literal mutable} test {@link Properties}.
|
||||
* @see java.util.Properties
|
||||
*/
|
||||
protected @NonNull Properties getTestProperties() {
|
||||
return this.testProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a {@link Resource} referring to {@literal test.properties} in the application classpath.
|
||||
*
|
||||
* @return a {@link Resource} referring to {@literal test.properties} in the application classpath.
|
||||
* @see org.springframework.core.io.ClassPathResource
|
||||
* @see org.springframework.core.io.Resource
|
||||
*/
|
||||
protected @NonNull Resource getTestPropertiesResource() {
|
||||
return new ClassPathResource(TEST_PROPERTIES_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the given {@link String named} property is a test
|
||||
* {@link System#getProperties() System property}.
|
||||
*
|
||||
* A {@literal test} {@link System#getProperties() System property} is any property prefixed with {@literal system.}.
|
||||
* This property would be added to the {@link System#getProperties()} if {@link #configureSystemProperties()}
|
||||
* were called.
|
||||
*
|
||||
* @param propertyName {@link String} containing the {@literal name} of the property.
|
||||
* @return a boolean valued indicating whether the given {@link String named} property is a test
|
||||
* {@link System#getProperties() System property}.
|
||||
\ */
|
||||
protected boolean isTestSystemProperty(@Nullable String propertyName) {
|
||||
return propertyName != null && propertyName.startsWith(SYSTEM_PROPERTY_PREFIX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the given {@link String named} property to a {@literal non-null} property {@link String name}.
|
||||
*
|
||||
* @param propertyName {@link String} containing the {@literal name} of the property to resolve as
|
||||
* a {@literal non-null} {@link String value}
|
||||
* @return the resolved property {@link String name}.
|
||||
*/
|
||||
protected @NonNull String toNonNullPropertyName(@Nullable String propertyName) {
|
||||
return String.valueOf(propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given {@link String named} property into a test system property prefixed with {@literal system.}.
|
||||
*
|
||||
* @param propertyName {@link String} containing the {@literal name} of the property to convert into
|
||||
* a test system property name.
|
||||
* @return the given {@link String named} property as a test system property prefixed with {@literal system.}.
|
||||
* @see #isTestSystemProperty(String)
|
||||
* @see #toNonNullPropertyName(String)
|
||||
*/
|
||||
protected @NonNull String toSystemPropertyName(@Nullable String propertyName) {
|
||||
|
||||
return isTestSystemProperty(propertyName) ? propertyName
|
||||
: SYSTEM_PROPERTY_PREFIX.concat(toNonNullPropertyName(propertyName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all properties from {@literal this} {@link TestProperties} instance.
|
||||
*/
|
||||
public void clear() {
|
||||
getTestProperties().clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all configured test properties from {@link System#getProperties()}.
|
||||
*/
|
||||
public void clearSystemProperties() {
|
||||
getSystemPropertyNames().forEach(System::clearProperty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures {@link System#getProperties()} with all {@literal test.properties} prefixed with {@literal system.}.
|
||||
*/
|
||||
public void configureSystemProperties() {
|
||||
|
||||
getSystemPropertyNames()
|
||||
.forEach(property -> System.setProperty(stripSystemPropertyPrefix(property), get(property)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link String value} of the given {@link String named} property.
|
||||
*
|
||||
* @param propertyName {@link String} containing the {@literal name} of the property;
|
||||
* must not be {@literal null}.
|
||||
* @return the {@link String value} for the given {@link String named} property,
|
||||
* or {@literal null} if a given property is undefined.
|
||||
*/
|
||||
public @Nullable String get(@NonNull String propertyName) {
|
||||
|
||||
Properties testProperties = getTestProperties();
|
||||
|
||||
String resolvedPropertyName = toNonNullPropertyName(propertyName);
|
||||
String propertyValue = testProperties.getProperty(resolvedPropertyName);
|
||||
|
||||
return propertyValue != null ? propertyValue
|
||||
: testProperties.getProperty(toSystemPropertyName(resolvedPropertyName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link String value} of the given {@link String named} property, or returns
|
||||
* the {@link String defaultPropertyValue} if the given {@link String named} property
|
||||
* is undefined.
|
||||
*
|
||||
* @param propertyName {@link String} containing the {@literal name} of the property; must not be {@literal null}.
|
||||
* @param defaultPropertyValue {@link String} containing the value to return if the given {@link String named}
|
||||
* property is undefined.
|
||||
* @return a {@link String value} for the given {@link String named} property, or {@link String defaultPropertyValue}
|
||||
* if a given {@link String named} property is undefined.
|
||||
* @see #get(String)
|
||||
*/
|
||||
public @Nullable String get(@NonNull String propertyName, @Nullable String defaultPropertyValue) {
|
||||
|
||||
String propertyValue = get(propertyName);
|
||||
|
||||
return propertyValue != null ? propertyValue : defaultPropertyValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a {@link Set} of property {@link String names} for all properties defined in {@literal test.properties}.
|
||||
*
|
||||
* @return a {@link Set} of property {@link String names} for all properties defined in {@literal test.properties}.
|
||||
* @see java.util.Set
|
||||
*/
|
||||
public Set<String> getPropertyNames() {
|
||||
return Collections.unmodifiableSet(getTestProperties().stringPropertyNames());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a {@link Set} of {@literal system} property {@link String names} from {@literal test.properties}.
|
||||
*
|
||||
* @return a {@link Set} of {@literal system} property {@link String names} from {@literal test.properties}.
|
||||
* @see #getSystemPropertyNames(boolean)
|
||||
* @see java.util.Set
|
||||
*/
|
||||
public Set<String> getSystemPropertyNames() {
|
||||
return getSystemPropertyNames(NORMALIZE_SYSTEM_PROPERTY_NAMES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a {@link Set} of {@literal system} property {@link String names} from {@literal test.properties}.
|
||||
*
|
||||
* @param normalize boolean value indicating whether to strip the System property prefix ({@literal system.})
|
||||
* from the property {@link String name} before adding the property {@link String name} to the {@link Set}.
|
||||
* @return a {@link Set} of {@literal system} property {@link String names} from {@literal test.properties}.
|
||||
* @see #isTestSystemProperty(String)
|
||||
* @see #stripSystemPropertyPrefix(String)
|
||||
* @see java.util.Set
|
||||
*/
|
||||
public Set<String> getSystemPropertyNames(boolean normalize) {
|
||||
|
||||
return getPropertyNames().stream()
|
||||
.filter(this::isTestSystemProperty)
|
||||
.map(propertyName -> normalize ? stripSystemPropertyPrefix(propertyName) : propertyName)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
String stripSystemPropertyPrefix(@NonNull String propertyName) {
|
||||
|
||||
return propertyName.startsWith(SYSTEM_PROPERTY_PREFIX)
|
||||
? propertyName.substring(SYSTEM_PROPERTY_PREFIX.length())
|
||||
: propertyName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
return getPropertyNames().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link String value} of the given {@link String named} property.
|
||||
*
|
||||
* The given {@link String named} property must have a {@literal non-null}, {@literal non-empty}
|
||||
* and {@literal non-blank} {@link String value}.
|
||||
*
|
||||
* @param propertyName {@link String} containing the {@literal name} of the property to get.
|
||||
* @return the {@link String value} of the given {@link String named} property.
|
||||
* @throws IllegalStateException if the given {@link String named} property is undefined.
|
||||
* @see #get(String)
|
||||
*/
|
||||
public @NonNull String requireProperty(@NonNull String propertyName) {
|
||||
|
||||
return Optional.ofNullable(get(propertyName))
|
||||
.filter(StringUtils::hasText)
|
||||
.orElseThrow(() -> newIllegalStateException("Property [%s] is undefined", propertyName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the given {@link String named} property to the given {@link String propertyValue}.
|
||||
*
|
||||
* @param propertyName {@link String} containing the {@literal name} of the property to set.
|
||||
* @param propertyValue {@link String} containing the {@literal value} of the property to set.
|
||||
* @return {@literal this} {@link TestProperties}.
|
||||
* @throws IllegalArgumentException if the property {@link String name} of {@link String value}
|
||||
* are {@literal null}.
|
||||
*/
|
||||
public @Nullable TestProperties set(@NonNull String propertyName, @NonNull String propertyValue) {
|
||||
|
||||
Assert.hasText(propertyName, String.format("PropertyName [%s] must not be null or empty", propertyName));
|
||||
Assert.notNull(propertyValue, "PropertyValue must not be null");
|
||||
|
||||
getTestProperties().setProperty(propertyName, propertyValue);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset the given {@link String named} property.
|
||||
*
|
||||
* @param propertyName {@link String} containing the {@literal name} of the property to unsed.
|
||||
* @return the {@link String value} of the given {@link String named} property
|
||||
* or {@literal null} if the property was undefined.
|
||||
*/
|
||||
public @Nullable String unset(@NonNull String propertyName) {
|
||||
return propertyName != null ? (String) getTestProperties().remove(propertyName) : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,383 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.gemfire.tests.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Integration Tests for {@link TestProperties}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see java.lang.System#getProperties()
|
||||
* @see org.junit.Test
|
||||
* @see org.springframework.data.gemfire.tests.config.TestProperties
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class TestPropertiesIntegrationTests {
|
||||
|
||||
@AfterClass
|
||||
public static void clearSystemProperties() {
|
||||
TestProperties.getInstance().clearSystemProperties();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructTestProperties() {
|
||||
|
||||
TestProperties properties = new TestProperties();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
assertThat(properties.getTestProperties()).isNotNull();
|
||||
assertThat(properties).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructedTestPropertiesIsNotTheSingleton() {
|
||||
|
||||
TestProperties properties = new TestProperties();
|
||||
TestProperties singleProperties = TestProperties.getInstance();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
assertThat(properties).isNotSameAs(singleProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleTestPropertiesInstanceIsSame() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
assertThat(properties).isSameAs(TestProperties.getInstance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTestPropertiesResourceIsCorrect() {
|
||||
|
||||
Resource testPropertiesResource = TestProperties.getInstance().getTestPropertiesResource();
|
||||
|
||||
assertThat(testPropertiesResource).isNotNull();
|
||||
assertThat(testPropertiesResource.getFilename()).isEqualTo(TestProperties.TEST_PROPERTIES_PATH);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isTestSystemPropertyWithSystemProperty() {
|
||||
assertThat(TestProperties.getInstance().isTestSystemProperty("system.property-name")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isTestSystemPropertyWithNullProperty() {
|
||||
assertThat(TestProperties.getInstance().isTestSystemProperty(null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isTestSystemPropertyWithNonSystemProperties() {
|
||||
|
||||
TestProperties testProperties = TestProperties.getInstance();
|
||||
|
||||
assertThat(testProperties.isTestSystemProperty("systemPropertyName")).isFalse();
|
||||
assertThat(testProperties.isTestSystemProperty("sys.prop.name")).isFalse();
|
||||
assertThat(testProperties.isTestSystemProperty("spring.property.name")).isFalse();
|
||||
assertThat(testProperties.isTestSystemProperty("log.property.name")).isFalse();
|
||||
assertThat(testProperties.isTestSystemProperty("property.name")).isFalse();
|
||||
assertThat(testProperties.isTestSystemProperty("propertyName")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toNonNullPropertyNameWithNonNullProperty() {
|
||||
assertThat(TestProperties.getInstance().toNonNullPropertyName("testProperty")).isEqualTo("testProperty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toNonNullPropertyNameWithNullProperty() {
|
||||
assertThat(TestProperties.getInstance().toNonNullPropertyName(null)).isEqualTo("null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toSystemPropertyNameWithSystemProperty() {
|
||||
assertThat(TestProperties.getInstance().toSystemPropertyName("system.property")).isEqualTo("system.property");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toSystemPropertyNameWithNullProperty() {
|
||||
assertThat(TestProperties.getInstance().toSystemPropertyName(null)).isEqualTo("system.null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toSystemPropertyNameWithNonNullProperty() {
|
||||
assertThat(TestProperties.getInstance().toSystemPropertyName("testProperty")).isEqualTo("system.testProperty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearProperties() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
properties.getTestProperties().setProperty("abc", "ONE");
|
||||
properties.getTestProperties().setProperty("xyz", "TWO");
|
||||
|
||||
assertThat(properties).contains("abc", "xyz");
|
||||
|
||||
properties.clear();
|
||||
|
||||
assertThat(properties).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
properties.getTestProperties().setProperty("myProperty", "X");
|
||||
|
||||
assertThat(properties).contains("myProperty");
|
||||
assertThat(properties.get("myProperty")).isEqualTo("X");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNullPropertyIsNullSafe() {
|
||||
assertThat(TestProperties.getInstance().get(null)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSystemProperty() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
properties.getTestProperties().setProperty("system.app.testProperty", "X");
|
||||
|
||||
assertThat(properties).contains("system.app.testProperty");
|
||||
assertThat(properties.get("system.app.testProperty")).isEqualTo("X");
|
||||
assertThat(properties.get("app.testProperty")).isEqualTo("X");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUndefinedProperty() {
|
||||
assertThat(TestProperties.getInstance().get("undefinedProperty")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUndefinedPropertyWithDefaultValue() {
|
||||
assertThat(TestProperties.getInstance().get("undefinedProperty", "TEST"))
|
||||
.isEqualTo("TEST");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requireDefinedProperty() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
properties.getTestProperties().setProperty("definedProperty", "X");
|
||||
|
||||
assertThat(properties).contains("definedProperty");
|
||||
assertThat(properties.requireProperty("definedProperty")).isEqualTo("X");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void requireUndefinedProperty() {
|
||||
|
||||
try {
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
assertThat(properties).doesNotContain("undefinedProperty");
|
||||
|
||||
properties.requireProperty("undefinedProperty");
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
|
||||
assertThat(expected).hasMessage("Property [undefinedProperty] is undefined");
|
||||
assertThat(expected).hasNoCause();
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void requireEmptyProperty() {
|
||||
|
||||
try {
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
properties.getTestProperties().put("emptyProperty", "");
|
||||
|
||||
assertThat(properties).contains("emptyProperty");
|
||||
|
||||
properties.requireProperty("emptyProperty");
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
|
||||
assertThat(expected).hasMessage("Property [emptyProperty] is undefined");
|
||||
assertThat(expected).hasNoCause();
|
||||
|
||||
throw expected;
|
||||
}
|
||||
finally {
|
||||
TestProperties.getInstance().getTestProperties().remove("emptyProperty");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void requireBlankProperty() {
|
||||
|
||||
try {
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
properties.getTestProperties().put("blankProperty", " ");
|
||||
|
||||
assertThat(properties).contains("blankProperty");
|
||||
|
||||
properties.requireProperty("blankProperty");
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
|
||||
assertThat(expected).hasMessage("Property [blankProperty] is undefined");
|
||||
assertThat(expected).hasNoCause();
|
||||
|
||||
throw expected;
|
||||
}
|
||||
finally {
|
||||
TestProperties.getInstance().getTestProperties().remove("blankProperty");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void set() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance()
|
||||
.set("propertyOne", "ONE")
|
||||
.set("propertyTwo", "TWO");
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
assertThat(properties).contains("propertyOne", "propertyTwo");
|
||||
assertThat(properties).contains("propertyOne", "propertyTwo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAndGetProperty() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
assertThat(properties.set("testProperty", "A")).isSameAs(properties);
|
||||
assertThat(properties.get("testProperty")).isEqualTo("A");
|
||||
assertThat(properties.set("testProperty", "B")).isSameAs(properties);
|
||||
assertThat(properties.get("testProperty")).isEqualTo("B");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAndUnsetProperty() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
assertThat(properties.set("mockProperty", "X")).isSameAs(properties);
|
||||
assertThat(properties.get("mockProperty")).isEqualTo("X");
|
||||
assertThat(properties.unset("mockProperty")).isEqualTo("X");
|
||||
assertThat(properties.get("mockProperty")).isNull();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setWithNullPropertyName() {
|
||||
|
||||
try {
|
||||
TestProperties.getInstance().set(null, "TEST");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected).hasMessage("PropertyName [null] must not be null or empty");
|
||||
assertThat(expected).hasNoCause();
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setWithNullPropertyValue() {
|
||||
|
||||
try {
|
||||
TestProperties.getInstance().set("invalidProperty", null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected).hasMessage("PropertyValue must not be null");
|
||||
assertThat(expected).hasNoCause();
|
||||
|
||||
throw expected;
|
||||
}
|
||||
finally {
|
||||
assertThat(TestProperties.getInstance()).doesNotContain("invalidProperty");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stripSystemPropertyPrefix() {
|
||||
|
||||
TestProperties properties = TestProperties.getInstance();
|
||||
|
||||
assertThat(properties.stripSystemPropertyPrefix("testProperty")).isEqualTo("testProperty");
|
||||
assertThat(properties.stripSystemPropertyPrefix("systemProperty")).isEqualTo("systemProperty");
|
||||
assertThat(properties.stripSystemPropertyPrefix("mockProperty")).isEqualTo("mockProperty");
|
||||
assertThat(properties.stripSystemPropertyPrefix("sys.prop")).isEqualTo("sys.prop");
|
||||
assertThat(properties.stripSystemPropertyPrefix("SYSTEM.mockProperty")).isEqualTo("SYSTEM.mockProperty");
|
||||
assertThat(properties.stripSystemPropertyPrefix("system.testProperty")).isEqualTo("testProperty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsetNullPropertyIsNullSafe() {
|
||||
assertThat(TestProperties.getInstance().unset(null)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configureAndClearSystemProperties() {
|
||||
|
||||
try {
|
||||
TestProperties properties = TestProperties.getInstance()
|
||||
.set("system.propOne", "X")
|
||||
.set("propTwo", "Y")
|
||||
.set("system.propThree", "Z")
|
||||
.set("SYSTEM.propFour", "F");
|
||||
|
||||
assertThat(System.getProperties()).doesNotContainKeys("system.propOne", "propOne", "propTwo",
|
||||
"system.propThree", "propThree", "SYSTEM.propFour", "propFour");
|
||||
|
||||
properties.configureSystemProperties();
|
||||
|
||||
assertThat(System.getProperties()).containsKeys("propOne", "propThree");
|
||||
assertThat(System.getProperties())
|
||||
.doesNotContainKeys("system.propOne", "propTwo", "system.propThree", "SYSTEM.propFour", "propFour");
|
||||
assertThat(System.getProperty("propOne")).isEqualTo("X");
|
||||
assertThat(System.getProperty("system.propOne")).isNull();
|
||||
assertThat(System.getProperty("propTwo")).isNull();
|
||||
assertThat(System.getProperty("propThree")).isEqualTo("Z");
|
||||
assertThat(System.getProperty("system.propThree")).isNull();
|
||||
assertThat(System.getProperty("propFour")).isNull();
|
||||
assertThat(System.getProperty("SYSTEM.propFour")).isNull();
|
||||
|
||||
properties.clearSystemProperties();
|
||||
|
||||
assertThat(System.getProperties()).doesNotContainKeys("system.propOne", "propOne", "propTwo",
|
||||
"system.propThree", "propThree", "SYSTEM.propFour", "propFour");
|
||||
}
|
||||
finally {
|
||||
Arrays.asList("propOne", "propThree").forEach(System::clearProperty);
|
||||
assertThat(System.getProperties()).doesNotContainKeys("propOne", "propThree");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.gemfire.tests.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
/**
|
||||
* Integration Tests with {@link TestProperties} having a {@link ClassPathResource}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see java.util.Properties
|
||||
* @see org.junit.Test
|
||||
* @see org.springframework.core.io.Resource
|
||||
* @see org.springframework.core.io.ClassPathResource
|
||||
* @see org.springframework.data.gemfire.tests.config.TestProperties
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class TestPropertiesWithClasspathResourceIntegrationTests {
|
||||
|
||||
private static final TestProperties testProperties = new TestProperties() {
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@Override
|
||||
protected @NonNull Resource getTestPropertiesResource() {
|
||||
return new ClassPathResource("mock-test.properties");
|
||||
}
|
||||
};
|
||||
|
||||
@BeforeClass
|
||||
public static void testPropertiesArePresent() {
|
||||
assertThat(testProperties).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMockTestProperties() {
|
||||
|
||||
assertThat(testProperties)
|
||||
.containsExactlyInAnyOrder("app.testProperty", "gem.mockProperty", "system.gem.testProperty");
|
||||
|
||||
assertThat(testProperties.get("app.testProperty")).isEqualTo("X");
|
||||
assertThat(testProperties.get("gem.mockProperty")).isEqualTo("Y");
|
||||
assertThat(testProperties.get("system.gem.testProperty")).isEqualTo("NO");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setGetAndClearProperties() {
|
||||
|
||||
try {
|
||||
testProperties.set("myProperty", "TEST") // new
|
||||
.set("app.testProperty", "A") // override
|
||||
.set("app.mockProperty", "B") // new
|
||||
.set("system.gem.testProperty", "YES"); // override
|
||||
|
||||
assertThat(testProperties).containsExactlyInAnyOrder("myProperty",
|
||||
"app.testProperty", "app.mockProperty", "gem.mockProperty", "system.gem.testProperty");
|
||||
|
||||
assertThat(testProperties.get("myProperty")).isEqualTo("TEST");
|
||||
assertThat(testProperties.get("app.testProperty")).isEqualTo("A");
|
||||
assertThat(testProperties.get("app.mockProperty")).isEqualTo("B");
|
||||
assertThat(testProperties.get("gem.mockProperty")).isEqualTo("Y");
|
||||
assertThat(testProperties.get("system.gem.testProperty")).isEqualTo("YES");
|
||||
|
||||
testProperties.clear();
|
||||
|
||||
assertThat(testProperties).containsExactlyInAnyOrder("app.testProperty",
|
||||
"gem.mockProperty", "system.gem.testProperty");
|
||||
|
||||
assertThat(testProperties).doesNotContain("myProperty", "app.mockProperty");
|
||||
assertThat(testProperties.get("app.testProperty")).isEqualTo("X");
|
||||
assertThat(testProperties.get("gem.mockProperty")).isEqualTo("Y");
|
||||
assertThat(testProperties.get("system.gem.testProperty")).isEqualTo("NO");
|
||||
}
|
||||
finally {
|
||||
testProperties.getTestProperties().clear();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configureAndClearSystemProperties() {
|
||||
|
||||
try {
|
||||
assertThat(System.getProperties())
|
||||
.doesNotContainKeys("app.testProperty", "gem.mockProperty", "gem.testProperty");
|
||||
|
||||
testProperties.configureSystemProperties();
|
||||
|
||||
assertThat(System.getProperties()).containsKeys("gem.testProperty");
|
||||
assertThat(System.getProperties()).doesNotContainKeys("app.testProperty", "gem.mockProperty");
|
||||
assertThat(System.getProperty("gem.testProperty")).isEqualTo("NO");
|
||||
|
||||
testProperties.clearSystemProperties();
|
||||
|
||||
assertThat(System.getProperties())
|
||||
.doesNotContainKeys("app.testProperty", "gem.mockProperty", "gem.testProperty");
|
||||
}
|
||||
finally {
|
||||
testProperties.forEach(System::clearProperty);
|
||||
assertThat(System.getProperties())
|
||||
.doesNotContainKeys("app.testProperty", "gem.mockProperty", "gem.testProperty");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# Mock test properties
|
||||
|
||||
app.testProperty=X
|
||||
gem.mockProperty=Y
|
||||
system.gem.testProperty=NO
|
||||
Reference in New Issue
Block a user