1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.io.Serializable ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import javax.resource.cci.Connection ; 28 import javax.resource.cci.ConnectionFactory ; 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 49 public class ManagedConnectionFactoryWrapperTest extends TestCase { 50 51 private Kernel kernel; 52 private AbstractName managedConnectionFactoryName; 53 private static final String KERNEL_NAME = "testKernel"; 54 private static final String TARGET_NAME = "testCFName"; 55 56 public void testProxy() throws Exception { 57 Object proxy = kernel.invoke(managedConnectionFactoryName, "$getResource"); 58 assertNotNull(proxy); 59 assertTrue(proxy instanceof ConnectionFactory ); 60 Connection connection = ((ConnectionFactory ) proxy).getConnection(); 61 assertNotNull(connection); 62 kernel.stopGBean(managedConnectionFactoryName); 63 try { 64 ((ConnectionFactory ) proxy).getConnection(); 65 fail(); 66 } catch (IllegalStateException ise) { 67 } 68 kernel.startGBean(managedConnectionFactoryName); 69 ((ConnectionFactory ) proxy).getConnection(); 70 assertTrue(proxy instanceof Serializable ); 72 assertTrue(proxy instanceof ConnectionFactoryExtension); 73 assertEquals("SomethingElse", ((ConnectionFactoryExtension)proxy).doSomethingElse()); 74 } 75 76 public void testSerialization() throws Exception { 77 ConnectionFactory proxy = (ConnectionFactory ) kernel.invoke(managedConnectionFactoryName, "$getResource"); 78 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 79 ObjectOutputStream oos = new ObjectOutputStream (baos); 80 oos.writeObject(proxy); 81 oos.flush(); 82 byte[] bytes = baos.toByteArray(); 83 oos.close(); 84 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (bytes)); 85 Object proxy2 = ois.readObject(); 86 assertNotNull(proxy2); 87 assertTrue(proxy instanceof ConnectionFactory ); 88 Connection connection = proxy.getConnection(); 89 assertNotNull(connection); 90 kernel.stopGBean(managedConnectionFactoryName); 91 ObjectInputStream ois2 = new ObjectInputStream (new ByteArrayInputStream (bytes)); 92 ConnectionFactory proxy3 = (ConnectionFactory ) ois2.readObject(); 93 try { 94 proxy3.getConnection(); 95 fail(); 96 } catch (IllegalStateException ise) { 97 } 98 kernel.startGBean(managedConnectionFactoryName); 99 proxy3.getConnection(); 100 101 } 102 103 protected void setUp() throws Exception { 104 super.setUp(); 105 kernel = KernelFactory.newInstance().createKernel(KERNEL_NAME); 106 kernel.boot(); 107 ClassLoader 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 .class.getName()); 125 mcfw.setAttribute("implementedInterfaces", new String []{Serializable .class.getName(), ConnectionFactoryExtension.class.getName()}); 126 mcfw.setAttribute("connectionFactoryImplClass", MockConnectionFactory.class.getName()); 127 mcfw.setAttribute("connectionInterface", Connection .class.getName()); 128 mcfw.setAttribute("connectionImplClass", MockConnection.class.getName()); 129 mcfw.setReferencePattern("ConnectionManagerContainer", cmfName); 131 kernel.loadGBean(mcfw, cl); 133 134 kernel.startGBean(ctcName); 135 kernel.startGBean(cmfName); 136 kernel.startGBean(managedConnectionFactoryName); 137 } 138 private GBeanData buildGBeanData(String key, String value, GBeanInfo info) { 139 AbstractName abstractName = buildAbstractName(key, value); 140 return new GBeanData(abstractName, info); 141 } 142 143 private AbstractName buildAbstractName(String key, String value) { 144 Map names = new HashMap (); 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 { 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 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 |