]> source.dussan.org Git - sonarqube.git/commitdiff
ServerTester as a JUnit rule
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 21 Apr 2014 21:41:35 +0000 (23:41 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 21 Apr 2014 21:41:35 +0000 (23:41 +0200)
sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java
sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java

index 810a7a111db663c2bb532648acedbe8e716bb621..b76c7800ff5ea67f340bcc8abc80cf8d3d24aaa4 100644 (file)
@@ -20,8 +20,7 @@
 
 package org.sonar.server.debt;
 
-import org.junit.After;
-import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.server.debt.DebtCharacteristic;
 import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
@@ -33,17 +32,8 @@ import static org.fest.assertions.Assertions.assertThat;
 
 public class DebtMediumTest {
 
-  ServerTester serverTester = new ServerTester();
-
-  @Before
-  public void before() throws Exception {
-    serverTester.start();
-  }
-
-  @After
-  public void after() throws Exception {
-    serverTester.stop();
-  }
+  @Rule
+  public ServerTester serverTester = new ServerTester();
 
   @Test
   public void find_characteristics() throws Exception {
index 4a92e0a79b3ed4eb8ef4d344928558256e0ac05d..ff439395243f274b433c02a8e0263835196b0daa 100644 (file)
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
-
 package org.sonar.server.tester;
 
 import org.apache.commons.io.FileUtils;
+import org.junit.rules.ExternalResource;
 import org.sonar.api.CoreProperties;
+import org.sonar.api.database.DatabaseProperties;
 import org.sonar.server.platform.Platform;
 
 import java.io.File;
 import java.util.Properties;
-import java.util.UUID;
-
-public class ServerTester {
 
-  private File temp;
+/**
+ * Entry point to implement medium tests of server components
+ *
+ * @since 4.3
+ */
+public class ServerTester extends ExternalResource {
 
+  private File tempDir;
   private Platform platform;
 
-  public ServerTester() {
-    platform = new Platform();
-  }
-
-  public void start() {
-    temp = new File("target/" + UUID.randomUUID().toString());
-    temp.mkdirs();
+  @Override
+  protected void before() {
+    tempDir = createTempDir();
 
     Properties properties = new Properties();
-    properties.setProperty(CoreProperties.SONAR_HOME, temp.getAbsolutePath());
-    properties.setProperty("sonar.jdbc.dialect", "h2");
-    properties.setProperty("sonar.jdbc.url", "jdbc:h2:" + temp.getAbsolutePath() + "/h2");
-
+    properties.setProperty(CoreProperties.SONAR_HOME, tempDir.getAbsolutePath());
+    properties.setProperty(DatabaseProperties.PROP_URL, "jdbc:h2:" + tempDir.getAbsolutePath() + "/h2");
+    platform = new Platform();
     platform.init(properties);
+
     platform.doStart();
   }
 
-  public void stop() {
+  private File createTempDir() {
+    try {
+      // Technique to create a temp directory from a temp file
+      File f = File.createTempFile("SonarQube", "");
+      f.delete();
+      f.mkdir();
+      return f;
+    } catch (Exception e) {
+      throw new IllegalStateException("Fail to create temp dir", e);
+    }
+  }
+
+  @Override
+  protected void after() {
     platform.doStop();
-    FileUtils.deleteQuietly(temp);
+    platform = null;
+    FileUtils.deleteQuietly(tempDir);
   }
 
-  public <E> E get(Class<E> component) {
+  public <C> C get(Class<C> component) {
+    if (platform == null) {
+      throw new IllegalStateException("Not started");
+    }
     return platform.getContainer().getComponentByType(component);
   }
 }