From f40d89dd7beef93e10fea0d9a8885a7ac78543bd Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sun, 16 Mar 2014 00:08:39 +0100 Subject: Refactor org.sonar.server.plugins.ApplicationDeployer --- .../server/platform/ClassLoaderUtilsTest.java | 131 +++++++++++++++++++++ .../server/platform/RailsAppsDeployerTest.java | 118 +++++++++++++++++++ .../server/plugins/ApplicationDeployerTest.java | 116 ------------------ .../sonar/server/plugins/ClassLoaderUtilsTest.java | 130 -------------------- 4 files changed, 249 insertions(+), 246 deletions(-) create mode 100644 sonar-server/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java create mode 100644 sonar-server/src/test/java/org/sonar/server/platform/RailsAppsDeployerTest.java delete mode 100644 sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java delete mode 100644 sonar-server/src/test/java/org/sonar/server/plugins/ClassLoaderUtilsTest.java (limited to 'sonar-server/src/test/java') diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java new file mode 100644 index 00000000000..400da37c733 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java @@ -0,0 +1,131 @@ +/* + * 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.platform; + +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 org.sonar.server.platform.ClassLoaderUtils; + +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/platform/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar"); + classLoader = new URLClassLoader(new URL[]{jarUrl}, /* no parent classloader */null); + } + + @Test + public void listResources_unknown_root() { + Collection strings = ClassLoaderUtils.listResources(classLoader, "unknown/directory"); + assertThat(strings.size(), Is.is(0)); + } + + @Test + public void listResources_all() { + Collection 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 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 strings = ClassLoaderUtils.listResources(classLoader, "org/sonar/sqale", new Predicate() { + 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 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() { + 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)); + } +} diff --git a/sonar-server/src/test/java/org/sonar/server/platform/RailsAppsDeployerTest.java b/sonar-server/src/test/java/org/sonar/server/platform/RailsAppsDeployerTest.java new file mode 100644 index 00000000000..daec51fbd2f --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/platform/RailsAppsDeployerTest.java @@ -0,0 +1,118 @@ +/* + * 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.platform; + +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.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class RailsAppsDeployerTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void hasRubyRailsApp() throws Exception { + ClassLoader classLoader = new URLClassLoader(new URL[]{ + getClass().getResource("/org/sonar/server/platform/RailsAppsDeployerTest/FakeRubyRailsApp.jar").toURI().toURL()}, null); + + assertTrue(RailsAppsDeployer.hasRailsApp("fake", classLoader)); + assertFalse(RailsAppsDeployer.hasRailsApp("other", classLoader)); + } + + @Test + public void deployRubyRailsApp() throws Exception { + File tempDir = this.temp.getRoot(); + ClassLoader classLoader = new URLClassLoader(new URL[]{ + getClass().getResource("/org/sonar/server/platform/RailsAppsDeployerTest/FakeRubyRailsApp.jar").toURI().toURL()}, null); + + RailsAppsDeployer.deployRailsApp(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.emptyList()); + new RailsAppsDeployer(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 RailsAppsDeployer(fileSystem, mock(PluginRepository.class)).prepareRailsDirectory(); + + 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 RailsAppsDeployer(fileSystem, mock(PluginRepository.class)).prepareRailsDirectory(); + + 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/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.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 strings = ClassLoaderUtils.listResources(classLoader, "unknown/directory"); - assertThat(strings.size(), Is.is(0)); - } - - @Test - public void listResources_all() { - Collection 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 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 strings = ClassLoaderUtils.listResources(classLoader, "org/sonar/sqale", new Predicate() { - 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 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() { - 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)); - } -} -- cgit v1.2.3