From: Simon Brandhof Date: Sat, 6 Apr 2013 14:37:30 +0000 (+0200) Subject: Add unit tests X-Git-Tag: 2.5-rc1~168 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=931d06f7cb02929c87fffbf10e32e67ba9d7277a;p=sonar-scanner-cli.git Add unit tests --- diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/Jars30Test.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/Jars30Test.java new file mode 100644 index 0000000..664e195 --- /dev/null +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/Jars30Test.java @@ -0,0 +1,82 @@ +/* + * Sonar Runner - Implementation + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.runner.impl; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.util.List; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +public class Jars30Test { + + ServerConnection connection = mock(ServerConnection.class); + JarExtractor jarExtractor = mock(JarExtractor.class); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void should_download_jar_files() throws Exception { + File batchJar = temp.newFile("sonar-runner-batch.jar"); + when(jarExtractor.extract("sonar-runner-batch")).thenReturn(batchJar); + // index of the files to download + when(connection.downloadString("/batch/")).thenReturn("cpd.jar,squid.jar"); + + Jars30 jars30 = new Jars30(connection); + List files = jars30.download(temp.newFolder(), jarExtractor); + + assertThat(files).isNotNull(); + verify(connection, times(1)).downloadString("/batch/"); + verify(connection, times(1)).download(eq("/batch/cpd.jar"), any(File.class)); + verify(connection, times(1)).download(eq("/batch/squid.jar"), any(File.class)); + verifyNoMoreInteractions(connection); + } + + @Test + public void should_fail_to_download_files() throws Exception { + File batchJar = temp.newFile("sonar-runner-batch.jar"); + when(jarExtractor.extract("sonar-runner-batch")).thenReturn(batchJar); + // index of files to download + when(connection.downloadString("/batch/")).thenReturn("cpd.jar,squid.jar"); + doThrow(new IllegalStateException()).when(connection).download(eq("/batch/squid.jar"), any(File.class)); + + Jars30 jars30 = new Jars30(connection); + try { + jars30.download(temp.newFolder(), jarExtractor); + fail(); + } catch (RuntimeException e) { + assertThat(e).hasMessage("Fail to download libraries from server"); + } + + } +} diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/Jars35Test.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/Jars35Test.java new file mode 100644 index 0000000..93e1d06 --- /dev/null +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/Jars35Test.java @@ -0,0 +1,93 @@ +/* + * Sonar Runner - Implementation + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.runner.impl; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.home.cache.FileCache; + +import java.io.File; +import java.util.List; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +public class Jars35Test { + + ServerConnection connection = mock(ServerConnection.class); + JarExtractor jarExtractor = mock(JarExtractor.class); + FileCache fileCache = mock(FileCache.class); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void should_download_jar_files() throws Exception { + File batchJar = temp.newFile("sonar-runner-batch.jar"); + when(jarExtractor.extract("sonar-runner-batch")).thenReturn(batchJar); + // index of the files to download + when(connection.downloadString("/batch_bootstrap/index")).thenReturn( + "cpd.jar|CA124VADFSDS" + System.lineSeparator() + + "squid.jar|34535FSFSDF" + System.lineSeparator() + ); + + Jars35 jars35 = new Jars35(fileCache, connection, jarExtractor); + List files = jars35.download(); + + assertThat(files).isNotNull(); + verify(connection, times(1)).downloadString("/batch_bootstrap/index"); + verifyNoMoreInteractions(connection); + verify(fileCache, times(1)).get(eq("cpd.jar"), eq("CA124VADFSDS"), any(FileCache.Downloader.class)); + verify(fileCache, times(1)).get(eq("squid.jar"), eq("34535FSFSDF"), any(FileCache.Downloader.class)); + verifyNoMoreInteractions(fileCache); + } + + @Test + public void should_fail_to_download_files() throws Exception { + File batchJar = temp.newFile("sonar-runner-batch.jar"); + when(jarExtractor.extract("sonar-runner-batch")).thenReturn(batchJar); + // index of the files to download + when(connection.downloadString("/batch_bootstrap/index")).thenThrow(new IllegalStateException()); + + Jars35 jars35 = new Jars35(fileCache, connection, jarExtractor); + try { + jars35.download(); + fail(); + } catch (RuntimeException e) { + assertThat(e).hasMessage("Fail to download libraries from server"); + } + } + + @Test + public void test_jar_downloader() throws Exception { + Jars35.BatchFileDownloader downloader = new Jars35.BatchFileDownloader(connection); + File toFile = temp.newFile(); + downloader.download("squid.jar", toFile); + verify(connection).download("/batch/squid.jar", toFile); + } +}