aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2014-02-20 10:28:11 +0100
committerJulien Lancelot <julien.lancelot@gmail.com>2014-02-25 18:55:35 +0100
commit4568cdc77f65907bce502fc1b94ac84dae83ba29 (patch)
treed298a9c1707e424397ac1233a7cd6dc331a603df /sonar-core/src
parent6690248e5ed039ea734aa09b6ccdd4a311c0a731 (diff)
downloadsonarqube-4568cdc77f65907bce502fc1b94ac84dae83ba29.tar.gz
sonarqube-4568cdc77f65907bce502fc1b94ac84dae83ba29.zip
SONAR-4996 Fix unit test on migration
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java6
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java73
2 files changed, 45 insertions, 34 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
index 99947e58009..1bff5b512bb 100644
--- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
@@ -20,7 +20,7 @@
package org.sonar.core.measure;
import org.junit.Before;
-import org.junit.Rule;
+import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
@@ -53,8 +53,8 @@ public class MeasureFilterExecutorTest {
private MeasureFilterExecutor executor;
- @Rule
- public TestDatabase db = new TestDatabase();
+ @ClassRule
+ public static TestDatabase db = new TestDatabase();
@Before
public void before() {
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java b/sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java
index 389a565e336..00eb8c66033 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java
@@ -64,17 +64,20 @@ import java.util.Properties;
import static org.junit.Assert.fail;
+/**
+ * This class should be call using @ClassRule, in order to create the schema once per test class.
+ * Data will be truncated each time you call prepareDbUnit().
+ */
public class TestDatabase extends ExternalResource {
private static final Logger LOG = LoggerFactory.getLogger(TestDatabase.class);
- private static Database db;
- private static DatabaseCommands commands;
- private static IDatabaseTester tester;
- private static MyBatis myBatis;
+ private Database db;
+ private DatabaseCommands commands;
+ private IDatabaseTester tester;
+ private MyBatis myBatis;
private String schemaPath = null;
-
public TestDatabase schema(Class baseClass, String filename) {
String path = StringUtils.replaceChars(baseClass.getCanonicalName(), '.', '/');
schemaPath = path + "/" + filename;
@@ -83,36 +86,41 @@ public class TestDatabase extends ExternalResource {
@Override
protected void before() throws Throwable {
- if (db == null) {
- Settings settings = new Settings().setProperties(Maps.fromProperties(System.getProperties()));
- if (settings.hasKey("orchestrator.configUrl")) {
- loadOrchestratorSettings(settings);
- }
- for (String key : settings.getKeysStartingWith("sonar.jdbc")) {
- LOG.info(key + ": " + settings.getString(key));
- }
- boolean hasDialect = settings.hasKey("sonar.jdbc.dialect");
- if (hasDialect) {
- db = new DefaultDatabase(settings);
- } else {
- db = new H2Database("h2Tests" + DigestUtils.md5Hex(StringUtils.defaultString(schemaPath)), schemaPath == null);
- }
- db.start();
- if (schemaPath != null) {
- // will fail if not H2
- ((H2Database) db).executeScript(schemaPath);
- }
- LOG.info("Test Database: " + db);
+ Settings settings = new Settings().setProperties(Maps.fromProperties(System.getProperties()));
+ if (settings.hasKey("orchestrator.configUrl")) {
+ loadOrchestratorSettings(settings);
+ }
+ for (String key : settings.getKeysStartingWith("sonar.jdbc")) {
+ LOG.info(key + ": " + settings.getString(key));
+ }
+ boolean hasDialect = settings.hasKey("sonar.jdbc.dialect");
+ if (hasDialect) {
+ db = new DefaultDatabase(settings);
+ } else {
+ db = new H2Database("h2Tests" + DigestUtils.md5Hex(StringUtils.defaultString(schemaPath)), schemaPath == null);
+ }
+ db.start();
+ if (schemaPath != null) {
+ // will fail if not H2
+ ((H2Database) db).executeScript(schemaPath);
+ }
+ LOG.info("Test Database: " + db);
- commands = DatabaseCommands.forDialect(db.getDialect());
- tester = new DataSourceDatabaseTester(db.getDataSource());
+ commands = DatabaseCommands.forDialect(db.getDialect());
+ tester = new DataSourceDatabaseTester(db.getDataSource());
+
+ myBatis = new MyBatis(db, new Logback());
+ myBatis.start();
- myBatis = new MyBatis(db, new Logback());
- myBatis.start();
- }
commands.truncateDatabase(db.getDataSource());
}
+ @Override
+ protected void after() {
+ db.stop();
+ db = null;
+ }
+
public Database database() {
return db;
}
@@ -148,7 +156,7 @@ public class TestDatabase extends ExternalResource {
public int count(String sql) {
Connection connection = null;
PreparedStatement stmt = null;
- ResultSet rs = null;
+ ResultSet rs = null;
try {
connection = openConnection();
stmt = connection.prepareStatement(sql);
@@ -170,6 +178,9 @@ public class TestDatabase extends ExternalResource {
public void prepareDbUnit(Class testClass, String... testNames) {
InputStream[] streams = new InputStream[testNames.length];
try {
+ // Purge previous data
+ commands.truncateDatabase(db.getDataSource());
+
for (int i = 0; i < testNames.length; i++) {
String path = "/" + testClass.getName().replace('.', '/') + "/" + testNames[i];
streams[i] = testClass.getResourceAsStream(path);