]> source.dussan.org Git - sonarqube.git/commitdiff
Allow to use ServerTester outside a JUnit @Rule
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 25 Apr 2014 11:07:46 +0000 (13:07 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 25 Apr 2014 11:07:46 +0000 (13:07 +0200)
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java
sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java

index ecbf00a3cc8408a46db40dec981b54b854e1494a..005f991bd9e58fd7ec52252d843e54020ae8d15e 100644 (file)
@@ -46,7 +46,7 @@ import static org.fest.assertions.Assertions.assertThat;
 public class QProfilesMediumTest {
 
   @org.junit.Rule
-  public ServerTester serverTester = new ServerTester().addExtensions(XooRulesDefinition.class, XooProfileDefinition.class);
+  public ServerTester serverTester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
 
   @Test
   public void register_profile_at_startup() throws Exception {
index f5a443491dd1f23958a83aef8f79bdc4eb8f7e8f..93e79da17014e4a4489044f7b474b5534f294d5d 100644 (file)
@@ -31,25 +31,33 @@ import java.util.Properties;
 /**
  * Entry point to implement medium tests of server components
  *
- * @since 4.3
+ * @since 4.4
  */
 public class ServerTester extends ExternalResource {
 
-  private File tempDir;
   private Platform platform;
+  private File tempDir;
+  private Object[] components;
 
-  private Object[] extensions;
-
+  /**
+   * Called only when JUnit @Rule or @ClassRule is used.
+   */
   @Override
   protected void before() {
-    tempDir = createTempDir();
+    start();
+  }
 
+  public void start() {
+    if (platform != null) {
+      throw new IllegalStateException("Already started");
+    }
+    tempDir = createTempDir();
     Properties properties = new Properties();
     properties.setProperty(CoreProperties.SONAR_HOME, tempDir.getAbsolutePath());
     properties.setProperty(DatabaseProperties.PROP_URL, "jdbc:h2:" + tempDir.getAbsolutePath() + "/h2");
     platform = new Platform();
     platform.init(properties);
-    platform.addExtensions(extensions);
+    platform.addExtensions(components);
     platform.doStart();
   }
 
@@ -65,15 +73,24 @@ public class ServerTester extends ExternalResource {
     }
   }
 
+  /**
+   * Called only when JUnit @Rule or @ClassRule is used.
+   */
   @Override
   protected void after() {
-    platform.doStop();
-    platform = null;
+    stop();
+  }
+
+  public void stop() {
+    if (platform != null) {
+      platform.doStop();
+      platform = null;
+    }
     FileUtils.deleteQuietly(tempDir);
   }
 
-  public ServerTester addExtensions(Object... extensions) {
-    this.extensions = extensions;
+  public ServerTester addComponents(Object... components) {
+    this.components = components;
     return this;
   }