1 21 22 package org.dbunit.operation; 23 24 import org.dbunit.AbstractDatabaseTest; 25 import org.dbunit.DatabaseUnitException; 26 import org.dbunit.dataset.IDataSet; 27 import org.dbunit.dataset.ITable; 28 import org.dbunit.dataset.xml.XmlDataSet; 29 30 import java.io.File ; 31 import java.io.FileReader ; 32 import java.io.Reader ; 33 import java.sql.Connection ; 34 import java.sql.SQLException ; 35 36 41 public class TransactionOperationTest extends AbstractDatabaseTest 42 { 43 public TransactionOperationTest(String s) 44 { 45 super(s); 46 } 47 48 public void testExecuteCommit() throws Exception 49 { 50 String tableName = "TEST_TABLE"; 51 Reader in = new FileReader ( 52 new File ("src/xml/transactionOperationTest.xml")); 53 IDataSet xmlDataSet = new XmlDataSet(in); 54 Connection jdbcConnection = _connection.getConnection(); 55 56 ITable tableBefore = _connection.createDataSet().getTable(tableName); 57 assertEquals("before row count", 6, tableBefore.getRowCount()); 58 assertEquals("autocommit before", true, jdbcConnection.getAutoCommit()); 59 60 DatabaseOperation operation = new CompositeOperation( 61 DatabaseOperation.DELETE_ALL, DatabaseOperation.INSERT); 62 operation = new TransactionOperation(operation); 63 operation.execute(_connection, xmlDataSet); 64 65 ITable tableAfter = _connection.createDataSet().getTable(tableName); 67 assertEquals("after row count", 1, tableAfter.getRowCount()); 68 assertEquals("autocommit after", true, jdbcConnection.getAutoCommit()); 69 } 70 71 public void testExclusiveTransaction() throws Exception 72 { 73 String tableName = "TEST_TABLE"; 74 Reader in = new FileReader ( 75 new File ("src/xml/transactionOperationTest.xml")); 76 IDataSet xmlDataSet = new XmlDataSet(in); 77 Connection jdbcConnection = _connection.getConnection(); 78 79 jdbcConnection.setAutoCommit(false); 80 81 assertEquals("autocommit before", false, jdbcConnection.getAutoCommit()); 83 ITable tableBefore = _connection.createDataSet().getTable(tableName); 84 assertEquals("before exclusive", 6, tableBefore.getRowCount()); 85 86 try 87 { 88 DatabaseOperation operation = new TransactionOperation( 90 DatabaseOperation.DELETE); 91 operation.execute(_connection, xmlDataSet); 92 fail("Should throw ExclusiveTransactionException"); 93 } 94 catch (ExclusiveTransactionException e) 95 { 96 } 97 finally 98 { 99 jdbcConnection.setAutoCommit(true); 100 } 101 102 ITable tableAfter = _connection.createDataSet().getTable(tableName); 104 assertEquals("after", 6, tableAfter.getRowCount()); 105 } 106 107 public void testExecuteRollback() throws Exception 108 { 109 String tableName = "TEST_TABLE"; 110 Reader in = new FileReader ( 111 new File ("src/xml/transactionOperationTest.xml")); 112 IDataSet xmlDataSet = new XmlDataSet(in); 113 Exception [] exceptions = new Exception []{ 114 new SQLException (), 115 new DatabaseUnitException(), 116 new RuntimeException (), 117 }; 118 Connection jdbcConnection = _connection.getConnection(); 119 120 121 for (int i = 0; i < exceptions.length; i++) 122 { 123 124 ITable tableBefore = _connection.createDataSet().getTable(tableName); 126 assertEquals("before row count", 6, tableBefore.getRowCount()); 127 assertEquals("autocommit before", true, jdbcConnection.getAutoCommit()); 128 129 MockDatabaseOperation mockOperation = new MockDatabaseOperation(); 130 mockOperation.setExpectedExecuteCalls(1); 131 mockOperation.setupThrowExceptionOnExecute(exceptions[i]); 132 133 try 134 { 135 DatabaseOperation operation = new CompositeOperation( 136 DatabaseOperation.DELETE_ALL, 137 mockOperation); 138 operation = new TransactionOperation(operation); 139 operation.execute(_connection, xmlDataSet); 140 fail("Should throw an exception"); 141 } 142 catch (Exception e) 143 { 144 mockOperation.verify(); 145 } 146 147 ITable tableAfter = _connection.createDataSet().getTable(tableName); 149 assertEquals("after row count", 6, tableAfter.getRowCount()); 150 assertEquals("autocommit after", true, jdbcConnection.getAutoCommit()); 151 152 } 153 } 154 } 155 156 157 158 159 | Popular Tags |