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;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import junit.framework.TestCase;
026: import org.apache.geronimo.connector.mock.MockAdminObject;
027: import org.apache.geronimo.connector.mock.MockAdminObjectImpl;
028: import org.apache.geronimo.gbean.AbstractName;
029: import org.apache.geronimo.gbean.GBeanData;
030: import org.apache.geronimo.gbean.GBeanInfo;
031: import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContext;
032: import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContextImpl;
033: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
034: import org.apache.geronimo.kernel.Kernel;
035: import org.apache.geronimo.kernel.KernelFactory;
036: import org.apache.geronimo.kernel.repository.Artifact;
037:
038: /**
039: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
040: */
041: public class AdminObjectWrapperTest extends TestCase {
042:
043: private Kernel kernel;
044: private AbstractName selfName;
045: private static final String TARGET_NAME = "testAOName";
046:
047: public void testProxy() throws Exception {
048: Object proxy = kernel.invoke(selfName, "$getResource");
049: assertNotNull(proxy);
050: assertTrue(proxy instanceof MockAdminObject);
051: MockAdminObject mockAdminObject = ((MockAdminObject) proxy)
052: .getSomething();
053: assertNotNull(mockAdminObject);
054: }
055:
056: public void testSerialization() throws Exception {
057: MockAdminObject proxy = (MockAdminObject) kernel.invoke(
058: selfName, "$getResource");
059: ByteArrayOutputStream baos = new ByteArrayOutputStream();
060: ObjectOutputStream oos = new ObjectOutputStream(baos);
061: oos.writeObject(proxy);
062: oos.flush();
063: byte[] bytes = baos.toByteArray();
064: oos.close();
065: ObjectInputStream ois = new ObjectInputStream(
066: new ByteArrayInputStream(bytes));
067: Object proxy2 = ois.readObject();
068: assertNotNull(proxy2);
069: assertTrue(proxy instanceof MockAdminObject);
070: MockAdminObject mockAdminObject = proxy.getSomething();
071: assertNotNull(mockAdminObject);
072: kernel.stopGBean(selfName);
073: ObjectInputStream ois2 = new ObjectInputStream(
074: new ByteArrayInputStream(bytes));
075: MockAdminObject proxy3 = (MockAdminObject) ois2.readObject();
076: kernel.startGBean(selfName);
077: proxy3.getSomething();
078:
079: }
080:
081: protected void setUp() throws Exception {
082: super .setUp();
083: J2eeContext j2eeContext = new J2eeContextImpl("test.domain",
084: "geronimo.server", "testapp",
085: NameFactory.RESOURCE_ADAPTER_MODULE, "testmodule",
086: TARGET_NAME, NameFactory.JMS_RESOURCE);
087: kernel = KernelFactory.newInstance().createKernel(
088: j2eeContext.getJ2eeDomainName());
089: kernel.boot();
090:
091: GBeanData aow = buildGBeanData("name", TARGET_NAME,
092: AdminObjectWrapperGBean.getGBeanInfo());
093: selfName = aow.getAbstractName();
094: aow.setAttribute("adminObjectInterface", MockAdminObject.class
095: .getName());
096: aow.setAttribute("adminObjectClass", MockAdminObjectImpl.class
097: .getName());
098: kernel.loadGBean(aow, this .getClass().getClassLoader());
099:
100: kernel.startGBean(selfName);
101: }
102:
103: private GBeanData buildGBeanData(String key, String value,
104: GBeanInfo info) {
105: AbstractName abstractName = buildAbstractName(key, value);
106: return new GBeanData(abstractName, info);
107: }
108:
109: private AbstractName buildAbstractName(String key, String value) {
110: Map names = new HashMap();
111: names.put(key, value);
112: return new AbstractName(
113: new Artifact("test", "foo", "1", "car"), names);
114: }
115:
116: protected void tearDown() throws Exception {
117: kernel.stopGBean(selfName);
118: kernel.shutdown();
119: super.tearDown();
120: }
121: }
|