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;
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 {
* 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);
}
}