1 17 18 package org.apache.geronimo.connector; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import junit.framework.TestCase; 28 import org.apache.geronimo.connector.mock.MockAdminObject; 29 import org.apache.geronimo.connector.mock.MockAdminObjectImpl; 30 import org.apache.geronimo.gbean.AbstractName; 31 import org.apache.geronimo.gbean.GBeanData; 32 import org.apache.geronimo.gbean.GBeanInfo; 33 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContext; 34 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContextImpl; 35 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 36 import org.apache.geronimo.kernel.Kernel; 37 import org.apache.geronimo.kernel.KernelFactory; 38 import org.apache.geronimo.kernel.repository.Artifact; 39 40 43 public class AdminObjectWrapperTest extends TestCase { 44 45 private Kernel kernel; 46 private AbstractName selfName; 47 private static final String TARGET_NAME = "testAOName"; 48 49 public void testProxy() throws Exception { 50 Object proxy = kernel.invoke(selfName, "$getResource"); 51 assertNotNull(proxy); 52 assertTrue(proxy instanceof MockAdminObject); 53 MockAdminObject mockAdminObject = ((MockAdminObject) proxy).getSomething(); 54 assertNotNull(mockAdminObject); 55 } 56 57 public void testSerialization() throws Exception { 58 MockAdminObject proxy = (MockAdminObject) kernel.invoke(selfName, "$getResource"); 59 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 60 ObjectOutputStream oos = new ObjectOutputStream (baos); 61 oos.writeObject(proxy); 62 oos.flush(); 63 byte[] bytes = baos.toByteArray(); 64 oos.close(); 65 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (bytes)); 66 Object proxy2 = ois.readObject(); 67 assertNotNull(proxy2); 68 assertTrue(proxy instanceof MockAdminObject); 69 MockAdminObject mockAdminObject = proxy.getSomething(); 70 assertNotNull(mockAdminObject); 71 kernel.stopGBean(selfName); 72 ObjectInputStream ois2 = new ObjectInputStream (new ByteArrayInputStream (bytes)); 73 MockAdminObject proxy3 = (MockAdminObject) ois2.readObject(); 74 kernel.startGBean(selfName); 75 proxy3.getSomething(); 76 77 } 78 79 protected void setUp() throws Exception { 80 super.setUp(); 81 J2eeContext j2eeContext = new J2eeContextImpl("test.domain", "geronimo.server", "testapp", NameFactory.RESOURCE_ADAPTER_MODULE, "testmodule", TARGET_NAME, NameFactory.JMS_RESOURCE); 82 kernel = KernelFactory.newInstance().createKernel(j2eeContext.getJ2eeDomainName()); 83 kernel.boot(); 84 85 GBeanData aow = buildGBeanData("name", TARGET_NAME, AdminObjectWrapperGBean.getGBeanInfo()); 86 selfName = aow.getAbstractName(); 87 aow.setAttribute("adminObjectInterface", MockAdminObject.class.getName()); 88 aow.setAttribute("adminObjectClass", MockAdminObjectImpl.class.getName()); 89 kernel.loadGBean(aow, this.getClass().getClassLoader()); 90 91 kernel.startGBean(selfName); 92 } 93 94 private GBeanData buildGBeanData(String key, String value, GBeanInfo info) { 95 AbstractName abstractName = buildAbstractName(key, value); 96 return new GBeanData(abstractName, info); 97 } 98 99 private AbstractName buildAbstractName(String key, String value) { 100 Map names = new HashMap (); 101 names.put(key, value); 102 return new AbstractName(new Artifact("test", "foo", "1", "car"), names); 103 } 104 105 protected void tearDown() throws Exception { 106 kernel.stopGBean(selfName); 107 kernel.shutdown(); 108 super.tearDown(); 109 } 110 } 111 | Popular Tags |