]> source.dussan.org Git - sonarqube.git/commitdiff
Better testing
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 26 Oct 2012 18:29:12 +0000 (20:29 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 26 Oct 2012 18:29:12 +0000 (20:29 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/ExtensionUtilsTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/JdbcDriverHolderTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/MetricProviderTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempDirectoriesTest.java
sonar-batch/src/test/resources/org/sonar/batch/bootstrap/JdbcDriverHolderTest/foo.jar [deleted file]
sonar-batch/src/test/resources/org/sonar/batch/bootstrap/JdbcDriverHolderTest/jdbc-driver.jar [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectReactor.java

index ece6282fbb0cdbde88117f0e810f1ae6fc1058e6..b8638c914535d41a241174969c8d70bb13efc24e 100644 (file)
@@ -72,7 +72,9 @@ public class BootstrapModule extends Module {
     container.addSingleton(I18nManager.class);
     container.addSingleton(RuleI18nManager.class);
     for (Object component : boostrapperComponents) {
-      container.addSingleton(component);
+      if (component != null) {
+        container.addSingleton(component);
+      }
     }
     container.addSingleton(BootstrapExtensionExecutor.class);
     if (!isMavenPluginExecutorRegistered()) {
index 56f17e613549733ba735e80bd9588a59881953d4..8e75b5518191250ab765852260f6c85203909610 100644 (file)
@@ -106,6 +106,9 @@ public class JdbcDriverHolder {
   public void stop() {
     if (classLoader != null) {
       classLoader.clearReferencesJdbc();
+      if (Thread.currentThread().getContextClassLoader()==classLoader) {
+        Thread.currentThread().setContextClassLoader(classLoader.getParent());
+      }
       classLoader = null;
     }
   }
index b6d20d190e6378f3dc1992bda45274c90aab5142..105a3f693b5e7025b7143bb0bcc3c45b5accf719 100644 (file)
@@ -97,6 +97,7 @@ public abstract class Module {
     try {
       doStop();
       container.stopComponents();
+      container.removeChild();
     } catch (Exception e) {
       // ignore
     }
@@ -108,8 +109,8 @@ public abstract class Module {
   }
 
   /**
-   * Implementation of this method must not contain conditional logic and just should contain several invocations of
-   * {@link #addCoreSingleton(Object)}, {@link #addExtension(org.sonar.api.platform.PluginMetadata, Object)} or {@link #addAdapter(ComponentAdapter)}.
+   * Implementation of this method must not contain conditional logic and just should contain several invocations on
+   * {@link #container}.
    */
   protected abstract void configure();
 
index 1f7004956d87a39d123016450eeae78c3983a54a..2b57de9bd402adcb80ea172344b994e613a17aae 100644 (file)
  */
 package org.sonar.batch.bootstrap;
 
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
 import org.junit.Test;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.batch.maven.MavenPluginHandler;
 import org.sonar.api.resources.Project;
+import org.sonar.batch.FakeMavenPluginExecutor;
 import org.sonar.batch.MavenPluginExecutor;
 
+import static org.fest.assertions.Assertions.assertThat;
+
+
 public class BootstrapModuleTest {
 
-  class MyMavenPluginExecutor implements MavenPluginExecutor {
+  private ProjectReactor reactor = new ProjectReactor(ProjectDefinition.create());
+
+  @Test
+  public void should_register_fake_maven_executor_if_not_maven_env() {
+    BootstrapModule module = new BootstrapModule(reactor, null, MyMavenPluginExecutor.class);
+    module.init();
+
+    assertThat(module.isMavenPluginExecutorRegistered()).isTrue();
+    assertThat(module.container.getComponentByType(MavenPluginExecutor.class)).isInstanceOf(MyMavenPluginExecutor.class);
+  }
+
+  @Test
+  public void should_use_plugin_executor_provided_by_maven() {
+    BootstrapModule module = new BootstrapModule(reactor);
+    module.init();
+    assertThat(module.isMavenPluginExecutorRegistered()).isFalse();
+    assertThat(module.container.getComponentByType(MavenPluginExecutor.class)).isInstanceOf(FakeMavenPluginExecutor.class);
+  }
+
+  @Test
+  public void should_register_bootstrap_components() {
+    BootstrapModule module = new BootstrapModule(reactor, new FakeComponent());
+    module.init();
+
+    assertThat(module.container).isNotNull();
+    assertThat(module.container.getComponentByType(FakeComponent.class)).isNotNull();
+    assertThat(module.container.getComponentByType(ProjectReactor.class)).isSameAs(reactor);
+  }
+
+  @Test
+  public void should_not_fail_if_no_bootstrap_components() {
+    BootstrapModule module = new BootstrapModule(reactor);
+    module.init();
+
+    assertThat(module.container).isNotNull();
+    assertThat(module.container.getComponentByType(ProjectReactor.class)).isSameAs(reactor);
+  }
+
+  public static class FakeComponent {
+
+  }
+
+  public static class MyMavenPluginExecutor implements MavenPluginExecutor {
     public void execute(Project project, ProjectDefinition projectDef, String goal) {
     }
 
@@ -39,14 +82,4 @@ public class BootstrapModuleTest {
       return handler;
     }
   }
-
-  @Test
-  public void shouldSearchMavenPluginExecutor() {
-    ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create());
-    BootstrapModule module = new BootstrapModule(projectReactor, null, MyMavenPluginExecutor.class);
-    assertThat(module.isMavenPluginExecutorRegistered(), is(true));
-
-    module = new BootstrapModule(projectReactor);
-    assertThat(module.isMavenPluginExecutorRegistered(), is(false));
-  }
 }
index ef85fb296c45d22d465ff7be19ff60206dd5d741..5b08e03ccab4fd50bab966fac738605b012ba120 100644 (file)
@@ -27,66 +27,65 @@ import org.sonar.api.batch.SupportedEnvironment;
 import org.sonar.batch.bootstrapper.EnvironmentInformation;
 import org.sonar.core.NotDryRun;
 
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
 
 public class ExtensionUtilsTest {
 
   @Test
   public void shouldBeBatchInstantiationStrategy() {
-    assertThat(ExtensionUtils.isInstantiationStrategy(BatchService.class, InstantiationStrategy.BATCH), is(true));
-    assertThat(ExtensionUtils.isInstantiationStrategy(new BatchService(), InstantiationStrategy.BATCH), is(true));
-    assertThat(ExtensionUtils.isInstantiationStrategy(ProjectService.class, InstantiationStrategy.BATCH), is(false));
-    assertThat(ExtensionUtils.isInstantiationStrategy(new ProjectService(), InstantiationStrategy.BATCH), is(false));
-    assertThat(ExtensionUtils.isInstantiationStrategy(DefaultService.class, InstantiationStrategy.BATCH), is(false));
-    assertThat(ExtensionUtils.isInstantiationStrategy(new DefaultService(), InstantiationStrategy.BATCH), is(false));
+    assertThat(ExtensionUtils.isInstantiationStrategy(BatchService.class, InstantiationStrategy.BATCH)).isTrue();
+    assertThat(ExtensionUtils.isInstantiationStrategy(new BatchService(), InstantiationStrategy.BATCH)).isTrue();
+    assertThat(ExtensionUtils.isInstantiationStrategy(ProjectService.class, InstantiationStrategy.BATCH)).isFalse();
+    assertThat(ExtensionUtils.isInstantiationStrategy(new ProjectService(), InstantiationStrategy.BATCH)).isFalse();
+    assertThat(ExtensionUtils.isInstantiationStrategy(DefaultService.class, InstantiationStrategy.BATCH)).isFalse();
+    assertThat(ExtensionUtils.isInstantiationStrategy(new DefaultService(), InstantiationStrategy.BATCH)).isFalse();
   }
 
   @Test
   public void shouldBeProjectInstantiationStrategy() {
-    assertThat(ExtensionUtils.isInstantiationStrategy(BatchService.class, InstantiationStrategy.PROJECT), is(false));
-    assertThat(ExtensionUtils.isInstantiationStrategy(new BatchService(), InstantiationStrategy.PROJECT), is(false));
-    assertThat(ExtensionUtils.isInstantiationStrategy(ProjectService.class, InstantiationStrategy.PROJECT), is(true));
-    assertThat(ExtensionUtils.isInstantiationStrategy(new ProjectService(), InstantiationStrategy.PROJECT), is(true));
-    assertThat(ExtensionUtils.isInstantiationStrategy(DefaultService.class, InstantiationStrategy.PROJECT), is(true));
-    assertThat(ExtensionUtils.isInstantiationStrategy(new DefaultService(), InstantiationStrategy.PROJECT), is(true));
+    assertThat(ExtensionUtils.isInstantiationStrategy(BatchService.class, InstantiationStrategy.PROJECT)).isFalse();
+    assertThat(ExtensionUtils.isInstantiationStrategy(new BatchService(), InstantiationStrategy.PROJECT)).isFalse();
+    assertThat(ExtensionUtils.isInstantiationStrategy(ProjectService.class, InstantiationStrategy.PROJECT)).isTrue();
+    assertThat(ExtensionUtils.isInstantiationStrategy(new ProjectService(), InstantiationStrategy.PROJECT)).isTrue();
+    assertThat(ExtensionUtils.isInstantiationStrategy(DefaultService.class, InstantiationStrategy.PROJECT)).isTrue();
+    assertThat(ExtensionUtils.isInstantiationStrategy(new DefaultService(), InstantiationStrategy.PROJECT)).isTrue();
   }
 
   @Test
   public void testIsBatchExtension() {
-    assertThat(ExtensionUtils.isBatchExtension(BatchService.class), is(true));
-    assertThat(ExtensionUtils.isBatchExtension(new BatchService()), is(true));
+    assertThat(ExtensionUtils.isBatchExtension(BatchService.class)).isTrue();
+    assertThat(ExtensionUtils.isBatchExtension(new BatchService())).isTrue();
 
-    assertThat(ExtensionUtils.isBatchExtension(ServerService.class), is(false));
-    assertThat(ExtensionUtils.isBatchExtension(new ServerService()), is(false));
+    assertThat(ExtensionUtils.isBatchExtension(ServerService.class)).isFalse();
+    assertThat(ExtensionUtils.isBatchExtension(new ServerService())).isFalse();
   }
 
   @Test
   public void shouldCheckEnvironment() {
-    assertThat(ExtensionUtils.supportsEnvironment(new MavenService(), new EnvironmentInformation("maven", "2.2.1")), is(true));
-    assertThat(ExtensionUtils.supportsEnvironment(new BuildToolService(), new EnvironmentInformation("maven", "2.2.1")), is(true));
-    assertThat(ExtensionUtils.supportsEnvironment(new DefaultService(), new EnvironmentInformation("maven", "2.2.1")), is(true));
+    assertThat(ExtensionUtils.supportsEnvironment(new MavenService(), new EnvironmentInformation("maven", "2.2.1"))).isTrue();
+    assertThat(ExtensionUtils.supportsEnvironment(new BuildToolService(), new EnvironmentInformation("maven", "2.2.1"))).isTrue();
+    assertThat(ExtensionUtils.supportsEnvironment(new DefaultService(), new EnvironmentInformation("maven", "2.2.1"))).isTrue();
 
-    assertThat(ExtensionUtils.supportsEnvironment(new BuildToolService(), new EnvironmentInformation("eclipse", "0.1")), is(false));
+    assertThat(ExtensionUtils.supportsEnvironment(new BuildToolService(), new EnvironmentInformation("eclipse", "0.1"))).isFalse();
   }
 
   @Test
   public void shouldBeMavenExtensionOnly() {
-    assertThat(ExtensionUtils.isMavenExtensionOnly(MavenService.class), is(true));
-    assertThat(ExtensionUtils.isMavenExtensionOnly(BuildToolService.class), is(false));
+    assertThat(ExtensionUtils.isMavenExtensionOnly(DefaultService.class)).isFalse();
+    assertThat(ExtensionUtils.isMavenExtensionOnly(new DefaultService())).isFalse();
+    assertThat(ExtensionUtils.isMavenExtensionOnly(MavenService.class)).isTrue();
+    assertThat(ExtensionUtils.isMavenExtensionOnly(new MavenService())).isTrue();
+    assertThat(ExtensionUtils.isMavenExtensionOnly(BuildToolService.class)).isFalse();
+    assertThat(ExtensionUtils.isMavenExtensionOnly(new BuildToolService())).isFalse();
   }
 
-//  @Test
-//  public void shouldCheckDryRun() {
-//    assertThat(ExtensionUtils.supportsDryRun(BatchService.class, true), is(true));
-//    assertThat(ExtensionUtils.supportsDryRun(PersistentService.class, true), is(false));
-//  }
-//
-//  @Test
-//  public void shouldNotCheckDryRun() {
-//    assertThat(ExtensionUtils.supportsDryRun(BatchService.class, false), is(true));
-//    assertThat(ExtensionUtils.supportsDryRun(PersistentService.class, false), is(true));
-//  }
+  @Test
+  public void shouldSupportDryRun() {
+    assertThat(ExtensionUtils.supportsDryRun(BatchService.class)).isTrue();
+    assertThat(ExtensionUtils.supportsDryRun(new BatchService())).isTrue();
+    assertThat(ExtensionUtils.supportsDryRun(PersistentService.class)).isFalse();
+    assertThat(ExtensionUtils.supportsDryRun(new PersistentService())).isFalse();
+  }
 
   @InstantiationStrategy(InstantiationStrategy.BATCH)
   public static class BatchService implements BatchExtension {
index fe97564a344a075002c6f28617b111b1659eaf97..ffa5cec799fca6974270d7b1e52e85ba07ae3c48 100644 (file)
@@ -24,7 +24,6 @@ import org.junit.Before;
 import org.junit.Test;
 
 import java.io.File;
-import java.net.URISyntaxException;
 import java.net.URL;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -48,15 +47,26 @@ public class JdbcDriverHolderTest {
   }
 
   @Test
-  public void should_extend_classloader_with_jdbc_driver() throws URISyntaxException {
-    /* foo.jar has just one file /foo/foo.txt */
-    assertThat(getClass().getClassLoader().getResource("foo/foo.txt")).isNull();
-
-    URL url = getClass().getResource("/org/sonar/batch/bootstrap/JdbcDriverHolderTest/foo.jar");
-    JdbcDriverHolder.JdbcDriverClassLoader classloader = JdbcDriverHolder.initClassloader(new File(url.toURI()));
-    assertThat(classloader).isNotNull();
-    assertThat(classloader.getResource("foo/foo.txt")).isNotNull();
-    assertThat(Thread.currentThread().getContextClassLoader()).isSameAs(classloader);
+  public void should_extend_classloader_with_jdbc_driver() throws Exception {
+    /* jdbc-driver.jar has just one file /foo/foo.txt */
+    assertThat(Thread.currentThread().getContextClassLoader().getResource("foo/foo.txt")).isNull();
+
+    File fakeDriver = new File(getClass().getResource("/org/sonar/batch/bootstrap/JdbcDriverHolderTest/jdbc-driver.jar").toURI());
+    TempDirectories tempDirectories = mock(TempDirectories.class);
+    when(tempDirectories.getRoot()).thenReturn(fakeDriver.getParentFile());
+    ServerClient server = mock(ServerClient.class);
+
+    JdbcDriverHolder holder = new JdbcDriverHolder(mock(DryRun.class), tempDirectories, server);
+    holder.start();
+
+    verify(server).download("/deploy/jdbc-driver.jar", fakeDriver);
+    assertThat(holder.getClassLoader().getResource("foo/foo.txt")).isNotNull();
+    assertThat(Thread.currentThread().getContextClassLoader()).isSameAs(holder.getClassLoader());
+    assertThat(holder.getClassLoader().getParent()).isSameAs(getClass().getClassLoader());
+
+    holder.stop();
+    assertThat(Thread.currentThread().getContextClassLoader()).isSameAs(getClass().getClassLoader());
+    assertThat(holder.getClassLoader()).isNull();
   }
 
   @Test
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/MetricProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/MetricProviderTest.java
new file mode 100644 (file)
index 0000000..0e23140
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.batch.bootstrap;
+
+import org.junit.Test;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.Metrics;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class MetricProviderTest {
+  @Test
+  public void should_provide_at_least_core_metrics() {
+    MetricProvider provider = new MetricProvider();
+    List<Metric> metrics = provider.provide();
+
+    assertThat(metrics).hasSize(CoreMetrics.getMetrics().size());
+    assertThat(metrics).onProperty("key").contains("ncloc");
+  }
+
+  @Test
+  public void should_provide_plugin_metrics() {
+    Metrics factory = new Metrics(){
+      public List<Metric> getMetrics() {
+        return Arrays.asList(new Metric.Builder("custom", "Custom", Metric.ValueType.FLOAT).create());
+      }
+    };
+    MetricProvider provider = new MetricProvider(new Metrics[]{factory});
+    List<Metric> metrics = provider.provide();
+
+    assertThat(metrics.size()).isEqualTo(1 + CoreMetrics.getMetrics().size());
+    assertThat(metrics).onProperty("key").contains("custom");
+  }
+}
index ce5e7929ea7bcb767eaa8e391bd120b4f903c3f9..7eb4d8b304fb4f1ecb801b7fc53294edf9a0660b 100644 (file)
@@ -49,6 +49,7 @@ public class TempDirectoriesTest {
     assertThat(tempDirectories.getRoot()).isNotNull();
     assertThat(tempDirectories.getRoot()).exists();
     assertThat(tempDirectories.getRoot()).isDirectory();
+    assertThat(tempDirectories.getDir("")).isEqualTo(tempDirectories.getRoot());
   }
 
   @Test
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/JdbcDriverHolderTest/foo.jar b/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/JdbcDriverHolderTest/foo.jar
deleted file mode 100644 (file)
index c2bde4e..0000000
Binary files a/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/JdbcDriverHolderTest/foo.jar and /dev/null differ
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/JdbcDriverHolderTest/jdbc-driver.jar b/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/JdbcDriverHolderTest/jdbc-driver.jar
new file mode 100644 (file)
index 0000000..c2bde4e
Binary files /dev/null and b/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/JdbcDriverHolderTest/jdbc-driver.jar differ
index caad3cc3d12c2fe06fe0bc10945c79641436a6d2..20ce3d21fbb65e0bf018157d57d8e3650dfc2815 100644 (file)
@@ -25,7 +25,7 @@ import java.util.List;
 /**
  * @since 2.9
  */
-public final class ProjectReactor {
+public class ProjectReactor {
 
   private ProjectDefinition root;