diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactoryTests.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactoryTests.java index dae3305c8e..dacd7b9610 100644 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactoryTests.java +++ b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactoryTests.java @@ -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 = 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 = 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 = 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 = 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 = 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 = 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 = 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( diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/SettingsXmlRepositorySystemSessionAutoConfigurationTests.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/SettingsXmlRepositorySystemSessionAutoConfigurationTests.java index 872a2ff89d..8df861acd3 100644 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/SettingsXmlRepositorySystemSessionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/SettingsXmlRepositorySystemSessionAutoConfigurationTests.java @@ -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); diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/testutil/SystemProperties.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/testutil/SystemProperties.java deleted file mode 100644 index f9d8f303b3..0000000000 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/testutil/SystemProperties.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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 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 entry : originalValues.entrySet()) { - if (entry.getValue() == null) { - System.clearProperty(entry.getKey()); - } - else { - System.setProperty(entry.getKey(), entry.getValue()); - } - } - } - } - -}