KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > ConnectionManagerTest


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

17
18 package org.apache.geronimo.connector.outbound;
19
20 import org.apache.geronimo.connector.mock.MockConnection;
21 import org.apache.geronimo.connector.mock.MockXAResource;
22 import org.apache.geronimo.connector.outbound.connectiontracking.DefaultInterceptor;
23 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectorInstanceContext;
24 import org.apache.geronimo.transaction.GeronimoUserTransaction;
25
26 /**
27  *
28  *
29  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
30  *
31  * */

32 public class ConnectionManagerTest extends ConnectionManagerTestUtils {
33
34
35     public void testSingleTransactionCall() throws Throwable JavaDoc {
36         transactionManager.begin();
37         defaultComponentInterceptor.invoke(connectorInstanceContext);
38         MockXAResource mockXAResource = (MockXAResource) mockManagedConnection.getXAResource();
39         assertEquals("XAResource should know one xid", 1, mockXAResource.getKnownXids().size());
40         assertNull("Should not be committed", mockXAResource.getCommitted());
41         transactionManager.commit();
42         assertNotNull("Should be committed", mockXAResource.getCommitted());
43     }
44
45     public void testNoTransactionCall() throws Throwable JavaDoc {
46         defaultComponentInterceptor.invoke(connectorInstanceContext);
47         MockXAResource mockXAResource = (MockXAResource) mockManagedConnection.getXAResource();
48         assertEquals("XAResource should know 0 xid", 0, mockXAResource.getKnownXids().size());
49         assertNull("Should not be committed", mockXAResource.getCommitted());
50     }
51
52     public void testOneTransactionTwoCalls() throws Throwable JavaDoc {
53         transactionManager.begin();
54         defaultComponentInterceptor.invoke(connectorInstanceContext);
55         MockXAResource mockXAResource = (MockXAResource) mockManagedConnection.getXAResource();
56         assertEquals("XAResource should know one xid", 1, mockXAResource.getKnownXids().size());
57         assertNull("Should not be committed", mockXAResource.getCommitted());
58         defaultComponentInterceptor.invoke(connectorInstanceContext);
59         assertEquals("Expected same XAResource", mockXAResource, mockManagedConnection.getXAResource());
60         assertEquals("XAResource should know one xid", 1, mockXAResource.getKnownXids().size());
61         assertNull("Should not be committed", mockXAResource.getCommitted());
62         transactionManager.commit();
63         assertNotNull("Should be committed", mockXAResource.getCommitted());
64     }
65
66     public void testUserTransactionEnlistingExistingConnections() throws Throwable JavaDoc {
67         mockComponent = new DefaultInterceptor() {
68             public Object JavaDoc invoke(ConnectorInstanceContext newConnectorInstanceContext) throws Throwable JavaDoc {
69                 MockConnection mockConnection = (MockConnection) connectionFactory.getConnection();
70                 mockManagedConnection = mockConnection.getManagedConnection();
71                 userTransaction.begin();
72                 MockXAResource mockXAResource = (MockXAResource) mockManagedConnection.getXAResource();
73                 assertEquals("XAResource should know one xid", 1, mockXAResource.getKnownXids().size());
74                 assertNull("Should not be committed", mockXAResource.getCommitted());
75                 userTransaction.commit();
76                 assertNotNull("Should be committed", mockXAResource.getCommitted());
77                 mockConnection.close();
78                 return null;
79             }
80         };
81         userTransaction = new GeronimoUserTransaction(transactionManager);
82         defaultComponentInterceptor.invoke(connectorInstanceContext);
83         MockXAResource mockXAResource = (MockXAResource) mockManagedConnection.getXAResource();
84         assertEquals("XAResource should know 1 xid", 1, mockXAResource.getKnownXids().size());
85         assertNotNull("Should be committed", mockXAResource.getCommitted());
86         mockXAResource.clear();
87     }
88
89     public void testConnectionCloseReturnsCxAfterUserTransaction() throws Throwable JavaDoc {
90         for (int i = 0; i < maxSize + 1; i++) {
91             testUserTransactionEnlistingExistingConnections();
92         }
93     }
94
95     public void testUnshareableConnections() throws Throwable JavaDoc {
96         unshareableResources.add(name);
97         mockComponent = new DefaultInterceptor() {
98             public Object JavaDoc invoke(ConnectorInstanceContext newConnectorInstanceContext) throws Throwable JavaDoc {
99                 MockConnection mockConnection1 = (MockConnection) connectionFactory.getConnection();
100                 mockManagedConnection = mockConnection1.getManagedConnection();
101                 MockConnection mockConnection2 = (MockConnection) connectionFactory.getConnection();
102                 //the 2 cx are for the same RM, so tm will call commit only one one (the first)
103
//mockManagedConnection = mockConnection2.getManagedConnection();
104
assertNotNull("Expected non-null managedconnection 1", mockConnection1.getManagedConnection());
105                 assertNotNull("Expected non-null managedconnection 2", mockConnection2.getManagedConnection());
106                 assertTrue("Expected different managed connections for each unshared handle", mockConnection1.getManagedConnection() != mockConnection2.getManagedConnection());
107
108                 mockConnection1.close();
109                 mockConnection2.close();
110                 return null;
111             }
112
113         };
114         transactionManager.begin();
115         defaultComponentInterceptor.invoke(connectorInstanceContext);
116         MockXAResource mockXAResource = (MockXAResource) mockManagedConnection.getXAResource();
117         assertEquals("XAResource should know one xid", 1, mockXAResource.getKnownXids().size());
118         assertNull("Should not be committed", mockXAResource.getCommitted());
119         transactionManager.commit();
120         assertNotNull("Should be committed", mockXAResource.getCommitted());
121     }
122 }
123
Popular Tags