]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2507 add unit tests to ServerPluginRepository
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 10 Jun 2011 14:17:01 +0000 (16:17 +0200)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 10 Jun 2011 14:17:01 +0000 (16:17 +0200)
sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java
sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginRepository.java
sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java
sonar-server/src/test/resources/org/sonar/server/plugins/ServerPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar [new file with mode: 0644]

index b39126da4bb140f5b04969b61d25213fab47020a..a5c9281dbd3cb53be9898557a50c5ffd6ceb60f5 100644 (file)
@@ -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);
 
index 8f175f5139d7f2b201318fcc224160c3386d8723..a3bd938116f566e29321b8a83d2ad32988d2e6e2 100644 (file)
@@ -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);
index 82af3fcc7700a03f0aa20a0460ae32351f2ee939..dcc85e5b519de299de0d973bd6442b961a352f74 100644 (file)
  */
 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
new file mode 100644 (file)
index 0000000..1953323
Binary files /dev/null and b/sonar-server/src/test/resources/org/sonar/server/plugins/ServerPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar differ