PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/src/test/java/org/sigmah/endtoend/fixture/MSSQLDataSetLoader.java

http://sigma-h.googlecode.com/
Java | 117 lines | 96 code | 17 blank | 4 comment | 3 complexity | 4f657973527072e6bcb7da9c397bbe75 MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.endtoend.fixture;
  7
  8import org.dbunit.DatabaseUnitException;
  9import org.dbunit.database.IDatabaseConnection;
 10import org.dbunit.dataset.DataSetException;
 11import org.dbunit.dataset.IDataSet;
 12import org.dbunit.dataset.xml.FlatXmlDataSet;
 13import org.dbunit.ext.mssql.InsertIdentityOperation;
 14import org.dbunit.ext.mssql.MsSqlConnection;
 15import org.dbunit.operation.DatabaseOperation;
 16
 17import java.io.IOException;
 18import java.io.InputStream;
 19import java.io.InputStreamReader;
 20import java.sql.Connection;
 21import java.sql.DriverManager;
 22import java.sql.SQLException;
 23import java.sql.Statement;
 24
 25public class MSSQLDataSetLoader {
 26
 27    private Connection connection;
 28    private IDataSet dataSet;
 29    private String name;
 30
 31
 32    public void load(String name) {
 33        this.name = name;
 34        try {
 35            load();
 36        } catch (Exception e) {
 37            throw new AssertionError(e);
 38        }
 39    }
 40
 41    public void load() throws IOException, DatabaseUnitException, SQLException, ClassNotFoundException {
 42        openConnection();
 43        try {
 44            loadDataSet();
 45            removeAllRows();
 46            populate();
 47        } finally {
 48            closeConnection();
 49        }
 50    }
 51
 52    private void loadDataSet() throws IOException, DataSetException {
 53        InputStream in = getClass().getResourceAsStream(name);
 54        if (in == null)
 55            throw new Error("Could not find resource '" + name + "'");
 56        dataSet = new FlatXmlDataSet(new InputStreamReader(in), false, true, false);
 57    }
 58
 59    private void openConnection() throws SQLException, ClassNotFoundException {
 60        Class.forName("net.sourceforge.jtds.jdbc.Driver");
 61        connection = DriverManager.getConnection(
 62            "jdbc:jtds:sqlserver://localhost:1433/actinfo;user=sa;password=adminpwd");
 63    }
 64
 65    private void removeAllRows() throws SQLException {
 66        String tables[] = {
 67            "AttributeGroupInActivity",
 68            "PartnerInDatabase",
 69            "LocationAdminLink",
 70            "IndicatorValue",
 71            "AttributeValue",
 72            "ReportingPeriod",
 73            "Site",
 74            "Location",
 75            "Indicator",
 76            "Attribute",
 77            "AttributeGroup",
 78            "Activity",
 79            "UserPermission",
 80            "Partner",
 81            "ReportSubscription",
 82            "ReportTemplate",
 83            "UserDatabase",
 84            "Authentication",
 85            "UserLogin",
 86            "LocationType",
 87            "AdminEntity",
 88            "AdminLevel",
 89            "Country" };
 90
 91        Statement stmt = connection.createStatement();
 92        for(String tableName : tables) {
 93            stmt.execute("delete from " + tableName);
 94        }
 95        stmt.close();
 96    }
 97
 98
 99    private void populate() throws SQLException, DatabaseUnitException {
100        executeOperation(InsertIdentityOperation.INSERT, dataSet);
101    }
102
103    private void executeOperation(final DatabaseOperation op, final IDataSet dataSet) throws SQLException, DatabaseUnitException {
104        IDatabaseConnection dbUnitConnection = createDbUnitConnection(connection);
105        op.execute(dbUnitConnection, dataSet);
106    }
107
108    private IDatabaseConnection createDbUnitConnection(Connection connection) throws DatabaseUnitException {
109        IDatabaseConnection con = new MsSqlConnection(connection);
110        con.getConfig().setProperty("http://www.dbunit.org/properties/escapePattern", "[?]");
111        return con;
112    }
113
114    private void closeConnection() throws SQLException {
115        connection.close();
116    }
117}