diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-06-29 15:08:28 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-07-04 23:47:46 +0200 |
commit | 875e23e29f4ed2d41146d8bba8b22448b23df113 (patch) | |
tree | e100819edca0bc4c2d75adce8ba760f4fef87059 /sonar-core/src | |
parent | d7f436725b0172217ff8156f1425b4a4a702529c (diff) | |
download | sonarqube-875e23e29f4ed2d41146d8bba8b22448b23df113.tar.gz sonarqube-875e23e29f4ed2d41146d8bba8b22448b23df113.zip |
SONAR-9478 Deprecate Settings and introduce new Configuration interface
Diffstat (limited to 'sonar-core/src')
3 files changed, 45 insertions, 47 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java b/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java index 0044bcbdae6..35c150acd66 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java +++ b/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java @@ -42,14 +42,13 @@ import javax.annotation.Nullable; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.sonar.api.CoreProperties; -import org.sonar.api.config.Settings; +import org.sonar.api.config.Configuration; import org.sonar.api.platform.Server; import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.log.Loggers; import static org.apache.commons.io.FileUtils.copyInputStreamToFile; -import static org.apache.commons.lang.StringUtils.isNotEmpty; import static org.sonar.core.util.FileUtils.deleteQuietly; /** @@ -63,32 +62,32 @@ public class DefaultHttpDownloader extends HttpDownloader { private final Integer readTimeout; private final Integer connectTimeout; - public DefaultHttpDownloader(Server server, Settings settings) { - this(server, settings, null); + public DefaultHttpDownloader(Server server, Configuration config) { + this(server, config, null); } - public DefaultHttpDownloader(Server server, Settings settings, @Nullable Integer readTimeout) { - this(server, settings, null, readTimeout); + public DefaultHttpDownloader(Server server, Configuration config, @Nullable Integer readTimeout) { + this(server, config, null, readTimeout); } - public DefaultHttpDownloader(Server server, Settings settings, @Nullable Integer connectTimeout, @Nullable Integer readTimeout) { + public DefaultHttpDownloader(Server server, Configuration config, @Nullable Integer connectTimeout, @Nullable Integer readTimeout) { this.readTimeout = readTimeout; this.connectTimeout = connectTimeout; - downloader = new BaseHttpDownloader(new AuthenticatorFacade(), settings, server.getVersion()); + downloader = new BaseHttpDownloader(new AuthenticatorFacade(), config, server.getVersion()); } - public DefaultHttpDownloader(Settings settings) { - this(settings, null); + public DefaultHttpDownloader(Configuration config) { + this(config, null); } - public DefaultHttpDownloader(Settings settings, @Nullable Integer readTimeout) { - this(settings, null, readTimeout); + public DefaultHttpDownloader(Configuration config, @Nullable Integer readTimeout) { + this(config, null, readTimeout); } - public DefaultHttpDownloader(Settings settings, @Nullable Integer connectTimeout, @Nullable Integer readTimeout) { + public DefaultHttpDownloader(Configuration config, @Nullable Integer connectTimeout, @Nullable Integer readTimeout) { this.readTimeout = readTimeout; this.connectTimeout = connectTimeout; - downloader = new BaseHttpDownloader(new AuthenticatorFacade(), settings, null); + downloader = new BaseHttpDownloader(new AuthenticatorFacade(), config, null); } @Override @@ -173,22 +172,22 @@ public class DefaultHttpDownloader extends HttpDownloader { private String userAgent; - BaseHttpDownloader(AuthenticatorFacade system, Settings settings, @Nullable String userAgent) { - initProxy(system, settings); - initUserAgent(userAgent, settings); + BaseHttpDownloader(AuthenticatorFacade system, Configuration config, @Nullable String userAgent) { + initProxy(system, config); + initUserAgent(userAgent, config); } - private void initProxy(AuthenticatorFacade system, Settings settings) { + private void initProxy(AuthenticatorFacade system, Configuration config) { // register credentials - String login = settings.getString(HTTP_PROXY_USER); - if (isNotEmpty(login)) { - system.setDefaultAuthenticator(new ProxyAuthenticator(login, settings.getString(HTTP_PROXY_PASSWORD))); + Optional<String> login = config.get(HTTP_PROXY_USER); + if (login.isPresent()) { + system.setDefaultAuthenticator(new ProxyAuthenticator(login.get(), config.get(HTTP_PROXY_PASSWORD).orElse(null))); } } - private void initUserAgent(@Nullable String sonarVersion, Settings settings) { - String serverId = settings.getString(CoreProperties.SERVER_ID); - userAgent = sonarVersion == null ? "SonarQube" : String.format("SonarQube %s # %s", sonarVersion, Optional.ofNullable(serverId).orElse("")); + private void initUserAgent(@Nullable String sonarVersion, Configuration settings) { + Optional<String> serverId = settings.get(CoreProperties.SERVER_ID); + userAgent = sonarVersion == null ? "SonarQube" : String.format("SonarQube %s # %s", sonarVersion, serverId.orElse("")); System.setProperty("http.agent", userAgent); } diff --git a/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java index 4cf76e10ccd..305d83ec6c1 100644 --- a/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java @@ -24,8 +24,8 @@ import java.util.Locale; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.sonar.api.config.MapSettings; import org.sonar.api.config.Settings; +import org.sonar.api.config.internal.MapSettings; import org.sonar.api.i18n.I18n; import static org.mockito.Matchers.anyString; diff --git a/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java index 77fa075d13f..090abdb8be3 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java @@ -54,8 +54,7 @@ import org.simpleframework.http.Response; import org.simpleframework.http.core.Container; import org.simpleframework.transport.connect.SocketConnection; import org.sonar.api.CoreProperties; -import org.sonar.api.config.MapSettings; -import org.sonar.api.config.Settings; +import org.sonar.api.config.internal.MapSettings; import org.sonar.api.platform.Server; import org.sonar.api.utils.SonarException; @@ -141,41 +140,41 @@ public class DefaultHttpDownloaderTest { @Override public boolean matches(Object ex) { return - // Java 8 - ex instanceof NoRouteToHostException || ex instanceof SocketException - // Java 7 or before - || ex instanceof SocketTimeoutException; + // Java 8 + ex instanceof NoRouteToHostException || ex instanceof SocketException + // Java 7 or before + || ex instanceof SocketTimeoutException; } @Override public void describeTo(Description arg0) { } })); - DefaultHttpDownloader downloader = new DefaultHttpDownloader(new MapSettings(), 10, 50000); + DefaultHttpDownloader downloader = new DefaultHttpDownloader(new MapSettings().asConfig(), 10, 50000); downloader.openStream(new URI(url)); } @Test public void downloadBytes() throws URISyntaxException { - byte[] bytes = new DefaultHttpDownloader(new MapSettings()).readBytes(new URI(baseUrl)); + byte[] bytes = new DefaultHttpDownloader(new MapSettings().asConfig()).readBytes(new URI(baseUrl)); assertThat(bytes.length).isGreaterThan(10); } @Test public void readString() throws URISyntaxException { - String text = new DefaultHttpDownloader(new MapSettings()).readString(new URI(baseUrl), StandardCharsets.UTF_8); + String text = new DefaultHttpDownloader(new MapSettings().asConfig()).readString(new URI(baseUrl), StandardCharsets.UTF_8); assertThat(text.length()).isGreaterThan(10); } @Test public void readGzipString() throws URISyntaxException { - String text = new DefaultHttpDownloader(new MapSettings()).readString(new URI(baseUrl + "/gzip/"), StandardCharsets.UTF_8); + String text = new DefaultHttpDownloader(new MapSettings().asConfig()).readString(new URI(baseUrl + "/gzip/"), StandardCharsets.UTF_8); assertThat(text).isEqualTo("GZIP response"); } @Test public void readStringWithDefaultTimeout() throws URISyntaxException { - String text = new DefaultHttpDownloader(new MapSettings()).readString(new URI(baseUrl + "/timeout/"), StandardCharsets.UTF_8); + String text = new DefaultHttpDownloader(new MapSettings().asConfig()).readString(new URI(baseUrl + "/timeout/"), StandardCharsets.UTF_8); assertThat(text.length()).isGreaterThan(10); } @@ -191,7 +190,7 @@ public class DefaultHttpDownloaderTest { public void describeTo(Description arg0) { } }); - new DefaultHttpDownloader(new MapSettings(), 50).readString(new URI(baseUrl + "/timeout/"), StandardCharsets.UTF_8); + new DefaultHttpDownloader(new MapSettings().asConfig(), 50).readString(new URI(baseUrl + "/timeout/"), StandardCharsets.UTF_8); } @Test @@ -199,7 +198,7 @@ public class DefaultHttpDownloaderTest { File toDir = temporaryFolder.newFolder(); File toFile = new File(toDir, "downloadToFile.txt"); - new DefaultHttpDownloader(new MapSettings()).download(new URI(baseUrl), toFile); + new DefaultHttpDownloader(new MapSettings().asConfig()).download(new URI(baseUrl), toFile); assertThat(toFile).exists(); assertThat(toFile.length()).isGreaterThan(10l); } @@ -211,7 +210,7 @@ public class DefaultHttpDownloaderTest { try { int port = new InetSocketAddress("localhost", 0).getPort(); - new DefaultHttpDownloader(new MapSettings()).download(new URI("http://localhost:" + port), toFile); + new DefaultHttpDownloader(new MapSettings().asConfig()).download(new URI("http://localhost:" + port), toFile); } catch (SonarException e) { assertThat(toFile).doesNotExist(); } @@ -224,7 +223,7 @@ public class DefaultHttpDownloaderTest { MapSettings settings = new MapSettings(); settings.setProperty(CoreProperties.SERVER_ID, "blablabla"); - InputStream stream = new DefaultHttpDownloader(server, settings).openStream(new URI(baseUrl)); + InputStream stream = new DefaultHttpDownloader(server, settings.asConfig()).openStream(new URI(baseUrl)); Properties props = new Properties(); props.load(stream); stream.close(); @@ -237,7 +236,7 @@ public class DefaultHttpDownloaderTest { Server server = mock(Server.class); when(server.getVersion()).thenReturn("2.2"); - InputStream stream = new DefaultHttpDownloader(server, new MapSettings()).openStream(new URI(baseUrl)); + InputStream stream = new DefaultHttpDownloader(server, new MapSettings().asConfig()).openStream(new URI(baseUrl)); Properties props = new Properties(); props.load(stream); stream.close(); @@ -247,7 +246,7 @@ public class DefaultHttpDownloaderTest { @Test public void userAgent_is_static_value_when_server_is_not_provided() throws URISyntaxException, IOException { - InputStream stream = new DefaultHttpDownloader(new MapSettings()).openStream(new URI(baseUrl)); + InputStream stream = new DefaultHttpDownloader(new MapSettings().asConfig()).openStream(new URI(baseUrl)); Properties props = new Properties(); props.load(stream); stream.close(); @@ -257,7 +256,7 @@ public class DefaultHttpDownloaderTest { @Test public void followRedirect() throws URISyntaxException { - String content = new DefaultHttpDownloader(new MapSettings()).readString(new URI(baseUrl + "/redirect/"), StandardCharsets.UTF_8); + String content = new DefaultHttpDownloader(new MapSettings().asConfig()).readString(new URI(baseUrl + "/redirect/"), StandardCharsets.UTF_8); assertThat(content).contains("agent"); } @@ -277,24 +276,24 @@ public class DefaultHttpDownloaderTest { @Test public void supported_schemes() { - assertThat(new DefaultHttpDownloader(new MapSettings()).getSupportedSchemes()).contains("http"); + assertThat(new DefaultHttpDownloader(new MapSettings().asConfig()).getSupportedSchemes()).contains("http"); } @Test public void uri_description() throws URISyntaxException { - String description = new DefaultHttpDownloader(new MapSettings()).description(new URI("http://sonarsource.org")); + String description = new DefaultHttpDownloader(new MapSettings().asConfig()).description(new URI("http://sonarsource.org")); assertThat(description).matches("http://sonarsource.org \\(.*\\)"); } @Test public void configure_http_proxy_credentials() { DefaultHttpDownloader.AuthenticatorFacade system = mock(DefaultHttpDownloader.AuthenticatorFacade.class); - Settings settings = new MapSettings(); + MapSettings settings = new MapSettings(); settings.setProperty("https.proxyHost", "1.2.3.4"); settings.setProperty("http.proxyUser", "the_login"); settings.setProperty("http.proxyPassword", "the_passwd"); - new DefaultHttpDownloader.BaseHttpDownloader(system, settings, null); + new DefaultHttpDownloader.BaseHttpDownloader(system, settings.asConfig(), null); verify(system).setDefaultAuthenticator(argThat(new TypeSafeMatcher<Authenticator>() { @Override |