diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-03-16 00:08:39 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-03-16 00:08:39 +0100 |
commit | f40d89dd7beef93e10fea0d9a8885a7ac78543bd (patch) | |
tree | 6d51966410f64a79f5c115fda4f0a1c85e057fec /sonar-server/src/test/java/org/sonar/server/plugins | |
parent | 0703df654d4166a553f9b5f6d392341a116e2e3f (diff) | |
download | sonarqube-f40d89dd7beef93e10fea0d9a8885a7ac78543bd.tar.gz sonarqube-f40d89dd7beef93e10fea0d9a8885a7ac78543bd.zip |
Refactor org.sonar.server.plugins.ApplicationDeployer
Diffstat (limited to 'sonar-server/src/test/java/org/sonar/server/plugins')
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java | 116 | ||||
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/plugins/ClassLoaderUtilsTest.java | 130 |
2 files changed, 0 insertions, 246 deletions
diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java deleted file mode 100644 index 3c5728ef582..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java +++ /dev/null @@ -1,116 +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.server.plugins; - -import org.apache.commons.io.FileUtils; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.platform.PluginMetadata; -import org.sonar.api.platform.PluginRepository; -import org.sonar.api.platform.ServerFileSystem; - -import java.io.File; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Collections; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class ApplicationDeployerTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Test - public void hasRubyRailsApp() throws Exception { - ClassLoader classLoader = new URLClassLoader(new URL[]{ - getClass().getResource("/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar").toURI().toURL()}, null); - - assertTrue(ApplicationDeployer.hasRubyRailsApp("fake", classLoader)); - assertFalse(ApplicationDeployer.hasRubyRailsApp("other", classLoader)); - } - - @Test - public void deployRubyRailsApp() throws Exception { - File tempDir = this.temp.getRoot(); - ClassLoader classLoader = new URLClassLoader(new URL[]{ - getClass().getResource("/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar").toURI().toURL()}, null); - - ApplicationDeployer.deployRubyRailsApp(tempDir, "fake", classLoader); - - File appDir = new File(tempDir, "fake"); - assertThat(appDir.isDirectory(), is(true)); - assertThat(appDir.exists(), is(true)); - assertThat(FileUtils.listFiles(appDir, null, true).size(), is(3)); - assertThat(new File(appDir, "init.rb").exists(), is(true)); - assertThat(new File(appDir, "app/controllers/fake_controller.rb").exists(), is(true)); - assertThat(new File(appDir, "app/views/fake/index.html.erb").exists(), is(true)); - } - - @Test - public void deployRubyRailsApps_no_apps() throws Exception { - ServerFileSystem fileSystem = mock(ServerFileSystem.class); - File tempDir = this.temp.getRoot(); - when(fileSystem.getTempDir()).thenReturn(tempDir); - - PluginRepository pluginRepository = mock(PluginRepository.class); - when(pluginRepository.getMetadata()).thenReturn(Collections.<PluginMetadata>emptyList()); - new ApplicationDeployer(fileSystem, pluginRepository).start(); - - File appDir = new File(tempDir, "ror"); - assertThat(appDir.isDirectory(), is(true)); - assertThat(appDir.exists(), is(true)); - assertThat(FileUtils.listFiles(appDir, null, true).size(), is(0)); - } - - @Test - public void prepareRubyRailsRootDirectory() throws Exception { - ServerFileSystem fileSystem = mock(ServerFileSystem.class); - File tempDir = this.temp.getRoot(); - when(fileSystem.getTempDir()).thenReturn(tempDir); - - File dir = new ApplicationDeployer(fileSystem, mock(PluginRepository.class)).prepareRubyRailsRootDirectory(); - - assertThat(dir.isDirectory(), is(true)); - assertThat(dir.exists(), is(true)); - assertThat(dir.getCanonicalPath(), is(new File(tempDir, "ror").getCanonicalPath())); - } - - @Test - public void prepareRubyRailsRootDirectory_delete_existing_dir() throws Exception { - ServerFileSystem fileSystem = mock(ServerFileSystem.class); - File tempDir = this.temp.getRoot(); - when(fileSystem.getTempDir()).thenReturn(tempDir); - - File file = new File(tempDir, "ror/foo/bar.txt"); - FileUtils.writeStringToFile(file, "foooo"); - - File dir = new ApplicationDeployer(fileSystem, mock(PluginRepository.class)).prepareRubyRailsRootDirectory(); - - assertThat(dir.isDirectory(), is(true)); - assertThat(dir.exists(), is(true)); - assertThat(dir.getCanonicalPath(), is(new File(tempDir, "ror").getCanonicalPath())); - assertThat(FileUtils.listFiles(new File(tempDir, "ror"), null, true).size(), is(0)); - } -} diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/ClassLoaderUtilsTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/ClassLoaderUtilsTest.java deleted file mode 100644 index 21ef3d6c3f8..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/plugins/ClassLoaderUtilsTest.java +++ /dev/null @@ -1,130 +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.server.plugins; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.FilenameUtils; -import org.apache.commons.lang.StringUtils; -import org.hamcrest.core.Is; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -import javax.annotation.Nullable; -import java.io.File; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Collection; - -import static org.junit.Assert.assertThat; -import static org.junit.matchers.JUnitMatchers.hasItems; - -public class ClassLoaderUtilsTest { - - private ClassLoader classLoader; - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Before - public void prepareClassLoader() { - // This JAR file has the three following files : - // org/sonar/sqale/app/copyright.txt - // org/sonar/sqale/app/README.md - // org/sonar/other/other.txt - URL jarUrl = getClass().getResource("/org/sonar/server/plugins/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar"); - classLoader = new URLClassLoader(new URL[]{jarUrl}, /* no parent classloader */null); - } - - @Test - public void listResources_unknown_root() { - Collection<String> strings = ClassLoaderUtils.listResources(classLoader, "unknown/directory"); - assertThat(strings.size(), Is.is(0)); - } - - @Test - public void listResources_all() { - Collection<String> strings = ClassLoaderUtils.listResources(classLoader, "org/sonar/sqale"); - assertThat(strings, hasItems( - "org/sonar/sqale/", - "org/sonar/sqale/app/", - "org/sonar/sqale/app/copyright.txt", - "org/sonar/sqale/app/README.md")); - assertThat(strings.size(), Is.is(4)); - } - - @Test - public void listResources_root_path_starts_with_slash() { - Collection<String> strings = ClassLoaderUtils.listResources(classLoader, "/org/sonar/sqale"); - assertThat(strings, hasItems( - "org/sonar/sqale/", - "org/sonar/sqale/app/", - "org/sonar/sqale/app/copyright.txt", - "org/sonar/sqale/app/README.md")); - assertThat(strings.size(), Is.is(4)); - } - - @Test - public void listResources_use_predicate() { - Collection<String> strings = ClassLoaderUtils.listResources(classLoader, "org/sonar/sqale", new Predicate<String>() { - public boolean apply(@Nullable String s) { - return StringUtils.endsWith(s, "md"); - } - }); - assertThat(strings.size(), Is.is(1)); - assertThat(strings, hasItems("org/sonar/sqale/app/README.md")); - } - - @Test - public void listFiles() { - Collection<String> strings = ClassLoaderUtils.listFiles(classLoader, "org/sonar/sqale"); - assertThat(strings, hasItems( - "org/sonar/sqale/app/copyright.txt", - "org/sonar/sqale/app/README.md")); - assertThat(strings.size(), Is.is(2)); - } - - @Test - public void copyRubyRailsApp() { - File toDir = temp.newFolder("dest"); - ClassLoaderUtils.copyResources(classLoader, "org/sonar/sqale", toDir); - - assertThat(FileUtils.listFiles(toDir, null, true).size(), Is.is(2)); - assertThat(new File(toDir, "org/sonar/sqale/app/copyright.txt").exists(), Is.is(true)); - assertThat(new File(toDir, "org/sonar/sqale/app/README.md").exists(), Is.is(true)); - } - - @Test - public void copyRubyRailsApp_relocate_files() { - File toDir = temp.newFolder("dest"); - ClassLoaderUtils.copyResources(classLoader, "org/sonar/sqale", toDir, new Function<String, String>() { - public String apply(@Nullable String path) { - return "foo/" + FilenameUtils.getName(path); - } - }); - - assertThat(FileUtils.listFiles(toDir, null, true).size(), Is.is(2)); - assertThat(new File(toDir, "foo/copyright.txt").exists(), Is.is(true)); - assertThat(new File(toDir, "foo/README.md").exists(), Is.is(true)); - } -} |