KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.resource.cci.Connection JavaDoc;
28 import javax.resource.cci.ConnectionFactory JavaDoc;
29
30 import junit.framework.TestCase;
31 import org.apache.geronimo.connector.mock.ConnectionFactoryExtension;
32 import org.apache.geronimo.connector.mock.MockConnection;
33 import org.apache.geronimo.connector.mock.MockConnectionFactory;
34 import org.apache.geronimo.connector.mock.MockManagedConnectionFactory;
35 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool;
36 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoTransactions;
37 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker;
38 import org.apache.geronimo.gbean.AbstractName;
39 import org.apache.geronimo.gbean.GBeanData;
40 import org.apache.geronimo.gbean.GBeanInfo;
41 import org.apache.geronimo.gbean.GBeanInfoBuilder;
42 import org.apache.geronimo.kernel.Kernel;
43 import org.apache.geronimo.kernel.KernelFactory;
44 import org.apache.geronimo.kernel.repository.Artifact;
45
46 /**
47  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
48  */

49 public class ManagedConnectionFactoryWrapperTest extends TestCase {
50
51     private Kernel kernel;
52     private AbstractName managedConnectionFactoryName;
53     private static final String JavaDoc KERNEL_NAME = "testKernel";
54     private static final String JavaDoc TARGET_NAME = "testCFName";
55
56     public void testProxy() throws Exception JavaDoc {
57         Object JavaDoc proxy = kernel.invoke(managedConnectionFactoryName, "$getResource");
58         assertNotNull(proxy);
59         assertTrue(proxy instanceof ConnectionFactory JavaDoc);
60         Connection JavaDoc connection = ((ConnectionFactory JavaDoc) proxy).getConnection();
61         assertNotNull(connection);
62         kernel.stopGBean(managedConnectionFactoryName);
63         try {
64             ((ConnectionFactory JavaDoc) proxy).getConnection();
65             fail();
66         } catch (IllegalStateException JavaDoc ise) {
67         }
68         kernel.startGBean(managedConnectionFactoryName);
69         ((ConnectionFactory JavaDoc) proxy).getConnection();
70         //check implemented interfaces
71
assertTrue(proxy instanceof Serializable JavaDoc);
72         assertTrue(proxy instanceof ConnectionFactoryExtension);
73         assertEquals("SomethingElse", ((ConnectionFactoryExtension)proxy).doSomethingElse());
74     }
75
76     public void testSerialization() throws Exception JavaDoc {
77         ConnectionFactory JavaDoc proxy = (ConnectionFactory JavaDoc) kernel.invoke(managedConnectionFactoryName, "$getResource");
78         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
79         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
80         oos.writeObject(proxy);
81         oos.flush();
82         byte[] bytes = baos.toByteArray();
83         oos.close();
84         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
85         Object JavaDoc proxy2 = ois.readObject();
86         assertNotNull(proxy2);
87         assertTrue(proxy instanceof ConnectionFactory JavaDoc);
88         Connection JavaDoc connection = proxy.getConnection();
89         assertNotNull(connection);
90         kernel.stopGBean(managedConnectionFactoryName);
91         ObjectInputStream JavaDoc ois2 = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
92         ConnectionFactory JavaDoc proxy3 = (ConnectionFactory JavaDoc) ois2.readObject();
93         try {
94             proxy3.getConnection();
95             fail();
96         } catch (IllegalStateException JavaDoc ise) {
97         }
98         kernel.startGBean(managedConnectionFactoryName);
99         proxy3.getConnection();
100
101     }
102
103     protected void setUp() throws Exception JavaDoc {
104         super.setUp();
105         kernel = KernelFactory.newInstance().createKernel(KERNEL_NAME);
106         kernel.boot();
107         ClassLoader JavaDoc cl = MockConnectionTrackingCoordinator.class.getClassLoader();
108
109         GBeanData ctc = buildGBeanData("name", "ConnectionTrackingCoordinator", MockConnectionTrackingCoordinator.getGBeanInfo());
110         AbstractName ctcName = ctc.getAbstractName();
111         kernel.loadGBean(ctc, cl);
112
113         GBeanData cmf = buildGBeanData("name", "ConnectionManagerContainer", GenericConnectionManagerGBean.getGBeanInfo());
114         AbstractName cmfName = cmf.getAbstractName();
115         cmf.setAttribute("transactionSupport", NoTransactions.INSTANCE);
116         cmf.setAttribute("pooling", new NoPool());
117         cmf.setReferencePattern("ConnectionTracker", ctcName);
118         kernel.loadGBean(cmf, cl);
119
120
121         GBeanData mcfw = buildGBeanData("name", TARGET_NAME, ManagedConnectionFactoryWrapperGBean.getGBeanInfo());
122         managedConnectionFactoryName = mcfw.getAbstractName();
123         mcfw.setAttribute("managedConnectionFactoryClass", MockManagedConnectionFactory.class.getName());
124         mcfw.setAttribute("connectionFactoryInterface", ConnectionFactory JavaDoc.class.getName());
125         mcfw.setAttribute("implementedInterfaces", new String JavaDoc[]{Serializable JavaDoc.class.getName(), ConnectionFactoryExtension.class.getName()});
126         mcfw.setAttribute("connectionFactoryImplClass", MockConnectionFactory.class.getName());
127         mcfw.setAttribute("connectionInterface", Connection JavaDoc.class.getName());
128         mcfw.setAttribute("connectionImplClass", MockConnection.class.getName());
129         //"ResourceAdapterWrapper",
130
mcfw.setReferencePattern("ConnectionManagerContainer", cmfName);
131         //"ManagedConnectionFactoryListener",
132
kernel.loadGBean(mcfw, cl);
133
134         kernel.startGBean(ctcName);
135         kernel.startGBean(cmfName);
136         kernel.startGBean(managedConnectionFactoryName);
137     }
138     private GBeanData buildGBeanData(String JavaDoc key, String JavaDoc value, GBeanInfo info) {
139         AbstractName abstractName = buildAbstractName(key, value);
140         return new GBeanData(abstractName, info);
141     }
142
143     private AbstractName buildAbstractName(String JavaDoc key, String JavaDoc value) {
144         Map JavaDoc names = new HashMap JavaDoc();
145         names.put(key, value);
146         return new AbstractName(new Artifact("test", "foo", "1", "car"), names);
147     }
148
149
150     protected void tearDown() throws Exception JavaDoc {
151         kernel.stopGBean(managedConnectionFactoryName);
152         kernel.shutdown();
153         super.tearDown();
154     }
155
156     public static class MockConnectionTrackingCoordinator implements ConnectionTracker {
157         public void handleObtained(ConnectionTrackingInterceptor connectionTrackingInterceptor,
158                                    ConnectionInfo connectionInfo) {
159         }
160
161         public void handleReleased(ConnectionTrackingInterceptor connectionTrackingInterceptor,
162                                    ConnectionInfo connectionInfo) {
163         }
164
165         public void setEnvironment(ConnectionInfo connectionInfo, String JavaDoc key) {
166         }
167
168         static final GBeanInfo GBEAN_INFO;
169
170         static {
171             GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(MockConnectionTrackingCoordinator.class);
172             infoFactory.addInterface(ConnectionTracker.class);
173             GBEAN_INFO = infoFactory.getBeanInfo();
174         }
175
176         public static GBeanInfo getGBeanInfo() {
177             return GBEAN_INFO;
178         }
179     }
180 }
181
Popular Tags