001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.entity.cmr;
017:
018: import javax.transaction.SystemException;
019: import javax.transaction.HeuristicMixedException;
020: import javax.transaction.HeuristicRollbackException;
021: import javax.transaction.RollbackException;
022: import javax.transaction.Status;
023: import javax.transaction.TransactionManager;
024: import javax.sql.DataSource;
025: import javax.naming.InitialContext;
026: import javax.naming.Context;
027: import javax.naming.NamingException;
028: import java.util.Properties;
029: import java.sql.SQLException;
030: import java.sql.Connection;
031: import java.sql.Statement;
032: import java.sql.ResultSet;
033: import java.sql.ResultSetMetaData;
034:
035: import org.apache.openejb.test.TestManager;
036:
037: /**
038: * @version $Revision: 609119 $ $Date: 2008-01-05 02:14:31 -0800 $
039: */
040: public abstract class AbstractCMRTest extends
041: org.apache.openejb.test.NamedTestCase {
042: private TransactionManager transactionManager;
043: protected DataSource ds;
044: protected InitialContext initialContext;
045:
046: public AbstractCMRTest(String name) {
047: super ("Entity.CMR." + name);
048: }
049:
050: protected void beginTransaction() throws Exception {
051: transactionManager.begin();
052: }
053:
054: protected void completeTransaction() throws SystemException,
055: HeuristicMixedException, HeuristicRollbackException,
056: RollbackException {
057: int status = transactionManager.getStatus();
058: if (status == Status.STATUS_ACTIVE) {
059: transactionManager.commit();
060: } else if (status != Status.STATUS_NO_TRANSACTION) {
061: transactionManager.rollback();
062: }
063: }
064:
065: /**
066: * Sets up the fixture, for example, open a network connection.
067: * This method is called before a test is executed.
068: */
069: protected void setUp() throws Exception {
070: super .setUp();
071:
072: Properties properties = TestManager.getServer()
073: .getContextEnvironment();
074: //properties.put(Context.SECURITY_PRINCIPAL, "ENTITY_TEST_CLIENT");
075: //properties.put(Context.SECURITY_CREDENTIALS, "ENTITY_TEST_CLIENT");
076:
077: initialContext = new InitialContext(properties);
078:
079: InitialContext jndiContext = new InitialContext();
080: transactionManager = (TransactionManager) jndiContext
081: .lookup("java:openejb/TransactionManager");
082: try {
083: ds = (DataSource) jndiContext
084: .lookup("java:openejb/Resource/My DataSource");
085: } catch (NamingException e) {
086: ds = (DataSource) jndiContext
087: .lookup("java:openejb/Resource/Default JDBC Database");
088: }
089: }
090:
091: protected static void dumpTable(DataSource ds, String table)
092: throws SQLException {
093: Connection connection = null;
094: Statement statement = null;
095: ResultSet resultSet = null;
096: try {
097: connection = ds.getConnection();
098: statement = connection.createStatement();
099: resultSet = statement
100: .executeQuery("SELECT * FROM " + table);
101: ResultSetMetaData setMetaData = resultSet.getMetaData();
102: int columnCount = setMetaData.getColumnCount();
103: while (resultSet.next()) {
104: StringBuilder row = new StringBuilder();
105: for (int i = 1; i <= columnCount; i++) {
106: if (i > 1) {
107: row.append(", ");
108: }
109: String name = setMetaData.getColumnName(i);
110: Object value = resultSet.getObject(i);
111: row.append(name).append("=").append(value);
112: }
113: System.out.println(row);
114: }
115: } finally {
116: close(resultSet);
117: close(statement);
118: close(connection);
119: }
120: }
121:
122: protected static void close(ResultSet resultSet) {
123: if (resultSet == null)
124: return;
125: try {
126: resultSet.close();
127: } catch (SQLException e) {
128: }
129: }
130:
131: protected static void close(Statement statement) {
132: if (statement == null)
133: return;
134: try {
135: statement.close();
136: } catch (SQLException e) {
137: }
138: }
139:
140: protected static void close(Connection connection) {
141: if (connection == null)
142: return;
143: try {
144: connection.close();
145: } catch (SQLException e) {
146: }
147: }
148: }
|