From c6d22a745bfe5e9b34331ed57a1c64e1d04fce8a Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 16 Jul 2012 10:05:34 +0200 Subject: [PATCH] Remove UriReader#openStream() Let's keep API simple. --- .../org/sonar/api/utils/HttpDownloader.java | 11 ---------- .../java/org/sonar/api/utils/UriReader.java | 18 --------------- .../sonar/api/utils/HttpDownloaderTest.java | 8 ------- .../org/sonar/api/utils/UriReaderTest.java | 15 ------------- .../server/plugins/UpdateCenterClient.java | 22 ++++++++++--------- .../plugins/UpdateCenterClientTest.java | 22 +++++++++---------- 6 files changed, 23 insertions(+), 73 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java index 7b22120fe57..94e7c80e26f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java @@ -185,17 +185,6 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo return downloadPlainText(uri, charset.name()); } - @Override - InputStream openStream(URI uri) { - try { - HttpURLConnection connection = newHttpConnection(uri); - return connection.getInputStream(); - - } catch (Exception e) { - throw new SonarException("Fail to download the file: " + uri + " (" + getProxySynthesis(uri) + ")", e); - } - } - private HttpURLConnection newHttpConnection(URI uri) throws IOException { LoggerFactory.getLogger(getClass()).debug("Download: " + uri + " (" + getProxySynthesis(uri) + ")"); HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java index b1c037be155..2314036e0bd 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java @@ -70,13 +70,6 @@ public class UriReader implements BatchComponent, ServerComponent { return searchForSupportedProcessor(uri).readString(uri, charset); } - /** - * Opens an input stream over the given uri. - */ - public InputStream openStream(URI uri) { - return searchForSupportedProcessor(uri).openStream(uri); - } - /** * Returns a detailed description of the given uri. For example HTTP URIs are completed * with the configured HTTP proxy. @@ -101,8 +94,6 @@ public class UriReader implements BatchComponent, ServerComponent { abstract String readString(URI uri, Charset charset); - abstract InputStream openStream(URI uri); - abstract String description(URI uri); } @@ -135,15 +126,6 @@ public class UriReader implements BatchComponent, ServerComponent { } } - @Override - InputStream openStream(URI uri) { - try { - return Files.newInputStreamSupplier(new File(uri)).getInput(); - } catch (IOException e) { - throw Throwables.propagate(e); - } - } - @Override String description(URI uri) { return new File(uri).getAbsolutePath(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java index c8bc80d83e2..c45d8076d21 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java @@ -32,7 +32,6 @@ import org.sonar.api.platform.Server; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.net.*; import java.util.Arrays; import java.util.Properties; @@ -172,13 +171,6 @@ public class HttpDownloaderTest { assertThat(text.length()).isGreaterThan(10); } - @Test - public void openStream() throws Exception { - InputStream input = new HttpDownloader(new Settings()).openStream(new URI(baseUrl)); - assertThat(IOUtils.toByteArray(input).length).isGreaterThan(10); - IOUtils.closeQuietly(input); - } - @Test(expected = SonarException.class) public void failIfServerDown() throws Exception { // I hope that the port 1 is not used ! diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java index b33e77ccd15..2434e84682c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java @@ -65,14 +65,6 @@ public class UriReaderTest { assertThat(new String(uriReader.readBytes(testFile))).isEqualTo("in foo"); } - @Test - public void file_openStream() throws Exception { - UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]); - InputStream input = uriReader.openStream(testFile); - assertThat(IOUtils.toString(input)).isEqualTo("in foo"); - IOUtils.closeQuietly(input); - } - @Test public void file_readString_fails_if_file_not_found() throws Exception { thrown.expect(RuntimeException.class); @@ -87,13 +79,6 @@ public class UriReaderTest { uriReader.readBytes(new URI("file:/notfound")); } - @Test - public void file_openStream_fails_if_file_not_found() throws Exception { - thrown.expect(RuntimeException.class); - UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]); - uriReader.openStream(new URI("file:/notfound")); - } - @Test public void file_description() { UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]); 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 -- 2.39.5