aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-11-02 20:18:48 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-11-02 21:11:52 +0400
commit5934c582baf9f8959e188e698dbf85ae03d2b329 (patch)
tree17768aad89a9a8ce51b94154525e09e9ee3c1371
parentf01f06c9c251159b9a5b56897d11df27c09837a4 (diff)
downloadsonarqube-5934c582baf9f8959e188e698dbf85ae03d2b329.tar.gz
sonarqube-5934c582baf9f8959e188e698dbf85ae03d2b329.zip
Simplify MyBatis tests by reusing AbstractDbUnitTestCase
-rw-r--r--sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql4
-rw-r--r--sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java29
-rw-r--r--sonar-core/src/test/java/org/sonar/persistence/dao/DuplicationDaoTest.java155
-rw-r--r--sonar-core/src/test/java/org/sonar/persistence/dao/RuleDaoTest.java88
4 files changed, 28 insertions, 248 deletions
diff --git a/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql b/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql
index 66adfa97455..344c11868e1 100644
--- a/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql
+++ b/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql
@@ -1,7 +1,7 @@
-- Version 2.11
-- All the rows inserted during Rails migrations. Rows inserted during server startup tasks (Java) are excluded : rules, profiles, metrics, ...
--- Note: do not split a request on multiple lines and do not end with ;
+
INSERT INTO ACTIVE_DASHBOARDS(ID, DASHBOARD_ID, USER_ID, ORDER_INDEX) VALUES (1, 1, null, 1);
ALTER TABLE ACTIVE_DASHBOARDS ALTER COLUMN ID RESTART WITH 2;
@@ -179,4 +179,4 @@ INSERT INTO WIDGETS(ID, DASHBOARD_ID, WIDGET_KEY, NAME, DESCRIPTION, COLUMN_INDE
INSERT INTO WIDGETS(ID, DASHBOARD_ID, WIDGET_KEY, NAME, DESCRIPTION, COLUMN_INDEX, ROW_INDEX, CONFIGURED, CREATED_AT, UPDATED_AT) VALUES (9, 1, 'file_design', 'File design', null, 2, 3, true, '2011-09-26 22:27:56.0', '2011-09-26 22:27:56.0');
INSERT INTO WIDGETS(ID, DASHBOARD_ID, WIDGET_KEY, NAME, DESCRIPTION, COLUMN_INDEX, ROW_INDEX, CONFIGURED, CREATED_AT, UPDATED_AT) VALUES (10, 1, 'package_design', 'Package design', null, 2, 4, true, '2011-09-26 22:27:56.0', '2011-09-26 22:27:56.0');
INSERT INTO WIDGETS(ID, DASHBOARD_ID, WIDGET_KEY, NAME, DESCRIPTION, COLUMN_INDEX, ROW_INDEX, CONFIGURED, CREATED_AT, UPDATED_AT) VALUES (11, 1, 'ckjm', 'CKJM', null, 2, 5, true, '2011-09-26 22:27:56.0', '2011-09-26 22:27:56.0');
-ALTER TABLE WIDGETS ALTER COLUMN ID RESTART WITH 12; \ No newline at end of file
+ALTER TABLE WIDGETS ALTER COLUMN ID RESTART WITH 12;
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 e90ee4ed48d..d37769e3467 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
@@ -48,10 +48,7 @@ import org.sonar.jpa.session.DatabaseSessionFactory;
import org.sonar.jpa.session.DefaultDatabaseConnector;
import org.sonar.jpa.session.JpaDatabaseSession;
import org.sonar.jpa.session.MemoryDatabaseConnector;
-import org.sonar.persistence.Database;
-import org.sonar.persistence.DerbyUtils;
-import org.sonar.persistence.HsqlDatabase;
-import org.sonar.persistence.InMemoryDatabase;
+import org.sonar.persistence.*;
public abstract class AbstractDbUnitTestCase {
@@ -64,9 +61,10 @@ public abstract class AbstractDbUnitTestCase {
private DefaultDatabaseConnector dbConnector;
private JpaDatabaseSession session;
private DaoFacade dao;
- protected IDatabaseTester databaseTester;
- protected IDatabaseConnection connection;
+ private IDatabaseTester databaseTester;
+ private IDatabaseConnection connection;
private Database database;
+ private MyBatis myBatis;
@Before
public void startDatabase() throws Exception {
@@ -75,8 +73,11 @@ public abstract class AbstractDbUnitTestCase {
} else {
database = new InMemoryDatabase();
}
-
database.start();
+
+ myBatis = new MyBatis(database);
+ myBatis.start();
+
dbConnector = new MemoryDatabaseConnector(database);
dbConnector.start();
@@ -98,6 +99,10 @@ public abstract class AbstractDbUnitTestCase {
database.stop();
}
+ protected MyBatis getMyBatis() {
+ return myBatis;
+ }
+
public DaoFacade getDao() {
if (dao == null) {
dao = new DaoFacade(new ProfilesDao(session), new RulesDao(session), new MeasuresDao(session));
@@ -272,16 +277,6 @@ public abstract class AbstractDbUnitTestCase {
return runtimeException;
}
- /*public static class HsqlDataTypeFactory extends DefaultDataTypeFactory {
-
- public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException {
- if (sqlType == Types.BOOLEAN) {
- return DataType.BOOLEAN;
- }
- return super.createDataType(sqlType, sqlTypeName);
- }
- }*/
-
protected Long getHQLCount(final Class<?> hqlClass) {
String hqlCount = "SELECT count(o) from " + hqlClass.getSimpleName() + " o";
return (Long) getSession().createQuery(hqlCount).getSingleResult();
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dao/DuplicationDaoTest.java b/sonar-core/src/test/java/org/sonar/persistence/dao/DuplicationDaoTest.java
index 1622570ad3f..353a9cee987 100644
--- a/sonar-core/src/test/java/org/sonar/persistence/dao/DuplicationDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/persistence/dao/DuplicationDaoTest.java
@@ -22,56 +22,24 @@ package org.sonar.persistence.dao;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
-import java.io.InputStream;
-import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
-import org.apache.commons.io.IOUtils;
-import org.dbunit.Assertion;
-import org.dbunit.DataSourceDatabaseTester;
-import org.dbunit.DatabaseUnitException;
-import org.dbunit.IDatabaseTester;
-import org.dbunit.database.IDatabaseConnection;
-import org.dbunit.dataset.*;
-import org.dbunit.dataset.filter.DefaultColumnFilter;
-import org.dbunit.dataset.xml.FlatXmlDataSet;
-import org.dbunit.operation.DatabaseOperation;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.persistence.InMemoryDatabase;
-import org.sonar.persistence.MyBatis;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
import org.sonar.persistence.model.DuplicationUnit;
import com.google.common.collect.Lists;
-public class DuplicationDaoTest {
+public class DuplicationDaoTest extends AbstractDbUnitTestCase {
- private static IDatabaseTester databaseTester;
- private static InMemoryDatabase database;
- private static DuplicationDao dao;
+ private DuplicationDao dao;
@Before
- public void startDatabase() throws Exception {
- database = new InMemoryDatabase();
- MyBatis myBatis = new MyBatis(database);
-
- database.start();
- myBatis.start();
-
- dao = new DuplicationDao(myBatis);
- databaseTester = new DataSourceDatabaseTester(database.getDataSource());
- }
-
- @After
- public void stopDatabase() throws Exception {
- if (databaseTester != null) {
- databaseTester.onTearDown();
- }
- database.stop();
+ public void createDao() throws Exception {
+ dao = new DuplicationDao(getMyBatis());
}
@Test
@@ -116,117 +84,4 @@ public class DuplicationDaoTest {
}
}
- // ============================================================
- // TODO Godin: a kind of copy-paste from AbstractDbUnitTestCase
-
- private final void setupData(String... testNames) {
- InputStream[] streams = new InputStream[testNames.length];
- 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);
- }
- }
-
- setupData(streams);
-
- } finally {
- for (InputStream stream : streams) {
- IOUtils.closeQuietly(stream);
- }
- }
- }
-
- private final void setupData(InputStream... dataSetStream) {
- 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);
- dataSets[i] = dataSet;
- }
- CompositeDataSet compositeDataSet = new CompositeDataSet(dataSets);
-
- databaseTester.setDataSet(compositeDataSet);
- IDatabaseConnection connection = databaseTester.getConnection();
-
- DatabaseOperation.CLEAN_INSERT.execute(connection, databaseTester.getDataSet());
-
- connection.getConnection().commit();
- connection.close();
- } catch (Exception e) {
- throw translateException("Could not setup DBUnit data", e);
- }
- }
-
- private final void checkTables(String testName, String... tables) {
- checkTables(testName, new String[] {}, tables);
- }
-
- private final void checkTables(String testName, String[] excludedColumnNames, String... tables) {
- IDatabaseConnection connection = null;
- try {
- connection = databaseTester.getConnection();
- IDataSet dataSet = connection.createDataSet();
- IDataSet expectedDataSet = getExpectedData(testName);
- for (String table : tables) {
- ITable filteredTable = DefaultColumnFilter.excludedColumnsTable(dataSet.getTable(table), excludedColumnNames);
- Assertion.assertEquals(expectedDataSet.getTable(table), filteredTable);
- }
- } catch (DataSetException e) {
- throw translateException("Error while checking results", e);
- } catch (DatabaseUnitException e) {
- fail(e.getMessage());
- } catch (Exception e) {
- throw translateException("Error while checking results", e);
- } finally {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- throw translateException("Error while checking results", e);
- }
- }
- }
- }
-
- private final IDataSet getExpectedData(String testName) {
- String className = getClass().getName();
- className = String.format("/%s/%s-result.xml", className.replace(".", "/"), testName);
-
- InputStream in = getClass().getResourceAsStream(className);
- try {
- return getData(in);
- } finally {
- IOUtils.closeQuietly(in);
- }
- }
-
- private final IDataSet getData(InputStream stream) {
- try {
- ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream));
- dataSet.addReplacementObject("[null]", null);
- return dataSet;
- } catch (Exception e) {
- throw translateException("Could not read the dataset stream", e);
- }
- }
-
- private final IDataSet getCurrentDataSet() {
- try {
- IDatabaseConnection connection = databaseTester.getConnection();
- return connection.createDataSet();
- } catch (Exception e) {
- throw translateException("Could not create the current 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());
- return runtimeException;
- }
}
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dao/RuleDaoTest.java b/sonar-core/src/test/java/org/sonar/persistence/dao/RuleDaoTest.java
index 024deedb625..a0dc3694f94 100644
--- a/sonar-core/src/test/java/org/sonar/persistence/dao/RuleDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/persistence/dao/RuleDaoTest.java
@@ -19,53 +19,22 @@
*/
package org.sonar.persistence.dao;
-import org.apache.commons.io.IOUtils;
-import org.apache.ibatis.session.SqlSession;
-import org.dbunit.DataSourceDatabaseTester;
-import org.dbunit.IDatabaseTester;
-import org.dbunit.database.IDatabaseConnection;
-import org.dbunit.dataset.CompositeDataSet;
-import org.dbunit.dataset.IDataSet;
-import org.dbunit.dataset.ReplacementDataSet;
-import org.dbunit.dataset.xml.FlatXmlDataSet;
-import org.dbunit.operation.DatabaseOperation;
-import org.hamcrest.core.Is;
-import org.junit.*;
-import org.junit.Rule;
-import org.sonar.persistence.DerbyUtils;
-import org.sonar.persistence.InMemoryDatabase;
-import org.sonar.persistence.MyBatis;
-import org.sonar.persistence.model.*;
+import static org.junit.Assert.assertThat;
-import java.io.InputStream;
import java.util.List;
-import static org.junit.Assert.assertThat;
+import org.hamcrest.core.Is;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
-public class RuleDaoTest {
+public class RuleDaoTest extends AbstractDbUnitTestCase {
- protected static IDatabaseTester databaseTester;
- private static InMemoryDatabase database;
private static RuleDao dao;
- @BeforeClass
- public static void startDatabase() throws Exception {
- database = new InMemoryDatabase();
- MyBatis myBatis = new MyBatis(database);
-
- database.start();
- myBatis.start();
-
- dao = new RuleDao(myBatis);
- databaseTester = new DataSourceDatabaseTester(database.getDataSource());
- }
-
- @AfterClass
- public static void stopDatabase() throws Exception {
- if (databaseTester != null) {
- databaseTester.onTearDown();
- }
- database.stop();
+ @Before
+ public void createDao() throws Exception {
+ dao = new RuleDao(getMyBatis());
}
@Test
@@ -94,43 +63,4 @@ public class RuleDaoTest {
assertThat(rule.getRepositoryKey(), Is.is("checkstyle"));
}
- protected final void setupData(String... testNames) throws Exception {
- InputStream[] streams = new InputStream[testNames.length];
- 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);
- }
- }
-
- setupData(streams);
-
- } finally {
- for (InputStream stream : streams) {
- IOUtils.closeQuietly(stream);
- }
- }
- }
-
- protected final void setupData(InputStream... dataSetStream) throws Exception {
- 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);
- dataSets[i] = dataSet;
- }
- CompositeDataSet compositeDataSet = new CompositeDataSet(dataSets);
-
- databaseTester.setDataSet(compositeDataSet);
- IDatabaseConnection connection = databaseTester.getConnection();
- DatabaseOperation.CLEAN_INSERT.execute(connection, databaseTester.getDataSet());
-
- connection.getConnection().commit();
- connection.close();
-
- }
-
}