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 164 165 166 167 168 169 170 |
package com.confrage; import static org.junit.Assert.fail; import java.io.File; import java.io.FileInputStream; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.util.List; import java.util.Objects; import mockit.Deencapsulation; import org.dbunit.DefaultDatabaseTester; import org.dbunit.IDatabaseTester; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.IDatabaseConnection; import org.dbunit.dataset.Column; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ITable; import org.dbunit.dataset.excel.XlsDataSet; import org.junit.After; import org.junit.Before; public abstract class AbstractDBUnit{ /** DBUnit用コネクション */ private IDatabaseConnection dbConnection = null; /** DBUnit用データベーステスター */ private IDatabaseTester tester = null; /** * ベースファイルのパス */ protected final String baseFile = "./src/com/confrage/baseFile.xlsx"; /** * JDBCコネクションを取得します * @return connection コネクション * @throws Exception 例外 */ private Connection getJDBCConnection() throws Exception { String path = "jdbc:oracle:thin:@localhost:1521:XE"; String id = "USER002"; String pw = "USER002"; Connection connection = DriverManager.getConnection(path, id, pw); connection.setAutoCommit(false); return connection; } /** * DBUnit用コネクションを取得します * @return IDatabaseConnection DBUnit用コネクション * @throws Exception 例外 */ protected IDatabaseConnection getConnection() throws Exception { if (dbConnection == null || dbConnection.getConnection().isClosed()) { Connection jdbcConnection = getJDBCConnection(); dbConnection = new DatabaseConnection(jdbcConnection, jdbcConnection.getSchema()); } return dbConnection; } /** * IDatabaseTesterを取得します * @return IDatabaseTester データベーステスター * @throws Exception 例外 */ protected IDatabaseTester getDatabaseTester() throws Exception { if (this.tester == null) { this.tester = new DefaultDatabaseTester(getConnection()); } return this.tester; } /** * テストメソッド前のsetupを実行します */ @Before public void setUp() throws Exception { final IDatabaseTester databaseTester = getDatabaseTester(); databaseTester.setDataSet(getDataSet()); databaseTester.onSetup(); } /** * IDataSetを取得します * */ protected IDataSet getDataSet() throws Exception { return new XlsDataSet(new FileInputStream(new File(baseFile))); } /** * テストメソッド後のtearDownを実行します */ @After public void tearDown() throws Exception { if (!dbConnection.getConnection().isClosed()) { try { final IDatabaseTester databaseTester = getDatabaseTester(); databaseTester.onTearDown(); } finally { tester = null; } dbConnection.getConnection().close(); dbConnection.close(); } } /** * DTOリストを検証します * @param dirPath * @param fileName * @param sheetName * @param dtoList */ protected void assertDTO(String dirPath, String fileName, String sheetName, List dtoList) { try { FileInputStream fis = new FileInputStream(new File(dirPath, fileName)); IDataSet dataSet = new XlsDataSet(fis); ITable table = dataSet.getTable(sheetName); Column[] cols = table.getTableMetaData().getColumns(); // 行数検証 if (table.getRowCount() != dtoList.size()) { fail("期待値と実効値の行数が異なります。\r\n" + "期待値:" + String.valueOf(table.getRowCount()) + "\r\n" + "実効値:" + String.valueOf(dtoList.size()) + "\r\n"); } for (int row = 0; row < table.getRowCount(); row++) { D dto = dtoList.get(row); for(Column col :cols) { // 期待値 Object expectedObj = table.getValue(row, col.getColumnName()); // 実効値 Object actualObj = Deencapsulation.getField(dto, col.getColumnName()); // 比較 boolean isEqual = false; if (expectedObj != null && actualObj != null && actualObj instanceof BigDecimal) { // BigDecimalで比較 BigDecimal expected = new BigDecimal(String.valueOf(expectedObj)); BigDecimal actual = (BigDecimal) actualObj; isEqual = actual.compareTo(expected) == 0; } else { // Stringで比較 String expected = expectedObj == null ? null : String.valueOf(expectedObj); String actual = actualObj == null ? null : String.valueOf(actualObj); isEqual = Objects.equals(actual, expected); } if (!isEqual) { // 期待値と実効値が異なる場合 fail(Integer.toString(row + 1) + "行目:フィールド" + col.getColumnName() + "\r\n" + "期待値:" + String.valueOf(expectedObj) + "\r\n" + "実効値:" + String.valueOf(actualObj) + "\r\n"); } } } } catch (AssertionError ex) { throw ex; } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException("DTOリスト検証エラー"); } } } |