diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2020-08-26 18:30:15 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-09-18 20:07:13 +0000 |
commit | d189558e9c7b3994254eaa8d67dff0169c21d5dd (patch) | |
tree | db9d6937b26ad9ce923c408a8d3ee94045203d82 /sonar-core | |
parent | 13ef209f1b2cc5f82d0c3bde502ba02649b6c5e2 (diff) | |
download | sonarqube-d189558e9c7b3994254eaa8d67dff0169c21d5dd.tar.gz sonarqube-d189558e9c7b3994254eaa8d67dff0169c21d5dd.zip |
SONAR-13643 Save plugins with type
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/platform/ExplodedPlugin.java | 9 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/platform/PluginClassLoader.java (renamed from sonar-core/src/main/java/org/sonar/core/platform/PluginLoader.java) | 46 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java | 164 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/platform/PluginJarExploder.java | 6 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java | 4 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/platform/PluginClassLoaderTest.java (renamed from sonar-core/src/test/java/org/sonar/core/platform/PluginLoaderTest.java) | 44 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java | 31 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/platform/PluginJarExploderTest.java | 4 |
8 files changed, 159 insertions, 149 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ExplodedPlugin.java b/sonar-core/src/main/java/org/sonar/core/platform/ExplodedPlugin.java index 09d846aec04..bda09ba02c1 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ExplodedPlugin.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ExplodedPlugin.java @@ -23,12 +23,13 @@ import java.io.File; import java.util.Collection; public class ExplodedPlugin { - + private final PluginInfo pluginInfo; private final String key; private final File main; private final Collection<File> libs; - public ExplodedPlugin(String key, File main, Collection<File> libs) { + public ExplodedPlugin(PluginInfo pluginInfo, String key, File main, Collection<File> libs) { + this.pluginInfo = pluginInfo; this.key = key; this.main = main; this.libs = libs; @@ -45,4 +46,8 @@ public class ExplodedPlugin { public Collection<File> getLibs() { return libs; } + + public PluginInfo getPluginInfo() { + return pluginInfo; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/PluginLoader.java b/sonar-core/src/main/java/org/sonar/core/platform/PluginClassLoader.java index 5b997d9d407..503bc4dae0a 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/PluginLoader.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/PluginClassLoader.java @@ -25,12 +25,13 @@ import java.io.Closeable; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; import org.apache.commons.lang.SystemUtils; import org.sonar.api.Plugin; import org.sonar.api.utils.log.Loggers; import org.sonar.updatecenter.common.Version; -import static java.util.Arrays.asList; +import static java.util.Collections.singleton; /** * Loads the plugin JAR files by creating the appropriate classloaders and by instantiating @@ -46,22 +47,22 @@ import static java.util.Arrays.asList; * <p/> * This class is stateless. It does not keep pointers to classloaders and {@link org.sonar.api.Plugin}. */ -public class PluginLoader { - +public class PluginClassLoader { private static final String[] DEFAULT_SHARED_RESOURCES = {"org/sonar/plugins", "com/sonar/plugins", "com/sonarsource/plugins"}; - private static final Version COMPATIBILITY_MODE_MAX_VERSION = Version.create("5.2"); - private final PluginJarExploder jarExploder; private final PluginClassloaderFactory classloaderFactory; - public PluginLoader(PluginJarExploder jarExploder, PluginClassloaderFactory classloaderFactory) { - this.jarExploder = jarExploder; + public PluginClassLoader(PluginClassloaderFactory classloaderFactory) { this.classloaderFactory = classloaderFactory; } - public Map<String, Plugin> load(Map<String, PluginInfo> infoByKeys) { - Collection<PluginClassLoaderDef> defs = defineClassloaders(infoByKeys); + public Map<String, Plugin> load(Collection<ExplodedPlugin> plugins) { + return load(plugins.stream().collect(Collectors.toMap(ExplodedPlugin::getKey, x -> x))); + } + + public Map<String, Plugin> load(Map<String, ExplodedPlugin> pluginsByKey) { + Collection<PluginClassLoaderDef> defs = defineClassloaders(pluginsByKey); Map<PluginClassLoaderDef, ClassLoader> classloaders = classloaderFactory.create(defs); return instantiatePluginClasses(classloaders); } @@ -71,19 +72,19 @@ public class PluginLoader { * different than number of plugins. */ @VisibleForTesting - Collection<PluginClassLoaderDef> defineClassloaders(Map<String, PluginInfo> infoByKeys) { + Collection<PluginClassLoaderDef> defineClassloaders(Map<String, ExplodedPlugin> pluginsByKey) { Map<String, PluginClassLoaderDef> classloadersByBasePlugin = new HashMap<>(); - for (PluginInfo info : infoByKeys.values()) { - String baseKey = basePluginKey(info, infoByKeys); + for (ExplodedPlugin plugin : pluginsByKey.values()) { + PluginInfo info = plugin.getPluginInfo(); + String baseKey = basePluginKey(info, pluginsByKey); PluginClassLoaderDef def = classloadersByBasePlugin.get(baseKey); if (def == null) { def = new PluginClassLoaderDef(baseKey); classloadersByBasePlugin.put(baseKey, def); } - ExplodedPlugin explodedPlugin = jarExploder.explode(info); - def.addFiles(asList(explodedPlugin.getMain())); - def.addFiles(explodedPlugin.getLibs()); + def.addFiles(singleton(plugin.getMain())); + def.addFiles(plugin.getLibs()); def.addMainClass(info.getKey(), info.getMainClass()); for (String defaultSharedResource : DEFAULT_SHARED_RESOURCES) { @@ -115,8 +116,7 @@ public class PluginLoader { * @return the instances grouped by plugin key * @throws IllegalStateException if at least one plugin can't be correctly loaded */ - @VisibleForTesting - Map<String, Plugin> instantiatePluginClasses(Map<PluginClassLoaderDef, ClassLoader> classloaders) { + private static Map<String, Plugin> instantiatePluginClasses(Map<PluginClassLoaderDef, ClassLoader> classloaders) { // instantiate plugins Map<String, Plugin> instancesByPluginKey = new HashMap<>(); for (Map.Entry<PluginClassLoaderDef, ClassLoader> entry : classloaders.entrySet()) { @@ -128,13 +128,11 @@ public class PluginLoader { String pluginKey = mainClassEntry.getKey(); String mainClass = mainClassEntry.getValue(); try { - instancesByPluginKey.put(pluginKey, (Plugin) classLoader.loadClass(mainClass).newInstance()); + instancesByPluginKey.put(pluginKey, (Plugin) classLoader.loadClass(mainClass).getDeclaredConstructor().newInstance()); } catch (UnsupportedClassVersionError e) { - throw new IllegalStateException(String.format("The plugin [%s] does not support Java %s", - pluginKey, SystemUtils.JAVA_VERSION_TRIMMED), e); + throw new IllegalStateException(String.format("The plugin [%s] does not support Java %s", pluginKey, SystemUtils.JAVA_VERSION_TRIMMED), e); } catch (Throwable e) { - throw new IllegalStateException(String.format( - "Fail to instantiate class [%s] of plugin [%s]", mainClass, pluginKey), e); + throw new IllegalStateException(String.format("Fail to instantiate class [%s] of plugin [%s]", mainClass, pluginKey), e); } } } @@ -158,11 +156,11 @@ public class PluginLoader { * Get the root key of a tree of plugins. For example if plugin C depends on B, which depends on A, then * B and C must be attached to the classloader of A. The method returns A in the three cases. */ - static String basePluginKey(PluginInfo plugin, Map<String, PluginInfo> allPluginsPerKey) { + private static String basePluginKey(PluginInfo plugin, Map<String, ExplodedPlugin> pluginsByKey) { String base = plugin.getKey(); String parentKey = plugin.getBasePlugin(); while (!Strings.isNullOrEmpty(parentKey)) { - PluginInfo parentPlugin = allPluginsPerKey.get(parentKey); + PluginInfo parentPlugin = pluginsByKey.get(parentKey).getPluginInfo(); base = parentPlugin.getKey(); parentKey = parentPlugin.getBasePlugin(); } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java b/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java index be62f1e8859..fe2a6e3145a 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java @@ -27,6 +27,7 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.HashSet; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.jar.JarFile; @@ -48,57 +49,6 @@ public class PluginInfo implements Comparable<PluginInfo> { private static final Joiner SLASH_JOINER = Joiner.on(" / ").skipNulls(); - public static class RequiredPlugin { - - private static final Pattern PARSER = Pattern.compile("\\w+:.+"); - - private final String key; - private final Version minimalVersion; - - public RequiredPlugin(String key, Version minimalVersion) { - this.key = key; - this.minimalVersion = minimalVersion; - } - - public String getKey() { - return key; - } - - public Version getMinimalVersion() { - return minimalVersion; - } - - public static RequiredPlugin parse(String s) { - if (!PARSER.matcher(s).matches()) { - throw new IllegalArgumentException("Manifest field does not have correct format: " + s); - } - String[] fields = StringUtils.split(s, ':'); - return new RequiredPlugin(fields[0], Version.create(fields[1]).removeQualifier()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - RequiredPlugin that = (RequiredPlugin) o; - return key.equals(that.key); - } - - @Override - public int hashCode() { - return key.hashCode(); - } - - @Override - public String toString() { - return new StringBuilder().append(key).append(':').append(minimalVersion.getName()).toString(); - } - } - private final String key; private String name; @@ -385,18 +335,13 @@ public class PluginInfo implements Comparable<PluginInfo> { return false; } PluginInfo info = (PluginInfo) o; - if (!key.equals(info.key)) { - return false; - } - return !(version != null ? !version.equals(info.version) : info.version != null); + return Objects.equals(key, info.key) && Objects.equals(version, info.version); } @Override public int hashCode() { - int result = key.hashCode(); - result = 31 * result + (version != null ? version.hashCode() : 0); - return result; + return Objects.hash(key, version); } @Override @@ -419,41 +364,48 @@ public class PluginInfo implements Comparable<PluginInfo> { @VisibleForTesting static PluginInfo create(File jarFile, PluginManifest manifest) { + validateManifest(jarFile, manifest); + PluginInfo info = new PluginInfo(manifest.getKey()); + info.fillFields(jarFile, manifest); + return info; + } + + private static void validateManifest(File jarFile, PluginManifest manifest) { if (StringUtils.isBlank(manifest.getKey())) { throw MessageException.of(String.format("File is not a plugin. Please delete it and restart: %s", jarFile.getAbsolutePath())); } - PluginInfo info = new PluginInfo(manifest.getKey()); + } - info.setJarFile(jarFile); - info.setName(manifest.getName()); - info.setMainClass(manifest.getMainClass()); - info.setVersion(Version.create(manifest.getVersion())); - info.setDocumentationPath(getDocumentationPath(jarFile)); + protected void fillFields(File jarFile, PluginManifest manifest) { + setJarFile(jarFile); + setName(manifest.getName()); + setMainClass(manifest.getMainClass()); + setVersion(Version.create(manifest.getVersion())); + setDocumentationPath(getDocumentationPath(jarFile)); // optional fields - info.setDescription(manifest.getDescription()); - info.setLicense(manifest.getLicense()); - info.setOrganizationName(manifest.getOrganization()); - info.setOrganizationUrl(manifest.getOrganizationUrl()); - info.setDisplayVersion(manifest.getDisplayVersion()); + setDescription(manifest.getDescription()); + setLicense(manifest.getLicense()); + setOrganizationName(manifest.getOrganization()); + setOrganizationUrl(manifest.getOrganizationUrl()); + setDisplayVersion(manifest.getDisplayVersion()); String minSqVersion = manifest.getSonarVersion(); if (minSqVersion != null) { - info.setMinimalSqVersion(Version.create(minSqVersion)); + setMinimalSqVersion(Version.create(minSqVersion)); } - info.setHomepageUrl(manifest.getHomepage()); - info.setIssueTrackerUrl(manifest.getIssueTrackerUrl()); - info.setUseChildFirstClassLoader(manifest.isUseChildFirstClassLoader()); - info.setSonarLintSupported(manifest.isSonarLintSupported()); - info.setBasePlugin(manifest.getBasePlugin()); - info.setImplementationBuild(manifest.getImplementationBuild()); - String[] requiredPlugins = manifest.getRequirePlugins(); - if (requiredPlugins != null) { - Arrays.stream(requiredPlugins) + setHomepageUrl(manifest.getHomepage()); + setIssueTrackerUrl(manifest.getIssueTrackerUrl()); + setUseChildFirstClassLoader(manifest.isUseChildFirstClassLoader()); + setSonarLintSupported(manifest.isSonarLintSupported()); + setBasePlugin(manifest.getBasePlugin()); + setImplementationBuild(manifest.getImplementationBuild()); + String[] requiredPluginsFromManifest = manifest.getRequirePlugins(); + if (requiredPluginsFromManifest != null) { + Arrays.stream(requiredPluginsFromManifest) .map(RequiredPlugin::parse) .filter(t -> !"license".equals(t.key)) - .forEach(info::addRequiredPlugin); + .forEach(this::addRequiredPlugin); } - return info; } private static String getDocumentationPath(File file) { @@ -467,4 +419,54 @@ public class PluginInfo implements Comparable<PluginInfo> { return null; } + public static class RequiredPlugin { + + private static final Pattern PARSER = Pattern.compile("\\w+:.+"); + + private final String key; + private final Version minimalVersion; + + public RequiredPlugin(String key, Version minimalVersion) { + this.key = key; + this.minimalVersion = minimalVersion; + } + + public String getKey() { + return key; + } + + public Version getMinimalVersion() { + return minimalVersion; + } + + public static RequiredPlugin parse(String s) { + if (!PARSER.matcher(s).matches()) { + throw new IllegalArgumentException("Manifest field does not have correct format: " + s); + } + String[] fields = StringUtils.split(s, ':'); + return new RequiredPlugin(fields[0], Version.create(fields[1]).removeQualifier()); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + RequiredPlugin that = (RequiredPlugin) o; + return key.equals(that.key); + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + @Override + public String toString() { + return key + ':' + minimalVersion.getName(); + } + } } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/PluginJarExploder.java b/sonar-core/src/main/java/org/sonar/core/platform/PluginJarExploder.java index 508aa642666..7c97461c034 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/PluginJarExploder.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/PluginJarExploder.java @@ -31,13 +31,13 @@ public abstract class PluginJarExploder { protected static final String LIB_RELATIVE_PATH_IN_JAR = "META-INF/lib"; - public abstract ExplodedPlugin explode(PluginInfo info); + public abstract ExplodedPlugin explode(PluginInfo plugin); protected Predicate<ZipEntry> newLibFilter() { return ze -> ze.getName().startsWith(LIB_RELATIVE_PATH_IN_JAR); } - protected ExplodedPlugin explodeFromUnzippedDir(String pluginKey, File jarFile, File unzippedDir) { + protected ExplodedPlugin explodeFromUnzippedDir(PluginInfo pluginInfo, File jarFile, File unzippedDir) { File libDir = new File(unzippedDir, PluginJarExploder.LIB_RELATIVE_PATH_IN_JAR); Collection<File> libs; if (libDir.isDirectory() && libDir.exists()) { @@ -45,6 +45,6 @@ public abstract class PluginJarExploder { } else { libs = Collections.emptyList(); } - return new ExplodedPlugin(pluginKey, jarFile, libs); + return new ExplodedPlugin(pluginInfo, pluginInfo.getKey(), jarFile, libs); } } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java b/sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java index c78a88f6d49..b49599f9c12 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java @@ -21,8 +21,8 @@ package org.sonar.core.platform; import java.util.Collection; import org.sonar.api.Plugin; -import org.sonar.api.scanner.ScannerSide; import org.sonar.api.ce.ComputeEngineSide; +import org.sonar.api.scanner.ScannerSide; import org.sonar.api.server.ServerSide; /** @@ -45,5 +45,7 @@ public interface PluginRepository { */ Plugin getPluginInstance(String key); + Collection<Plugin> getPluginInstances(); + boolean hasPlugin(String key); } diff --git a/sonar-core/src/test/java/org/sonar/core/platform/PluginLoaderTest.java b/sonar-core/src/test/java/org/sonar/core/platform/PluginClassLoaderTest.java index 1d12a4edab5..446bb257ae3 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/PluginLoaderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/PluginClassLoaderTest.java @@ -37,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.mockito.Mockito.mock; -public class PluginLoaderTest { +public class PluginClassLoaderTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -46,25 +46,26 @@ public class PluginLoaderTest { public LogTester logTester = new LogTester(); private PluginClassloaderFactory classloaderFactory = mock(PluginClassloaderFactory.class); - private PluginLoader underTest = new PluginLoader(new FakePluginExploder(), classloaderFactory); + private PluginClassLoader underTest = new PluginClassLoader(classloaderFactory); @Test public void define_classloader() throws Exception { File jarFile = temp.newFile(); - PluginInfo info = new PluginInfo("foo") + PluginInfo plugin = new PluginInfo("foo") .setJarFile(jarFile) .setMainClass("org.foo.FooPlugin") .setMinimalSqVersion(Version.create("5.2")); - Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders(ImmutableMap.of("foo", info)); + ExplodedPlugin explodedPlugin = createExplodedPlugin(plugin); + Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders( + ImmutableMap.of("foo", explodedPlugin)); assertThat(defs).hasSize(1); PluginClassLoaderDef def = defs.iterator().next(); assertThat(def.getBasePluginKey()).isEqualTo("foo"); assertThat(def.isSelfFirstStrategy()).isFalse(); - assertThat(def.getFiles()).containsOnly(jarFile); + assertThat(def.getFiles()).containsAll(explodedPlugin.getLibs()); assertThat(def.getMainClassesByPluginKey()).containsOnly(MapEntry.entry("foo", "org.foo.FooPlugin")); - // TODO test mask - require change in sonar-classloader } /** @@ -95,19 +96,27 @@ public class PluginLoaderTest { .setBasePlugin("foo") .setUseChildFirstClassLoader(true); + ExplodedPlugin baseExplodedPlugin = createExplodedPlugin(base); + ExplodedPlugin extension1ExplodedPlugin = createExplodedPlugin(extension1); + ExplodedPlugin extension2ExplodedPlugin = createExplodedPlugin(extension2); Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders(ImmutableMap.of( - base.getKey(), base, extension1.getKey(), extension1, extension2.getKey(), extension2)); + base.getKey(), baseExplodedPlugin, + extension1.getKey(), extension1ExplodedPlugin, + extension2.getKey(), extension2ExplodedPlugin)); assertThat(defs).hasSize(1); PluginClassLoaderDef def = defs.iterator().next(); assertThat(def.getBasePluginKey()).isEqualTo("foo"); assertThat(def.isSelfFirstStrategy()).isFalse(); - assertThat(def.getFiles()).containsOnly(baseJarFile, extensionJar1, extensionJar2); + + assertThat(def.getFiles()) + .containsAll(baseExplodedPlugin.getLibs()) + .containsAll(extension1ExplodedPlugin.getLibs()) + .containsAll(extension2ExplodedPlugin.getLibs()); assertThat(def.getMainClassesByPluginKey()).containsOnly( entry("foo", "org.foo.FooPlugin"), entry("fooExtension1", "org.foo.Extension1Plugin"), entry("fooExtension2", "org.foo.Extension2Plugin")); - // TODO test mask - require change in sonar-classloader } @Test @@ -118,7 +127,8 @@ public class PluginLoaderTest { .setUseChildFirstClassLoader(true) .setMainClass("org.foo.FooPlugin"); - Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders(ImmutableMap.of("foo", info)); + Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders( + ImmutableMap.of("foo", createExplodedPlugin(info))); assertThat(defs).extracting(PluginClassLoaderDef::getBasePluginKey).containsExactly("foo"); List<String> warnings = logTester.logs(LoggerLevel.WARN); @@ -133,20 +143,16 @@ public class PluginLoaderTest { .setMainClass("org.foo.FooPlugin") .setMinimalSqVersion(Version.create("4.5.2")); - Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders(ImmutableMap.of("foo", info)); + Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders( + ImmutableMap.of("foo", createExplodedPlugin(info))); assertThat(defs).extracting(PluginClassLoaderDef::getBasePluginKey).containsExactly("foo"); List<String> warnings = logTester.logs(LoggerLevel.WARN); assertThat(warnings).contains("API compatibility mode is no longer supported. In case of error, plugin foo [foo] should package its dependencies."); } - /** - * Does not unzip jar file. It directly returns the JAR file defined on PluginInfo. - */ - private static class FakePluginExploder extends PluginJarExploder { - @Override - public ExplodedPlugin explode(PluginInfo info) { - return new ExplodedPlugin(info.getKey(), info.getNonNullJarFile(), Collections.emptyList()); - } + private ExplodedPlugin createExplodedPlugin(PluginInfo plugin) { + return new ExplodedPlugin(plugin, plugin.getKey(), new File(plugin.getKey() + ".jar"), Collections + .singleton(new File(plugin.getKey() + "-lib.jar"))); } } diff --git a/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java b/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java index ebc67bf175f..d85d614c848 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java @@ -24,6 +24,7 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import javax.annotation.Nullable; @@ -31,7 +32,6 @@ import org.apache.commons.io.FileUtils; import org.assertj.core.api.Assertions; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.sonar.api.utils.MessageException; @@ -41,6 +41,7 @@ import org.sonar.updatecenter.common.Version; import static com.google.common.collect.Ordering.natural; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.fail; @RunWith(DataProviderRunner.class) @@ -49,17 +50,14 @@ public class PluginInfoTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void test_RequiredPlugin() { PluginInfo.RequiredPlugin plugin = PluginInfo.RequiredPlugin.parse("java:1.1"); assertThat(plugin.getKey()).isEqualTo("java"); assertThat(plugin.getMinimalVersion().getName()).isEqualTo("1.1"); - assertThat(plugin.toString()).isEqualTo("java:1.1"); - assertThat(plugin.equals(PluginInfo.RequiredPlugin.parse("java:1.2"))).isTrue(); - assertThat(plugin.equals(PluginInfo.RequiredPlugin.parse("php:1.2"))).isFalse(); + assertThat(plugin).hasToString("java:1.1") + .isEqualTo(PluginInfo.RequiredPlugin.parse("java:1.2")) + .isNotEqualTo(PluginInfo.RequiredPlugin.parse("php:1.2")); try { PluginInfo.RequiredPlugin.parse("java"); @@ -210,7 +208,7 @@ public class PluginInfoTest { manifest.setOrganization("SonarSource"); manifest.setOrganizationUrl("http://sonarsource.com"); manifest.setIssueTrackerUrl("http://jira.com"); - manifest.setRequirePlugins(new String[]{"java:2.0", "pmd:1.3"}); + manifest.setRequirePlugins(new String[] {"java:2.0", "pmd:1.3"}); manifest.setSonarLintSupported(true); File jarFile = temp.newFile(); @@ -237,7 +235,7 @@ public class PluginInfoTest { manifest.setVersion("1.0"); manifest.setName("Java"); manifest.setMainClass("org.foo.FooPlugin"); - manifest.setRequirePlugins(new String[]{"license:" + version}); + manifest.setRequirePlugins(new String[] {"license:" + version}); File jarFile = temp.newFile(); PluginInfo pluginInfo = PluginInfo.create(jarFile, manifest); @@ -252,7 +250,7 @@ public class PluginInfoTest { manifest.setVersion("1.0"); manifest.setName("Java"); manifest.setMainClass("org.foo.FooPlugin"); - manifest.setRequirePlugins(new String[]{"java:2.0", "license:" + version, "pmd:1.3"}); + manifest.setRequirePlugins(new String[] {"java:2.0", "license:" + version, "pmd:1.3"}); File jarFile = temp.newFile(); PluginInfo pluginInfo = PluginInfo.create(jarFile, manifest); @@ -261,7 +259,7 @@ public class PluginInfoTest { @DataProvider public static Object[][] licenseVersions() { - return new Object[][]{ + return new Object[][] { {"0.3"}, {"7.2.0.1253"} }; @@ -292,7 +290,7 @@ public class PluginInfoTest { assertThat(pluginInfo.toString()).isEqualTo("[java / 1.1]"); pluginInfo.setImplementationBuild("SHA1"); - assertThat(pluginInfo.toString()).isEqualTo("[java / 1.1 / SHA1]"); + assertThat(pluginInfo).hasToString("[java / 1.1 / SHA1]"); } /** @@ -309,14 +307,13 @@ public class PluginInfoTest { public void fail_when_jar_is_not_a_plugin() throws IOException { // this JAR has a manifest but is not a plugin File jarRootDir = temp.newFolder(); - FileUtils.write(new File(jarRootDir, "META-INF/MANIFEST.MF"), "Build-Jdk: 1.6.0_15"); + FileUtils.write(new File(jarRootDir, "META-INF/MANIFEST.MF"), "Build-Jdk: 1.6.0_15", StandardCharsets.UTF_8); File jar = temp.newFile(); ZipUtils.zipDir(jarRootDir, jar); - expectedException.expect(MessageException.class); - expectedException.expectMessage("File is not a plugin. Please delete it and restart: " + jar.getAbsolutePath()); - - PluginInfo.create(jar); + assertThatThrownBy(() -> PluginInfo.create(jar)) + .isInstanceOf(MessageException.class) + .hasMessage("File is not a plugin. Please delete it and restart: " + jar.getAbsolutePath()); } PluginInfo withMinSqVersion(@Nullable String version) { diff --git a/sonar-core/src/test/java/org/sonar/core/platform/PluginJarExploderTest.java b/sonar-core/src/test/java/org/sonar/core/platform/PluginJarExploderTest.java index 4e103b1d349..59824693472 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/PluginJarExploderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/PluginJarExploderTest.java @@ -44,7 +44,7 @@ public class PluginJarExploderTest { public ExplodedPlugin explode(PluginInfo info) { try { ZipUtils.unzip(jarFile, toDir, newLibFilter()); - return explodeFromUnzippedDir(info.getKey(), info.getNonNullJarFile(), toDir); + return explodeFromUnzippedDir(info, info.getNonNullJarFile(), toDir); } catch (Exception e) { throw new IllegalStateException(e); } @@ -65,7 +65,7 @@ public class PluginJarExploderTest { PluginJarExploder exploder = new PluginJarExploder() { @Override public ExplodedPlugin explode(PluginInfo info) { - return explodeFromUnzippedDir("foo", info.getNonNullJarFile(), toDir); + return explodeFromUnzippedDir(pluginInfo, info.getNonNullJarFile(), toDir); } }; ExplodedPlugin exploded = exploder.explode(pluginInfo); |