]> source.dussan.org Git - sonarqube.git/commitdiff
Add ServerTester#addPluginJar()
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 30 Apr 2014 20:46:03 +0000 (22:46 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 30 Apr 2014 20:46:03 +0000 (22:46 +0200)
sonar-server/src/main/java/org/sonar/server/platform/Platform.java
sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java
sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java

index 40caa840d28e93e0671f82bc65cf84d67e89b748..76146a8f8ac3ea5f4dae223cc60f2a6a2a905d72 100644 (file)
@@ -83,6 +83,10 @@ public class Platform {
     }
   }
 
+  public boolean isStarted() {
+    return started;
+  }
+
   /**
    * Start level 1 only
    */
index 70f511e7b12dd3395eb981f022a091483a554311..e0d262fbd964b97db06777f39fd53e13dcdaa1a4 100644 (file)
@@ -264,8 +264,6 @@ class ServerComponents {
       TempFolderCleaner.class,
       new TempFolderProvider(),
       System2.INSTANCE,
-
-      /* new RuleDao working with ES */
       org.sonar.server.rule2.RuleDao.class
     ));
     components.addAll(CorePropertyDefinitions.all());
@@ -528,8 +526,8 @@ class ServerComponents {
     }
 
 
-    ServerExtensionInstaller extensionRegistrar = pico.getComponentByType(ServerExtensionInstaller.class);
-    extensionRegistrar.installExtensions(pico);
+    ServerExtensionInstaller extensionInstaller = pico.getComponentByType(ServerExtensionInstaller.class);
+    extensionInstaller.installExtensions(pico);
 
     pico.startComponents();
     executeStartupTaks(pico);
index f6dcc3150c5b768a9fda7a284b9a67db3c00e66a..77e7b12a368c34d538dad307d058aa26d08e3e43 100644 (file)
@@ -41,8 +41,7 @@ import static org.fest.assertions.Assertions.assertThat;
 public class RuleMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester()
-    .setProperty("sonar.es.http.port", "8888");
+  public static ServerTester tester = new ServerTester();
 
   @After
   public void clear_data_store() {
@@ -85,13 +84,11 @@ public class RuleMediumTest {
     RuleQuery query = service.newRuleQuery().setRepositories(Arrays.asList("findbugs", "java"));
     Results results = service.search(query, new QueryOptions());
 
-
     assertThat(results.getTotal()).isEqualTo(1);
     assertThat(results.getHits()).hasSize(1);
     assertThat(Iterables.getFirst(results.getHits(), null).getFieldAsString("key")).isEqualTo("S002");
   }
 
-
   private RuleDto newRuleDto(RuleKey ruleKey) {
     return new RuleDto()
       .setRuleKey(ruleKey.rule())
index e3b0e48063be2f42e7f4712625bdd993f0e5d41a..1d0da14deb4fa7c5457fb7d2d68a5f5b96d4da81 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.tester;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import org.apache.commons.io.FileUtils;
 import org.junit.rules.ExternalResource;
@@ -39,11 +40,16 @@ import java.util.Properties;
  */
 public class ServerTester extends ExternalResource {
 
-  private Platform platform;
-  private File tempDir;
-  private List components = Lists.newArrayList(DataStoreCleanup.class);
+  private final Platform platform;
+  private final File homeDir;
+  private final List components = Lists.newArrayList(DataStoreCleanup.class);
   private final Properties initialProps = new Properties();
 
+  public ServerTester() {
+    homeDir = createTempDir();
+    platform = new Platform();
+  }
+
   /**
    * Called only when JUnit @Rule or @ClassRule is used.
    */
@@ -57,12 +63,11 @@ public class ServerTester extends ExternalResource {
    */
   public void start() {
     checkNotStarted();
-    tempDir = createTempDir();
+
     Properties properties = new Properties();
     properties.putAll(initialProps);
-    properties.setProperty(CoreProperties.SONAR_HOME, tempDir.getAbsolutePath());
-    properties.setProperty(DatabaseProperties.PROP_URL, "jdbc:h2:" + tempDir.getAbsolutePath() + "/h2");
-    platform = new Platform();
+    properties.setProperty(CoreProperties.SONAR_HOME, homeDir.getAbsolutePath());
+    properties.setProperty(DatabaseProperties.PROP_URL, "jdbc:h2:" + homeDir.getAbsolutePath() + "/h2");
     platform.init(properties);
     platform.addComponents(components);
     platform.doStart();
@@ -92,11 +97,10 @@ public class ServerTester extends ExternalResource {
    * This method should not be called by test when ServerTester is annotated with {@link org.junit.Rule}
    */
   public void stop() {
-    if (platform != null) {
+    if (platform.isStarted()) {
       platform.doStop();
-      platform = null;
     }
-    FileUtils.deleteQuietly(tempDir);
+    FileUtils.deleteQuietly(homeDir);
   }
 
   /**
@@ -111,6 +115,18 @@ public class ServerTester extends ExternalResource {
     return this;
   }
 
+  public ServerTester addPluginJar(File jar) {
+    Preconditions.checkArgument(jar.exists() && jar.isFile(), "Plugin JAR file does not exist: " + jar.getAbsolutePath());
+    try {
+      File pluginsDir = new File(homeDir, "extensions/plugins");
+      FileUtils.forceMkdir(pluginsDir);
+      FileUtils.copyFileToDirectory(jar, pluginsDir);
+      return this;
+    } catch (Exception e) {
+      throw new IllegalStateException("Fail to copy plugin JAR file: " + jar.getAbsolutePath(), e);
+    }
+  }
+
   /**
    * Set a property available for startup. Must be called before {@link #start()}.
    */
@@ -121,7 +137,7 @@ public class ServerTester extends ExternalResource {
   }
 
   /**
-   * Truncate all db tables and es indices
+   * Truncate all db tables and es indices. Can be executed only if ServerTester is started.
    */
   public void clearDataStores() {
     checkStarted();
@@ -137,13 +153,13 @@ public class ServerTester extends ExternalResource {
   }
 
   private void checkStarted() {
-    if (platform == null) {
+    if (!platform.isStarted()) {
       throw new IllegalStateException("Not started");
     }
   }
 
   private void checkNotStarted() {
-    if (platform != null) {
+    if (platform.isStarted()) {
       throw new IllegalStateException("Already started");
     }
   }