--- /dev/null
+/*
+ * 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.*;
+import com.google.common.collect.Lists;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.CharEncoding;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+/**
+ * @since 3.0
+ */
+class ClassLoaderUtils {
+
+ private ClassLoaderUtils() {
+ }
+
+ static File copyResources(ClassLoader classLoader, String rootPath, File toDir) {
+ return copyResources(classLoader, rootPath, toDir, Functions.<String>identity());
+ }
+
+ static File copyResources(ClassLoader classLoader, String rootPath, File toDir, Function<String, String> relocationFunction) {
+ Collection<String> relativePaths = listFiles(classLoader, rootPath);
+ for (String relativePath : relativePaths) {
+ URL resource = classLoader.getResource(relativePath);
+ String filename = relocationFunction.apply(relativePath);
+ File toFile = new File(toDir, filename);
+ try {
+ FileUtils.copyURLToFile(resource, toFile);
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to extract " + relativePath + " to " + toFile.getAbsolutePath(), e);
+ }
+ }
+
+ return toDir;
+ }
+
+ /**
+ * Finds files within a given directory and its subdirectories
+ *
+ * @param classLoader
+ * @param rootPath the root directory, for example org/sonar/sqale
+ * @return a list of relative paths, for example {"org/sonar/sqale/foo/bar.txt}. Never null.
+ */
+ static Collection<String> listFiles(ClassLoader classLoader, String rootPath) {
+ return listResources(classLoader, rootPath, new Predicate<String>() {
+ public boolean apply(@Nullable String path) {
+ return !StringUtils.endsWith(path, "/");
+ }
+ });
+ }
+
+
+ static Collection<String> listResources(ClassLoader classLoader, String rootPath) {
+ return listResources(classLoader, rootPath, Predicates.<String>alwaysTrue());
+ }
+
+ /**
+ * Finds directories and files within a given directory and its subdirectories.
+ *
+ * @param classLoader
+ * @param rootPath the root directory, for example org/sonar/sqale, or a file in this root directory, for example org/sonar/sqale/index.txt
+ * @param
+ * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null.
+ */
+ static Collection<String> listResources(ClassLoader classLoader, String rootPath, Predicate<String> predicate) {
+ String jarPath = null;
+ JarFile jar = null;
+ try {
+ Collection<String> paths = Lists.newArrayList();
+ rootPath = StringUtils.removeStart(rootPath, "/");
+
+ URL root = classLoader.getResource(rootPath);
+ if (root != null) {
+ checkJarFile(root);
+
+ // Path of the root directory
+ // Examples :
+ // org/sonar/sqale/index.txt -> rootDirectory is org/sonar/sqale
+ // org/sonar/sqale/ -> rootDirectory is org/sonar/sqale
+ // org/sonar/sqale -> rootDirectory is org/sonar/sqale
+ String rootDirectory = rootPath;
+ if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) {
+ rootDirectory = StringUtils.substringBeforeLast(rootPath, "/");
+ }
+ //strip out only the JAR file
+ jarPath = root.getPath().substring(5, root.getPath().indexOf("!"));
+ jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8));
+ Enumeration<JarEntry> entries = jar.entries();
+ while (entries.hasMoreElements()) {
+ String name = entries.nextElement().getName();
+ if (name.startsWith(rootDirectory) && predicate.apply(name)) {
+ paths.add(name);
+ }
+ }
+ }
+ return paths;
+ } catch (Exception e) {
+ throw Throwables.propagate(e);
+ } finally {
+ closeJar(jar, jarPath);
+ }
+ }
+
+ private static void closeJar(JarFile jar, String jarPath) {
+ if (jar != null) {
+ try {
+ jar.close();
+ } catch (Exception e) {
+ LoggerFactory.getLogger(ClassLoaderUtils.class).error("Fail to close JAR file: " + jarPath, e);
+ }
+ }
+ }
+
+ private static void checkJarFile(URL root) {
+ if (!"jar".equals(root.getProtocol())) {
+ throw new IllegalStateException("Unsupported protocol: " + root.getProtocol());
+ }
+ }
+}
import org.sonar.server.es.ESNode;
import org.sonar.server.platform.ws.PlatformWs;
import org.sonar.server.platform.ws.RestartHandler;
-import org.sonar.server.plugins.ApplicationDeployer;
import org.sonar.server.plugins.DefaultServerPluginRepository;
import org.sonar.server.plugins.InstalledPluginReferentialFactory;
import org.sonar.server.plugins.PluginDeployer;
level1Container.addSingleton(InstalledPluginReferentialFactory.class);
level1Container.addSingleton(DefaultServerPluginRepository.class);
level1Container.addSingleton(DefaultServerFileSystem.class);
- level1Container.addSingleton(ApplicationDeployer.class);
+ level1Container.addSingleton(RailsAppsDeployer.class);
level1Container.addSingleton(JRubyI18n.class);
level1Container.addSingleton(DefaultI18n.class);
level1Container.addSingleton(RuleI18nManager.class);
--- /dev/null
+/*
+ * 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.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.platform.PluginMetadata;
+import org.sonar.api.platform.PluginRepository;
+import org.sonar.api.platform.ServerFileSystem;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Ruby on Rails requires the files to be on filesystem but not in Java classpath (JAR). This component extracts
+ * all the needed files from plugins and copy them to $SONAR_HOME/temp
+ *
+ * @since 3.0
+ */
+public class RailsAppsDeployer {
+ private static final Logger LOG = LoggerFactory.getLogger(RailsAppsDeployer.class);
+ private static final String ROR_PATH = "org/sonar/ror/";
+
+ private final ServerFileSystem fileSystem;
+ private final PluginRepository pluginRepository;
+
+ public RailsAppsDeployer(ServerFileSystem fileSystem, PluginRepository pluginRepository) {
+ this.fileSystem = fileSystem;
+ this.pluginRepository = pluginRepository;
+ }
+
+ public void start() {
+ LOG.info("Deploy Ruby on Rails applications");
+ File appsDir = prepareRailsDirectory();
+
+ for (PluginMetadata pluginMetadata : pluginRepository.getMetadata()) {
+ String pluginKey = pluginMetadata.getKey();
+ try {
+ deployRailsApp(appsDir, pluginKey, pluginRepository.getPlugin(pluginKey).getClass().getClassLoader());
+ } catch (Exception e) {
+ throw new IllegalStateException("Fail to deploy Ruby on Rails application: " + pluginKey, e);
+ }
+ }
+ }
+
+ @VisibleForTesting
+ File prepareRailsDirectory() {
+ File appsDir = new File(fileSystem.getTempDir(), "ror");
+ prepareDir(appsDir);
+ return appsDir;
+ }
+
+ @VisibleForTesting
+ static void deployRailsApp(File appsDir, final String pluginKey, ClassLoader appClassLoader) {
+ if (hasRailsApp(pluginKey, appClassLoader)) {
+ LOG.info("Deploy app: " + pluginKey);
+ File appDir = new File(appsDir, pluginKey);
+ ClassLoaderUtils.copyResources(appClassLoader, pathToRubyInitFile(pluginKey), appDir, new Function<String, String>() {
+ public String apply(@Nullable String relativePath) {
+ // Relocate the deployed files :
+ // relativePath format is: org/sonar/ror/sqale/app/controllers/foo_controller.rb
+ // app path is: org/sonar/ror/sqale
+ // -> deployed file is app/controllers/foo_controller.rb
+ return StringUtils.substringAfter(relativePath, pluginKey + "/");
+ }
+ });
+ }
+ }
+
+ private static String pathToRubyInitFile(String pluginKey) {
+ return ROR_PATH + pluginKey + "/init.rb";
+ }
+
+ @VisibleForTesting
+ static boolean hasRailsApp(String pluginKey, ClassLoader classLoader) {
+ return classLoader.getResource(pathToRubyInitFile(pluginKey)) != null;
+ }
+
+ private void prepareDir(File appsDir) {
+ if (appsDir.exists() && appsDir.isDirectory()) {
+ try {
+ FileUtils.deleteDirectory(appsDir);
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to delete temp directory: " + appsDir, e);
+ }
+ }
+ try {
+ FileUtils.forceMkdir(appsDir);
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to create temp directory: " + appsDir, e);
+ }
+ }
+}
+++ /dev/null
-/*
- * 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.annotations.VisibleForTesting;
-import com.google.common.base.Function;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.platform.PluginMetadata;
-import org.sonar.api.platform.PluginRepository;
-import org.sonar.api.platform.ServerFileSystem;
-
-import javax.annotation.Nullable;
-
-import java.io.File;
-import java.io.IOException;
-
-/**
- * Ruby on Rails requires the files to be on filesystem but not in Java classpath (JAR). This component extracts
- * all the needed files from plugins and copy them to $SONAR_HOME/temp
- *
- * @since 3.0
- */
-public class ApplicationDeployer {
- private static final Logger LOG = LoggerFactory.getLogger(ApplicationDeployer.class);
- private static final String ROR_PATH = "org/sonar/ror/";
-
- private final ServerFileSystem fileSystem;
- private final PluginRepository pluginRepository;
-
- public ApplicationDeployer(ServerFileSystem fileSystem, PluginRepository pluginRepository) {
- this.fileSystem = fileSystem;
- this.pluginRepository = pluginRepository;
- }
-
- public void start() {
- deployRubyRailsApps();
- }
-
- private void deployRubyRailsApps() {
- LOG.info("Deploy Ruby on Rails applications");
- File appsDir = prepareRubyRailsRootDirectory();
-
- for (PluginMetadata pluginMetadata : pluginRepository.getMetadata()) {
- String pluginKey = pluginMetadata.getKey();
- try {
- deployRubyRailsApp(appsDir, pluginKey, pluginRepository.getPlugin(pluginKey).getClass().getClassLoader());
- } catch (Exception e) {
- throw new IllegalStateException("Fail to deploy Ruby on Rails application: " + pluginKey, e);
- }
- }
- }
-
- @VisibleForTesting
- File prepareRubyRailsRootDirectory() {
- File appsDir = new File(fileSystem.getTempDir(), "ror");
- prepareDir(appsDir);
- return appsDir;
- }
-
- @VisibleForTesting
- static void deployRubyRailsApp(File appsDir, final String pluginKey, ClassLoader appClassLoader) {
- if (hasRubyRailsApp(pluginKey, appClassLoader)) {
- LOG.info("Deploy app: " + pluginKey);
- File appDir = new File(appsDir, pluginKey);
- ClassLoaderUtils.copyResources(appClassLoader, pathToRubyInitFile(pluginKey), appDir, new Function<String, String>() {
- public String apply(@Nullable String relativePath) {
- // Relocate the deployed files :
- // relativePath format is: org/sonar/ror/sqale/app/controllers/foo_controller.rb
- // app path is: org/sonar/ror/sqale
- // -> deployed file is app/controllers/foo_controller.rb
- return StringUtils.substringAfter(relativePath, pluginKey + "/");
- }
- });
- }
- }
-
- private static String pathToRubyInitFile(String pluginKey) {
- return ROR_PATH + pluginKey + "/init.rb";
- }
-
- @VisibleForTesting
- static boolean hasRubyRailsApp(String pluginKey, ClassLoader classLoader) {
- return classLoader.getResource(pathToRubyInitFile(pluginKey)) != null;
-
- }
-
- private void prepareDir(File appsDir) {
- if (appsDir.exists() && appsDir.isDirectory()) {
- try {
- FileUtils.deleteDirectory(appsDir);
- } catch (IOException e) {
- throw new IllegalStateException("Fail to delete temp directory: " + appsDir, e);
- }
- }
- try {
- FileUtils.forceMkdir(appsDir);
- } catch (IOException e) {
- throw new IllegalStateException("Fail to create temp directory: " + appsDir, e);
- }
- }
-}
+++ /dev/null
-/*
- * 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.*;
-import com.google.common.collect.Lists;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.CharEncoding;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.LoggerFactory;
-
-import javax.annotation.Nullable;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.URL;
-import java.net.URLDecoder;
-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
-
-/**
- * @since 3.0
- */
-public final class ClassLoaderUtils {
-
- private ClassLoaderUtils() {
- }
-
- public static File copyResources(ClassLoader classLoader, String rootPath, File toDir) {
- return copyResources(classLoader, rootPath, toDir, Functions.<String>identity());
- }
-
- public static File copyResources(ClassLoader classLoader, String rootPath, File toDir, Function<String, String> relocationFunction) {
- Collection<String> relativePaths = listFiles(classLoader, rootPath);
- for (String relativePath : relativePaths) {
- URL resource = classLoader.getResource(relativePath);
- String filename = relocationFunction.apply(relativePath);
- File toFile = new File(toDir, filename);
- try {
- FileUtils.copyURLToFile(resource, toFile);
- } catch (IOException e) {
- throw new IllegalStateException("Fail to extract " + relativePath + " to " + toFile.getAbsolutePath(), e);
- }
- }
-
- return toDir;
- }
-
- /**
- * Finds files within a given directory and its subdirectories
- *
- * @param classLoader
- * @param rootPath the root directory, for example org/sonar/sqale
- * @return a list of relative paths, for example {"org/sonar/sqale/foo/bar.txt}. Never null.
- */
- public static Collection<String> listFiles(ClassLoader classLoader, String rootPath) {
- return listResources(classLoader, rootPath, new Predicate<String>() {
- public boolean apply(@Nullable String path) {
- return !StringUtils.endsWith(path, "/");
- }
- });
- }
-
-
- public static Collection<String> listResources(ClassLoader classLoader, String rootPath) {
- return listResources(classLoader, rootPath, Predicates.<String>alwaysTrue());
- }
-
- /**
- * Finds directories and files within a given directory and its subdirectories.
- *
- * @param classLoader
- * @param rootPath the root directory, for example org/sonar/sqale, or a file in this root directory, for example org/sonar/sqale/index.txt
- * @param
- * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null.
- */
- public static Collection<String> listResources(ClassLoader classLoader, String rootPath, Predicate<String> predicate) {
- String jarPath = null;
- JarFile jar = null;
- try {
- Collection<String> paths = Lists.newArrayList();
- rootPath = StringUtils.removeStart(rootPath, "/");
-
- URL root = classLoader.getResource(rootPath);
- if (root != null) {
- checkJarFile(root);
-
- // Path of the root directory
- // Examples :
- // org/sonar/sqale/index.txt -> rootDirectory is org/sonar/sqale
- // org/sonar/sqale/ -> rootDirectory is org/sonar/sqale
- // org/sonar/sqale -> rootDirectory is org/sonar/sqale
- String rootDirectory = rootPath;
- if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) {
- rootDirectory = StringUtils.substringBeforeLast(rootPath, "/");
- }
- //strip out only the JAR file
- jarPath = root.getPath().substring(5, root.getPath().indexOf("!"));
- jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8));
- Enumeration<JarEntry> entries = jar.entries();
- while (entries.hasMoreElements()) {
- String name = entries.nextElement().getName();
- if (name.startsWith(rootDirectory) && predicate.apply(name)) {
- paths.add(name);
- }
- }
- }
- return paths;
- } catch (Exception e) {
- throw Throwables.propagate(e);
- } finally {
- closeJar(jar, jarPath);
- }
- }
-
- private static void closeJar(JarFile jar, String jarPath) {
- if (jar != null) {
- try {
- jar.close();
- } catch (Exception e) {
- LoggerFactory.getLogger(ClassLoaderUtils.class).error("Fail to close JAR file: " + jarPath, e);
- }
- }
- }
-
- private static void checkJarFile(URL root) {
- if (!"jar".equals(root.getProtocol())) {
- throw new IllegalStateException("Unsupported protocol: " + root.getProtocol());
- }
- }
-}
--- /dev/null
+/*
+ * 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<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));
+ }
+}
--- /dev/null
+/*
+ * 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.<PluginMetadata>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));
+ }
+}
+++ /dev/null
-/*
- * 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));
- }
-}
+++ /dev/null
-/*
- * 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));
- }
-}