From c866595bea723d451c7aa771af3f5742cf609ded Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 29 Aug 2012 12:07:06 +0200 Subject: [PATCH] Jetty9 - Fixed test that was dependent on case-insensitive file systems. Reformatted, JDK7-ified and cleaned up the code. --- .../java/org/eclipse/jetty/plugins/Main.java | 205 ++++++++++-------- .../eclipse/jetty/plugins/MavenService.java | 15 +- .../eclipse/jetty/plugins/PluginManager.java | 5 - .../plugins/impl/HttpMavenServiceImpl.java | 205 ++++++++++-------- .../jetty/plugins/impl/PluginManagerImpl.java | 140 ++++++------ .../eclipse/jetty/plugins/model/Plugin.java | 31 +-- .../jetty/plugins/util/RepositoryParser.java | 69 +++--- .../jetty/plugins/util/StreamUtils.java | 24 +- .../plugins/impl/HttpMavenServiceTest.java | 64 +++--- .../jetty/plugins/impl/PluginManagerTest.java | 194 ++++++++--------- .../plugins/util/RepositoryParserTest.java | 40 ++-- 11 files changed, 501 insertions(+), 491 deletions(-) diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/Main.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/Main.java index 0a56a77c807..6278d313cbc 100644 --- a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/Main.java +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/Main.java @@ -24,111 +24,126 @@ import java.util.Map; import org.eclipse.jetty.plugins.impl.HttpMavenServiceImpl; import org.eclipse.jetty.plugins.impl.PluginManagerImpl; -/* ------------------------------------------------------------ */ -/** - */ -public class Main { - private static final String JETTY_HOME = "JETTY_HOME"; +public class Main +{ + private static final String JETTY_HOME = "JETTY_HOME"; - private MavenService _mavenService = new HttpMavenServiceImpl(); - private PluginManager _pluginManager; - private String _jettyHome; - private String _installPlugin; - private boolean _listPlugins; - private String _repositoryUrl; - private String _groupId; - private String _version; + private MavenService _mavenService = new HttpMavenServiceImpl(); + private PluginManager _pluginManager; + private String _jettyHome; + private String _installPlugin; + private boolean _listPlugins; + private String _repositoryUrl; + private String _groupId; + private String _version; - /* ------------------------------------------------------------ */ - /** - * @param args - */ - public static void main(String[] args) { - Main main = new Main(); - main.execute(args); - } + public static void main(String[] args) + { + Main main = new Main(); + main.execute(args); + } - private void execute(String[] args) { - parseEnvironmentVariables(); - parseCommandline(args); - configureMavenService(); + private void execute(String[] args) + { + parseEnvironmentVariables(); + parseCommandline(args); + configureMavenService(); - _pluginManager = new PluginManagerImpl(_mavenService, _jettyHome); + _pluginManager = new PluginManagerImpl(_mavenService, _jettyHome); - if (_listPlugins) { - listPlugins(); - } else if (_installPlugin != null) { - installPlugin(); - } - } + if (_listPlugins) + { + listPlugins(); + } + else if (_installPlugin != null) + { + installPlugin(); + } + } - private void configureMavenService() { - if (_repositoryUrl != null) { - _mavenService.setRepositoryUrl(_repositoryUrl); - } - if (_groupId != null) { - _mavenService.setGroupId(_groupId); - } - if (_version != null) { - _mavenService.setVersion(_version); - } - } + private void configureMavenService() + { + if (_repositoryUrl != null) + { + _mavenService.setRepositoryUrl(_repositoryUrl); + } + if (_groupId != null) + { + _mavenService.setGroupId(_groupId); + } + if (_version != null) + { + _mavenService.setVersion(_version); + } + } - private void listPlugins() { - List availablePlugins = _pluginManager.listAvailablePlugins(); - for (String pluginName : availablePlugins) { - System.out.println(pluginName); - } - } + private void listPlugins() + { + List availablePlugins = _pluginManager.listAvailablePlugins(); + for (String pluginName : availablePlugins) + { + System.out.println(pluginName); + } + } - private void installPlugin() { - _pluginManager.installPlugin(_installPlugin); - System.out.println("Successfully installed plugin: " + _installPlugin - + " to " + _jettyHome); - } + private void installPlugin() + { + _pluginManager.installPlugin(_installPlugin); + System.out.println("Successfully installed plugin: " + _installPlugin + + " to " + _jettyHome); + } - private void parseEnvironmentVariables() { - Map env = System.getenv(); - if (env.containsKey(JETTY_HOME)) { - _jettyHome = env.get(JETTY_HOME); - } - } + private void parseEnvironmentVariables() + { + Map env = System.getenv(); + if (env.containsKey(JETTY_HOME)) + { + _jettyHome = env.get(JETTY_HOME); + } + } - private void parseCommandline(String[] args) { - int i = 0; - for (String arg : args) { - i++; - - if (arg.startsWith("--jettyHome=")) { - _jettyHome = arg.substring(12); - } - if (arg.startsWith("--repositoryUrl=")) { - _repositoryUrl = arg.substring(16); - } - if (arg.startsWith("--groupId=")) { - _groupId = arg.substring(10); - } - if (arg.startsWith("--version=")) { - _version = arg.substring(10); - } - if (arg.startsWith("install")) { - _installPlugin = args[i]; - } - if ("list".equals(arg)) { - _listPlugins = true; - } - } + private void parseCommandline(String[] args) + { + int i = 0; + for (String arg : args) + { + i++; - // TODO: Usage instead of throwing exceptions - if (_jettyHome == null && _installPlugin != null) - throw new IllegalArgumentException( - "No --jettyHome commandline option specified and no \"JETTY_HOME\" environment variable found!"); - if (_installPlugin == null && _listPlugins == false) - throw new IllegalArgumentException( - "Neither install nor list commandline option specified. Nothing to do for me!"); - if (_installPlugin != null && _listPlugins) - throw new IllegalArgumentException( - "Please specify either install or list commandline options, but not both at the same time!"); - } + if (arg.startsWith("--jettyHome=")) + { + _jettyHome = arg.substring(12); + } + if (arg.startsWith("--repositoryUrl=")) + { + _repositoryUrl = arg.substring(16); + } + if (arg.startsWith("--groupId=")) + { + _groupId = arg.substring(10); + } + if (arg.startsWith("--version=")) + { + _version = arg.substring(10); + } + if (arg.startsWith("install")) + { + _installPlugin = args[i]; + } + if ("list".equals(arg)) + { + _listPlugins = true; + } + } + // TODO: Usage instead of throwing exceptions + if (_jettyHome == null && _installPlugin != null) + throw new IllegalArgumentException( + "No --jettyHome commandline option specified and no \"JETTY_HOME\" environment variable found!"); + if (_installPlugin == null && !_listPlugins) + throw new IllegalArgumentException( + "Neither install nor list commandline option specified. Nothing to do for me!"); + if (_installPlugin != null && _listPlugins) + throw new IllegalArgumentException( + "Please specify either install or list commandline options, but not both at the same time!"); + } } diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/MavenService.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/MavenService.java index 0d2bd6a8034..7505fa8bbba 100644 --- a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/MavenService.java +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/MavenService.java @@ -16,26 +16,21 @@ // ======================================================================== // - package org.eclipse.jetty.plugins; import java.util.List; import org.eclipse.jetty.plugins.model.Plugin; - -/* ------------------------------------------------------------ */ -/** - */ public interface MavenService { public List listAvailablePlugins(); - + public Plugin getPlugin(String pluginName); - - public void setGroupId(String groupId); - public void setRepositoryUrl(String repositoryUrl); + public void setGroupId(String groupId); - public void setVersion(String version); + public void setRepositoryUrl(String repositoryUrl); + + public void setVersion(String version); } diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/PluginManager.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/PluginManager.java index 0a18bbec500..1e2266a4535 100644 --- a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/PluginManager.java +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/PluginManager.java @@ -16,15 +16,10 @@ // ======================================================================== // - package org.eclipse.jetty.plugins; import java.util.List; - -/* ------------------------------------------------------------ */ -/** - */ public interface PluginManager { public List listAvailablePlugins(); diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceImpl.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceImpl.java index b45f1054e6a..f102537d631 100644 --- a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceImpl.java +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceImpl.java @@ -34,111 +34,128 @@ import org.eclipse.jetty.plugins.model.Plugin; import org.eclipse.jetty.plugins.util.RepositoryParser; import org.eclipse.jetty.plugins.util.StreamUtils; -public class HttpMavenServiceImpl implements MavenService { - private static final String REPOSITORY_URL = "http://repo2.maven.org/maven2/"; - private static final String GROUP_ID = "org/eclipse/jetty"; - private static final String VERSION = "7.6.0.v20120127"; // TODO: should be - // automatically - // set - private String _repositoryUrl = REPOSITORY_URL; - private String _groupId = GROUP_ID; - private String _version = VERSION; +public class HttpMavenServiceImpl implements MavenService +{ + private static final String REPOSITORY_URL = "http://repo2.maven.org/maven2/"; + private static final String GROUP_ID = "org/eclipse/jetty"; + private static final String VERSION = "7.6.0.v20120127"; // TODO: should be automatically set + private String _repositoryUrl = REPOSITORY_URL; + private String _groupId = GROUP_ID; + private String _version = VERSION; - public List listAvailablePlugins() { - List availablePlugins = new ArrayList(); + public List listAvailablePlugins() + { + List availablePlugins = new ArrayList<>(); - String moduleListing = fetchDirectoryListingOfJettyModules(); - List modules = RepositoryParser - .parseLinksInDirectoryListing(moduleListing); + String moduleListing = fetchDirectoryListingOfJettyModules(); + List modules = RepositoryParser + .parseLinksInDirectoryListing(moduleListing); - for (String module : modules) { - String listing = fetchModuleDirectoryListing(module); - if (RepositoryParser.isModuleAPlugin(listing)) { - availablePlugins.add(module); - } - } - - return availablePlugins; - } + for (String module : modules) + { + String listing = fetchModuleDirectoryListing(module); + if (RepositoryParser.isModuleAPlugin(listing)) + { + availablePlugins.add(module); + } + } - private String fetchDirectoryListingOfJettyModules() { - try { - URL url = new URL(_repositoryUrl + _groupId); - URLConnection connection = url.openConnection(); - InputStream inputStream = connection.getInputStream(); - return StreamUtils.inputStreamToString(inputStream); - } catch (MalformedURLException e) { - throw new IllegalStateException(e); - } catch (IOException e) { - throw new IllegalStateException(e); - } - } + return availablePlugins; + } - private String fetchModuleDirectoryListing(String module) { - try { - URL configJar = new URL(getModuleDirectory(module)); - URLConnection connection = configJar.openConnection(); - InputStream inputStream = connection.getInputStream(); - return StreamUtils.inputStreamToString(inputStream); - } catch (MalformedURLException e) { - throw new IllegalStateException(e); - } catch (IOException e) { - // Honestly, I'm not a friend of ignoring exceptions as it might - // hide something important. In this case however it "usually" - // just means: THIS IS NOT A PLUGIN! However it still might hide - // things. If that'll be the case, I hope I'm not the one who - // has to debug my own code. ;) - return "not a plugin"; - } - } + private String fetchDirectoryListingOfJettyModules() + { + try + { + URL url = new URL(_repositoryUrl + _groupId); + URLConnection connection = url.openConnection(); + InputStream inputStream = connection.getInputStream(); + return StreamUtils.inputStreamToString(inputStream); + } + catch (IOException e) + { + throw new IllegalStateException(e); + } + } - public Plugin getPlugin(String pluginName) { - File configJar = getFile(getModulePrefix(pluginName) + "-plugin.jar"); - return new Plugin(pluginName, configJar); - } + private String fetchModuleDirectoryListing(String module) + { + try + { + URL configJar = new URL(getModuleDirectory(module)); + URLConnection connection = configJar.openConnection(); + InputStream inputStream = connection.getInputStream(); + return StreamUtils.inputStreamToString(inputStream); + } + catch (MalformedURLException e) + { + throw new IllegalStateException(e); + } + catch (IOException e) + { + // Honestly, I'm not a friend of ignoring exceptions as it might + // hide something important. In this case however it "usually" + // just means: THIS IS NOT A PLUGIN! However it still might hide + // things. If that'll be the case, I hope I'm not the one who + // has to debug my own code. ;) + return "not a plugin"; + } + } - private String getModuleDirectory(String pluginName) { - return _repositoryUrl + _groupId + "/" + pluginName + "/" + _version - + "/"; - } + public Plugin getPlugin(String pluginName) + { + File configJar = getFile(getModulePrefix(pluginName) + "-plugin.jar"); + return new Plugin(pluginName, configJar); + } - private String getModulePrefix(String pluginName) { - return getModuleDirectory(pluginName) + pluginName + "-" + _version; - } + private String getModuleDirectory(String pluginName) + { + return _repositoryUrl + _groupId + "/" + pluginName + "/" + _version + + "/"; + } - private File getFile(String urlString) { - String fileName = urlString.substring(urlString.lastIndexOf("/") + 1); - try { - URL url = new URL(urlString); - URLConnection connection = url.openConnection(); - InputStream inputStream = connection.getInputStream(); - File tempFile = new File(System.getProperty("java.io.tmpdir"), - fileName); - OutputStream out = new FileOutputStream(tempFile); - byte buf[] = new byte[1024]; - int len; - while ((len = inputStream.read(buf)) > 0) - out.write(buf, 0, len); - out.close(); - inputStream.close(); - return tempFile; - } catch (MalformedURLException e) { - throw new IllegalStateException(e); - } catch (IOException e) { - throw new IllegalStateException(e); - } - } + private String getModulePrefix(String pluginName) + { + return getModuleDirectory(pluginName) + pluginName + "-" + _version; + } - public void setGroupId(String groupId) { - this._groupId = groupId.replace(".", "/"); - } + private File getFile(String urlString) + { + String fileName = urlString.substring(urlString.lastIndexOf("/") + 1); + try + { + URL url = new URL(urlString); + URLConnection connection = url.openConnection(); + InputStream inputStream = connection.getInputStream(); + File tempFile = new File(System.getProperty("java.io.tmpdir"), + fileName); + OutputStream out = new FileOutputStream(tempFile); + byte buf[] = new byte[1024]; + int len; + while ((len = inputStream.read(buf)) > 0) + out.write(buf, 0, len); + out.close(); + inputStream.close(); + return tempFile; + } + catch (IOException e) + { + throw new IllegalStateException(e); + } + } - public void setRepositoryUrl(String repositoryUrl) { - this._repositoryUrl = repositoryUrl; - } + public void setGroupId(String groupId) + { + this._groupId = groupId.replace(".", "/"); + } - public void setVersion(String version) { - this._version = version; - } + public void setRepositoryUrl(String repositoryUrl) + { + this._repositoryUrl = repositoryUrl; + } + public void setVersion(String version) + { + this._version = version; + } } diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/PluginManagerImpl.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/PluginManagerImpl.java index d08a4ff697e..41c33411026 100644 --- a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/PluginManagerImpl.java +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/PluginManagerImpl.java @@ -32,84 +32,80 @@ import org.eclipse.jetty.plugins.MavenService; import org.eclipse.jetty.plugins.PluginManager; import org.eclipse.jetty.plugins.model.Plugin; -/* ------------------------------------------------------------ */ -/** - */ -public class PluginManagerImpl implements PluginManager { - private String _jettyHome; - private MavenService _mavenService; +public class PluginManagerImpl implements PluginManager +{ + private String _jettyHome; + private MavenService _mavenService; - private static List excludes = Arrays.asList("META-INF"); + private static List excludes = Arrays.asList("META-INF"); - public PluginManagerImpl(MavenService mavenService, String jettyHome) { - this._mavenService = mavenService; - this._jettyHome = jettyHome; - } + public PluginManagerImpl(MavenService mavenService, String jettyHome) + { + this._mavenService = mavenService; + this._jettyHome = jettyHome; + } - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.plugins.PluginManager#listAvailablePlugins() - */ - public List listAvailablePlugins() { - return _mavenService.listAvailablePlugins(); - } + public List listAvailablePlugins() + { + return _mavenService.listAvailablePlugins(); + } - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.plugins.PluginManager#installPlugin(String) - */ - public void installPlugin(String pluginName) { - Plugin plugin = _mavenService.getPlugin(pluginName); - installPlugin(plugin); - } - private void installPlugin(Plugin plugin) { - try { - JarFile pluginJar = new JarFile(plugin.getPluginJar()); - extractJar(pluginJar); - } catch (IOException e) { - throw new IllegalStateException(e); - } - } + public void installPlugin(String pluginName) + { + Plugin plugin = _mavenService.getPlugin(pluginName); + installPlugin(plugin); + } - private void extractJar(JarFile file) { - Enumeration entries = file.entries(); - while (entries.hasMoreElements()) { - extractFileFromJar(file, entries.nextElement()); - } - } + private void installPlugin(Plugin plugin) + { + try + { + JarFile pluginJar = new JarFile(plugin.getPluginJar()); + extractJar(pluginJar); + } + catch (IOException e) + { + throw new IllegalStateException(e); + } + } - private void extractFileFromJar(JarFile jarFile, JarEntry jarEntry) { - for (String exclude : excludes) - if (jarEntry.getName().startsWith(exclude)) - return; + private void extractJar(JarFile file) + { + Enumeration entries = file.entries(); + while (entries.hasMoreElements()) + { + extractFileFromJar(file, entries.nextElement()); + } + } - System.out.println("Extracting: " + jarEntry.getName()); - File f = new File(_jettyHome + File.separator + jarEntry.getName()); - if (jarEntry.isDirectory()) { // if its a directory, create it - f.mkdir(); - return; - } - InputStream is = null; - FileOutputStream fos = null; - try { - is = jarFile.getInputStream(jarEntry); - fos = new FileOutputStream(f); - while (is.available() > 0) { - fos.write(is.read()); - } - } catch (IOException e) { - throw new IllegalStateException( - "IOException while extracting plugin jar: ", e); - } finally { - try { - fos.close(); - is.close(); - } catch (IOException e) { - throw new IllegalStateException( - "Couldn't close InputStream or FileOutputStream. This might be a file leak!", - e); - } - } - } + private void extractFileFromJar(JarFile jarFile, JarEntry jarEntry) + { + for (String exclude : excludes) + if (jarEntry.getName().startsWith(exclude)) + return; + + System.out.println("Extracting: " + jarEntry.getName()); + File f = new File(_jettyHome + File.separator + jarEntry.getName()); + if (jarEntry.isDirectory()) + { + // if its a directory, create it + f.mkdir(); // TODO: check the result: what if the directory cannot be created ? + return; + } + + + try (InputStream is = jarFile.getInputStream(jarEntry); + FileOutputStream fos = new FileOutputStream(f)) + { + while (is.available() > 0) + { + fos.write(is.read()); + } + } + catch (IOException e) + { + throw new IllegalStateException("Could not extract plugin jar", e); + } + } } diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/model/Plugin.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/model/Plugin.java index 70f49020658..b7840537ab0 100644 --- a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/model/Plugin.java +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/model/Plugin.java @@ -20,21 +20,24 @@ package org.eclipse.jetty.plugins.model; import java.io.File; -public class Plugin { - private String name; +public class Plugin +{ + private final String name; + private final File pluginJar; - private File pluginJar; + public Plugin(String name, File configJar) + { + this.name = name; + this.pluginJar = configJar; + } - public Plugin(String name, File configJar) { - this.name = name; - this.pluginJar = configJar; - } - - public String getName() { - return name; - } + public String getName() + { + return name; + } - public File getPluginJar() { - return pluginJar; - } + public File getPluginJar() + { + return pluginJar; + } } diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/RepositoryParser.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/RepositoryParser.java index ad91aecf35c..ed16dc82877 100644 --- a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/RepositoryParser.java +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/RepositoryParser.java @@ -17,7 +17,7 @@ // /** - * + * */ package org.eclipse.jetty.plugins.util; @@ -27,38 +27,41 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -/** - * @author tbecker - * - */ -public class RepositoryParser { - private final static List EXCLUDES = Arrays.asList(".."); +public class RepositoryParser +{ + private final static List EXCLUDES = Arrays.asList(".."); - public static List parseLinksInDirectoryListing(String listing) { - List modules = new ArrayList(); - List lines = Arrays.asList(listing.split("\n")); - for (String line : lines) { - Pattern p = Pattern.compile(".*?]+>(?=([^ lines = Arrays.asList(listing.split("\n")); - for (String line : lines) { - Pattern p = Pattern.compile("-plugin\\.jar"); - Matcher m = p.matcher(line); - if (m.find()) { - return true; - } - } - return false; - } + public static List parseLinksInDirectoryListing(String listing) + { + List modules = new ArrayList<>(); + List lines = Arrays.asList(listing.split("\n")); + for (String line : lines) + { + Pattern p = Pattern.compile(".*?]+>(?=([^ lines = Arrays.asList(listing.split("\n")); + for (String line : lines) + { + Pattern p = Pattern.compile("-plugin\\.jar"); + Matcher m = p.matcher(line); + if (m.find()) + { + return true; + } + } + return false; + } } diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/StreamUtils.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/StreamUtils.java index bd563e8e20f..cd1a93bb52a 100644 --- a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/StreamUtils.java +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/StreamUtils.java @@ -23,17 +23,17 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -public class StreamUtils { - public static String inputStreamToString(InputStream inputStream) throws IOException { - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - StringBuilder stringBuilder = new StringBuilder(); - String line = null; - - while ((line = bufferedReader.readLine()) != null) { - stringBuilder.append(line + "\n"); - } - - bufferedReader.close(); - return stringBuilder.toString(); +public class StreamUtils +{ + public static String inputStreamToString(InputStream inputStream) throws IOException + { + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) + { + StringBuilder stringBuilder = new StringBuilder(); + String line; + while ((line = bufferedReader.readLine()) != null) + stringBuilder.append(line).append("\n"); + return stringBuilder.toString(); + } } } diff --git a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceTest.java b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceTest.java index d2922474474..443ed7c9d44 100644 --- a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceTest.java +++ b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceTest.java @@ -38,46 +38,48 @@ import static org.junit.Assert.assertThat; * This is currently more an integration test downloading real stuff from real * maven repositories. Actually it's preferred to have a real unit test or at * least a local repository server. But since HttpClient.send(exchange) has an - * api which is really hard to mock, I will leave that excercise for later. - * + * api which is really hard to mock, I will leave that exercise for later. + *

* However this tests should be disabled for the general build and ci. - * - * @author tbecker - * */ -public class HttpMavenServiceTest { - private MavenService _mavenService = new HttpMavenServiceImpl(); +public class HttpMavenServiceTest +{ + private MavenService _mavenService = new HttpMavenServiceImpl(); - private static final String JETTY_JMX_PLUGIN_NAME = "jetty-jmx"; - private static final String PRIVATE_NEXUS_REPOSITORY_URL = "http://gravity-design.de:8080/nexus/content/repositories/releases/"; - private static final String MAVEN_CENTRAL_URL = "http://repo2.maven.org/maven2/"; + private static final String JETTY_JMX_PLUGIN_NAME = "jetty-jmx"; + private static final String PRIVATE_NEXUS_REPOSITORY_URL = "http://gravity-design.de:8080/nexus/content/repositories/releases/"; + private static final String MAVEN_CENTRAL_URL = "http://repo2.maven.org/maven2/"; - @Before - public void setUp() throws Exception { - _mavenService.setRepositoryUrl(PRIVATE_NEXUS_REPOSITORY_URL); - } + @Before + public void setUp() throws Exception + { + _mavenService.setRepositoryUrl(PRIVATE_NEXUS_REPOSITORY_URL); + } - @Test + @Test @Ignore("requires online repo") - public void testListAvailablePlugins() { - List pluginNames = _mavenService.listAvailablePlugins(); - assertThat(pluginNames.size(), is(2)); - } + public void testListAvailablePlugins() + { + List pluginNames = _mavenService.listAvailablePlugins(); + assertThat(pluginNames.size(), is(2)); + } - @Test + @Test @Ignore("requires online repo") - public void testGetPluginJar() throws IOException { - Plugin plugin = _mavenService.getPlugin(JETTY_JMX_PLUGIN_NAME); - assertThat("jetty-jmx should contain a plugin-jar", - plugin.getPluginJar(), is(notNullValue())); - } + public void testGetPluginJar() throws IOException + { + Plugin plugin = _mavenService.getPlugin(JETTY_JMX_PLUGIN_NAME); + assertThat("jetty-jmx should contain a plugin-jar", + plugin.getPluginJar(), is(notNullValue())); + } - @Test + @Test @Ignore("requires online repo") - public void testGetConfigJar() throws IOException { - Plugin plugin = _mavenService.getPlugin(JETTY_JMX_PLUGIN_NAME); - File configJar = plugin.getPluginJar(); - assertThat(configJar, is(not(nullValue()))); - } + public void testGetConfigJar() throws IOException + { + Plugin plugin = _mavenService.getPlugin(JETTY_JMX_PLUGIN_NAME); + File configJar = plugin.getPluginJar(); + assertThat(configJar, is(not(nullValue()))); + } } diff --git a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/PluginManagerTest.java b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/PluginManagerTest.java index 59e8cd29605..d2f38a05ca3 100644 --- a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/PluginManagerTest.java +++ b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/PluginManagerTest.java @@ -27,6 +27,7 @@ import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.List; +import junit.framework.Assert; import org.eclipse.jetty.plugins.MavenService; import org.eclipse.jetty.plugins.model.Plugin; import org.junit.Before; @@ -40,123 +41,110 @@ import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.when; -/* ------------------------------------------------------------ */ -/** - */ @RunWith(MockitoJUnitRunner.class) -public class PluginManagerTest { - @Mock - private MavenService _mavenService; +public class PluginManagerTest +{ + @Mock + private MavenService _mavenService; + private PluginManagerImpl _pluginManager; + private List availablePlugins = createAvailablePluginsTestData(); + private ClassLoader _classLoader = this.getClass().getClassLoader(); + private String _tmpDir; + private File _javaTmpDir = new File(System.getProperty("java.io.tmpdir")); - private PluginManagerImpl _pluginManager; + @Before + public void setUp() throws Exception + { + URL resource = this.getClass().getResource("/jetty_home"); + _tmpDir = resource.getFile(); + _pluginManager = new PluginManagerImpl(_mavenService, _tmpDir); + } - private List availablePlugins = createAvailablePluginsTestData(); - private ClassLoader _classLoader = this.getClass().getClassLoader(); - private String _tmpDir; - private File _javaTmpDir = new File(System.getProperty("java.io.tmpdir")); + @Test + public void testListAvailablePlugins() + { + when(_mavenService.listAvailablePlugins()).thenReturn(availablePlugins); + List availablePlugins = _pluginManager.listAvailablePlugins(); + assertThat("jetty-jmx not found", + availablePlugins.contains("jetty-jmx"), is(true)); + assertThat("jetty-jta not found", + availablePlugins.contains("jetty-jta"), is(true)); + } - /* ------------------------------------------------------------ */ - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - URL resource = this.getClass().getResource("/jetty_home"); - _tmpDir = resource.getFile(); - _pluginManager = new PluginManagerImpl(_mavenService, _tmpDir); - } + @Test + public void testInstallPluginJar() + { + String pluginName = "jetty-plugin-with-plugin-jar"; + URL resource = _classLoader.getResource("example-plugin.jar"); + Assert.assertNotNull(resource); + String pluginJar = resource.getFile(); + File pluginJarFile = new File(pluginJar); + Plugin plugin = createTestPlugin(pluginName, pluginJarFile); - @Test - public void testListAvailablePlugins() { - when(_mavenService.listAvailablePlugins()).thenReturn(availablePlugins); - List availablePlugins = _pluginManager.listAvailablePlugins(); - assertThat("jetty-jmx not found", - availablePlugins.contains("jetty-jmx"), is(true)); - assertThat("jetty-jta not found", - availablePlugins.contains("jetty-jta"), is(true)); - } + when(_mavenService.getPlugin(pluginName)).thenReturn(plugin); - @Test - public void testInstallPluginJar() { - String pluginName = "jetty-plugin-with-plugin-jar"; - String pluginJar = _classLoader.getResource("example-plugin.jar") - .getFile(); - File pluginJarFile = new File(pluginJar); - Plugin plugin = createTestPlugin(pluginName, pluginJarFile); + _pluginManager.installPlugin(pluginName); - when(_mavenService.getPlugin(pluginName)).thenReturn(plugin); + File someJar = new File(_tmpDir + File.separator + "lib" + File.separator + "somejar.jar"); + assertThat("someJar.jar does not exist", someJar.exists(), is(true)); + File someOtherJar = new File(_tmpDir + File.separator + "lib" + + File.separator + "someotherjar.jar"); + assertThat("someOtherJar.jar does not exist", someOtherJar.exists(), + is(true)); + } - _pluginManager.installPlugin(pluginName); + @Test + public void testInstallPlugins() throws IOException + { + String pluginName = "jetty-jmx"; + URL resource = _classLoader.getResource("jetty-jmx-7.6.0.v20120127-plugin.jar"); + Assert.assertNotNull(resource); + String jmxPluginConfigJar = resource.getFile(); + File jmxPluginConfigJarFile = new File(jmxPluginConfigJar); - File someJar = new File(_tmpDir + File.separator + "lib" - + File.separator + "somejar.jar"); - assertThat("somejar.jar does not exist", someJar.exists(), is(true)); - File someOtherJar = new File(_tmpDir + File.separator + "lib" - + File.separator + "someotherjar.jar"); - assertThat("someotherjar.jar does not exist", someOtherJar.exists(), - is(true)); - } + // Need to copy it to a temp file since the implementation will move the + // file and we need to keep the test files where they are. + File jmxPluginConfigTempCopy = copyToTempFile(jmxPluginConfigJarFile); - @Test - public void testInstallPlugins() throws IOException { + Plugin plugin = new Plugin(pluginName, jmxPluginConfigTempCopy); - String pluginName = "jetty-jmx"; - String jmxPluginConfigJar = _classLoader.getResource( - "jetty-jmx-7.6.0.v20120127-plugin.jar").getFile(); - File jmxPluginConfigJarFile = new File(jmxPluginConfigJar); + when(_mavenService.getPlugin(pluginName)).thenReturn(plugin); - // Need to copy it to a temp file since the implementation will move the - // file and we need to keep the test files where they are. - File jmxPluginConfigTempCopy = copyToTempFile(jmxPluginConfigJarFile); + _pluginManager.installPlugin(pluginName); - Plugin plugin = new Plugin(pluginName, jmxPluginConfigTempCopy); + File metaInf = new File(_tmpDir + File.separator + "META-INF"); + File jettyXmlConfigFile = new File(_tmpDir + File.separator + "start.d" + + File.separator + "20-jetty-jmx.xml"); + File jettyJmxJarFile = new File(_tmpDir + File.separator + "lib" + + File.separator + "jetty-jmx-7.6.0.v20120127.jar"); + assertThat("META-INF should be skipped", metaInf.exists(), not(true)); + assertThat("20-jetty-jmx.xml does not exist", + jettyXmlConfigFile.exists(), is(true)); + assertThat("jetty-jmx-7.6.0.v20120127.jar does not exist", + jettyJmxJarFile.exists(), is(true)); + } - when(_mavenService.getPlugin(pluginName)).thenReturn(plugin); + public File copyToTempFile(File sourceFile) throws IOException + { + File destFile = new File(_javaTmpDir + File.separator + sourceFile.getName()); + try (FileChannel destination = new FileOutputStream(destFile).getChannel(); + FileChannel source = new FileInputStream(sourceFile).getChannel()) + { + destination.transferFrom(source, 0, source.size()); + } + return destFile; + } - _pluginManager.installPlugin(pluginName); - - File metaInf = new File(_tmpDir + File.separator + "META-INF"); - File jettyXmlConfigFile = new File(_tmpDir + File.separator + "start.d" - + File.separator + "20-jetty-jmx.xml"); - File jettyJmxJarFile = new File(_tmpDir + File.separator + "lib" - + File.separator + "jetty-jmx-7.6.0.v20120127.jar"); - assertThat("META-INF should be skipped", metaInf.exists(), not(true)); - assertThat("20-jetty-jmx.xml does not exist", - jettyXmlConfigFile.exists(), is(true)); - assertThat("jetty-jmx-7.6.0.v20120127.jar does not exist", - jettyJmxJarFile.exists(), is(true)); - } - - public File copyToTempFile(File sourceFile) throws IOException { - File destFile = new File(_javaTmpDir + File.separator - + sourceFile.getName()); - FileChannel source = null; - FileChannel destination = null; - try { - source = new FileInputStream(sourceFile).getChannel(); - destination = new FileOutputStream(destFile).getChannel(); - destination.transferFrom(source, 0, source.size()); - } finally { - if (source != null) { - source.close(); - } - if (destination != null) { - destination.close(); - } - } - return destFile; - } - - private List createAvailablePluginsTestData() { - List availablePlugins = new ArrayList(); - availablePlugins.add("jetty-jmx"); - availablePlugins.add("jetty-jta"); - return availablePlugins; - } - - private Plugin createTestPlugin(String name, File jar) { - Plugin plugin = new Plugin(name, jar); - return plugin; - } + private List createAvailablePluginsTestData() + { + List availablePlugins = new ArrayList<>(); + availablePlugins.add("jetty-jmx"); + availablePlugins.add("jetty-jta"); + return availablePlugins; + } + private Plugin createTestPlugin(String name, File jar) + { + return new Plugin(name, jar); + } } diff --git a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/util/RepositoryParserTest.java b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/util/RepositoryParserTest.java index 8dda99a4650..45adcdfe3bd 100644 --- a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/util/RepositoryParserTest.java +++ b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/util/RepositoryParserTest.java @@ -21,33 +21,29 @@ package org.eclipse.jetty.plugins.util; import java.io.IOException; import java.util.List; -import org.junit.Before; import org.junit.Test; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; -public class RepositoryParserTest { - - @Before - public void setUp() throws Exception { - } - - @Test - public void testParseLinksInDirectoryListing() throws IOException { - String listing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyDirectoryListing.html")); - List modules = RepositoryParser.parseLinksInDirectoryListing(listing); - assertThat("At least ten jetty modules expected",modules.size(), greaterThan(10)); - assertThat("jetty-jmx module expected", modules.contains("jetty-jmx"), is(true)); - } - - @Test - public void testIsPlugin() throws IOException{ - String listing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyJMXDirectoryListing.html")); - assertThat("listing describes a plugin", RepositoryParser.isModuleAPlugin(listing), is(true)); - String nonPluginListing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyJNDIDirectoryListing.html")); - assertThat("listing doesn't describe a plugin", RepositoryParser.isModuleAPlugin(nonPluginListing), is(false)); - } +public class RepositoryParserTest +{ + @Test + public void testParseLinksInDirectoryListing() throws IOException + { + String listing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyDirectoryListing.html")); + List modules = RepositoryParser.parseLinksInDirectoryListing(listing); + assertThat("At least ten jetty modules expected", modules.size(), greaterThan(10)); + assertThat("jetty-jmx module expected", modules.contains("jetty-jmx"), is(true)); + } + @Test + public void testIsPlugin() throws IOException + { + String listing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyJMXDirectoryListing.html")); + assertThat("listing describes a plugin", RepositoryParser.isModuleAPlugin(listing), is(true)); + String nonPluginListing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyJNDIDirectoryListing.html")); + assertThat("listing doesn't describe a plugin", RepositoryParser.isModuleAPlugin(nonPluginListing), is(false)); + } }