]> source.dussan.org Git - sonarqube.git/commitdiff
Make it impossible to use AbstractDbUnitTestCase for iBatis tests
authorDavid Gageot <david@gageot.net>
Tue, 19 Jun 2012 15:33:09 +0000 (17:33 +0200)
committerDavid Gageot <david@gageot.net>
Tue, 19 Jun 2012 15:54:06 +0000 (17:54 +0200)
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest.java
plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseResolvedManualViolations-result.xml
plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldCloseReviewsOnResolvedViolations-result.xml
plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest/shouldReopenViolations-result.xml
sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java
sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
sonar-server/src/test/java/org/sonar/server/configuration/RuleChangeTest.java
sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java

index 4fb70af490a632d7a82a4d5f0e67ec0a790bb0af..a26303c212f09625b23a4ce37b87c3b192dd7def 100644 (file)
@@ -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
index f0355573bde2faa1bbe33d834c3f93bcd8ee7d4e..6a1c2c59e9cda2d8f4b11b70cac8c37c3e5da983 100644 (file)
@@ -4,12 +4,12 @@
 
 
   <reviews id="1" project_id="[null]" resource_id="100" status="OPEN" rule_failure_permanent_id="1000"
-           resolution="[null]" created_at="[null]" updated_at="[null]"
+           resolution="[null]" created_at="[null]"
            resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="true" manual_severity="false"
            title="[null]" assignee_id="[null]" data="[null]"/>
 
   <reviews id="2" project_id="[null]" resource_id="100" status="CLOSED" rule_failure_permanent_id="1000"
-             resolution="FIXED" created_at="[null]" updated_at="[null]"
+             resolution="FIXED" created_at="[null]"
              resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="true" manual_severity="false"
              title="[null]" assignee_id="[null]" data="[null]"/>
 
index 73f3114da02d4cffeaadceca52eb57341b74c29f..ea6fcd98f7da5e4f4d73dec3a0d5c34384f62a5b 100644 (file)
@@ -5,25 +5,25 @@
 
   <!-- open reviews ==> CLOSED -->
   <reviews id="1" project_id="[null]" resource_id="100" status="CLOSED" rule_failure_permanent_id="1000"
-           resolution="[null]" created_at="[null]" updated_at="[null]"
+           resolution="[null]" created_at="[null]"
            resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
            title="[null]" assignee_id="[null]" data="[null]"/>
 
   <reviews id="2" project_id="[null]" resource_id="100" status="CLOSED" rule_failure_permanent_id="1001"
-             resolution="FALSE-POSITIVE" created_at="[null]" updated_at="[null]"
+             resolution="FALSE-POSITIVE" created_at="[null]"
              resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
              title="[null]" assignee_id="[null]" data="[null]"/>
 
   <!-- closed reviews -->
   <reviews id="3" project_id="[null]" resource_id="100" status="CLOSED" rule_failure_permanent_id="1002"
-               resolution="RESOLVED" created_at="[null]" updated_at="[null]"
+               resolution="RESOLVED" created_at="[null]"
                resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
                title="[null]" assignee_id="[null]" data="[null]"/>
 
 
   <!-- other resource -->
   <reviews id="4" project_id="[null]" resource_id="101" status="OPEN" rule_failure_permanent_id="1100"
-                 resolution="[null]" created_at="[null]" updated_at="[null]"
+                 resolution="[null]" created_at="[null]"
                  resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
                  title="[null]" assignee_id="[null]" data="[null]"/>
 </dataset>
\ No newline at end of file
index 96286e98d8b59f2e21b06237074f6203a9f34720..457f4d4f8bf86660b80685f6594dd95d376c071d 100644 (file)
@@ -4,7 +4,7 @@
 
 
   <reviews id="1" project_id="[null]" resource_id="100" status="REOPENED" rule_failure_permanent_id="1000"
-           resolution="[null]" created_at="[null]" updated_at="[null]"
+           resolution="[null]" created_at="[null]"
            resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
            title="[null]" assignee_id="[null]" data="[null]"/>
 
index 03e580d68f17734b9bb04a07f1dee97e85a8886a..8b34120b9f0250d7e76c31082a6a9aafbe7c5b89 100644 (file)
@@ -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");
   }
 
 }
index 34c696e283fb2c4918f045b04ab1e0decdf7ae08..3e27d0ccb38ffb508a96d92bbea0946295ec9007 100644 (file)
@@ -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;
-  }
-
 }
index 1b501ee5d22da3c4da4037ea36c03b5174d0d758..450c48a933d6b9aef755ad1f58d34902efa84b05 100644 (file)
@@ -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");
   }
 
 
index 0fefc603f254156ffde15fef4ca44d4d95ae87c8..102905e97ca5a030b91bd8561ff72ab134e7c13b 100644 (file)
@@ -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();