View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.operation;
23  
24  import java.io.File;
25  import java.io.FileReader;
26  import java.io.Reader;
27  import java.sql.Connection;
28  import java.sql.SQLException;
29  
30  import org.dbunit.AbstractDatabaseIT;
31  import org.dbunit.DatabaseUnitException;
32  import org.dbunit.TestFeature;
33  import org.dbunit.dataset.IDataSet;
34  import org.dbunit.dataset.ITable;
35  import org.dbunit.dataset.xml.XmlDataSet;
36  import org.dbunit.testutil.TestUtils;
37  
38  /**
39   * @author Manuel Laflamme
40   * @version $Revision$
41   * @since Feb 21, 2002
42   */
43  public class TransactionOperationIT extends AbstractDatabaseIT
44  {
45      public TransactionOperationIT(String s)
46      {
47          super(s);
48      }
49      
50      protected boolean runTest(String testName) {
51        return environmentHasFeature(TestFeature.TRANSACTION);
52      }
53  
54      public void testExecuteCommit() throws Exception
55      {
56          String tableName = "TEST_TABLE";
57          Reader in = new FileReader(
58                  TestUtils.getFile("xml/transactionOperationTest.xml"));
59          IDataSet xmlDataSet = new XmlDataSet(in);
60          Connection jdbcConnection = _connection.getConnection();
61  
62          ITable tableBefore = _connection.createDataSet().getTable(tableName);
63          assertEquals("before row count", 6, tableBefore.getRowCount());
64          assertEquals("autocommit before", true, jdbcConnection.getAutoCommit());
65  
66          DatabaseOperation operation = new CompositeOperation(
67                  DatabaseOperation.DELETE_ALL, DatabaseOperation.INSERT);
68          operation = new TransactionOperation(operation);
69          operation.execute(_connection, xmlDataSet);
70  
71          // snapshot after operation
72          ITable tableAfter = _connection.createDataSet().getTable(tableName);
73          assertEquals("after row count", 1, tableAfter.getRowCount());
74          assertEquals("autocommit after", true, jdbcConnection.getAutoCommit());
75      }
76  
77      public void testExclusiveTransaction() throws Exception
78      {
79          String tableName = "TEST_TABLE";
80          Reader in = new FileReader(
81                  TestUtils.getFile("xml/transactionOperationTest.xml"));
82          IDataSet xmlDataSet = new XmlDataSet(in);
83          Connection jdbcConnection = _connection.getConnection();
84  
85          jdbcConnection.setAutoCommit(false);
86  
87          // before operation
88          assertEquals("autocommit before", false, jdbcConnection.getAutoCommit());
89          ITable tableBefore = _connection.createDataSet().getTable(tableName);
90          assertEquals("before exclusive", 6, tableBefore.getRowCount());
91  
92          try
93          {
94              // try with exclusive transaction
95              DatabaseOperation operation = new TransactionOperation(
96                      DatabaseOperation.DELETE);
97              operation.execute(_connection, xmlDataSet);
98              fail("Should throw ExclusiveTransactionException");
99          }
100         catch (ExclusiveTransactionException e)
101         {
102         }
103         finally
104         {
105             jdbcConnection.setAutoCommit(true);
106         }
107 
108         // after operation
109         ITable tableAfter = _connection.createDataSet().getTable(tableName);
110         assertEquals("after", 6, tableAfter.getRowCount());
111     }
112 
113     public void testExecuteRollback() throws Exception
114     {
115         String tableName = "TEST_TABLE";
116         Reader in = new FileReader(
117                 TestUtils.getFile("xml/transactionOperationTest.xml"));
118         IDataSet xmlDataSet = new XmlDataSet(in);
119         Exception[] exceptions = new Exception[]{
120             new SQLException(),
121             new DatabaseUnitException(),
122             new RuntimeException(),
123         };
124         Connection jdbcConnection = _connection.getConnection();
125 
126 
127         for (int i = 0; i < exceptions.length; i++)
128         {
129 
130             // snapshot before operation
131             ITable tableBefore = _connection.createDataSet().getTable(tableName);
132             assertEquals("before row count", 6, tableBefore.getRowCount());
133             assertEquals("autocommit before", true, jdbcConnection.getAutoCommit());
134 
135             MockDatabaseOperation mockOperation = new MockDatabaseOperation();
136             mockOperation.setExpectedExecuteCalls(1);
137             mockOperation.setupThrowExceptionOnExecute(exceptions[i]);
138 
139             try
140             {
141                 DatabaseOperation operation = new CompositeOperation(
142                         DatabaseOperation.DELETE_ALL,
143                         mockOperation);
144                 operation = new TransactionOperation(operation);
145                 operation.execute(_connection, xmlDataSet);
146                 fail("Should throw an exception");
147             }
148             catch (Exception e)
149             {
150                 mockOperation.verify();
151             }
152 
153             // snapshot after operation
154             ITable tableAfter = _connection.createDataSet().getTable(tableName);
155             assertEquals("after row count", 6, tableAfter.getRowCount());
156             assertEquals("autocommit after", true, jdbcConnection.getAutoCommit());
157 
158         }
159     }
160 }
161 
162 
163 
164