From: David Gageot Date: Tue, 19 Jun 2012 15:33:09 +0000 (+0200) Subject: Make it impossible to use AbstractDbUnitTestCase for iBatis tests X-Git-Tag: 3.2~307 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=061451b53d8a8fc2e3f4716705d6c6a28689baa8;p=sonarqube.git Make it impossible to use AbstractDbUnitTestCase for iBatis tests --- diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest.java index 4fb70af490a..a26303c212f 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest.java @@ -32,17 +32,23 @@ import org.sonar.api.rules.Rule; import org.sonar.api.rules.Violation; import org.sonar.api.violations.ViolationQuery; import org.sonar.batch.index.ResourcePersister; +import org.sonar.core.persistence.DaoTestCase; import org.sonar.core.review.ReviewDao; import org.sonar.core.review.ReviewDto; -import org.sonar.jpa.test.AbstractDbUnitTestCase; import java.util.Collections; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -public class ReviewWorkflowDecoratorTest extends AbstractDbUnitTestCase { +public class ReviewWorkflowDecoratorTest extends DaoTestCase { private ReviewWorkflowDecorator decorator; private ReviewNotifications notifications; @@ -82,7 +88,7 @@ public class ReviewWorkflowDecoratorTest extends AbstractDbUnitTestCase { decorator.decorate(resource, context); verify(notifications, times(2)).notifyClosed(any(ReviewDto.class), any(Project.class), eq(resource)); - checkTablesWithExcludedColumns("shouldCloseReviewsOnResolvedViolations", new String[]{"updated_at"}, "reviews"); + checkTables("shouldCloseReviewsOnResolvedViolations", new String[] {"updated_at"}, "reviews"); } @Test @@ -95,7 +101,7 @@ public class ReviewWorkflowDecoratorTest extends AbstractDbUnitTestCase { decorator.decorate(resource, context); verify(notifications).notifyClosed(any(ReviewDto.class), any(Project.class), eq(resource)); - checkTablesWithExcludedColumns("shouldCloseResolvedManualViolations", new String[]{"updated_at"}, "reviews"); + checkTables("shouldCloseResolvedManualViolations", new String[] {"updated_at"}, "reviews"); } @Test @@ -110,7 +116,7 @@ public class ReviewWorkflowDecoratorTest extends AbstractDbUnitTestCase { decorator.decorate(resource, context); verify(notifications).notifyReopened(any(ReviewDto.class), any(Project.class), eq(resource)); - checkTablesWithExcludedColumns("shouldReopenViolations", new String[]{"updated_at"}, "reviews"); + checkTables("shouldReopenViolations", new String[] {"updated_at"}, "reviews"); } @Test diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseResolvedManualViolations-result.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseResolvedManualViolations-result.xml index f0355573bde..6a1c2c59e9c 100644 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseResolvedManualViolations-result.xml +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseResolvedManualViolations-result.xml @@ -4,12 +4,12 @@ diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseReviewsOnResolvedViolations-result.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseReviewsOnResolvedViolations-result.xml index 73f3114da02..ea6fcd98f7d 100644 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseReviewsOnResolvedViolations-result.xml +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseReviewsOnResolvedViolations-result.xml @@ -5,25 +5,25 @@ \ No newline at end of file diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldReopenViolations-result.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldReopenViolations-result.xml index 96286e98d8b..457f4d4f8bf 100644 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldReopenViolations-result.xml +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldReopenViolations-result.xml @@ -4,7 +4,7 @@ diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java index 03e580d68f1..8b34120b9f0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java @@ -67,7 +67,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { ResourcePersister persister = new DefaultResourcePersister(getSession()); persister.saveProject(singleProject, null); - checkTablesWithExcludedColumns("shouldSaveNewProject", new String[] {"build_date"}, "projects", "snapshots"); + checkTables("shouldSaveNewProject", new String[] {"build_date"}, "projects", "snapshots"); } @Test @@ -80,7 +80,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { persister.saveProject(moduleB, multiModuleProject); persister.saveProject(moduleB1, moduleB); - checkTablesWithExcludedColumns("shouldSaveNewMultiModulesProject", new String[] {"build_date"}, "projects", "snapshots"); + checkTables("shouldSaveNewMultiModulesProject", new String[] {"build_date"}, "projects", "snapshots"); } @Test @@ -92,7 +92,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { persister.saveResource(singleProject, new JavaPackage("org.foo").setEffectiveKey("foo:org.foo")); // check that the directory is attached to the project - checkTablesWithExcludedColumns("shouldSaveNewDirectory", new String[] {"build_date"}, "projects", "snapshots"); + checkTables("shouldSaveNewDirectory", new String[] {"build_date"}, "projects", "snapshots"); } @Test @@ -105,7 +105,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"));// do nothing, already saved persister.saveResource(singleProject, new Library("junit:junit", "3.2").setEffectiveKey("junit:junit")); - checkTablesWithExcludedColumns("shouldSaveNewLibrary", new String[] {"build_date"}, "projects", "snapshots"); + checkTables("shouldSaveNewLibrary", new String[] {"build_date"}, "projects", "snapshots"); } @Test @@ -133,7 +133,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { singleProject.setDescription("new description"); persister.saveProject(singleProject, null); - checkTablesWithExcludedColumns("shouldUpdateExistingResource", new String[] {"build_date"}, "projects", "snapshots"); + checkTables("shouldUpdateExistingResource", new String[] {"build_date"}, "projects", "snapshots"); } // SONAR-1700 @@ -144,7 +144,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { ResourcePersister persister = new DefaultResourcePersister(getSession()); persister.saveProject(singleProject, null); - checkTablesWithExcludedColumns("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date"}, "projects", "snapshots"); + checkTables("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date"}, "projects", "snapshots"); } } 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 34c696e283f..3e27d0ccb38 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 @@ -25,23 +25,30 @@ 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.Column; +import org.dbunit.dataset.CompositeDataSet; +import org.dbunit.dataset.DataSetException; +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.ITable; +import org.dbunit.dataset.ITableMetaData; +import org.dbunit.dataset.ReplacementDataSet; import org.dbunit.dataset.filter.DefaultColumnFilter; import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.operation.DatabaseOperation; -import org.junit.*; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; import org.sonar.api.database.DatabaseSession; import org.sonar.core.persistence.Database; import org.sonar.core.persistence.DatabaseCommands; import org.sonar.core.persistence.InMemoryDatabase; -import org.sonar.core.persistence.MyBatis; 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 java.io.InputStream; -import java.io.StringWriter; import java.sql.ResultSet; import java.sql.SQLException; @@ -51,23 +58,19 @@ import static org.junit.Assert.fail; * Heavily duplicates DaoTestCase as long as Hibernate is in use. */ public abstract class AbstractDbUnitTestCase { + private static Database database; + private static DefaultDatabaseConnector dbConnector; + private static DatabaseCommands databaseCommands; private JpaDatabaseSession session; private IDatabaseTester databaseTester; private IDatabaseConnection connection; - private static Database database; - private static MyBatis myBatis; - private static DefaultDatabaseConnector dbConnector; - private static DatabaseCommands databaseCommands; @BeforeClass public static void startDatabase() throws Exception { database = new InMemoryDatabase(); database.start(); - myBatis = new MyBatis(database); - myBatis.start(); - dbConnector = new MemoryDatabaseConnector(database); dbConnector.start(); @@ -76,7 +79,7 @@ public abstract class AbstractDbUnitTestCase { @Before public void startConnection() throws Exception { - databaseCommands.truncateDatabase(myBatis.openSession().getConnection()); + databaseCommands.truncateDatabase(database.getDataSource().getConnection()); databaseTester = new DataSourceDatabaseTester(database.getDataSource()); session = new JpaDatabaseSession(dbConnector); @@ -99,10 +102,6 @@ public abstract class AbstractDbUnitTestCase { database.stop(); } - protected MyBatis getMyBatis() { - return myBatis; - } - public DatabaseSession getSession() { return session; } @@ -140,7 +139,7 @@ public abstract class AbstractDbUnitTestCase { } } - protected final void setupData(InputStream... dataSetStream) { + private final void setupData(InputStream... dataSetStream) { try { IDataSet[] dataSets = new IDataSet[dataSetStream.length]; for (int i = 0; i < dataSetStream.length; i++) { @@ -180,10 +179,10 @@ public abstract class AbstractDbUnitTestCase { } protected final void checkTables(String testName, String... tables) { - checkTablesWithExcludedColumns(testName, new String[]{}, tables); + checkTables(testName, new String[0], tables); } - protected final void checkTablesWithExcludedColumns(String testName, String[] excludedColumnNames, String... tables) { + protected final void checkTables(String testName, String[] excludedColumnNames, String... tables) { getSession().commit(); try { IDataSet dataSet = getCurrentDataSet(); @@ -200,17 +199,7 @@ public abstract class AbstractDbUnitTestCase { } } - protected final void assertEmptyTables(String... emptyTables) { - for (String table : emptyTables) { - try { - Assert.assertEquals(0, getCurrentDataSet().getTable(table).getRowCount()); - } catch (DataSetException e) { - throw translateException("Error while checking results", e); - } - } - } - - protected final IDataSet getExpectedData(String testName) { + private final IDataSet getExpectedData(String testName) { String className = getClass().getName(); className = String.format("/%s/%s-result.xml", className.replace(".", "/"), testName); @@ -222,7 +211,7 @@ public abstract class AbstractDbUnitTestCase { } } - protected final IDataSet getData(InputStream stream) { + private final IDataSet getData(InputStream stream) { try { ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream)); dataSet.addReplacementObject("[null]", null); @@ -232,7 +221,7 @@ public abstract class AbstractDbUnitTestCase { } } - protected final IDataSet getCurrentDataSet() { + private final IDataSet getCurrentDataSet() { try { return connection.createDataSet(); } catch (SQLException e) { @@ -240,37 +229,14 @@ public abstract class AbstractDbUnitTestCase { } } - 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()); return runtimeException; } - protected Long getHQLCount(final Class hqlClass) { + protected Long getHQLCount(Class hqlClass) { String hqlCount = "SELECT count(o) from " + hqlClass.getSimpleName() + " o"; return (Long) getSession().createQuery(hqlCount).getSingleResult(); } - - protected IDatabaseConnection getConnection() { - return connection; - } - - protected IDatabaseTester getDatabaseTester() { - return databaseTester; - } - } diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/RuleChangeTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/RuleChangeTest.java index 1b501ee5d22..450c48a933d 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/RuleChangeTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/RuleChangeTest.java @@ -50,42 +50,42 @@ public class RuleChangeTest extends AbstractDbUnitTestCase { public void testRuleActivated() { setupData("initialData"); profilesManager.activated(2, 3, "admin"); - checkTablesWithExcludedColumns("ruleActivated", new String[]{"change_date"}, "active_rule_changes"); + checkTables("ruleActivated", new String[]{"change_date"}, "active_rule_changes"); } @Test public void testRuleDeactivated() { setupData("initialData"); profilesManager.deactivated(2, 3, "admin"); - checkTablesWithExcludedColumns("ruleDeactivated", new String[]{"change_date"}, "active_rule_changes"); + checkTables("ruleDeactivated", new String[]{"change_date"}, "active_rule_changes"); } @Test public void testRuleParamChanged() { setupData("initialData"); profilesManager.ruleParamChanged(2, 3, "param1", "20", "30", "admin"); - checkTablesWithExcludedColumns("ruleParamChanged", new String[]{"change_date"}, "active_rule_changes", "active_rule_param_changes"); + checkTables("ruleParamChanged", new String[]{"change_date"}, "active_rule_changes", "active_rule_param_changes"); } @Test public void testRuleSeverityChanged() { setupData("initialData"); profilesManager.ruleSeverityChanged(2, 3, RulePriority.BLOCKER, RulePriority.CRITICAL, "admin"); - checkTablesWithExcludedColumns("ruleSeverityChanged", new String[]{"change_date"}, "active_rule_changes"); + checkTables("ruleSeverityChanged", new String[]{"change_date"}, "active_rule_changes"); } @Test public void testRuleReverted() { setupData("ruleReverted"); profilesManager.revert(2, 3, "admin"); - checkTablesWithExcludedColumns("ruleReverted", new String[]{"change_date"}, "active_rule_changes", "active_rule_param_changes"); + checkTables("ruleReverted", new String[]{"change_date"}, "active_rule_changes", "active_rule_param_changes"); } @Test public void testChangeParentProfile() { setupData("changeParentProfile"); profilesManager.changeParentProfile(2, "parent", "admin"); - checkTablesWithExcludedColumns("changeParentProfile", new String[]{"change_date"}, "active_rule_changes"); + checkTables("changeParentProfile", new String[]{"change_date"}, "active_rule_changes"); } diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java index 0fefc603f25..102905e97ca 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java @@ -22,8 +22,8 @@ package org.sonar.server.platform; import org.apache.commons.configuration.BaseConfiguration; import org.junit.Test; import org.sonar.api.config.PropertyDefinitions; +import org.sonar.core.persistence.DaoTestCase; import org.sonar.core.properties.PropertiesDao; -import org.sonar.jpa.test.AbstractDbUnitTestCase; import java.io.File; import java.net.URISyntaxException; @@ -32,7 +32,7 @@ import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; -public class ServerSettingsTest extends AbstractDbUnitTestCase { +public class ServerSettingsTest extends DaoTestCase { private static File home = getHome();