diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-07-16 10:05:34 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-07-16 10:05:34 +0200 |
commit | c6d22a745bfe5e9b34331ed57a1c64e1d04fce8a (patch) | |
tree | 488b070651d112a2b3b8a0d62a46d796a73961b9 /sonar-server/src | |
parent | 9bf3be187a4893ad424eb09e3cd3c8e7c54f06d6 (diff) | |
download | sonarqube-c6d22a745bfe5e9b34331ed57a1c64e1d04fce8a.tar.gz sonarqube-c6d22a745bfe5e9b34331ed57a1c64e1d04fce8a.zip |
Remove UriReader#openStream()
Let's keep API simple.
Diffstat (limited to 'sonar-server/src')
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java | 22 | ||||
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java | 22 |
2 files changed, 23 insertions, 21 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java index e7407901e76..7e99d0c044e 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java @@ -19,6 +19,7 @@ */ package org.sonar.server.plugins; +import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; import org.slf4j.LoggerFactory; import org.sonar.api.Properties; @@ -30,7 +31,8 @@ import org.sonar.api.utils.UriReader; import org.sonar.updatecenter.common.UpdateCenter; import org.sonar.updatecenter.common.UpdateCenterDeserializer; -import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; import java.net.URI; import java.net.URISyntaxException; import java.util.Date; @@ -95,21 +97,21 @@ public class UpdateCenterClient implements ServerComponent { } private UpdateCenter init() { - InputStream input = null; + Reader reader = null; try { - input = uriReader.openStream(uri); - if (input != null) { - java.util.Properties properties = new java.util.Properties(); - properties.load(input); - return UpdateCenterDeserializer.fromProperties(properties); - } + String content = uriReader.readString(uri, Charsets.UTF_8); + java.util.Properties properties = new java.util.Properties(); + reader = new StringReader(content); + properties.load(reader); + return UpdateCenterDeserializer.fromProperties(properties); + } catch (Exception e) { LoggerFactory.getLogger(getClass()).error("Fail to connect to update center", e); + return null; } finally { - IOUtils.closeQuietly(input); + IOUtils.closeQuietly(reader); } - return null; } } diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java index 05d49987993..dec99a70f4b 100644 --- a/sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java +++ b/sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java @@ -19,7 +19,7 @@ */ package org.sonar.server.plugins; -import org.apache.commons.io.IOUtils; +import com.google.common.base.Charsets; import org.junit.Before; import org.junit.Test; import org.sonar.api.config.Settings; @@ -49,42 +49,42 @@ public class UpdateCenterClientTest { @Test public void downloadUpdateCenter() throws URISyntaxException { - when(reader.openStream(new URI(BASE_URL))).thenReturn(IOUtils.toInputStream("sonar.versions=2.2,2.3")); + when(reader.readString(new URI(BASE_URL), Charsets.UTF_8)).thenReturn("sonar.versions=2.2,2.3"); UpdateCenter center = client.getCenter(); - verify(reader, times(1)).openStream(new URI(BASE_URL)); + verify(reader, times(1)).readString(new URI(BASE_URL), Charsets.UTF_8); assertThat(center.getSonar().getVersions()).containsOnly(Version.create("2.2"), Version.create("2.3")); assertThat(client.getLastRefreshDate()).isNotNull(); } @Test - public void not_available_before_initialization() { - assertThat(client.getLastRefreshDate()).isNull(); - } + public void not_available_before_initialization() { + assertThat(client.getLastRefreshDate()).isNull(); + } @Test public void ignore_connection_errors() { - when(reader.openStream(any(URI.class))).thenThrow(new SonarException()); + when(reader.readString(any(URI.class), eq(Charsets.UTF_8))).thenThrow(new SonarException()); assertThat(client.getCenter()).isNull(); } @Test public void cache_data() throws Exception { - when(reader.openStream(new URI(BASE_URL))).thenReturn(IOUtils.toInputStream("sonar.versions=2.2,2.3")); + when(reader.readString(new URI(BASE_URL), Charsets.UTF_8)).thenReturn("sonar.versions=2.2,2.3"); client.getCenter(); client.getCenter(); - verify(reader, times(1)).openStream(new URI(BASE_URL)); + verify(reader, times(1)).readString(new URI(BASE_URL), Charsets.UTF_8); } @Test public void forceRefresh() throws Exception { - when(reader.openStream(new URI(BASE_URL))).thenReturn(IOUtils.toInputStream("sonar.versions=2.2,2.3")); + when(reader.readString(new URI(BASE_URL), Charsets.UTF_8)).thenReturn("sonar.versions=2.2,2.3"); client.getCenter(); client.getCenter(true); - verify(reader, times(2)).openStream(new URI(BASE_URL)); + verify(reader, times(2)).readString(new URI(BASE_URL), Charsets.UTF_8); } }
\ No newline at end of file |