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.geronimo.connector.outbound;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.io.Serializable;
023: import java.util.HashMap;
024: import java.util.Map;
025: import javax.resource.cci.Connection;
026: import javax.resource.cci.ConnectionFactory;
027:
028: import junit.framework.TestCase;
029: import org.apache.geronimo.connector.mock.ConnectionFactoryExtension;
030: import org.apache.geronimo.connector.mock.MockConnection;
031: import org.apache.geronimo.connector.mock.MockConnectionFactory;
032: import org.apache.geronimo.connector.mock.MockManagedConnectionFactory;
033: import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool;
034: import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoTransactions;
035: import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker;
036: import org.apache.geronimo.gbean.AbstractName;
037: import org.apache.geronimo.gbean.GBeanData;
038: import org.apache.geronimo.gbean.GBeanInfo;
039: import org.apache.geronimo.gbean.GBeanInfoBuilder;
040: import org.apache.geronimo.kernel.Kernel;
041: import org.apache.geronimo.kernel.KernelFactory;
042: import org.apache.geronimo.kernel.repository.Artifact;
043:
044: /**
045: * @version $Rev: 539952 $ $Date: 2007-05-20 14:40:58 -0700 (Sun, 20 May 2007) $
046: */
047: public class ManagedConnectionFactoryWrapperTest extends TestCase {
048:
049: private Kernel kernel;
050: private AbstractName managedConnectionFactoryName;
051: private static final String KERNEL_NAME = "testKernel";
052: private static final String TARGET_NAME = "testCFName";
053:
054: public void testProxy() throws Exception {
055: Object proxy = kernel.invoke(managedConnectionFactoryName,
056: "$getResource");
057: assertNotNull(proxy);
058: assertTrue(proxy instanceof ConnectionFactory);
059: Connection connection = ((ConnectionFactory) proxy)
060: .getConnection();
061: assertNotNull(connection);
062: kernel.stopGBean(managedConnectionFactoryName);
063: try {
064: ((ConnectionFactory) proxy).getConnection();
065: // fail();
066: } catch (IllegalStateException ise) {
067: }
068: kernel.startGBean(managedConnectionFactoryName);
069: ((ConnectionFactory) proxy).getConnection();
070: //check implemented interfaces
071: assertTrue(proxy instanceof Serializable);
072: assertTrue(proxy instanceof ConnectionFactoryExtension);
073: assertEquals("SomethingElse",
074: ((ConnectionFactoryExtension) proxy).doSomethingElse());
075: }
076:
077: public void XtestSerialization() throws Exception {
078: ConnectionFactory proxy = (ConnectionFactory) kernel.invoke(
079: managedConnectionFactoryName, "$getResource");
080: ByteArrayOutputStream baos = new ByteArrayOutputStream();
081: ObjectOutputStream oos = new ObjectOutputStream(baos);
082: oos.writeObject(proxy);
083: oos.flush();
084: byte[] bytes = baos.toByteArray();
085: oos.close();
086: ObjectInputStream ois = new ObjectInputStream(
087: new ByteArrayInputStream(bytes));
088: Object proxy2 = ois.readObject();
089: assertNotNull(proxy2);
090: assertTrue(proxy instanceof ConnectionFactory);
091: Connection connection = proxy.getConnection();
092: assertNotNull(connection);
093: kernel.stopGBean(managedConnectionFactoryName);
094: ObjectInputStream ois2 = new ObjectInputStream(
095: new ByteArrayInputStream(bytes));
096: ConnectionFactory proxy3 = (ConnectionFactory) ois2
097: .readObject();
098: try {
099: proxy3.getConnection();
100: fail();
101: } catch (IllegalStateException ise) {
102: }
103: kernel.startGBean(managedConnectionFactoryName);
104: proxy3.getConnection();
105:
106: }
107:
108: protected void setUp() throws Exception {
109: super .setUp();
110: kernel = KernelFactory.newInstance().createKernel(KERNEL_NAME);
111: kernel.boot();
112: ClassLoader cl = MockConnectionTrackingCoordinator.class
113: .getClassLoader();
114:
115: GBeanData ctc = buildGBeanData("name",
116: "ConnectionTrackingCoordinator",
117: MockConnectionTrackingCoordinator.getGBeanInfo());
118: AbstractName ctcName = ctc.getAbstractName();
119: kernel.loadGBean(ctc, cl);
120:
121: GBeanData cmf = buildGBeanData("name",
122: "ConnectionManagerContainer",
123: GenericConnectionManagerGBean.getGBeanInfo());
124: AbstractName cmfName = cmf.getAbstractName();
125: cmf.setAttribute("transactionSupport", NoTransactions.INSTANCE);
126: cmf.setAttribute("pooling", new NoPool());
127: cmf.setReferencePattern("ConnectionTracker", ctcName);
128: kernel.loadGBean(cmf, cl);
129:
130: GBeanData mcfw = buildGBeanData("name", TARGET_NAME,
131: ManagedConnectionFactoryWrapperGBean.getGBeanInfo());
132: managedConnectionFactoryName = mcfw.getAbstractName();
133: mcfw.setAttribute("managedConnectionFactoryClass",
134: MockManagedConnectionFactory.class.getName());
135: mcfw.setAttribute("connectionFactoryInterface",
136: ConnectionFactory.class.getName());
137: mcfw.setAttribute("implementedInterfaces", new String[] {
138: Serializable.class.getName(),
139: ConnectionFactoryExtension.class.getName() });
140: mcfw.setAttribute("connectionFactoryImplClass",
141: MockConnectionFactory.class.getName());
142: mcfw.setAttribute("connectionInterface", Connection.class
143: .getName());
144: mcfw.setAttribute("connectionImplClass", MockConnection.class
145: .getName());
146: //"ResourceAdapterWrapper",
147: mcfw.setReferencePattern("ConnectionManagerContainer", cmfName);
148: //"ManagedConnectionFactoryListener",
149: kernel.loadGBean(mcfw, cl);
150:
151: kernel.startGBean(ctcName);
152: kernel.startGBean(cmfName);
153: kernel.startGBean(managedConnectionFactoryName);
154: }
155:
156: private GBeanData buildGBeanData(String key, String value,
157: GBeanInfo info) {
158: AbstractName abstractName = buildAbstractName(key, value);
159: return new GBeanData(abstractName, info);
160: }
161:
162: private AbstractName buildAbstractName(String key, String value) {
163: Map names = new HashMap();
164: names.put(key, value);
165: return new AbstractName(
166: new Artifact("test", "foo", "1", "car"), names);
167: }
168:
169: protected void tearDown() throws Exception {
170: kernel.stopGBean(managedConnectionFactoryName);
171: kernel.shutdown();
172: super .tearDown();
173: }
174:
175: public static class MockConnectionTrackingCoordinator implements
176: ConnectionTracker {
177: public void handleObtained(
178: ConnectionTrackingInterceptor connectionTrackingInterceptor,
179: ConnectionInfo connectionInfo, boolean reassociate) {
180: }
181:
182: public void handleReleased(
183: ConnectionTrackingInterceptor connectionTrackingInterceptor,
184: ConnectionInfo connectionInfo,
185: ConnectionReturnAction connectionReturnAction) {
186: }
187:
188: public void setEnvironment(ConnectionInfo connectionInfo,
189: String key) {
190: }
191:
192: static final GBeanInfo GBEAN_INFO;
193:
194: static {
195: GBeanInfoBuilder infoFactory = GBeanInfoBuilder
196: .createStatic(MockConnectionTrackingCoordinator.class);
197: infoFactory.addInterface(ConnectionTracker.class);
198: GBEAN_INFO = infoFactory.getBeanInfo();
199: }
200:
201: public static GBeanInfo getGBeanInfo() {
202: return GBEAN_INFO;
203: }
204: }
205: }
|