1 21 22 package org.dbunit.ext.mssql; 23 24 import org.dbunit.AbstractDatabaseTest; 25 import org.dbunit.Assertion; 26 import org.dbunit.dataset.*; 27 import org.dbunit.dataset.xml.FlatXmlDataSet; 28 import org.dbunit.dataset.xml.XmlDataSet; 29 30 import java.io.FileReader ; 31 import java.io.Reader ; 32 33 39 public class InsertIdentityOperationTest extends AbstractDatabaseTest 40 { 41 public InsertIdentityOperationTest(String s) 42 { 43 super(s); 44 } 45 46 public void testExecuteXML() throws Exception 47 { 48 Reader in = new FileReader ("src/xml/insertIdentityOperationTest.xml"); 49 IDataSet dataSet = new XmlDataSet(in); 50 51 testExecute(dataSet); 52 } 53 54 public void testExecuteFlatXML() throws Exception 55 { 56 Reader in = new FileReader ("src/xml/insertIdentityOperationTestFlat.xml"); 57 IDataSet dataSet = new FlatXmlDataSet(in); 58 59 testExecute(dataSet); 60 } 61 62 public void testExecuteLowerCase() throws Exception 63 { 64 Reader in = new FileReader ("src/xml/insertIdentityOperationTestFlat.xml"); 65 IDataSet dataSet = new LowerCaseDataSet(new FlatXmlDataSet(in)); 66 67 testExecute(dataSet); 68 } 69 70 public void testExecuteForwardOnly() throws Exception 71 { 72 Reader in = new FileReader ("src/xml/insertIdentityOperationTestFlat.xml"); 73 IDataSet dataSet = new ForwardOnlyDataSet(new FlatXmlDataSet(in)); 74 75 testExecute(dataSet); 76 } 77 78 private void testExecute(IDataSet dataSet) throws Exception 79 { 80 ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet()); 81 InsertIdentityOperation.INSERT.execute(_connection, dataSet); 83 ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet()); 84 85 assertEquals("table count", tablesBefore.length, tablesAfter.length); 86 87 for (int i = 0; i < tablesAfter.length; i++) 89 { 90 ITable tableBefore = tablesBefore[i]; 91 ITable tableAfter = tablesAfter[i]; 92 93 String name = tableAfter.getTableMetaData().getTableName(); 94 if (name.startsWith("IDENTITY")) 95 { 96 assertEquals("row count before: " + name, 0, tableBefore.getRowCount()); 97 if (dataSet instanceof ForwardOnlyDataSet) 98 { 99 assertTrue(name, tableAfter.getRowCount() > 0); 100 } 101 else 102 { 103 Assertion.assertEquals(dataSet.getTable(name), tableAfter); 104 } 105 } 106 else 107 { 108 Assertion.assertEquals(tableBefore, tableAfter); 110 } 111 } 112 } 113 114 118 public void testIdentityInsertNoPK() throws Exception 119 { 120 Reader in = new FileReader ("src/xml/insertIdentityOperationTestNoPK.xml"); 121 IDataSet xmlDataSet = new FlatXmlDataSet(in); 122 123 ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet()); 124 InsertIdentityOperation.CLEAN_INSERT.execute(_connection, xmlDataSet); 125 ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet()); 126 127 for (int i = 0; i < tablesAfter.length; i++) 129 { 130 ITable tableBefore = tablesBefore[i]; 131 ITable tableAfter = tablesAfter[i]; 132 133 String name = tableAfter.getTableMetaData().getTableName(); 134 if (name.equals("TEST_IDENTITY_NOT_PK")) 135 { 136 assertEquals("row count before: " + name, 0, tableBefore.getRowCount()); 137 Assertion.assertEquals(xmlDataSet.getTable(name), tableAfter); 138 } 139 else 140 { 141 Assertion.assertEquals(tableBefore, tableAfter); 143 } 144 } 145 } 146 } 147 148 149 150 151 152 153 154 155 156 157 | Popular Tags |