Commit 601b5e4d authored by Stephane Nicoll's avatar Stephane Nicoll

Replace SystemProperties by TestPropertyValues

Closes gh-9852
parent f0d94a46
......@@ -23,7 +23,7 @@ import java.util.Set;
import org.junit.Test;
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
import org.springframework.boot.cli.testutil.SystemProperties;
import org.springframework.boot.test.util.TestPropertyValues;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -36,55 +36,64 @@ public class RepositoryConfigurationFactoryTests {
@Test
public void defaultRepositories() {
SystemProperties.doWithSystemProperties(() -> {
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
.createDefaultRepositoryConfiguration();
assertRepositoryConfiguration(repositoryConfiguration, "central", "local",
"spring-snapshot", "spring-milestone");
}, "user.home:src/test/resources/maven-settings/basic");
TestPropertyValues.of("user.home:src/test/resources/maven-settings/basic")
.applyToSystemProperties(() -> {
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
.createDefaultRepositoryConfiguration();
assertRepositoryConfiguration(repositoryConfiguration, "central",
"local", "spring-snapshot", "spring-milestone");
return null;
});
}
@Test
public void snapshotRepositoriesDisabled() {
SystemProperties.doWithSystemProperties(() -> {
TestPropertyValues.of("user.home:src/test/resources/maven-settings/basic",
"disableSpringSnapshotRepos:true").applyToSystemProperties(() -> {
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
.createDefaultRepositoryConfiguration();
assertRepositoryConfiguration(repositoryConfiguration, "central", "local");
}, "user.home:src/test/resources/maven-settings/basic",
"disableSpringSnapshotRepos:true");
return null;
});
}
@Test
public void activeByDefaultProfileRepositories() {
SystemProperties.doWithSystemProperties(() -> {
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
.createDefaultRepositoryConfiguration();
assertRepositoryConfiguration(repositoryConfiguration, "central", "local",
"spring-snapshot", "spring-milestone", "active-by-default");
}, "user.home:src/test/resources/maven-settings/active-profile-repositories");
TestPropertyValues
.of("user.home:src/test/resources/maven-settings/active-profile-repositories")
.applyToSystemProperties(() -> {
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
.createDefaultRepositoryConfiguration();
assertRepositoryConfiguration(repositoryConfiguration, "central",
"local", "spring-snapshot", "spring-milestone",
"active-by-default");
return null;
});
}
@Test
public void activeByPropertyProfileRepositories() {
SystemProperties.doWithSystemProperties(() -> {
TestPropertyValues.of("user.home:src/test/resources/maven-settings/active-profile-repositories",
"foo:bar").applyToSystemProperties(() -> {
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
.createDefaultRepositoryConfiguration();
assertRepositoryConfiguration(repositoryConfiguration, "central", "local",
"spring-snapshot", "spring-milestone", "active-by-property");
}, "user.home:src/test/resources/maven-settings/active-profile-repositories",
"foo:bar");
return null;
});
}
@Test
public void interpolationProfileRepositories() {
SystemProperties.doWithSystemProperties(() -> {
TestPropertyValues.of("user.home:src/test/resources/maven-settings/active-profile-repositories",
"interpolate:true").applyToSystemProperties(() -> {
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
.createDefaultRepositoryConfiguration();
assertRepositoryConfiguration(repositoryConfiguration, "central", "local",
"spring-snapshot", "spring-milestone", "interpolate-releases",
"interpolate-snapshots");
}, "user.home:src/test/resources/maven-settings/active-profile-repositories",
"interpolate:true");
return null;
});
}
private void assertRepositoryConfiguration(
......
......@@ -32,7 +32,7 @@ import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.cli.testutil.SystemProperties;
import org.springframework.boot.test.util.TestPropertyValues;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
......@@ -74,12 +74,13 @@ public class SettingsXmlRepositorySystemSessionAutoConfigurationTests {
return new SimpleLocalRepositoryManagerFactory().newInstance(session,
localRepository);
});
SystemProperties.doWithSystemProperties(
() -> new SettingsXmlRepositorySystemSessionAutoConfiguration().apply(
session,
SettingsXmlRepositorySystemSessionAutoConfigurationTests.this.repositorySystem),
"user.home:src/test/resources/maven-settings/property-interpolation",
"foo:bar");
TestPropertyValues.of("user.home:src/test/resources/maven-settings/property-interpolation",
"foo:bar").applyToSystemProperties(() -> {
new SettingsXmlRepositorySystemSessionAutoConfiguration().apply(
session,
SettingsXmlRepositorySystemSessionAutoConfigurationTests.this.repositorySystem);
return null;
});
assertThat(session.getLocalRepository().getBasedir().getAbsolutePath())
.endsWith(File.separatorChar + "bar" + File.separatorChar + "repository");
}
......@@ -87,11 +88,12 @@ public class SettingsXmlRepositorySystemSessionAutoConfigurationTests {
private void assertSessionCustomization(String userHome) {
final DefaultRepositorySystemSession session = MavenRepositorySystemUtils
.newSession();
SystemProperties.doWithSystemProperties(
() -> new SettingsXmlRepositorySystemSessionAutoConfiguration().apply(
session,
SettingsXmlRepositorySystemSessionAutoConfigurationTests.this.repositorySystem),
"user.home:" + userHome);
TestPropertyValues.of("user.home:" + userHome).applyToSystemProperties(() -> {
new SettingsXmlRepositorySystemSessionAutoConfiguration().apply(
session,
SettingsXmlRepositorySystemSessionAutoConfigurationTests.this.repositorySystem);
return null;
});
RemoteRepository repository = new RemoteRepository.Builder("my-server", "default",
"http://maven.example.com").build();
assertMirrorSelectorConfiguration(session, repository);
......
/*
* Copyright 2012-2017 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
*
* http://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.boot.cli.testutil;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* Utilities for working with System properties in unit tests
*
* @author Andy Wilkinson
*/
public final class SystemProperties {
private SystemProperties() {
}
/**
* Performs the given {@code action} with the given system properties set. System
* properties are restored to their previous values once the action has run.
* @param action The action to perform
* @param systemPropertyPairs The system properties, each in the form
* {@code key:value}
*/
public static void doWithSystemProperties(Runnable action,
String... systemPropertyPairs) {
Map<String, String> originalValues = new HashMap<>();
for (String pair : systemPropertyPairs) {
String[] components = pair.split(":");
String key = components[0];
String value = components[1];
originalValues.put(key, System.setProperty(key, value));
}
try {
action.run();
}
finally {
for (Entry<String, String> entry : originalValues.entrySet()) {
if (entry.getValue() == null) {
System.clearProperty(entry.getKey());
}
else {
System.setProperty(entry.getKey(), entry.getValue());
}
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment