001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.assembler.classic;
018:
019: import junit.framework.TestCase;
020: import org.apache.openejb.config.ConfigurationFactory;
021: import org.apache.openejb.loader.SystemInstance;
022: import org.apache.openejb.spi.ContainerSystem;
023:
024: import javax.sql.DataSource;
025: import javax.transaction.TransactionManager;
026: import java.sql.Connection;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: /**
033: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
034: */
035: public class JdbcConfigTest extends TestCase {
036: public void test() throws Exception {
037: ConfigurationFactory config = new ConfigurationFactory();
038: Assembler assembler = new Assembler();
039:
040: // System services
041: assembler.createProxyFactory(config
042: .configureService(ProxyFactoryInfo.class));
043: assembler.createTransactionManager(config
044: .configureService(TransactionServiceInfo.class));
045: assembler.createSecurityService(config
046: .configureService(SecurityServiceInfo.class));
047:
048: // managed JDBC
049: assembler.createResource(config.configureService(
050: "Default JDBC Database", ResourceInfo.class));
051:
052: // unmanaged JDBC
053: assembler.createResource(config.configureService(
054: "Default Unmanaged JDBC Database", ResourceInfo.class));
055:
056: ContainerSystem containerSystem = SystemInstance.get()
057: .getComponent(ContainerSystem.class);
058:
059: DataSource managedDS = (DataSource) containerSystem
060: .getJNDIContext().lookup(
061: "java:openejb/Resource/Default JDBC Database");
062: assertNotNull("managedDS is null", managedDS);
063:
064: DataSource unmanagedDS = (DataSource) containerSystem
065: .getJNDIContext()
066: .lookup(
067: "java:openejb/Resource/Default Unmanaged JDBC Database");
068: assertNotNull("unmanagedDS is null", unmanagedDS);
069:
070: // test without a transaction
071: // NOTE: without a transaction all connections work as unmanaged
072: verifyUnmanagedConnections(managedDS);
073: verifyUnmanagedConnections(unmanagedDS);
074:
075: // test in the context of a transaction
076: TransactionManager transactionManager = SystemInstance.get()
077: .getComponent(TransactionManager.class);
078: transactionManager.begin();
079: try {
080: verifyManagedConnections(managedDS);
081: verifyUnmanagedConnections(unmanagedDS);
082: } finally {
083: // commit the transaction
084: transactionManager.commit();
085: }
086: }
087:
088: private void verifyManagedConnections(DataSource dataSource)
089: throws SQLException {
090: List<Connection> managedConnections = new ArrayList<Connection>();
091: try {
092: for (int i = 0; i < 4; i++) {
093: Connection connection = dataSource.getConnection();
094: managedConnections.add(connection);
095:
096: try {
097: connection.setAutoCommit(true);
098: fail("expected connection.setAutoCommit(true) to throw an exception");
099: } catch (SQLException expected) {
100: }
101:
102: Statement statement = connection.createStatement();
103: try {
104: statement.getQueryTimeout();
105: } finally {
106: statement.close();
107: }
108: }
109: } finally {
110: for (Connection connection : managedConnections) {
111: close(connection);
112: }
113: }
114: }
115:
116: private void verifyUnmanagedConnections(DataSource dataSource)
117: throws SQLException {
118: List<Connection> unmanagedConnections = new ArrayList<Connection>();
119: try {
120: for (int i = 0; i < 4; i++) {
121: Connection connection = dataSource.getConnection();
122: unmanagedConnections.add(connection);
123: assertTrue(
124: "Expected connection.getAutoCommit() to be true",
125: connection.getAutoCommit());
126: connection.setAutoCommit(true);
127: Statement statement = connection.createStatement();
128: try {
129: statement.getQueryTimeout();
130: } finally {
131: statement.close();
132: }
133: connection.commit();
134: connection.setAutoCommit(false);
135: }
136: } finally {
137: for (Connection connection : unmanagedConnections) {
138: close(connection);
139: }
140: }
141: }
142:
143: private static void close(Connection connection) {
144: if (connection == null)
145: return;
146:
147: try {
148: connection.close();
149: } catch (SQLException e) {
150: }
151: }
152: }
|