Java tutorial
/* * 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.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.text.StrSubstitutor; 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.dbunit.ext.mssql.InsertIdentityOperation; import org.dbunit.operation.DatabaseOperation; import org.junit.Assert; import org.junit.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.config.Settings; import org.sonar.core.config.Logback; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; import java.sql.Connection; import java.sql.SQLException; import java.util.Map; import java.util.Properties; import static org.junit.Assert.fail; public abstract class AbstractDaoTestCase { private static Logger LOG = LoggerFactory.getLogger(AbstractDaoTestCase.class); private static Database database; private static DatabaseCommands databaseCommands; private static IDatabaseTester databaseTester; private static MyBatis myBatis; @Before public void startDatabase() throws Exception { if (database == null) { Settings settings = new Settings().setProperties(Maps.fromProperties(System.getProperties())); if (settings.hasKey("orchestrator.configUrl")) { loadOrchestratorSettings(settings); } for (String key : settings.getKeysStartingWith("sonar.jdbc")) { LOG.info(key + ": " + settings.getString(key)); } boolean hasDialect = settings.hasKey("sonar.jdbc.dialect"); if (hasDialect) { database = new DefaultDatabase(settings); } else { database = new H2Database("h2Tests"); } database.start(); LOG.info("Test Database: " + database); databaseCommands = DatabaseCommands.forDialect(database.getDialect()); databaseTester = new DataSourceDatabaseTester(database.getDataSource()); myBatis = new MyBatis(database, settings, new Logback()); myBatis.start(); } databaseCommands.truncateDatabase(database.getDataSource()); } private void loadOrchestratorSettings(Settings settings) throws URISyntaxException, IOException { String url = settings.getString("orchestrator.configUrl"); URI uri = new URI(url); InputStream input = null; try { if (url.startsWith("file:")) { File file = new File(uri); input = FileUtils.openInputStream(file); } else { HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); int responseCode = connection.getResponseCode(); if (responseCode >= 400) { throw new IllegalStateException("Fail to request: " + uri + ". Status code=" + responseCode); } input = connection.getInputStream(); } Properties props = new Properties(); props.load(input); settings.addProperties(props); for (Map.Entry<String, String> entry : settings.getProperties().entrySet()) { String interpolatedValue = StrSubstitutor.replace(entry.getValue(), System.getenv(), "${", "}"); settings.setProperty(entry.getKey(), interpolatedValue); } } finally { IOUtils.closeQuietly(input); } } protected MyBatis getMyBatis() { return myBatis; } protected Database getDatabase() { return database; } protected 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); databaseCommands.resetPrimaryKeys(database.getDataSource()); } catch (SQLException e) { throw translateException("Could not setup DBUnit data", e); } finally { for (InputStream stream : streams) { IOUtils.closeQuietly(stream); } } } private void setupData(InputStream... dataSetStream) { IDatabaseConnection connection = null; try { IDataSet[] dataSets = new IDataSet[dataSetStream.length]; for (int i = 0; i < dataSetStream.length; i++) { dataSets[i] = getData(dataSetStream[i]); } databaseTester.setDataSet(new CompositeDataSet(dataSets)); connection = createConnection(); new InsertIdentityOperation(DatabaseOperation.INSERT).execute(connection, databaseTester.getDataSet()); } catch (Exception e) { throw translateException("Could not setup DBUnit data", e); } finally { closeQuietly(connection); } } private void closeQuietly(IDatabaseConnection connection) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // ignore } } protected void checkTables(String testName, String... tables) { checkTables(testName, new String[0], tables); } protected void checkTables(String testName, String[] excludedColumnNames, String... tables) { IDatabaseConnection connection = null; try { connection = createConnection(); IDataSet dataSet = connection.createDataSet(); IDataSet expectedDataSet = getExpectedData(testName); for (String table : tables) { ITable filteredTable = DefaultColumnFilter.excludedColumnsTable(dataSet.getTable(table), excludedColumnNames); ITable filteredExpectedTable = DefaultColumnFilter .excludedColumnsTable(expectedDataSet.getTable(table), excludedColumnNames); Assertion.assertEquals(filteredExpectedTable, filteredTable); } } catch (DatabaseUnitException e) { fail(e.getMessage()); } catch (SQLException e) { throw translateException("Error while checking results", e); } finally { closeQuietly(connection); } } protected void checkTable(String testName, String table, String... columns) { IDatabaseConnection connection = null; try { connection = createConnection(); IDataSet dataSet = connection.createDataSet(); 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 (DatabaseUnitException e) { fail(e.getMessage()); } catch (SQLException e) { throw translateException("Error while checking results", e); } finally { closeQuietly(connection); } } protected void assertEmptyTables(String... emptyTables) { IDatabaseConnection connection = null; try { connection = createConnection(); IDataSet dataSet = connection.createDataSet(); for (String table : emptyTables) { try { Assert.assertEquals("Table " + table + " not empty.", 0, dataSet.getTable(table).getRowCount()); } catch (DataSetException e) { throw translateException("Error while checking results", e); } } } catch (SQLException e) { throw translateException("Error while checking results", e); } finally { closeQuietly(connection); } } private IDatabaseConnection createConnection() { try { IDatabaseConnection connection = databaseTester.getConnection(); connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, databaseCommands.getDbUnitFactory()); return connection; } catch (Exception e) { throw translateException("Error while getting connection", 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]", Boolean.FALSE); dataSet.addReplacementObject("[true]", Boolean.TRUE); return dataSet; } catch (Exception e) { throw translateException("Could not read the dataset stream", 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 Connection getConnection() throws SQLException { return database.getDataSource().getConnection(); } }