]> source.dussan.org Git - sonarqube.git/commitdiff
Fixes and improvements on H2 migration
authorDavid Gageot <david@gageot.net>
Wed, 4 Jul 2012 14:36:16 +0000 (16:36 +0200)
committerDavid Gageot <david@gageot.net>
Wed, 4 Jul 2012 15:02:58 +0000 (17:02 +0200)
31 files changed:
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecoratorTest.java
sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java
sonar-core/src/test/java/org/sonar/core/dashboard/ActiveDashboardDaoTest.java
sonar-core/src/test/java/org/sonar/core/dashboard/DashboardDaoTest.java
sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java
sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java
sonar-core/src/test/java/org/sonar/core/duplication/DuplicationDaoTest.java
sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java
sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java [deleted file]
sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java
sonar-core/src/test/java/org/sonar/core/persistence/DatabaseVersionTest.java
sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java
sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java
sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java
sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
sonar-core/src/test/java/org/sonar/core/purge/PurgeCommandsTest.java
sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java
sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
sonar-core/src/test/java/org/sonar/core/resource/ResourceIndexerDaoTest.java
sonar-core/src/test/java/org/sonar/core/resource/ResourceKeyUpdaterDaoTest.java
sonar-core/src/test/java/org/sonar/core/review/ReviewCommentDaoTest.java
sonar-core/src/test/java/org/sonar/core/review/ReviewDaoTest.java
sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
sonar-core/src/test/java/org/sonar/core/template/LoadedTemplateDaoTest.java
sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java
sonar-core/src/test/java/org/sonar/jpa/session/ThreadLocalDatabaseSessionFactoryTest.java [deleted file]
sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java

index a26303c212f09625b23a4ce37b87c3b192dd7def..06ee37495955914493936888a73bc1a7b05f6764 100644 (file)
@@ -32,7 +32,7 @@ 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.persistence.AbstractDaoTestCase;
 import org.sonar.core.review.ReviewDao;
 import org.sonar.core.review.ReviewDto;
 
@@ -48,7 +48,7 @@ import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-public class ReviewWorkflowDecoratorTest extends DaoTestCase {
+public class ReviewWorkflowDecoratorTest extends AbstractDaoTestCase {
   private ReviewWorkflowDecorator decorator;
   private ReviewNotifications notifications;
 
index 66521373392ae7f3f11d517f24f7fb5ff278e1ca..dba97bb0a700272b0ba09ccf81e5c84bd463c3e6 100644 (file)
@@ -45,25 +45,30 @@ public class DatabaseMigrator implements ServerComponent {
    * @return true if the database has been created, false if this database is not supported
    */
   public boolean createDatabase() {
-    if (DdlUtils.supportsDialect(database.getDialect().getId())) {
-      LoggerFactory.getLogger(getClass()).info("Create database");
-      SqlSession session = myBatis.openSession();
-      Connection connection = session.getConnection();
+    if (!DdlUtils.supportsDialect(database.getDialect().getId())) {
+      return false;
+    }
+
+    LoggerFactory.getLogger(getClass()).info("Create database");
+    SqlSession session = null;
+    Connection connection = null;
+    try {
+      session = myBatis.openSession();
+      connection = session.getConnection();
+      DdlUtils.createSchema(connection, database.getDialect().getId());
+    } finally {
       try {
-        DdlUtils.createSchema(connection, database.getDialect().getId());
-      } finally {
-        try {
-          MyBatis.closeQuietly(session);
+        MyBatis.closeQuietly(session);
 
-          // The connection is probably already closed by session.close()
-          // but it's not documented in mybatis javadoc.
+        // The connection is probably already closed by session.close()
+        // but it's not documented in mybatis javadoc.
+        if (null != connection) {
           connection.close();
-        } catch (Exception e) {
-          // ignore
         }
+      } catch (Exception e) {
+        // ignore
       }
-      return true;
     }
-    return false;
+    return true;
   }
 }
index b60bcce9e86c8dc7680d52d2ad45d165a1e0ef41..dc647ccd15ef9213a2a07f67f445bb030db1d78f 100644 (file)
@@ -21,12 +21,12 @@ package org.sonar.core.dashboard;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 
-public class ActiveDashboardDaoTest extends DaoTestCase {
+public class ActiveDashboardDaoTest extends AbstractDaoTestCase {
 
   private ActiveDashboardDao dao;
 
index 36b464f67786dfb56ecd07bf7a8fc3fbd47b3809..6311d0869da3d42716ba0c972249c4db89fc271c 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.core.dashboard;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.Date;
 
@@ -30,7 +30,7 @@ import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 
-public class DashboardDaoTest extends DaoTestCase {
+public class DashboardDaoTest extends AbstractDaoTestCase {
 
   private DashboardDao dao;
 
index d236ff29ce1f77acd0a5cf06abf51a14559a53dd..fdf3b225613cb59d98a49062abf9523d7144d622 100644 (file)
@@ -24,14 +24,14 @@ import org.apache.ibatis.session.ResultContext;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.SqlSession;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 import org.sonar.core.persistence.MyBatis;
 
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
 
-public class DependencyMapperTest extends DaoTestCase {
+public class DependencyMapperTest extends AbstractDaoTestCase {
   @Test
   public void should_find_all() {
     setupData("fixture");
index 76ddf641b044f4c510db00928dd132fcb479060c..66e53638875cd6f888cdbe15419fa2f6cfd15d4c 100644 (file)
@@ -24,14 +24,14 @@ import org.apache.ibatis.session.ResultContext;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.SqlSession;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 import org.sonar.core.persistence.MyBatis;
 
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
 
-public class ResourceSnapshotMapperTest extends DaoTestCase {
+public class ResourceSnapshotMapperTest extends AbstractDaoTestCase {
   @Test
   public void should_find_all() {
     setupData("fixture");
index c5a0cddbc5b723ff40105e704f652f299096b0b5..ce49f1f154c4ed78668d05a79850a6263e1985c6 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.core.duplication;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.Arrays;
 import java.util.List;
@@ -29,7 +29,7 @@ import java.util.List;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 
-public class DuplicationDaoTest extends DaoTestCase {
+public class DuplicationDaoTest extends AbstractDaoTestCase {
 
   private DuplicationDao dao;
 
index 504f0a4bd787a11ad398bbfa182b5300ed728455..2be3f63b85a19b7ece6a3f732481c5a457fa26ca 100644 (file)
@@ -25,11 +25,11 @@ import org.sonar.core.filter.CriterionDto;
 import org.sonar.core.filter.FilterColumnDto;
 import org.sonar.core.filter.FilterDao;
 import org.sonar.core.filter.FilterDto;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import static org.fest.assertions.Assertions.assertThat;
 
-public class FilterDaoTest extends DaoTestCase {
+public class FilterDaoTest extends AbstractDaoTestCase {
   private FilterDao dao;
 
   @Before
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java b/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java
new file mode 100644 (file)
index 0000000..9222fcd
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.persistence;
+
+import com.google.common.collect.Maps;
+import com.google.common.io.Closeables;
+import org.dbunit.Assertion;
+import org.dbunit.DataSourceDatabaseTester;
+import org.dbunit.DatabaseUnitException;
+import org.dbunit.IDatabaseTester;
+import org.dbunit.database.DatabaseConfig;
+import org.dbunit.database.IDatabaseConnection;
+import org.dbunit.dataset.DataSetException;
+import org.dbunit.dataset.IDataSet;
+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.sonar.api.config.Settings;
+
+import java.io.InputStream;
+import java.sql.SQLException;
+
+import static org.junit.Assert.fail;
+
+public abstract class AbstractDaoTestCase {
+  private static Database database;
+  private static MyBatis myBatis;
+  private static DatabaseCommands databaseCommands;
+
+  private IDatabaseTester databaseTester;
+  private IDatabaseConnection connection;
+
+  @BeforeClass
+  public static void startDatabase() throws Exception {
+    Settings settings = new Settings().setProperties(Maps.fromProperties(System.getProperties()));
+
+    boolean hasDialect = settings.hasKey("sonar.jdbc.dialect");
+    if (hasDialect) {
+      database = new DefaultDatabase(settings);
+    } else {
+      database = new H2Database();
+    }
+    database.start();
+
+    myBatis = new MyBatis(database);
+    myBatis.start();
+
+    databaseCommands = DatabaseCommands.forDialect(database.getDialect());
+  }
+
+  @Before
+  public void setupDbUnit() throws SQLException {
+    databaseCommands.truncateDatabase(myBatis.openSession().getConnection());
+    databaseTester = new DataSourceDatabaseTester(database.getDataSource());
+  }
+
+  @After
+  public void stopConnection() throws Exception {
+    if (databaseTester != null) {
+      databaseTester.onTearDown();
+    }
+    if (connection != null) {
+      connection.close();
+    }
+  }
+
+  @AfterClass
+  public static void stopDatabase() {
+    if (database != null) {
+      database.stop();
+    }
+  }
+
+  protected MyBatis getMyBatis() {
+    return myBatis;
+  }
+
+  protected void setupData(String testName) {
+    InputStream stream = null;
+    try {
+      String className = getClass().getName();
+      className = String.format("/%s/%s.xml", className.replace(".", "/"), testName);
+      stream = getClass().getResourceAsStream(className);
+      if (stream == null) {
+        throw new RuntimeException("Test not found :" + className);
+      }
+
+      setupData(stream);
+    } finally {
+      Closeables.closeQuietly(stream);
+    }
+  }
+
+  private void setupData(InputStream dataStream) {
+    try {
+      ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(dataStream));
+      dataSet.addReplacementObject("[null]", null);
+      dataSet.addReplacementObject("[false]", databaseCommands.getFalse());
+      dataSet.addReplacementObject("[true]", databaseCommands.getTrue());
+
+      databaseTester.setDataSet(dataSet);
+      connection = databaseTester.getConnection();
+
+      connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, databaseCommands.getDbUnitFactory());
+      databaseCommands.getDbunitDatabaseOperation().execute(connection, databaseTester.getDataSet());
+
+    } catch (Exception e) {
+      throw translateException("Could not setup DBUnit data", e);
+    }
+  }
+
+  protected void checkTables(String testName, String... tables) {
+    checkTables(testName, new String[] {}, tables);
+  }
+
+  protected void checkTables(String testName, String[] excludedColumnNames, String... tables) {
+    try {
+      IDataSet dataSet = getCurrentDataSet();
+      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());
+    }
+  }
+
+  protected void assertEmptyTables(String... emptyTables) {
+    for (String table : emptyTables) {
+      try {
+        Assert.assertEquals("Table " + table + " not empty.", 0, getCurrentDataSet().getTable(table).getRowCount());
+      } catch (DataSetException e) {
+        throw translateException("Error while checking results", e);
+      }
+    }
+  }
+
+  private 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 {
+      Closeables.closeQuietly(in);
+    }
+  }
+
+  private IDataSet getData(InputStream stream) {
+    try {
+      ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream));
+      dataSet.addReplacementObject("[null]", null);
+      dataSet.addReplacementObject("[false]", databaseCommands.getFalse());
+      dataSet.addReplacementObject("[true]", databaseCommands.getTrue());
+      return dataSet;
+    } catch (Exception e) {
+      throw translateException("Could not read the dataset stream", e);
+    }
+  }
+
+  private IDataSet getCurrentDataSet() {
+    try {
+      return connection.createDataSet();
+    } catch (SQLException 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;
+  }
+
+  protected IDatabaseConnection getConnection() {
+    return connection;
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java b/sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java
deleted file mode 100644 (file)
index 0a6c5dc..0000000
+++ /dev/null
@@ -1,238 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.core.persistence;
-
-import com.google.common.collect.Maps;
-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.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.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.sonar.api.config.Settings;
-
-import java.io.InputStream;
-import java.io.StringWriter;
-import java.sql.SQLException;
-
-import static org.junit.Assert.fail;
-
-public abstract class DaoTestCase {
-
-  private static Database database;
-  private static MyBatis myBatis;
-  private static DatabaseCommands databaseCommands;
-
-  private IDatabaseTester databaseTester;
-  private IDatabaseConnection connection;
-
-  @BeforeClass
-  public static void startDatabase() throws Exception {
-    Settings settings = new Settings();
-    settings.setProperties(Maps.fromProperties(System.getProperties()));
-    boolean hasDialect = settings.hasKey("sonar.jdbc.dialect");
-
-    if (hasDialect) {
-      database = new DefaultDatabase(settings);
-    } else {
-      database = new H2Database();
-    }
-    database.start();
-
-    myBatis = new MyBatis(database);
-    myBatis.start();
-
-    databaseCommands = DatabaseCommands.forDialect(database.getDialect());
-  }
-
-  @Before
-  public void setupDbUnit() throws SQLException {
-    databaseCommands.truncateDatabase(myBatis.openSession().getConnection());
-    databaseTester = new DataSourceDatabaseTester(database.getDataSource());
-  }
-
-  @After
-  public void tearDownDbUnit() throws Exception {
-    if (databaseTester != null) {
-      databaseTester.onTearDown();
-    }
-    if (connection != null) {
-      connection.close();
-    }
-  }
-
-  @AfterClass
-  public static void stopDatabase() {
-    if (database != null) {
-      database.stop();
-    }
-  }
-
-  protected MyBatis getMyBatis() {
-    return myBatis;
-  }
-
-  protected 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);
-      }
-    }
-  }
-
-  protected 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);
-        dataSet.addReplacementObject("[false]", databaseCommands.getFalse());
-        dataSet.addReplacementObject("[true]", databaseCommands.getTrue());
-        dataSets[i] = dataSet;
-      }
-      CompositeDataSet compositeDataSet = new CompositeDataSet(dataSets);
-
-      databaseTester.setDataSet(compositeDataSet);
-      connection = databaseTester.getConnection();
-
-      connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, databaseCommands.getDbUnitFactory());
-      databaseCommands.getDbunitDatabaseOperation().execute(connection, databaseTester.getDataSet());
-
-    } catch (Exception e) {
-      throw translateException("Could not setup DBUnit data", e);
-    }
-  }
-
-  protected final void checkTables(String testName, String... tables) {
-    checkTables(testName, new String[] {}, tables);
-  }
-
-  protected final void checkTables(String testName, String[] excludedColumnNames, String... tables) {
-    try {
-      IDataSet dataSet = getCurrentDataSet();
-      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());
-    }
-  }
-
-  protected final void assertEmptyTables(String... emptyTables) {
-    for (String table : emptyTables) {
-      try {
-        Assert.assertEquals("Table " + table + " not empty.", 0, getCurrentDataSet().getTable(table).getRowCount());
-      } catch (DataSetException e) {
-        throw translateException("Error while checking results", e);
-      }
-    }
-  }
-
-  protected 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);
-    }
-  }
-
-  protected final IDataSet getData(InputStream stream) {
-    try {
-      ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream));
-      dataSet.addReplacementObject("[null]", null);
-      dataSet.addReplacementObject("[false]", databaseCommands.getFalse());
-      dataSet.addReplacementObject("[true]", databaseCommands.getTrue());
-      return dataSet;
-    } catch (Exception e) {
-      throw translateException("Could not read the dataset stream", e);
-    }
-  }
-
-  protected final IDataSet getCurrentDataSet() {
-    try {
-      return connection.createDataSet();
-    } catch (SQLException e) {
-      throw translateException("Could not create the current dataset", e);
-    }
-  }
-
-  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 IDatabaseConnection getConnection() {
-    return connection;
-  }
-
-  protected IDatabaseTester getDatabaseTester() {
-    return databaseTester;
-  }
-
-}
index 760c3e8df32b9c55eb85e486522d0f0e17513816..3c011b434262ca8cf5001713ba0c29a1d9337e48 100644 (file)
@@ -31,6 +31,6 @@ public class DaoUtilsTest {
   public void should_list_all_dao_classes() {
     List<Class<?>> daoClasses = DaoUtils.getDaoClasses();
 
-    assertThat(daoClasses.size()).isGreaterThan(1);
+    assertThat(daoClasses).isNotEmpty();
   }
 }
index 1ec1e6b84aa8ccff1735317901c0db8d02952785..df1dce3e4265321ea12ea3c55ebddc5e3ccaa2f6 100644 (file)
@@ -26,7 +26,7 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
 
-public class DatabaseVersionTest extends DaoTestCase {
+public class DatabaseVersionTest extends AbstractDaoTestCase {
   @Test
   public void getVersion() {
     setupData("getVersion");
index a2a6887ab95b951fb48cee0c5e4e4ce80a424f7b..250b86eb6d4cd77acd3466c37b72a561534e4674 100644 (file)
@@ -45,13 +45,13 @@ public class DdlUtilsTest {
     Connection connection = DriverManager.getConnection("jdbc:h2:mem:sonar_test");
     DdlUtils.createSchema(connection, "h2");
 
-    int tables = countTables(connection);
+    int tableCount = countTables(connection);
 
     connection.close();
-    assertThat(tables).isGreaterThan(30);
+    assertThat(tableCount).isGreaterThan(30);
   }
 
-  private int countTables(Connection connection) throws SQLException {
+  static int countTables(Connection connection) throws SQLException {
     int count = 0;
     ResultSet resultSet = connection.getMetaData().getTables(null, null, null, new String[] {"TABLE"});
     while (resultSet.next()) {
index 24723714c4e5495ec2399951a014ca0fabef1574..64945262ef2e6bcbdae9ebd91bd5b5f60d55318a 100644 (file)
@@ -101,6 +101,7 @@ public class DefaultDatabaseTest {
 
     DefaultDatabase db = new DefaultDatabase(settings);
     db.start();
+    db.stop();
 
     assertThat(db.getDialect().getId(), Is.is("h2"));
     assertThat(((BasicDataSource) db.getDataSource()).getMaxActive(), Is.is(1));
index a9690c06edbaa664cb470db3650b4f7303a910c5..bc893b4bd75760d93f8aa02cf5ce661881f44deb 100644 (file)
@@ -114,7 +114,6 @@ public class H2Database implements Database {
   public Properties getHibernateProperties() {
     Properties properties = new Properties();
     properties.put("hibernate.hbm2ddl.auto", "validate");
-    properties.put(Environment.DIALECT, getDialect().getHibernateDialectClass().getName());
     properties.put(Environment.CONNECTION_PROVIDER, CustomHibernateConnectionProvider.class.getName());
     return properties;
   }
index f08c05f68550bb9810d73d62fddc6f90d9829e39..f896c62a9f8f4027171a1fdffc2717575760acb1 100644 (file)
@@ -24,7 +24,6 @@ import org.junit.Before;
 import org.junit.Test;
 
 import java.sql.Connection;
-import java.sql.ResultSet;
 import java.sql.SQLException;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -45,21 +44,17 @@ public class H2DatabaseTest {
   @Test
   public void shouldExecuteDdlAtStartup() throws SQLException {
     Connection connection = db.getDataSource().getConnection();
-
-    int tables = 0;
-    ResultSet resultSet = connection.getMetaData().getTables(null, null, null, new String[] {"TABLE"});
-    while (resultSet.next()) {
-      tables++;
-    }
-
+    int tableCount = DdlUtilsTest.countTables(connection);
     connection.close();
 
-    assertThat(tables).isGreaterThan(30);
+    assertThat(tableCount).isGreaterThan(30);
   }
 
   @Test
   public void shouldLimitThePoolSize() {
-    assertThat(((BasicDataSource) db.getDataSource()).getMaxActive()).isEqualTo(2);
-    assertThat(((BasicDataSource) db.getDataSource()).getMaxIdle()).isEqualTo(2);
+    BasicDataSource dataSource = (BasicDataSource) db.getDataSource();
+
+    assertThat(dataSource.getMaxActive()).isEqualTo(2);
+    assertThat(dataSource.getMaxIdle()).isEqualTo(2);
   }
 }
index d6461c11fb58a29388cde2b9c1ff5ca1e222f777..20c39047808960ca7be7ad74682e3666e14888da 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.core.properties;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.List;
 
@@ -29,7 +29,7 @@ import static org.hamcrest.Matchers.hasItems;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 
-public class PropertiesDaoTest extends DaoTestCase {
+public class PropertiesDaoTest extends AbstractDaoTestCase {
 
   private PropertiesDao dao;
 
index cd200b5e8e3ad4a2a8923b45eea4ceb7309ea388..4f9e8bc29f0985d162b893cbac0ea6d20e2e6e4a 100644 (file)
@@ -21,12 +21,12 @@ package org.sonar.core.purge;
 
 import org.apache.ibatis.session.SqlSession;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 import org.sonar.core.persistence.MyBatis;
 
 import java.util.Arrays;
 
-public class PurgeCommandsTest extends DaoTestCase {
+public class PurgeCommandsTest extends AbstractDaoTestCase {
   /**
    * Test that all related data is deleted.
    */
index 9e7db168941b83318bae972a5f6c053fcecad350..bfa9316d3990b7b02f961dc0e436aa3b9501500a 100644 (file)
@@ -25,7 +25,7 @@ import org.hamcrest.Description;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.resources.Scopes;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 import org.sonar.core.persistence.MyBatis;
 import org.sonar.core.resource.ResourceDao;
 
@@ -35,7 +35,7 @@ import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
 
-public class PurgeDaoTest extends DaoTestCase {
+public class PurgeDaoTest extends AbstractDaoTestCase {
 
   private PurgeDao dao;
 
index 67faa00a489a3bc232fd549f61d4c2730bc10c85..a16eec194c8bbb82259515bbb68053d0fb1c2c16 100644 (file)
@@ -21,13 +21,13 @@ package org.sonar.core.resource;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
 
-public class ResourceDaoTest extends DaoTestCase {
+public class ResourceDaoTest extends AbstractDaoTestCase {
 
   private ResourceDao dao;
 
index 399c7fa69bc48c597d5810f762c543e227d44cce..e483fdb7245c0f0b4ce866885c56c87b57b30f62 100644 (file)
@@ -23,7 +23,7 @@ import org.hamcrest.core.Is;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.resources.Qualifiers;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -32,7 +32,7 @@ import java.sql.SQLException;
 import static org.hamcrest.number.OrderingComparisons.greaterThan;
 import static org.junit.Assert.assertThat;
 
-public class ResourceIndexerDaoTest extends DaoTestCase {
+public class ResourceIndexerDaoTest extends AbstractDaoTestCase {
 
   private static ResourceIndexerDao dao;
 
index e8a1e793bf65cabc298be22f33be5732a1a557ed..97367da23a794cc2afd036501dc8923ecafdd405 100644 (file)
@@ -23,13 +23,13 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.Map;
 
 import static org.fest.assertions.Assertions.assertThat;
 
-public class ResourceKeyUpdaterDaoTest extends DaoTestCase {
+public class ResourceKeyUpdaterDaoTest extends AbstractDaoTestCase {
 
   @Rule
   public ExpectedException thrown = ExpectedException.none();
index 5667b8b091cdfcc127718c745b5c0872c7279c74..0b44e63146cf19c1451d49e3f64ac2acd347a81b 100644 (file)
@@ -21,11 +21,11 @@ package org.sonar.core.review;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.Date;
 
-public class ReviewCommentDaoTest extends DaoTestCase {
+public class ReviewCommentDaoTest extends AbstractDaoTestCase {
 
   private ReviewCommentDao dao;
 
index ed070c34fef81172c258867917ca84fe8dee34d2..b538e04ff795d6a8cb68f0763fb691e121161d00 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.core.review;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.utils.DateUtils;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.Collection;
 
@@ -30,7 +30,7 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
 
-public class ReviewDaoTest extends DaoTestCase {
+public class ReviewDaoTest extends AbstractDaoTestCase {
 
   private ReviewDao dao;
 
index de524816b51c667f9a7487872c3707e681b1e91f..e5d3a50dcc0a5b94d71d5382ff463792723f6f2a 100644 (file)
@@ -22,13 +22,13 @@ package org.sonar.core.rule;
 import org.hamcrest.core.Is;
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.List;
 
 import static org.junit.Assert.assertThat;
 
-public class RuleDaoTest extends DaoTestCase {
+public class RuleDaoTest extends AbstractDaoTestCase {
 
   private static RuleDao dao;
 
index 3a5d05180057bc23819d9e729138d8dacf09ad4a..fa5c9344ba9cf88e32ddc3e581acd4335d63e101 100644 (file)
@@ -21,12 +21,12 @@ package org.sonar.core.template;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 
-public class LoadedTemplateDaoTest extends DaoTestCase {
+public class LoadedTemplateDaoTest extends AbstractDaoTestCase {
 
   private LoadedTemplateDao dao;
 
index 05f73abf63899fbbd2c01f11b791978f2308fc70..db93f7799873f60d8b18f2c648e6fdb9f351a7ac 100644 (file)
@@ -21,13 +21,13 @@ package org.sonar.core.user;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertThat;
 
-public class AuthorDaoTest extends DaoTestCase {
+public class AuthorDaoTest extends AbstractDaoTestCase {
 
   private AuthorDao dao;
 
index b89a421d5cf1c14246634d35254d3166766747ba..41bb6d24748918a0a2ea15403a8e800f9378e053 100644 (file)
@@ -24,14 +24,14 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.api.workflow.Comment;
 import org.sonar.api.workflow.internal.DefaultReview;
-import org.sonar.core.persistence.DaoTestCase;
+import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.Arrays;
 import java.util.Date;
 
 import static org.fest.assertions.Assertions.assertThat;
 
-public class ReviewDatabaseStoreTest extends DaoTestCase {
+public class ReviewDatabaseStoreTest extends AbstractDaoTestCase {
 
   @Test
   public void store() {
diff --git a/sonar-core/src/test/java/org/sonar/jpa/session/ThreadLocalDatabaseSessionFactoryTest.java b/sonar-core/src/test/java/org/sonar/jpa/session/ThreadLocalDatabaseSessionFactoryTest.java
deleted file mode 100644 (file)
index fa1df23..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.session;
-
-import org.junit.Test;
-
-public class ThreadLocalDatabaseSessionFactoryTest {
-
-
-  @Test
-  public void shouldCreateOneSessionPerThread() {
-//    final MemoryDatabaseConnector connector = new MemoryDatabaseConnector();
-//    connector.start();
-//    final DatabaseSessionFactory factory = new ThreadLocalDatabaseSessionFactory(connector);
-//
-//    final DatabaseSession junitThreadSession = factory.getSession();
-//    assertTrue(junitThreadSession == factory.getSession());
-//
-//    new Thread() {
-//      @Override
-//      public void run() {
-//        DatabaseSession threadSession = factory.getSession();
-//        assertTrue(threadSession != junitThreadSession);
-//      }
-//
-//    }.start();
-  }
-
-}
index 58afe6240ce84acb331ed5a1da2da9612ea1ee24..78d9fe4164eabfc5978af60bd5266da9c0215e53 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.jpa.test;
 
+import org.junit.AfterClass;
+
 import org.apache.commons.io.IOUtils;
 import org.dbunit.Assertion;
 import org.dbunit.DataSourceDatabaseTester;
@@ -53,7 +55,7 @@ import java.sql.SQLException;
 import static org.junit.Assert.fail;
 
 /**
- * Heavily duplicates DaoTestCase as long as Hibernate is in use.
+ * Heavily duplicates AbstractDaoTestCase as long as Hibernate is in use.
  */
 public abstract class AbstractDbUnitTestCase {
   private static Database database;
@@ -97,11 +99,18 @@ public abstract class AbstractDbUnitTestCase {
     }
   }
 
-  public DatabaseSession getSession() {
+  @AfterClass
+  public static void stopDatabase() {
+    if (database != null) {
+      database.stop();
+    }
+  }
+
+  protected DatabaseSession getSession() {
     return session;
   }
 
-  public DatabaseSessionFactory getSessionFactory() {
+  protected DatabaseSessionFactory getSessionFactory() {
     return new DatabaseSessionFactory() {
 
       public DatabaseSession getSession() {
@@ -113,7 +122,7 @@ public abstract class AbstractDbUnitTestCase {
     };
   }
 
-  protected final void setupData(String... testNames) {
+  protected void setupData(String... testNames) {
     InputStream[] streams = new InputStream[testNames.length];
     try {
       for (int i = 0; i < testNames.length; i++) {
@@ -134,7 +143,7 @@ public abstract class AbstractDbUnitTestCase {
     }
   }
 
-  private final void setupData(InputStream... dataSetStream) {
+  private void setupData(InputStream... dataSetStream) {
     try {
       IDataSet[] dataSets = new IDataSet[dataSetStream.length];
       for (int i = 0; i < dataSetStream.length; i++) {
@@ -155,11 +164,11 @@ public abstract class AbstractDbUnitTestCase {
     }
   }
 
-  protected final void checkTables(String testName, String... tables) {
+  protected void checkTables(String testName, String... tables) {
     checkTables(testName, new String[0], tables);
   }
 
-  protected final void checkTables(String testName, String[] excludedColumnNames, String... tables) {
+  protected void checkTables(String testName, String[] excludedColumnNames, String... tables) {
     getSession().commit();
     try {
       IDataSet dataSet = getCurrentDataSet();
@@ -176,7 +185,7 @@ public abstract class AbstractDbUnitTestCase {
     }
   }
 
-  private final IDataSet getExpectedData(String testName) {
+  private IDataSet getExpectedData(String testName) {
     String className = getClass().getName();
     className = String.format("/%s/%s-result.xml", className.replace(".", "/"), testName);
 
@@ -188,7 +197,7 @@ public abstract class AbstractDbUnitTestCase {
     }
   }
 
-  private final IDataSet getData(InputStream stream) {
+  private IDataSet getData(InputStream stream) {
     try {
       ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream));
       dataSet.addReplacementObject("[null]", null);
@@ -198,7 +207,7 @@ public abstract class AbstractDbUnitTestCase {
     }
   }
 
-  private final IDataSet getCurrentDataSet() {
+  private IDataSet getCurrentDataSet() {
     try {
       return connection.createDataSet();
     } catch (SQLException e) {
index 102905e97ca5a030b91bd8561ff72ab134e7c13b..9d79811cb5361fb47ec3d78e0a64dcf2a28d7fe0 100644 (file)
@@ -22,7 +22,7 @@ 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.persistence.AbstractDaoTestCase;
 import org.sonar.core.properties.PropertiesDao;
 
 import java.io.File;
@@ -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 DaoTestCase {
+public class ServerSettingsTest extends AbstractDaoTestCase {
 
   private static File home = getHome();