]> source.dussan.org Git - sonarqube.git/commitdiff
Remove UriReader#openStream()
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 16 Jul 2012 08:05:34 +0000 (10:05 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 16 Jul 2012 08:05:34 +0000 (10:05 +0200)
Let's keep API simple.

sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java
sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java
sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java

index 7b22120fe5771b2337cfe36dd522d899ce53a46a..94e7c80e26f3a9bdf3fa055cff59c218ede7f143 100644 (file)
@@ -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();
index b1c037be15575adea1da2240b59a82bb6fc4edd8..2314036e0bd01b901ba18b8b6e69e43e3ee166ba 100644 (file)
@@ -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();
index c8bc80d83e26dcd7443ab197d28ffb2f02cd0800..c45d8076d21851b02274138a3063d5a26c92ad7c 100644 (file)
@@ -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 !
index b33e77ccd15e9a52a25ee32e2d27e1eb6d32ec9b..2434e84682c7add9da8fc6164014c985fd62a410 100644 (file)
@@ -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]);
index e7407901e765c0aa74f19ce13086ddd46ccd5163..7e99d0c044e17528454b384666954e0f3b2eafb8 100644 (file)
@@ -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;
   }
 }
index 05d49987993b3d47743f7920391d1003ae175ec6..dec99a70f4b51460f129c51b55de4df8139a036a 100644 (file)
@@ -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