]> source.dussan.org Git - sonarqube.git/commitdiff
Add methods checkColumns() to dbunit helpers
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 9 Jul 2012 15:48:18 +0000 (17:48 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 9 Jul 2012 16:21:46 +0000 (18:21 +0200)
sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java
sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java

index 9222fcd239265ef438158ea6c404cd4f037770b2..39926b21b6ff236a448c1e69ca38941a96c061cb 100644 (file)
@@ -33,11 +33,7 @@ import org.dbunit.dataset.ITable;
 import org.dbunit.dataset.ReplacementDataSet;
 import org.dbunit.dataset.filter.DefaultColumnFilter;
 import org.dbunit.dataset.xml.FlatXmlDataSet;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.BeforeClass;
+import org.junit.*;
 import org.sonar.api.config.Settings;
 
 import java.io.InputStream;
@@ -133,7 +129,7 @@ public abstract class AbstractDaoTestCase {
   }
 
   protected void checkTables(String testName, String... tables) {
-    checkTables(testName, new String[] {}, tables);
+    checkTables(testName, new String[]{}, tables);
   }
 
   protected void checkTables(String testName, String[] excludedColumnNames, String... tables) {
@@ -151,6 +147,24 @@ public abstract class AbstractDaoTestCase {
     }
   }
 
+  /**
+   * Opposite of {@link #checkTables(String, String[], String...)}.
+   */
+  protected void checkColumns(String testName, String table, String... columns) {
+    try {
+      IDataSet dataSet = getCurrentDataSet();
+      IDataSet expectedDataSet = getExpectedData(testName);
+      ITable filteredTable = DefaultColumnFilter.includedColumnsTable(dataSet.getTable(table), columns);
+      ITable filteredExpectedTable = DefaultColumnFilter.includedColumnsTable(expectedDataSet.getTable(table), columns);
+      Assertion.assertEquals(filteredExpectedTable, filteredTable);
+
+    } catch (DataSetException e) {
+      throw translateException("Error while checking columns", e);
+    } catch (DatabaseUnitException e) {
+      fail(e.getMessage());
+    }
+  }
+
   protected void assertEmptyTables(String... emptyTables) {
     for (String table : emptyTables) {
       try {
index 78d9fe4164eabfc5978af60bd5266da9c0215e53..3efe742f30742f0d240f12a5106b493779a5dc75 100644 (file)
@@ -19,8 +19,6 @@
  */
 package org.sonar.jpa.test;
 
-import org.junit.AfterClass;
-
 import org.apache.commons.io.IOUtils;
 import org.dbunit.Assertion;
 import org.dbunit.DataSourceDatabaseTester;
@@ -28,16 +26,13 @@ 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;
-import org.dbunit.dataset.ReplacementDataSet;
+import org.dbunit.dataset.*;
 import org.dbunit.dataset.filter.DefaultColumnFilter;
 import org.dbunit.dataset.xml.FlatXmlDataSet;
 import org.dbunit.ext.h2.H2DataTypeFactory;
 import org.dbunit.operation.DatabaseOperation;
 import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.sonar.api.database.DatabaseSession;
@@ -185,6 +180,25 @@ public abstract class AbstractDbUnitTestCase {
     }
   }
 
+  /**
+   * Opposite of {@link #checkTables(String, String[], String...)}.
+   */
+  protected void checkColumns(String testName, String table, String... columns) {
+    getSession().commit();
+    try {
+      IDataSet dataSet = getCurrentDataSet();
+      IDataSet expectedDataSet = getExpectedData(testName);
+      ITable filteredTable = DefaultColumnFilter.includedColumnsTable(dataSet.getTable(table), columns);
+      ITable filteredExpectedTable = DefaultColumnFilter.includedColumnsTable(expectedDataSet.getTable(table), columns);
+      Assertion.assertEquals(filteredExpectedTable, filteredTable);
+
+    } catch (DataSetException e) {
+      throw translateException("Error while checking columns", e);
+    } catch (DatabaseUnitException e) {
+      fail(e.getMessage());
+    }
+  }
+
   private IDataSet getExpectedData(String testName) {
     String className = getClass().getName();
     className = String.format("/%s/%s-result.xml", className.replace(".", "/"), testName);