KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > XA40Test


1 /*
2  *
3  * Derby - Class XA40Test
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to you under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20
21 package org.apache.derbyTesting.functionTests.tests.jdbc4;
22
23 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
24 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
25
26 import junit.framework.*;
27
28 import java.sql.*;
29
30 import javax.sql.XAConnection JavaDoc;
31 import javax.sql.XADataSource JavaDoc;
32 import javax.transaction.xa.XAResource JavaDoc;
33
34 import org.apache.derby.iapi.jdbc.BrokeredStatement40;
35 import org.apache.derby.iapi.jdbc.BrokeredPreparedStatement40;
36 import org.apache.derby.iapi.jdbc.BrokeredCallableStatement40;
37
38
39 /**
40  * Test new methods added for XA in JDBC4.
41  */

42 public class XA40Test extends BaseJDBCTestCase {
43
44     /** Default XADataSource used by the tests. */
45     private XADataSource JavaDoc xads = null;
46
47     /** Default XAConnection used by the tests. */
48     private XAConnection JavaDoc xac = null;
49
50     /** Default XAResource used by the tests. */
51     private XAResource JavaDoc xar = null;
52
53     /** Default Connection used by the tests. */
54     private Connection JavaDoc con = null;
55     
56     /**
57      * Create a new test with the given name.
58      *
59      * @param name name of the test.
60      */

61     public XA40Test(String JavaDoc name) {
62         super(name);
63     }
64
65     /**
66      * Create default XADataSource, XAResource, XAConnection, and
67      * Connection for the tests.
68      *
69      * @throws SQLException if a database access exception occurs.
70      */

71     public void setUp()
72         throws SQLException {
73         xads = TestDataSourceFactory.getXADataSource();
74         xac = xads.getXAConnection();
75         xar = xac.getXAResource();
76         con = xac.getConnection();
77         assertFalse("Connection must be open initially", con.isClosed());
78         con.setAutoCommit(false);
79     }
80
81     /**
82      * Close default connection and XAConnection if necessary.
83      *
84      * @throws SQLException if a database access exception occurs.
85      */

86     public void tearDown()
87         throws SQLException {
88         // Close default connection
89
// Check if connection is open to avoid exception on rollback.
90
if (con != null && !con.isClosed()) {
91             // Abort changes that may have been done in the test.
92
// The test-method may however commit these itself.
93
con.rollback();
94             con.close();
95         }
96         if (xac != null) {
97             xac.close();
98         }
99     }
100
101     
102     /**
103      * Tests isPoolable(), setPoolable(boolean) and default
104      * poolability for Statement, (which for XA is actually a
105      * BrokeredStatement40 in embedded).
106      *
107      * @throws SQLException if a database access exception occurs.
108      */

109     public void testStatementPoolable() throws SQLException {
110         Statement s = con.createStatement();
111         if (usingEmbedded()) {
112             assertTrue("s must be an instance of BrokeredStatement40, " +
113                        "but is " + s.getClass(),
114                        (s instanceof BrokeredStatement40));
115         }
116         assertFalse("Statement must not be poolable by default",
117                     s.isPoolable());
118         s.setPoolable(true);
119         assertTrue("Statement must be poolable", s.isPoolable());
120
121         s.setPoolable(false);
122         assertFalse("Statement cannot be poolable", s.isPoolable());
123     }
124
125     /**
126      * Tests isPoolable() and setPoolable(boolean) for
127      * PreparedStatement, (which for XA is actually a
128      * BrokeredPreparedStatement40 in embedded).
129      *
130      * @throws SQLException if a database access exception occurs.
131      */

132     public void testPreparedStatementPoolable() throws SQLException {
133         PreparedStatement ps =
134             con.prepareStatement("CREATE TABLE foo(i int)");
135         if (usingEmbedded()) {
136             assertTrue("ps must be an instance of " +
137                        "BrokeredPreparedStatement40, " +
138                        "but is " + ps.getClass(),
139                        (ps instanceof BrokeredPreparedStatement40));
140         }
141         assertTrue("PreparedStatement must be poolable by default",
142                     ps.isPoolable());
143         ps.setPoolable(false);
144         assertFalse("PreparedStatement cannot be poolable", ps.isPoolable());
145
146         ps.setPoolable(true);
147         assertTrue("PreparedStatement must be poolable", ps.isPoolable());
148     }
149
150     /**
151      * Tests isPoolable() and setPoolable(boolean) and default
152      * poolability for CallableStatement (which for XA is actually a
153      * BrokeredCallableStatement40 in embedded).
154      *
155      * @throws SQLException if a database access exception occurs.
156      */

157     public void testCallableStatementPoolable() throws SQLException {
158         CallableStatement cs =
159             con.prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
160         if (usingEmbedded()) {
161             assertTrue("cs must be an instance of " +
162                        "BrokeredCallableStatement40, " +
163                        "but is " + cs.getClass(),
164                        (cs instanceof BrokeredCallableStatement40));
165         }
166         assertTrue("CallableStatement must be poolable by default",
167                     cs.isPoolable());
168         cs.setPoolable(false);
169         assertFalse("CallableStatement cannot be poolable", cs.isPoolable());
170
171         cs.setPoolable(true);
172         assertTrue("CallableStatement must be poolable", cs.isPoolable());
173     }
174
175
176     /**
177      * Create test suite for XA40Test.
178      */

179     public static Test suite() {
180         TestSuite suite = new TestSuite("XA40Test suite");
181         // Decorate test suite with a TestSetup class.
182
suite.addTest(new TestSuite(XA40Test.class));
183
184         return suite;
185     }
186     
187 } // End class XA40Test
188
Popular Tags