diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-06-10 16:17:01 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-06-10 16:17:01 +0200 |
commit | 318623dd708364abb660f029b2bbe7ecb642a743 (patch) | |
tree | 4340f72e004ad2be2f7e8eaba82cc61e67edf9c1 /sonar-server | |
parent | be64518eaa0b0a351b4e76f91bdb16061038eafa (diff) | |
download | sonarqube-318623dd708364abb660f029b2bbe7ecb642a743.tar.gz sonarqube-318623dd708364abb660f029b2bbe7ecb642a743.zip |
SONAR-2507 add unit tests to ServerPluginRepository
Diffstat (limited to 'sonar-server')
4 files changed, 106 insertions, 20 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java b/sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java index b39126da4bb..a5c9281dbd3 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java @@ -45,7 +45,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; -public final class PluginDeployer implements ServerComponent { +public class PluginDeployer implements ServerComponent { private static final Logger LOG = LoggerFactory.getLogger(PluginDeployer.class); diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java b/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java index 8f175f5139d..a3bd938116f 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java @@ -49,6 +49,13 @@ public class ServerPluginRepository implements PluginRepository { pluginsByKey = classloaders.init(deployer.getMetadata()); } + public void stop() { + if (classloaders != null) { + classloaders.clean(); + classloaders = null; + } + } + public Collection<Plugin> getPlugins() { return pluginsByKey.values(); } @@ -95,7 +102,11 @@ public class ServerPluginRepository implements PluginRepository { } public void registerExtensions(MutablePicoContainer container) { - for (Plugin plugin : getPlugins()) { + registerExtensions(container, getPlugins()); + } + + void registerExtensions(MutablePicoContainer container, Collection<Plugin> plugins) { + for (Plugin plugin : plugins) { container.as(Characteristics.CACHE).addComponent(plugin); for (Object extension : plugin.getExtensions()) { installExtension(container, extension); diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java index 82af3fcc770..dcc85e5b519 100644 --- a/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java +++ b/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java @@ -19,26 +19,93 @@ */ package org.sonar.server.plugins; -import org.junit.Ignore; -import org.sonar.api.BatchExtension; -import org.sonar.api.ServerExtension; +import org.apache.commons.io.FileUtils; +import org.hamcrest.core.Is; +import org.junit.After; +import org.junit.Test; +import org.picocontainer.containers.TransientPicoContainer; +import org.sonar.api.*; +import org.sonar.api.platform.PluginMetadata; +import org.sonar.core.plugins.DefaultPluginMetadata; + +import java.io.File; +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -@Ignore public class ServerPluginRepositoryTest { -// @Test -// public void shouldRegisterServerExtensions() { -// ServerPluginRepository repository = new ServerPluginRepository(); -// -// // check classes -// assertThat(repository.shouldRegisterExtension(null, "foo", FakeBatchExtension.class), is(false)); -// assertThat(repository.shouldRegisterExtension(null, "foo", FakeServerExtension.class), is(true)); -// assertThat(repository.shouldRegisterExtension(null, "foo", String.class), is(false)); -// -// // check objects -// assertThat(repository.shouldRegisterExtension(null, "foo", new FakeBatchExtension()), is(false)); -// assertThat(repository.shouldRegisterExtension(null, "foo", new FakeServerExtension()), is(true)); -// assertThat(repository.shouldRegisterExtension(null, "foo", "foo"), is(false)); -// } + + private ServerPluginRepository repository; + + @After + public void stop() { + if (repository != null) { + repository.stop(); + } + } + + @Test + public void testStart() { + PluginDeployer deployer = mock(PluginDeployer.class); + File pluginFile = FileUtils.toFile(getClass().getResource("/org/sonar/server/plugins/ServerPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar")); + PluginMetadata plugin = DefaultPluginMetadata.create(pluginFile) + .setKey("artifactsize") + .setMainClass("org.sonar.plugins.artifactsize.ArtifactSizePlugin") + .addDeployedFile(pluginFile); + when(deployer.getMetadata()).thenReturn(Arrays.asList(plugin)); + + repository = new ServerPluginRepository(deployer); + repository.start(); + + assertThat(repository.getPlugins().size(), Is.is(1)); + assertThat(repository.getPlugin("artifactsize"), not(nullValue())); + assertThat(repository.getClassloader("artifactsize"), not(nullValue())); + assertThat(repository.getClass("artifactsize", "org.sonar.plugins.artifactsize.ArtifactSizeMetrics"), not(nullValue())); + assertThat(repository.getClass("artifactsize", "org.Unknown"), nullValue()); + assertThat(repository.getClass("other", "org.sonar.plugins.artifactsize.ArtifactSizeMetrics"), nullValue()); + } + + @Test + public void shouldRegisterServerExtensions() { + ServerPluginRepository repository = new ServerPluginRepository(mock(PluginDeployer.class)); + + TransientPicoContainer container = new TransientPicoContainer(); + repository.registerExtensions(container, Arrays.<Plugin>asList(new FakePlugin(Arrays.<Class>asList(FakeBatchExtension.class, FakeServerExtension.class)))); + + assertThat(container.getComponents(Extension.class).size(), is(1)); + assertThat(container.getComponents(FakeServerExtension.class).size(), is(1)); + assertThat(container.getComponents(FakeBatchExtension.class).size(), is(0)); + } + + @Test + public void shouldInvokeServerExtensionProviderss() { + ServerPluginRepository repository = new ServerPluginRepository(mock(PluginDeployer.class)); + + TransientPicoContainer container = new TransientPicoContainer(); + repository.registerExtensions(container, Arrays.<Plugin>asList(new FakePlugin(Arrays.<Class>asList(FakeExtensionProvider.class)))); + + assertThat(container.getComponents(Extension.class).size(), is(2));// provider + FakeServerExtension + assertThat(container.getComponents(FakeServerExtension.class).size(), is(1)); + assertThat(container.getComponents(FakeBatchExtension.class).size(), is(0)); + } + + public static class FakePlugin extends SonarPlugin { + private List<Class> extensions; + + public FakePlugin(List<Class> extensions) { + this.extensions = extensions; + } + + public List getExtensions() { + return extensions; + } + } public static class FakeBatchExtension implements BatchExtension { @@ -47,4 +114,12 @@ public class ServerPluginRepositoryTest { public static class FakeServerExtension implements ServerExtension { } + + public static class FakeExtensionProvider extends ExtensionProvider implements ServerExtension { + + @Override + public Object provide() { + return Arrays.asList(FakeBatchExtension.class, FakeServerExtension.class); + } + } } diff --git a/sonar-server/src/test/resources/org/sonar/server/plugins/ServerPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar b/sonar-server/src/test/resources/org/sonar/server/plugins/ServerPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar Binary files differnew file mode 100644 index 00000000000..19533234582 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/plugins/ServerPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar |