KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > AdminObjectWrapperTest


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;
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.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
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 /**
41  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
42  */

43 public class AdminObjectWrapperTest extends TestCase {
44
45     private Kernel kernel;
46     private AbstractName selfName;
47     private static final String JavaDoc TARGET_NAME = "testAOName";
48
49     public void testProxy() throws Exception JavaDoc {
50         Object JavaDoc 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 JavaDoc {
58         MockAdminObject proxy = (MockAdminObject) kernel.invoke(selfName, "$getResource");
59         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
60         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
61         oos.writeObject(proxy);
62         oos.flush();
63         byte[] bytes = baos.toByteArray();
64         oos.close();
65         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
66         Object JavaDoc 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 JavaDoc ois2 = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
73         MockAdminObject proxy3 = (MockAdminObject) ois2.readObject();
74         kernel.startGBean(selfName);
75         proxy3.getSomething();
76
77     }
78
79     protected void setUp() throws Exception JavaDoc {
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 JavaDoc key, String JavaDoc value, GBeanInfo info) {
95         AbstractName abstractName = buildAbstractName(key, value);
96         return new GBeanData(abstractName, info);
97     }
98
99     private AbstractName buildAbstractName(String JavaDoc key, String JavaDoc value) {
100         Map JavaDoc names = new HashMap JavaDoc();
101         names.put(key, value);
102         return new AbstractName(new Artifact("test", "foo", "1", "car"), names);
103     }
104
105     protected void tearDown() throws Exception JavaDoc {
106         kernel.stopGBean(selfName);
107         kernel.shutdown();
108         super.tearDown();
109     }
110 }
111
Popular Tags