aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-07-04 16:36:16 +0200
committerDavid Gageot <david@gageot.net>2012-07-04 17:02:58 +0200
commit31b3ada78679642b54f23eb0000a2671485cfbd1 (patch)
treefa3368cc6de98d44d28a75e7b4b10ffebee89a82 /sonar-core/src
parent620edca954338d65269b26aa52e7f74b9ec52c52 (diff)
downloadsonarqube-31b3ada78679642b54f23eb0000a2671485cfbd1.tar.gz
sonarqube-31b3ada78679642b54f23eb0000a2671485cfbd1.zip
Fixes and improvements on H2 migration
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java33
-rw-r--r--sonar-core/src/test/java/org/sonar/core/dashboard/ActiveDashboardDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/dashboard/DashboardDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/duplication/DuplicationDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java (renamed from sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java)87
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java2
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/DatabaseVersionTest.java2
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java6
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java1
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java1
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java17
-rw-r--r--sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/purge/PurgeCommandsTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/resource/ResourceIndexerDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/resource/ResourceKeyUpdaterDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/review/ReviewCommentDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/review/ReviewDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/template/LoadedTemplateDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/jpa/session/ThreadLocalDatabaseSessionFactoryTest.java46
-rw-r--r--sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java29
28 files changed, 113 insertions, 183 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java
index 66521373392..dba97bb0a70 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java
@@ -45,25 +45,30 @@ public class DatabaseMigrator implements ServerComponent {
* @return true if the database has been created, false if this database is not supported
*/
public boolean createDatabase() {
- if (DdlUtils.supportsDialect(database.getDialect().getId())) {
- LoggerFactory.getLogger(getClass()).info("Create database");
- SqlSession session = myBatis.openSession();
- Connection connection = session.getConnection();
+ if (!DdlUtils.supportsDialect(database.getDialect().getId())) {
+ return false;
+ }
+
+ LoggerFactory.getLogger(getClass()).info("Create database");
+ SqlSession session = null;
+ Connection connection = null;
+ try {
+ session = myBatis.openSession();
+ connection = session.getConnection();
+ DdlUtils.createSchema(connection, database.getDialect().getId());
+ } finally {
try {
- DdlUtils.createSchema(connection, database.getDialect().getId());
- } finally {
- try {
- MyBatis.closeQuietly(session);
+ MyBatis.closeQuietly(session);
- // The connection is probably already closed by session.close()
- // but it's not documented in mybatis javadoc.
+ // The connection is probably already closed by session.close()
+ // but it's not documented in mybatis javadoc.
+ if (null != connection) {
connection.close();
- } catch (Exception e) {
- // ignore
}
+ } catch (Exception e) {
+ // ignore
}
- return true;
}
- return false;
+ return true;
}
}
diff --git a/sonar-core/src/test/java/org/sonar/core/dashboard/ActiveDashboardDaoTest.java b/sonar-core/src/test/java/org/sonar/core/dashboard/ActiveDashboardDaoTest.java
index b60bcce9e86..dc647ccd15e 100644
--- a/sonar-core/src/test/java/org/sonar/core/dashboard/ActiveDashboardDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/dashboard/ActiveDashboardDaoTest.java
@@ -21,12 +21,12 @@ package org.sonar.core.dashboard;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class ActiveDashboardDaoTest extends DaoTestCase {
+public class ActiveDashboardDaoTest extends AbstractDaoTestCase {
private ActiveDashboardDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/dashboard/DashboardDaoTest.java b/sonar-core/src/test/java/org/sonar/core/dashboard/DashboardDaoTest.java
index 36b464f6778..6311d0869da 100644
--- a/sonar-core/src/test/java/org/sonar/core/dashboard/DashboardDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/dashboard/DashboardDaoTest.java
@@ -21,7 +21,7 @@ package org.sonar.core.dashboard;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.Date;
@@ -30,7 +30,7 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
-public class DashboardDaoTest extends DaoTestCase {
+public class DashboardDaoTest extends AbstractDaoTestCase {
private DashboardDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java b/sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java
index d236ff29ce1..fdf3b225613 100644
--- a/sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java
@@ -24,14 +24,14 @@ import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.MyBatis;
import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
-public class DependencyMapperTest extends DaoTestCase {
+public class DependencyMapperTest extends AbstractDaoTestCase {
@Test
public void should_find_all() {
setupData("fixture");
diff --git a/sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java b/sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java
index 76ddf641b04..66e53638875 100644
--- a/sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java
@@ -24,14 +24,14 @@ import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.MyBatis;
import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
-public class ResourceSnapshotMapperTest extends DaoTestCase {
+public class ResourceSnapshotMapperTest extends AbstractDaoTestCase {
@Test
public void should_find_all() {
setupData("fixture");
diff --git a/sonar-core/src/test/java/org/sonar/core/duplication/DuplicationDaoTest.java b/sonar-core/src/test/java/org/sonar/core/duplication/DuplicationDaoTest.java
index c5a0cddbc5b..ce49f1f154c 100644
--- a/sonar-core/src/test/java/org/sonar/core/duplication/DuplicationDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/duplication/DuplicationDaoTest.java
@@ -21,7 +21,7 @@ package org.sonar.core.duplication;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.Arrays;
import java.util.List;
@@ -29,7 +29,7 @@ import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class DuplicationDaoTest extends DaoTestCase {
+public class DuplicationDaoTest extends AbstractDaoTestCase {
private DuplicationDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java b/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java
index 504f0a4bd78..2be3f63b85a 100644
--- a/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java
@@ -25,11 +25,11 @@ import org.sonar.core.filter.CriterionDto;
import org.sonar.core.filter.FilterColumnDto;
import org.sonar.core.filter.FilterDao;
import org.sonar.core.filter.FilterDto;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import static org.fest.assertions.Assertions.assertThat;
-public class FilterDaoTest extends DaoTestCase {
+public class FilterDaoTest extends AbstractDaoTestCase {
private FilterDao dao;
@Before
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java b/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java
index 0a6c5dc8712..9222fcd2392 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java
@@ -20,14 +20,13 @@
package org.sonar.core.persistence;
import com.google.common.collect.Maps;
-import org.apache.commons.io.IOUtils;
+import com.google.common.io.Closeables;
import org.dbunit.Assertion;
import org.dbunit.DataSourceDatabaseTester;
import org.dbunit.DatabaseUnitException;
import org.dbunit.IDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.IDatabaseConnection;
-import org.dbunit.dataset.CompositeDataSet;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
@@ -42,13 +41,11 @@ import org.junit.BeforeClass;
import org.sonar.api.config.Settings;
import java.io.InputStream;
-import java.io.StringWriter;
import java.sql.SQLException;
import static org.junit.Assert.fail;
-public abstract class DaoTestCase {
-
+public abstract class AbstractDaoTestCase {
private static Database database;
private static MyBatis myBatis;
private static DatabaseCommands databaseCommands;
@@ -58,10 +55,9 @@ public abstract class DaoTestCase {
@BeforeClass
public static void startDatabase() throws Exception {
- Settings settings = new Settings();
- settings.setProperties(Maps.fromProperties(System.getProperties()));
- boolean hasDialect = settings.hasKey("sonar.jdbc.dialect");
+ Settings settings = new Settings().setProperties(Maps.fromProperties(System.getProperties()));
+ boolean hasDialect = settings.hasKey("sonar.jdbc.dialect");
if (hasDialect) {
database = new DefaultDatabase(settings);
} else {
@@ -82,7 +78,7 @@ public abstract class DaoTestCase {
}
@After
- public void tearDownDbUnit() throws Exception {
+ public void stopConnection() throws Exception {
if (databaseTester != null) {
databaseTester.onTearDown();
}
@@ -102,40 +98,30 @@ public abstract class DaoTestCase {
return myBatis;
}
- protected final void setupData(String... testNames) {
- InputStream[] streams = new InputStream[testNames.length];
+ protected void setupData(String testName) {
+ InputStream stream = null;
try {
- for (int i = 0; i < testNames.length; i++) {
- String className = getClass().getName();
- className = String.format("/%s/%s.xml", className.replace(".", "/"), testNames[i]);
- streams[i] = getClass().getResourceAsStream(className);
- if (streams[i] == null) {
- throw new RuntimeException("Test not found :" + className);
- }
+ String className = getClass().getName();
+ className = String.format("/%s/%s.xml", className.replace(".", "/"), testName);
+ stream = getClass().getResourceAsStream(className);
+ if (stream == null) {
+ throw new RuntimeException("Test not found :" + className);
}
- setupData(streams);
-
+ setupData(stream);
} finally {
- for (InputStream stream : streams) {
- IOUtils.closeQuietly(stream);
- }
+ Closeables.closeQuietly(stream);
}
}
- protected final void setupData(InputStream... dataSetStream) {
+ private void setupData(InputStream dataStream) {
try {
- IDataSet[] dataSets = new IDataSet[dataSetStream.length];
- for (int i = 0; i < dataSetStream.length; i++) {
- ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(dataSetStream[i]));
- dataSet.addReplacementObject("[null]", null);
- dataSet.addReplacementObject("[false]", databaseCommands.getFalse());
- dataSet.addReplacementObject("[true]", databaseCommands.getTrue());
- dataSets[i] = dataSet;
- }
- CompositeDataSet compositeDataSet = new CompositeDataSet(dataSets);
+ ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(dataStream));
+ dataSet.addReplacementObject("[null]", null);
+ dataSet.addReplacementObject("[false]", databaseCommands.getFalse());
+ dataSet.addReplacementObject("[true]", databaseCommands.getTrue());
- databaseTester.setDataSet(compositeDataSet);
+ databaseTester.setDataSet(dataSet);
connection = databaseTester.getConnection();
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, databaseCommands.getDbUnitFactory());
@@ -146,11 +132,11 @@ public abstract class DaoTestCase {
}
}
- protected final void checkTables(String testName, String... tables) {
+ protected void checkTables(String testName, String... tables) {
checkTables(testName, new String[] {}, tables);
}
- protected final void checkTables(String testName, String[] excludedColumnNames, String... tables) {
+ protected void checkTables(String testName, String[] excludedColumnNames, String... tables) {
try {
IDataSet dataSet = getCurrentDataSet();
IDataSet expectedDataSet = getExpectedData(testName);
@@ -165,7 +151,7 @@ public abstract class DaoTestCase {
}
}
- protected final void assertEmptyTables(String... emptyTables) {
+ protected void assertEmptyTables(String... emptyTables) {
for (String table : emptyTables) {
try {
Assert.assertEquals("Table " + table + " not empty.", 0, getCurrentDataSet().getTable(table).getRowCount());
@@ -175,7 +161,7 @@ public abstract class DaoTestCase {
}
}
- protected final IDataSet getExpectedData(String testName) {
+ private IDataSet getExpectedData(String testName) {
String className = getClass().getName();
className = String.format("/%s/%s-result.xml", className.replace(".", "/"), testName);
@@ -183,11 +169,11 @@ public abstract class DaoTestCase {
try {
return getData(in);
} finally {
- IOUtils.closeQuietly(in);
+ Closeables.closeQuietly(in);
}
}
- protected final IDataSet getData(InputStream stream) {
+ private IDataSet getData(InputStream stream) {
try {
ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream));
dataSet.addReplacementObject("[null]", null);
@@ -199,7 +185,7 @@ public abstract class DaoTestCase {
}
}
- protected final IDataSet getCurrentDataSet() {
+ private IDataSet getCurrentDataSet() {
try {
return connection.createDataSet();
} catch (SQLException e) {
@@ -207,20 +193,6 @@ public abstract class DaoTestCase {
}
}
- protected String getCurrentDataSetAsXML() {
- return getDataSetAsXML(getCurrentDataSet());
- }
-
- protected String getDataSetAsXML(IDataSet dataset) {
- try {
- StringWriter writer = new StringWriter();
- FlatXmlDataSet.write(dataset, writer);
- return writer.getBuffer().toString();
- } catch (Exception e) {
- throw translateException("Could not build XML from dataset", e);
- }
- }
-
private static RuntimeException translateException(String msg, Exception cause) {
RuntimeException runtimeException = new RuntimeException(String.format("%s: [%s] %s", msg, cause.getClass().getName(), cause.getMessage()));
runtimeException.setStackTrace(cause.getStackTrace());
@@ -230,9 +202,4 @@ public abstract class DaoTestCase {
protected IDatabaseConnection getConnection() {
return connection;
}
-
- protected IDatabaseTester getDatabaseTester() {
- return databaseTester;
- }
-
}
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java
index 760c3e8df32..3c011b43426 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java
@@ -31,6 +31,6 @@ public class DaoUtilsTest {
public void should_list_all_dao_classes() {
List<Class<?>> daoClasses = DaoUtils.getDaoClasses();
- assertThat(daoClasses.size()).isGreaterThan(1);
+ assertThat(daoClasses).isNotEmpty();
}
}
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseVersionTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseVersionTest.java
index 1ec1e6b84aa..df1dce3e426 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseVersionTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseVersionTest.java
@@ -26,7 +26,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
-public class DatabaseVersionTest extends DaoTestCase {
+public class DatabaseVersionTest extends AbstractDaoTestCase {
@Test
public void getVersion() {
setupData("getVersion");
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java
index a2a6887ab95..250b86eb6d4 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java
@@ -45,13 +45,13 @@ public class DdlUtilsTest {
Connection connection = DriverManager.getConnection("jdbc:h2:mem:sonar_test");
DdlUtils.createSchema(connection, "h2");
- int tables = countTables(connection);
+ int tableCount = countTables(connection);
connection.close();
- assertThat(tables).isGreaterThan(30);
+ assertThat(tableCount).isGreaterThan(30);
}
- private int countTables(Connection connection) throws SQLException {
+ static int countTables(Connection connection) throws SQLException {
int count = 0;
ResultSet resultSet = connection.getMetaData().getTables(null, null, null, new String[] {"TABLE"});
while (resultSet.next()) {
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java
index 24723714c4e..64945262ef2 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java
@@ -101,6 +101,7 @@ public class DefaultDatabaseTest {
DefaultDatabase db = new DefaultDatabase(settings);
db.start();
+ db.stop();
assertThat(db.getDialect().getId(), Is.is("h2"));
assertThat(((BasicDataSource) db.getDataSource()).getMaxActive(), Is.is(1));
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java b/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
index a9690c06edb..bc893b4bd75 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
@@ -114,7 +114,6 @@ public class H2Database implements Database {
public Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.hbm2ddl.auto", "validate");
- properties.put(Environment.DIALECT, getDialect().getHibernateDialectClass().getName());
properties.put(Environment.CONNECTION_PROVIDER, CustomHibernateConnectionProvider.class.getName());
return properties;
}
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java
index f08c05f6855..f896c62a9f8 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java
@@ -24,7 +24,6 @@ import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
-import java.sql.ResultSet;
import java.sql.SQLException;
import static org.fest.assertions.Assertions.assertThat;
@@ -45,21 +44,17 @@ public class H2DatabaseTest {
@Test
public void shouldExecuteDdlAtStartup() throws SQLException {
Connection connection = db.getDataSource().getConnection();
-
- int tables = 0;
- ResultSet resultSet = connection.getMetaData().getTables(null, null, null, new String[] {"TABLE"});
- while (resultSet.next()) {
- tables++;
- }
-
+ int tableCount = DdlUtilsTest.countTables(connection);
connection.close();
- assertThat(tables).isGreaterThan(30);
+ assertThat(tableCount).isGreaterThan(30);
}
@Test
public void shouldLimitThePoolSize() {
- assertThat(((BasicDataSource) db.getDataSource()).getMaxActive()).isEqualTo(2);
- assertThat(((BasicDataSource) db.getDataSource()).getMaxIdle()).isEqualTo(2);
+ BasicDataSource dataSource = (BasicDataSource) db.getDataSource();
+
+ assertThat(dataSource.getMaxActive()).isEqualTo(2);
+ assertThat(dataSource.getMaxIdle()).isEqualTo(2);
}
}
diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
index d6461c11fb5..20c39047808 100644
--- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
@@ -21,7 +21,7 @@ package org.sonar.core.properties;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.List;
@@ -29,7 +29,7 @@ import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class PropertiesDaoTest extends DaoTestCase {
+public class PropertiesDaoTest extends AbstractDaoTestCase {
private PropertiesDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/purge/PurgeCommandsTest.java b/sonar-core/src/test/java/org/sonar/core/purge/PurgeCommandsTest.java
index cd200b5e8e3..4f9e8bc29f0 100644
--- a/sonar-core/src/test/java/org/sonar/core/purge/PurgeCommandsTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/purge/PurgeCommandsTest.java
@@ -21,12 +21,12 @@ package org.sonar.core.purge;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.MyBatis;
import java.util.Arrays;
-public class PurgeCommandsTest extends DaoTestCase {
+public class PurgeCommandsTest extends AbstractDaoTestCase {
/**
* Test that all related data is deleted.
*/
diff --git a/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java b/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java
index 9e7db168941..bfa9316d399 100644
--- a/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java
@@ -25,7 +25,7 @@ import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.resources.Scopes;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.resource.ResourceDao;
@@ -35,7 +35,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
-public class PurgeDaoTest extends DaoTestCase {
+public class PurgeDaoTest extends AbstractDaoTestCase {
private PurgeDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
index 67faa00a489..a16eec194c8 100644
--- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
@@ -21,13 +21,13 @@ package org.sonar.core.resource;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
-public class ResourceDaoTest extends DaoTestCase {
+public class ResourceDaoTest extends AbstractDaoTestCase {
private ResourceDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceIndexerDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceIndexerDaoTest.java
index 399c7fa69bc..e483fdb7245 100644
--- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceIndexerDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceIndexerDaoTest.java
@@ -23,7 +23,7 @@ import org.hamcrest.core.Is;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.resources.Qualifiers;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.sql.Connection;
import java.sql.ResultSet;
@@ -32,7 +32,7 @@ import java.sql.SQLException;
import static org.hamcrest.number.OrderingComparisons.greaterThan;
import static org.junit.Assert.assertThat;
-public class ResourceIndexerDaoTest extends DaoTestCase {
+public class ResourceIndexerDaoTest extends AbstractDaoTestCase {
private static ResourceIndexerDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceKeyUpdaterDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceKeyUpdaterDaoTest.java
index e8a1e793bf6..97367da23a7 100644
--- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceKeyUpdaterDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceKeyUpdaterDaoTest.java
@@ -23,13 +23,13 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
-public class ResourceKeyUpdaterDaoTest extends DaoTestCase {
+public class ResourceKeyUpdaterDaoTest extends AbstractDaoTestCase {
@Rule
public ExpectedException thrown = ExpectedException.none();
diff --git a/sonar-core/src/test/java/org/sonar/core/review/ReviewCommentDaoTest.java b/sonar-core/src/test/java/org/sonar/core/review/ReviewCommentDaoTest.java
index 5667b8b091c..0b44e63146c 100644
--- a/sonar-core/src/test/java/org/sonar/core/review/ReviewCommentDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/review/ReviewCommentDaoTest.java
@@ -21,11 +21,11 @@ package org.sonar.core.review;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.Date;
-public class ReviewCommentDaoTest extends DaoTestCase {
+public class ReviewCommentDaoTest extends AbstractDaoTestCase {
private ReviewCommentDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/review/ReviewDaoTest.java b/sonar-core/src/test/java/org/sonar/core/review/ReviewDaoTest.java
index ed070c34fef..b538e04ff79 100644
--- a/sonar-core/src/test/java/org/sonar/core/review/ReviewDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/review/ReviewDaoTest.java
@@ -22,7 +22,7 @@ package org.sonar.core.review;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.utils.DateUtils;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.Collection;
@@ -30,7 +30,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
-public class ReviewDaoTest extends DaoTestCase {
+public class ReviewDaoTest extends AbstractDaoTestCase {
private ReviewDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
index de524816b51..e5d3a50dcc0 100644
--- a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
@@ -22,13 +22,13 @@ package org.sonar.core.rule;
import org.hamcrest.core.Is;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.List;
import static org.junit.Assert.assertThat;
-public class RuleDaoTest extends DaoTestCase {
+public class RuleDaoTest extends AbstractDaoTestCase {
private static RuleDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/template/LoadedTemplateDaoTest.java b/sonar-core/src/test/java/org/sonar/core/template/LoadedTemplateDaoTest.java
index 3a5d0518005..fa5c9344ba9 100644
--- a/sonar-core/src/test/java/org/sonar/core/template/LoadedTemplateDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/template/LoadedTemplateDaoTest.java
@@ -21,12 +21,12 @@ package org.sonar.core.template;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class LoadedTemplateDaoTest extends DaoTestCase {
+public class LoadedTemplateDaoTest extends AbstractDaoTestCase {
private LoadedTemplateDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
index 05f73abf638..db93f779987 100644
--- a/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
@@ -21,13 +21,13 @@ package org.sonar.core.user;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
-public class AuthorDaoTest extends DaoTestCase {
+public class AuthorDaoTest extends AbstractDaoTestCase {
private AuthorDao dao;
diff --git a/sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java b/sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java
index b89a421d5cf..41bb6d24748 100644
--- a/sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java
@@ -24,14 +24,14 @@ import org.sonar.api.config.Settings;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.workflow.Comment;
import org.sonar.api.workflow.internal.DefaultReview;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.Arrays;
import java.util.Date;
import static org.fest.assertions.Assertions.assertThat;
-public class ReviewDatabaseStoreTest extends DaoTestCase {
+public class ReviewDatabaseStoreTest extends AbstractDaoTestCase {
@Test
public void store() {
diff --git a/sonar-core/src/test/java/org/sonar/jpa/session/ThreadLocalDatabaseSessionFactoryTest.java b/sonar-core/src/test/java/org/sonar/jpa/session/ThreadLocalDatabaseSessionFactoryTest.java
deleted file mode 100644
index fa1df231129..00000000000
--- a/sonar-core/src/test/java/org/sonar/jpa/session/ThreadLocalDatabaseSessionFactoryTest.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.jpa.session;
-
-import org.junit.Test;
-
-public class ThreadLocalDatabaseSessionFactoryTest {
-
-
- @Test
- public void shouldCreateOneSessionPerThread() {
-// final MemoryDatabaseConnector connector = new MemoryDatabaseConnector();
-// connector.start();
-// final DatabaseSessionFactory factory = new ThreadLocalDatabaseSessionFactory(connector);
-//
-// final DatabaseSession junitThreadSession = factory.getSession();
-// assertTrue(junitThreadSession == factory.getSession());
-//
-// new Thread() {
-// @Override
-// public void run() {
-// DatabaseSession threadSession = factory.getSession();
-// assertTrue(threadSession != junitThreadSession);
-// }
-//
-// }.start();
- }
-
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java b/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
index 58afe6240ce..78d9fe4164e 100644
--- a/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
+++ b/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
@@ -19,6 +19,8 @@
*/
package org.sonar.jpa.test;
+import org.junit.AfterClass;
+
import org.apache.commons.io.IOUtils;
import org.dbunit.Assertion;
import org.dbunit.DataSourceDatabaseTester;
@@ -53,7 +55,7 @@ import java.sql.SQLException;
import static org.junit.Assert.fail;
/**
- * Heavily duplicates DaoTestCase as long as Hibernate is in use.
+ * Heavily duplicates AbstractDaoTestCase as long as Hibernate is in use.
*/
public abstract class AbstractDbUnitTestCase {
private static Database database;
@@ -97,11 +99,18 @@ public abstract class AbstractDbUnitTestCase {
}
}
- public DatabaseSession getSession() {
+ @AfterClass
+ public static void stopDatabase() {
+ if (database != null) {
+ database.stop();
+ }
+ }
+
+ protected DatabaseSession getSession() {
return session;
}
- public DatabaseSessionFactory getSessionFactory() {
+ protected DatabaseSessionFactory getSessionFactory() {
return new DatabaseSessionFactory() {
public DatabaseSession getSession() {
@@ -113,7 +122,7 @@ public abstract class AbstractDbUnitTestCase {
};
}
- protected final void setupData(String... testNames) {
+ protected void setupData(String... testNames) {
InputStream[] streams = new InputStream[testNames.length];
try {
for (int i = 0; i < testNames.length; i++) {
@@ -134,7 +143,7 @@ public abstract class AbstractDbUnitTestCase {
}
}
- private final void setupData(InputStream... dataSetStream) {
+ private void setupData(InputStream... dataSetStream) {
try {
IDataSet[] dataSets = new IDataSet[dataSetStream.length];
for (int i = 0; i < dataSetStream.length; i++) {
@@ -155,11 +164,11 @@ public abstract class AbstractDbUnitTestCase {
}
}
- protected final void checkTables(String testName, String... tables) {
+ protected void checkTables(String testName, String... tables) {
checkTables(testName, new String[0], tables);
}
- protected final void checkTables(String testName, String[] excludedColumnNames, String... tables) {
+ protected void checkTables(String testName, String[] excludedColumnNames, String... tables) {
getSession().commit();
try {
IDataSet dataSet = getCurrentDataSet();
@@ -176,7 +185,7 @@ public abstract class AbstractDbUnitTestCase {
}
}
- private final IDataSet getExpectedData(String testName) {
+ private IDataSet getExpectedData(String testName) {
String className = getClass().getName();
className = String.format("/%s/%s-result.xml", className.replace(".", "/"), testName);
@@ -188,7 +197,7 @@ public abstract class AbstractDbUnitTestCase {
}
}
- private final IDataSet getData(InputStream stream) {
+ private IDataSet getData(InputStream stream) {
try {
ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream));
dataSet.addReplacementObject("[null]", null);
@@ -198,7 +207,7 @@ public abstract class AbstractDbUnitTestCase {
}
}
- private final IDataSet getCurrentDataSet() {
+ private IDataSet getCurrentDataSet() {
try {
return connection.createDataSet();
} catch (SQLException e) {