KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > operation > TransactionOperationTest


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 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 JavaDoc;
31 import java.io.FileReader JavaDoc;
32 import java.io.Reader JavaDoc;
33 import java.sql.Connection JavaDoc;
34 import java.sql.SQLException JavaDoc;
35
36 /**
37  * @author Manuel Laflamme
38  * @version $Revision: 1.8 $
39  * @since Feb 21, 2002
40  */

41 public class TransactionOperationTest extends AbstractDatabaseTest
42 {
43     public TransactionOperationTest(String JavaDoc s)
44     {
45         super(s);
46     }
47
48     public void testExecuteCommit() throws Exception JavaDoc
49     {
50         String JavaDoc tableName = "TEST_TABLE";
51         Reader JavaDoc in = new FileReader JavaDoc(
52                 new File JavaDoc("src/xml/transactionOperationTest.xml"));
53         IDataSet xmlDataSet = new XmlDataSet(in);
54         Connection JavaDoc 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         // snapshot after operation
66
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 JavaDoc
72     {
73         String JavaDoc tableName = "TEST_TABLE";
74         Reader JavaDoc in = new FileReader JavaDoc(
75                 new File JavaDoc("src/xml/transactionOperationTest.xml"));
76         IDataSet xmlDataSet = new XmlDataSet(in);
77         Connection JavaDoc jdbcConnection = _connection.getConnection();
78
79         jdbcConnection.setAutoCommit(false);
80
81         // before operation
82
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             // try with exclusive transaction
89
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         // after operation
103
ITable tableAfter = _connection.createDataSet().getTable(tableName);
104         assertEquals("after", 6, tableAfter.getRowCount());
105     }
106
107     public void testExecuteRollback() throws Exception JavaDoc
108     {
109         String JavaDoc tableName = "TEST_TABLE";
110         Reader JavaDoc in = new FileReader JavaDoc(
111                 new File JavaDoc("src/xml/transactionOperationTest.xml"));
112         IDataSet xmlDataSet = new XmlDataSet(in);
113         Exception JavaDoc[] exceptions = new Exception JavaDoc[]{
114             new SQLException JavaDoc(),
115             new DatabaseUnitException(),
116             new RuntimeException JavaDoc(),
117         };
118         Connection JavaDoc jdbcConnection = _connection.getConnection();
119
120
121         for (int i = 0; i < exceptions.length; i++)
122         {
123
124             // snapshot before operation
125
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 JavaDoc e)
143             {
144                 mockOperation.verify();
145             }
146
147             // snapshot after operation
148
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