001: /*
002: *
003: * Derby - Class XA40Test
004: *
005: * Licensed to the Apache Software Foundation (ASF) under one or more
006: * contributor license agreements. See the NOTICE file distributed with
007: * this work for additional information regarding copyright ownership.
008: * The ASF licenses this file to you under the Apache License, Version 2.0
009: * (the "License"); you may not use this file except in compliance with
010: * the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */
020:
021: package org.apache.derbyTesting.functionTests.tests.jdbc4;
022:
023: import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
024: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
025:
026: import junit.framework.*;
027:
028: import java.sql.*;
029:
030: import javax.sql.XAConnection;
031: import javax.sql.XADataSource;
032: import javax.transaction.xa.XAResource;
033:
034: import org.apache.derby.iapi.jdbc.BrokeredStatement40;
035: import org.apache.derby.iapi.jdbc.BrokeredPreparedStatement40;
036: import org.apache.derby.iapi.jdbc.BrokeredCallableStatement40;
037:
038: /**
039: * Test new methods added for XA in JDBC4.
040: */
041: public class XA40Test extends BaseJDBCTestCase {
042:
043: /** Default XADataSource used by the tests. */
044: private XADataSource xads = null;
045:
046: /** Default XAConnection used by the tests. */
047: private XAConnection xac = null;
048:
049: /** Default XAResource used by the tests. */
050: private XAResource xar = null;
051:
052: /** Default Connection used by the tests. */
053: private Connection con = null;
054:
055: /**
056: * Create a new test with the given name.
057: *
058: * @param name name of the test.
059: */
060: public XA40Test(String name) {
061: super (name);
062: }
063:
064: /**
065: * Create default XADataSource, XAResource, XAConnection, and
066: * Connection for the tests.
067: *
068: * @throws SQLException if a database access exception occurs.
069: */
070: public void setUp() throws SQLException {
071: xads = TestDataSourceFactory.getXADataSource();
072: xac = xads.getXAConnection();
073: xar = xac.getXAResource();
074: con = xac.getConnection();
075: assertFalse("Connection must be open initially", con.isClosed());
076: con.setAutoCommit(false);
077: }
078:
079: /**
080: * Close default connection and XAConnection if necessary.
081: *
082: * @throws SQLException if a database access exception occurs.
083: */
084: public void tearDown() throws SQLException {
085: // Close default connection
086: // Check if connection is open to avoid exception on rollback.
087: if (con != null && !con.isClosed()) {
088: // Abort changes that may have been done in the test.
089: // The test-method may however commit these itself.
090: con.rollback();
091: con.close();
092: }
093: if (xac != null) {
094: xac.close();
095: }
096: }
097:
098: /**
099: * Tests isPoolable(), setPoolable(boolean) and default
100: * poolability for Statement, (which for XA is actually a
101: * BrokeredStatement40 in embedded).
102: *
103: * @throws SQLException if a database access exception occurs.
104: */
105: public void testStatementPoolable() throws SQLException {
106: Statement s = con.createStatement();
107: if (usingEmbedded()) {
108: assertTrue("s must be an instance of BrokeredStatement40, "
109: + "but is " + s.getClass(),
110: (s instanceof BrokeredStatement40));
111: }
112: assertFalse("Statement must not be poolable by default", s
113: .isPoolable());
114: s.setPoolable(true);
115: assertTrue("Statement must be poolable", s.isPoolable());
116:
117: s.setPoolable(false);
118: assertFalse("Statement cannot be poolable", s.isPoolable());
119: }
120:
121: /**
122: * Tests isPoolable() and setPoolable(boolean) for
123: * PreparedStatement, (which for XA is actually a
124: * BrokeredPreparedStatement40 in embedded).
125: *
126: * @throws SQLException if a database access exception occurs.
127: */
128: public void testPreparedStatementPoolable() throws SQLException {
129: PreparedStatement ps = con
130: .prepareStatement("CREATE TABLE foo(i int)");
131: if (usingEmbedded()) {
132: assertTrue("ps must be an instance of "
133: + "BrokeredPreparedStatement40, " + "but is "
134: + ps.getClass(),
135: (ps instanceof BrokeredPreparedStatement40));
136: }
137: assertTrue("PreparedStatement must be poolable by default", ps
138: .isPoolable());
139: ps.setPoolable(false);
140: assertFalse("PreparedStatement cannot be poolable", ps
141: .isPoolable());
142:
143: ps.setPoolable(true);
144: assertTrue("PreparedStatement must be poolable", ps
145: .isPoolable());
146: }
147:
148: /**
149: * Tests isPoolable() and setPoolable(boolean) and default
150: * poolability for CallableStatement (which for XA is actually a
151: * BrokeredCallableStatement40 in embedded).
152: *
153: * @throws SQLException if a database access exception occurs.
154: */
155: public void testCallableStatementPoolable() throws SQLException {
156: CallableStatement cs = con
157: .prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
158: if (usingEmbedded()) {
159: assertTrue("cs must be an instance of "
160: + "BrokeredCallableStatement40, " + "but is "
161: + cs.getClass(),
162: (cs instanceof BrokeredCallableStatement40));
163: }
164: assertTrue("CallableStatement must be poolable by default", cs
165: .isPoolable());
166: cs.setPoolable(false);
167: assertFalse("CallableStatement cannot be poolable", cs
168: .isPoolable());
169:
170: cs.setPoolable(true);
171: assertTrue("CallableStatement must be poolable", cs
172: .isPoolable());
173: }
174:
175: /**
176: * Create test suite for XA40Test.
177: */
178: public static Test suite() {
179: TestSuite suite = new TestSuite("XA40Test suite");
180: // Decorate test suite with a TestSetup class.
181: suite.addTest(new TestSuite(XA40Test.class));
182:
183: return suite;
184: }
185:
186: } // End class XA40Test
|