diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2014-02-20 10:28:11 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2014-02-25 18:55:35 +0100 |
commit | 4568cdc77f65907bce502fc1b94ac84dae83ba29 (patch) | |
tree | d298a9c1707e424397ac1233a7cd6dc331a603df | |
parent | 6690248e5ed039ea734aa09b6ccdd4a311c0a731 (diff) | |
download | sonarqube-4568cdc77f65907bce502fc1b94ac84dae83ba29.tar.gz sonarqube-4568cdc77f65907bce502fc1b94ac84dae83ba29.zip |
SONAR-4996 Fix unit test on migration
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java | 6 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java | 73 | ||||
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkDurationFactory.java | 7 | ||||
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/db/migrations/debt/IssueMigrationTest.java | 8 | ||||
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/db/migrations/violation/ViolationMigrationTest.java | 7 | ||||
-rw-r--r-- | sonar-server/src/test/resources/org/sonar/server/db/migrations/debt/IssueMigrationTest/schema.sql (renamed from sonar-server/src/test/resources/org/sonar/server/db/migrations/debt/IssueMigrationTest/issueSchema.sql) | 0 |
6 files changed, 51 insertions, 50 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); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkDurationFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkDurationFactory.java index 79ac141f567..03a2ed62b8e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkDurationFactory.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkDurationFactory.java @@ -55,11 +55,4 @@ public final class WorkDurationFactory implements BatchComponent, ServerComponen return settings.getInt(CoreProperties.HOURS_IN_DAY); } - /** - * @since 4.3 - */ - public WorkDuration createFromSeconds(long durationInSeconds) { - return WorkDuration.createFromSeconds(durationInSeconds, hoursInDay); - } - } diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/debt/IssueMigrationTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/debt/IssueMigrationTest.java index 3b484aa3061..bcef449b73c 100644 --- a/sonar-server/src/test/java/org/sonar/server/db/migrations/debt/IssueMigrationTest.java +++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/debt/IssueMigrationTest.java @@ -21,8 +21,7 @@ package org.sonar.server.db.migrations.debt; import org.junit.Before; -import org.junit.Ignore; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -37,12 +36,11 @@ import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; import static org.mockito.Mockito.when; -@Ignore @RunWith(MockitoJUnitRunner.class) public class IssueMigrationTest { - @Rule - public TestDatabase db = new TestDatabase().schema(getClass(), "issueSchema.sql"); + @ClassRule + public static TestDatabase db = new TestDatabase().schema(IssueMigrationTest.class, "schema.sql"); @Mock System2 system2; diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/violation/ViolationMigrationTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/violation/ViolationMigrationTest.java index 360e699a802..851615db368 100644 --- a/sonar-server/src/test/java/org/sonar/server/db/migrations/violation/ViolationMigrationTest.java +++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/violation/ViolationMigrationTest.java @@ -19,7 +19,7 @@ */ package org.sonar.server.db.migrations.violation; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.core.persistence.TestDatabase; @@ -27,12 +27,11 @@ import org.sonar.core.persistence.TestDatabase; import java.util.Set; import static org.fest.assertions.Assertions.assertThat; -import static org.fest.assertions.Fail.fail; public class ViolationMigrationTest { - @Rule - public TestDatabase db = new TestDatabase().schema(getClass(), "schema.sql"); + @ClassRule + public static TestDatabase db = new TestDatabase().schema(ViolationMigrationTest.class, "schema.sql"); @Test public void migrate_violations() throws Exception { diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/debt/IssueMigrationTest/issueSchema.sql b/sonar-server/src/test/resources/org/sonar/server/db/migrations/debt/IssueMigrationTest/schema.sql index f3f71cfa229..f3f71cfa229 100644 --- a/sonar-server/src/test/resources/org/sonar/server/db/migrations/debt/IssueMigrationTest/issueSchema.sql +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/debt/IssueMigrationTest/schema.sql |