]> source.dussan.org Git - sonarqube.git/commitdiff
Simplify MyBatis tests by reusing AbstractDbUnitTestCase
authorEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 2 Nov 2011 16:18:48 +0000 (20:18 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 2 Nov 2011 17:11:52 +0000 (21:11 +0400)
sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql
sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
sonar-core/src/test/java/org/sonar/persistence/dao/DuplicationDaoTest.java
sonar-core/src/test/java/org/sonar/persistence/dao/RuleDaoTest.java

index 66adfa9745580c7a742415fedc8406e2d20d4c04..344c11868e1346f171e066ebdf8ba8b8f3a0deb5 100644 (file)
@@ -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;
index e90ee4ed48d2f9f37d30996a4dedffb6aaf18677..d37769e3467e3ebb138f0c9579c9f12a472e8d06 100644 (file)
@@ -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();
index 1622570ad3f5b5674c470e6c8c2e392eacb4d0e8..353a9cee9877839fb817835ddb63b8c70edda92c 100644 (file)
@@ -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;
-  }
 }
index 024deedb6257059de3a29edd70fc08a2052472b8..a0dc3694f949f88de30dc60dc7217ba0bbe44d1c 100644 (file)
  */
 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();
-
-  }
-
 }