diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-04-16 17:51:29 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-04-16 18:38:13 +0200 |
commit | 4e3996e3bdd48f3a3cd7bff1439f86c033a681c0 (patch) | |
tree | 2015ac87b90383fc1d31e6e7e4d639f4b9733319 /sonar-plugin-api/src/test/java/org/sonar | |
parent | b4f20bd7aeb78fceac074f38a2f74db33e6ee6b9 (diff) | |
download | sonarqube-4e3996e3bdd48f3a3cd7bff1439f86c033a681c0.tar.gz sonarqube-4e3996e3bdd48f3a3cd7bff1439f86c033a681c0.zip |
Remove coupling of sonar-plugin-api on Guava 10
in order to prepare upgrade to Guava 18 in core codebase
Diffstat (limited to 'sonar-plugin-api/src/test/java/org/sonar')
3 files changed, 2 insertions, 341 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java index b32de7f475d..71a39897940 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java @@ -19,12 +19,12 @@ */ package org.sonar.api.utils; -import com.google.common.collect.Lists; import org.assertj.core.api.Assertions; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -128,7 +128,7 @@ public class DateUtilsTest { public void shouldBeThreadSafe() throws Exception { final DateUtils.ThreadSafeDateFormat format = new DateUtils.ThreadSafeDateFormat("yyyy-MM-dd'T'HH:mm:ss,S z"); final Date now = new Date(); - final List<Throwable> throwables = Lists.newArrayList(); + final List<Throwable> throwables = new ArrayList<>(); final ThreadGroup tg = new ThreadGroup("shouldBeThreadSafe") { @Override 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 deleted file mode 100644 index 091b157ecfd..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java +++ /dev/null @@ -1,237 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.utils; - -import com.google.common.base.Charsets; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.DisableOnDebug; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; -import org.junit.rules.TestRule; -import org.junit.rules.Timeout; -import org.simpleframework.http.Request; -import org.simpleframework.http.Response; -import org.simpleframework.http.core.Container; -import org.simpleframework.transport.connect.SocketConnection; -import org.sonar.api.config.Settings; -import org.sonar.api.platform.Server; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.InetSocketAddress; -import java.net.Proxy; -import java.net.ProxySelector; -import java.net.SocketAddress; -import java.net.SocketTimeoutException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Arrays; -import java.util.Properties; -import java.util.zip.GZIPOutputStream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class HttpDownloaderTest { - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Rule - public TestRule timeout = new DisableOnDebug(Timeout.seconds(5)); - - private static SocketConnection socketConnection; - private static String baseUrl; - - @BeforeClass - public static void startServer() throws IOException { - socketConnection = new SocketConnection(new Container() { - public void handle(Request req, Response resp) { - try { - if (req.getPath().getPath().contains("/redirect/")) { - resp.setCode(303); - resp.add("Location", "/"); - } - else { - if (req.getPath().getPath().contains("/timeout/")) { - try { - Thread.sleep(500); - } catch (InterruptedException e) { - throw new IllegalStateException(e); - } - } - if (req.getPath().getPath().contains("/gzip/")) { - if (!"gzip".equals(req.getValue("Accept-Encoding"))) { - throw new IllegalStateException("Should accept gzip"); - } - resp.set("Content-Encoding", "gzip"); - GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream()); - gzipOutputStream.write("GZIP response".getBytes()); - gzipOutputStream.close(); - } - else { - resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0)); - } - } - } catch (IOException e) { - throw new IllegalStateException(e); - } finally { - try { - resp.close(); - } catch (IOException ignored) { - } - } - } - }); - SocketAddress address = socketConnection.connect(new InetSocketAddress(0)); - - baseUrl = "http://0.0.0.0:" + ((InetSocketAddress) address).getPort(); - } - - @AfterClass - public static void stopServer() throws IOException { - if (null != socketConnection) { - socketConnection.close(); - } - } - - @Test - public void downloadBytes() throws URISyntaxException { - byte[] bytes = new HttpDownloader(new Settings()).readBytes(new URI(baseUrl)); - assertThat(bytes.length).isGreaterThan(10); - } - - @Test - public void readString() throws URISyntaxException { - String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl), Charsets.UTF_8); - assertThat(text.length()).isGreaterThan(10); - } - - @Test - public void readGzipString() throws URISyntaxException { - String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/gzip/"), Charsets.UTF_8); - assertThat(text).isEqualTo("GZIP response"); - } - - @Test - public void readStringWithDefaultTimeout() throws URISyntaxException { - String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); - assertThat(text.length()).isGreaterThan(10); - } - - @Test - public void readStringWithTimeout() throws URISyntaxException { - thrown.expect(new BaseMatcher<Exception>() { - @Override - public boolean matches(Object ex) { - return ex instanceof SonarException && ((SonarException) ex).getCause() instanceof SocketTimeoutException; - } - - @Override - public void describeTo(Description arg0) { - } - }); - new HttpDownloader(new Settings(), 50).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); - } - - @Test - public void downloadToFile() throws URISyntaxException, IOException { - File toDir = temporaryFolder.newFolder(); - File toFile = new File(toDir, "downloadToFile.txt"); - - new HttpDownloader(new Settings()).download(new URI(baseUrl), toFile); - assertThat(toFile).exists(); - assertThat(toFile.length()).isGreaterThan(10l); - } - - @Test - public void shouldNotCreateFileIfFailToDownload() throws Exception { - File toDir = temporaryFolder.newFolder(); - File toFile = new File(toDir, "downloadToFile.txt"); - - try { - int port = new InetSocketAddress(0).getPort(); - new HttpDownloader(new Settings()).download(new URI("http://localhost:" + port), toFile); - } catch (SonarException e) { - assertThat(toFile).doesNotExist(); - } - } - - @Test - public void userAgentIsSonarVersion() throws URISyntaxException, IOException { - Server server = mock(Server.class); - when(server.getVersion()).thenReturn("2.2"); - - InputStream stream = new HttpDownloader(server, new Settings()).openStream(new URI(baseUrl)); - Properties props = new Properties(); - props.load(stream); - stream.close(); - - assertThat(props.getProperty("agent")).isEqualTo("SonarQube 2.2"); - } - - @Test - public void followRedirect() throws URISyntaxException { - String content = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/redirect/"), Charsets.UTF_8); - assertThat(content).contains("agent"); - } - - @Test - public void shouldGetDirectProxySynthesis() throws URISyntaxException { - ProxySelector proxySelector = mock(ProxySelector.class); - when(proxySelector.select(any(URI.class))).thenReturn(Arrays.asList(Proxy.NO_PROXY)); - assertThat(HttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("no proxy"); - } - - @Test - public void shouldGetProxySynthesis() throws URISyntaxException { - ProxySelector proxySelector = mock(ProxySelector.class); - when(proxySelector.select(any(URI.class))).thenReturn(Arrays.<Proxy>asList(new FakeProxy())); - assertThat(HttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("HTTP proxy: /123.45.67.89:4040"); - } - - @Test - public void supported_schemes() { - assertThat(new HttpDownloader(new Settings()).getSupportedSchemes()).contains("http"); - } - - @Test - public void uri_description() throws URISyntaxException { - String description = new HttpDownloader(new Settings()).description(new URI("http://sonarsource.org")); - assertThat(description).matches("http://sonarsource.org \\(.*\\)"); - } -} - -class FakeProxy extends Proxy { - public FakeProxy() { - super(Type.HTTP, new InetSocketAddress("123.45.67.89", 4040)); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpsTrustTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpsTrustTest.java deleted file mode 100644 index 40fdffbbca3..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpsTrustTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.utils; - -import org.junit.Test; - -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.TrustManager; -import java.io.IOException; -import java.net.URL; -import java.security.KeyManagementException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class HttpsTrustTest { - @Test - public void trustAllHosts() throws Exception { - HttpsURLConnection connection = newHttpsConnection(); - HttpsTrust.INSTANCE.trust(connection); - - assertThat(connection.getHostnameVerifier()).isNotNull(); - assertThat(connection.getHostnameVerifier().verify("foo", null)).isTrue(); - } - - @Test - public void singleHostnameVerifier() throws Exception { - HttpsURLConnection connection1 = newHttpsConnection(); - HttpsTrust.INSTANCE.trust(connection1); - HttpsURLConnection connection2 = newHttpsConnection(); - HttpsTrust.INSTANCE.trust(connection2); - - assertThat(connection1.getHostnameVerifier()).isSameAs(connection2.getHostnameVerifier()); - } - - @Test - public void trustAllCerts() throws Exception { - HttpsURLConnection connection1 = newHttpsConnection(); - HttpsTrust.INSTANCE.trust(connection1); - - assertThat(connection1.getSSLSocketFactory()).isNotNull(); - assertThat(connection1.getSSLSocketFactory().getDefaultCipherSuites()).isNotEmpty(); - } - - @Test - public void singleSslFactory() throws Exception { - HttpsURLConnection connection1 = newHttpsConnection(); - HttpsTrust.INSTANCE.trust(connection1); - HttpsURLConnection connection2 = newHttpsConnection(); - HttpsTrust.INSTANCE.trust(connection2); - - assertThat(connection1.getSSLSocketFactory()).isSameAs(connection2.getSSLSocketFactory()); - } - - @Test - public void testAlwaysTrustManager() throws Exception { - HttpsTrust.AlwaysTrustManager manager = new HttpsTrust.AlwaysTrustManager(); - assertThat(manager.getAcceptedIssuers()).isEmpty(); - // does nothing - manager.checkClientTrusted(null, null); - manager.checkServerTrusted(null, null); - } - - @Test - public void failOnError() throws Exception { - HttpsTrust.Ssl context = mock(HttpsTrust.Ssl.class); - KeyManagementException cause = new KeyManagementException("foo"); - when(context.newFactory(any(TrustManager.class))).thenThrow(cause); - - try { - new HttpsTrust(context); - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to build SSL factory"); - assertThat(e.getCause()).isSameAs(cause); - } - } - - private HttpsURLConnection newHttpsConnection() throws IOException { - return (HttpsURLConnection) new URL("https://localhost").openConnection(); - } -} |