From 15c3f708089ec2b51ee0ceae877647a64b2bf3a7 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 21 Apr 2014 23:41:35 +0200 Subject: [PATCH] ServerTester as a JUnit rule --- .../org/sonar/server/debt/DebtMediumTest.java | 16 +----- .../org/sonar/server/tester/ServerTester.java | 55 ++++++++++++------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java index 810a7a111db..b76c7800ff5 100644 --- a/sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtMediumTest.java @@ -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 { diff --git a/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java b/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java index 4a92e0a79b3..ff439395243 100644 --- a/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java +++ b/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java @@ -17,46 +17,63 @@ * 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 get(Class component) { + public C get(Class component) { + if (platform == null) { + throw new IllegalStateException("Not started"); + } return platform.getContainer().getComponentByType(component); } } -- 2.39.5