| 1 | |
package org.jtheque.core.utils.test; |
| 2 | |
|
| 3 | |
import org.dbunit.database.DatabaseDataSourceConnection; |
| 4 | |
import org.dbunit.database.IDatabaseConnection; |
| 5 | |
import org.dbunit.dataset.Column; |
| 6 | |
import org.dbunit.dataset.DataSetException; |
| 7 | |
import org.dbunit.dataset.IDataSet; |
| 8 | |
import org.dbunit.dataset.ITable; |
| 9 | |
import org.dbunit.dataset.ITableMetaData; |
| 10 | |
import org.dbunit.dataset.xml.FlatXmlDataSet; |
| 11 | |
import org.dbunit.operation.DatabaseOperation; |
| 12 | |
import org.junit.Before; |
| 13 | |
|
| 14 | |
import javax.sql.DataSource; |
| 15 | |
import java.io.IOException; |
| 16 | |
import java.sql.Connection; |
| 17 | |
import java.sql.SQLException; |
| 18 | |
|
| 19 | |
import static org.junit.Assert.fail; |
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public abstract class AbstractDBUnitTest { |
| 43 | |
private IDatabaseConnection connection; |
| 44 | |
|
| 45 | |
private final String datasetPath; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
protected AbstractDBUnitTest(String datasetPath) { |
| 53 | 0 | super(); |
| 54 | |
|
| 55 | 0 | this.datasetPath = datasetPath; |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
protected void initDB(DataSource dataSource) { |
| 64 | |
try { |
| 65 | 0 | connection = new DatabaseDataSourceConnection(dataSource); |
| 66 | |
|
| 67 | 0 | IDataSet dataSet = new FlatXmlDataSet(getClass().getResource(datasetPath)); |
| 68 | |
|
| 69 | 0 | createHsqldbTables(dataSet, connection.getConnection()); |
| 70 | 0 | } catch (SQLException e) { |
| 71 | 0 | fail(e.getMessage()); |
| 72 | 0 | } catch (IOException e) { |
| 73 | 0 | fail(e.getMessage()); |
| 74 | 0 | } catch (DataSetException e) { |
| 75 | 0 | fail(e.getMessage()); |
| 76 | 0 | } |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
@Before |
| 83 | |
public void setUp() { |
| 84 | |
try { |
| 85 | 0 | IDataSet dataSet = new FlatXmlDataSet(getClass().getResource(datasetPath)); |
| 86 | |
|
| 87 | 0 | DatabaseOperation.CLEAN_INSERT.execute( |
| 88 | |
connection, |
| 89 | |
dataSet); |
| 90 | 0 | } catch (Exception e) { |
| 91 | 0 | fail(e.getMessage()); |
| 92 | 0 | } |
| 93 | 0 | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
protected final ITable getTable(String s) { |
| 102 | |
try { |
| 103 | 0 | return connection.createDataSet().getTable(s); |
| 104 | 0 | } catch (Exception e) { |
| 105 | 0 | fail(e.getMessage()); |
| 106 | |
} |
| 107 | |
|
| 108 | 0 | return null; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
protected Object getValue(String table, int pk, String column) { |
| 120 | |
try { |
| 121 | 0 | return getTable(table).getValue(pk, column); |
| 122 | 0 | } catch (Exception e) { |
| 123 | 0 | fail(e.getMessage()); |
| 124 | |
} |
| 125 | |
|
| 126 | 0 | return null; |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
protected int getRowCount(String table) { |
| 136 | 0 | return getTable(table).getRowCount(); |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
protected final IDatabaseConnection getConnection() { |
| 145 | 0 | return connection; |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
protected static final void createHsqldbTables(IDataSet dataSet, Connection connection) throws DataSetException, SQLException { |
| 157 | 0 | String[] tableNames = dataSet.getTableNames(); |
| 158 | |
|
| 159 | 0 | StringBuilder sql = new StringBuilder(""); |
| 160 | |
|
| 161 | 0 | for (String tableName : tableNames) { |
| 162 | 0 | ITable table = dataSet.getTable(tableName); |
| 163 | 0 | ITableMetaData metadata = table.getTableMetaData(); |
| 164 | 0 | Column[] columns = metadata.getColumns(); |
| 165 | |
|
| 166 | 0 | sql.append("create table if not exists ").append(tableName).append("( "); |
| 167 | |
|
| 168 | 0 | boolean first = true; |
| 169 | 0 | for (Column column : columns) { |
| 170 | 0 | if (!first) { |
| 171 | 0 | sql.append(", "); |
| 172 | |
} |
| 173 | |
|
| 174 | 0 | String columnName = column.getColumnName(); |
| 175 | 0 | String type = resolveType((String) table.getValue(0, columnName)); |
| 176 | 0 | sql.append(columnName).append(' ').append(type); |
| 177 | |
|
| 178 | 0 | if (first) { |
| 179 | 0 | if (!columnName.contains("FK")) { |
| 180 | 0 | sql.append(" IDENTITY PRIMARY KEY"); |
| 181 | |
} |
| 182 | |
|
| 183 | 0 | first = false; |
| 184 | |
} |
| 185 | |
} |
| 186 | 0 | sql.append("); "); |
| 187 | |
} |
| 188 | |
|
| 189 | 0 | connection.prepareStatement(sql.toString()).executeUpdate(); |
| 190 | 0 | } |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
private static String resolveType(String str) { |
| 199 | |
try { |
| 200 | 0 | Integer.parseInt(str); |
| 201 | |
|
| 202 | 0 | return "int"; |
| 203 | 0 | } catch (Exception e) { |
| 204 | |
|
| 205 | |
} |
| 206 | |
|
| 207 | 0 | return "varchar"; |
| 208 | |
} |
| 209 | |
} |