aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/util/JdbcTestCase.java
blob: e419ab0d4f06485a981fbc425f108e1bddb4cd41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package at.ac.tuwien.sepm.assignment.groupphase.util;

import static org.dbunit.Assertion.assertEquals;

import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.Collections;
import org.dbunit.DefaultDatabaseTester;
import org.dbunit.IDatabaseTester;
import org.dbunit.IOperationListener;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.datatype.DataType;
import org.dbunit.dataset.datatype.DataTypeException;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.ext.h2.H2DataTypeFactory;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public abstract class JdbcTestCase {

    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    private static final String JDBC_URL =
            "jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:sql/database.sql'";

    private IDatabaseTester dbTester;
    private IDatabaseConnection connection;
    private IOperationListener operationListener;
    private JDBCConnectionManager jdbcConnectionManager;

    protected JdbcTestCase() {
        jdbcConnectionManager = new JDBCConnectionManager(JDBC_URL);
    }

    protected abstract IDataSet getDataSet() throws Exception;

    protected IDataSet getDataSet(String xmlname) throws DataSetException {
        InputStream res = getClass().getClassLoader().getResourceAsStream(xmlname);
        FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
        builder.setColumnSensing(true);
        return builder.build(res);
    }

    protected JDBCConnectionManager getJdbcConnectionManager() {
        return jdbcConnectionManager;
    }

    protected IDatabaseConnection getConnection() throws Exception {
        if (connection == null)
            connection = new DatabaseConnection(jdbcConnectionManager.getConnection(), null, true);

        return connection;
    }

    private IOperationListener getOperationListener() {
        if (operationListener == null) {
            operationListener =
                    new IOperationListener() {
                        @Override
                        public void connectionRetrieved(IDatabaseConnection connection) {
                            setUpDatabaseConfig(connection.getConfig());
                        }

                        @Override
                        public void operationSetUpFinished(IDatabaseConnection connection) {
                            LOG.debug("operationSetUpFinished(connection={}) - start", connection);
                        }

                        @Override
                        public void operationTearDownFinished(IDatabaseConnection connection) {
                            LOG.debug(
                                    "operationTearDownFinished(connection={}) - start", connection);
                            try {
                                connection.close();
                            } catch (SQLException e) {
                                LOG.error("Failed to close connection:" + e);
                                e.printStackTrace();
                            }
                        }
                    };
        }

        return operationListener;
    }

    // override DBUnit's enum handling
    private void setUpDatabaseConfig(DatabaseConfig config) {
        H2DataTypeFactory factory =
                new H2DataTypeFactory() {
                    boolean isEnumType(String sqlTypeName) {
                        return sqlTypeName.equalsIgnoreCase("enum");
                    }

                    boolean isTimestampWithTimeZoneType(String sqlTypeName) {
                        return sqlTypeName.equalsIgnoreCase("timestamp with time zone");
                    }

                    @Override
                    public DataType createDataType(int sqlType, String sqlTypeName)
                            throws DataTypeException {
                        if (isEnumType(sqlTypeName)) {
                            sqlType = Types.VARCHAR;
                        } else if (isTimestampWithTimeZoneType(sqlTypeName)) {
                            sqlType = Types.VARCHAR;
                        }

                        return super.createDataType(sqlType, sqlTypeName);
                    }

                    @Override
                    public Collection getValidDbProducts() {
                        return Collections.singletonList("H2");
                    }
                };

        config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, factory);
    }

    protected void compareWith(String xmlname, String[] tables) throws Exception {
        InputStream res = getClass().getClassLoader().getResourceAsStream(xmlname);
        IDataSet actual = getConnection().createDataSet();
        IDataSet expected = new FlatXmlDataSetBuilder().build(res);

        for (String table : tables) {
            assertEquals(expected.getTable(table), actual.getTable(table));
        }
    }

    @Before
    public void setUp() throws Exception {
        IDataSet dataSet = getDataSet();

        dbTester = new DefaultDatabaseTester(getConnection());
        getOperationListener().connectionRetrieved(getConnection());

        dbTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
        dbTester.setTearDownOperation(DatabaseOperation.REFRESH);
        dbTester.setDataSet(dataSet);
        dbTester.setOperationListener(getOperationListener());
        dbTester.onSetup();
    }

    @After
    public void tearDown() throws Exception {
        try {
            dbTester.onTearDown();
            getConnection().close();
        } finally {
            dbTester = null;
        }
    }
}