Browse Source

Remove UriReader#openStream()

Let's keep API simple.
tags/3.2
Simon Brandhof 12 years ago
parent
commit
c6d22a745b

+ 0
- 11
sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java View 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();

+ 0
- 18
sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java View 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();

+ 0
- 8
sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java View 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 !

+ 0
- 15
sonar-plugin-api/src/test/java/org/sonar/api/utils/UriReaderTest.java View 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]);

+ 12
- 10
sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java View 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;
}
}

+ 11
- 11
sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java View 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);
}
}

Loading…
Cancel
Save