1 21 22 package org.dbunit.operation; 23 24 import org.dbunit.AbstractDatabaseTest; 25 import org.dbunit.Assertion; 26 import org.dbunit.database.MockDatabaseConnection; 27 import org.dbunit.database.statement.MockBatchStatement; 28 import org.dbunit.database.statement.MockStatementFactory; 29 import org.dbunit.dataset.*; 30 import org.dbunit.dataset.datatype.DataType; 31 import org.dbunit.dataset.xml.FlatXmlDataSet; 32 33 import java.io.FileReader ; 34 import java.io.Reader ; 35 36 41 public class RefreshOperationTest extends AbstractDatabaseTest 42 { 43 public RefreshOperationTest(String s) 44 { 45 super(s); 46 } 47 48 public void testExecute() throws Exception 49 { 50 Reader reader = new FileReader ("src/xml/refreshOperationTest.xml"); 51 IDataSet dataSet = new FlatXmlDataSet(reader); 52 53 testExecute(dataSet); 54 } 55 56 public void testExecuteCaseInsensitive() throws Exception 57 { 58 Reader reader = new FileReader ("src/xml/refreshOperationTest.xml"); 59 IDataSet dataSet = new FlatXmlDataSet(reader); 60 61 testExecute(new LowerCaseDataSet(dataSet)); 62 } 63 64 public void testExecuteForwardOnly() throws Exception 65 { 66 Reader reader = new FileReader ("src/xml/refreshOperationTest.xml"); 67 IDataSet dataSet = new FlatXmlDataSet(reader); 68 69 testExecute(new ForwardOnlyDataSet(dataSet)); 70 } 71 72 private void testExecute(IDataSet dataSet) throws Exception 73 { 74 String [] tableNames = {"PK_TABLE", "ONLY_PK_TABLE"}; 75 int[] tableRowCount = {3, 1}; 76 String primaryKey = "PK0"; 77 78 assertEquals("array lenght", tableNames.length, tableRowCount.length); 80 for (int i = 0; i < tableNames.length; i++) 81 { 82 ITable tableBefore = createOrderedTable(tableNames[i], primaryKey); 83 assertEquals("row count before", tableRowCount[i], tableBefore.getRowCount()); 84 } 85 86 DatabaseOperation.REFRESH.execute(_connection, dataSet); 87 88 IDataSet expectedDataSet = new FlatXmlDataSet( 90 new FileReader ("src/xml/refreshOperationTestExpected.xml")); 91 92 for (int i = 0; i < tableNames.length; i++) 93 { 94 ITable expectedTable = expectedDataSet.getTable(tableNames[i]); 95 ITable tableAfter = createOrderedTable(tableNames[i], primaryKey); 96 Assertion.assertEquals(expectedTable, tableAfter); 97 } 98 } 99 100 public void testExecuteAndNoPrimaryKeys() throws Exception 101 { 102 String tableName = "test_table"; 103 104 Reader reader = new FileReader ("src/xml/refreshOperationNoPKTest.xml"); 105 IDataSet dataSet = new FlatXmlDataSet(reader); 106 107 assertEquals("row count before", 6, _connection.getRowCount(tableName)); 109 110 try 111 { 112 DatabaseOperation.REFRESH.execute(_connection, dataSet); 113 fail("Should not be here!"); 114 } 115 catch (NoPrimaryKeyException e) 116 { 117 118 } 119 120 assertEquals("row count before", 6, _connection.getRowCount(tableName)); 122 } 123 124 public void testExecuteWithDuplicateTables() throws Exception 125 { 126 String [] tableNames = {"PK_TABLE", "ONLY_PK_TABLE"}; 127 int[] tableRowCount = {3, 1}; 128 String primaryKey = "PK0"; 129 130 IDataSet xmlDataSet = new FlatXmlDataSet( 131 new FileReader ("src/xml/refreshOperationDuplicateTest.xml")); 132 assertEquals("table count", xmlDataSet.getTableNames().length, 4); 133 134 assertEquals("array lenght", tableNames.length, tableRowCount.length); 136 for (int i = 0; i < tableNames.length; i++) 137 { 138 ITable tableBefore = createOrderedTable(tableNames[i], primaryKey); 139 assertEquals("row count before", tableRowCount[i], tableBefore.getRowCount()); 140 } 141 142 DatabaseOperation.REFRESH.execute(_connection, xmlDataSet); 143 144 IDataSet expectedDataSet = new FlatXmlDataSet( 146 new FileReader ("src/xml/refreshOperationTestExpected.xml")); 147 148 for (int i = 0; i < tableNames.length; i++) 149 { 150 ITable expectedTable = expectedDataSet.getTable(tableNames[i]); 151 ITable tableAfter = createOrderedTable(tableNames[i], primaryKey); 152 Assertion.assertEquals(expectedTable, tableAfter); 153 } 154 } 155 156 public void testExecuteWithEmptyTable() throws Exception 157 { 158 Column[] columns = {new Column("c1", DataType.VARCHAR)}; 159 ITable table = new DefaultTable(new DefaultTableMetaData( 160 "name", columns, columns)); 161 IDataSet dataSet = new DefaultDataSet(table); 162 163 MockStatementFactory factory = new MockStatementFactory(); 165 factory.setExpectedCreatePreparedStatementCalls(0); 166 167 MockDatabaseConnection connection = new MockDatabaseConnection(); 168 connection.setupDataSet(dataSet); 169 connection.setupStatementFactory(factory); 170 connection.setExpectedCloseCalls(0); 171 172 DatabaseOperation.REFRESH.execute(connection, dataSet); 174 175 factory.verify(); 176 connection.verify(); 177 } 178 179 public void testExecuteUnknownColumn() throws Exception 180 { 181 String tableName = "table"; 182 183 Column[] columns = new Column[]{ 185 new Column("unknown", DataType.VARCHAR), 186 }; 187 DefaultTable table = new DefaultTable(tableName, columns); 188 table.addRow(); 189 table.setValue(0, columns[0].getColumnName(), "value"); 190 IDataSet insertDataset = new DefaultDataSet(table); 191 192 IDataSet databaseDataSet = new DefaultDataSet( 193 new DefaultTable(tableName, new Column[]{ 194 new Column("column", DataType.VARCHAR), 195 })); 196 197 MockBatchStatement statement = new MockBatchStatement(); 199 statement.setExpectedExecuteBatchCalls(0); 200 statement.setExpectedClearBatchCalls(0); 201 statement.setExpectedCloseCalls(0); 202 203 MockStatementFactory factory = new MockStatementFactory(); 204 factory.setExpectedCreatePreparedStatementCalls(0); 205 factory.setupStatement(statement); 206 207 MockDatabaseConnection connection = new MockDatabaseConnection(); 208 connection.setupDataSet(databaseDataSet); 209 connection.setupStatementFactory(factory); 210 connection.setExpectedCloseCalls(0); 211 212 try 214 { 215 new RefreshOperation().execute(connection, insertDataset); 216 fail("Should not be here!"); 217 } 218 catch (NoSuchColumnException e) 219 { 220 221 } 222 223 statement.verify(); 224 factory.verify(); 225 connection.verify(); 226 } 227 228 229 } 230 231 232 233 234 235 236 237 | Popular Tags |