001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.stateless;
018:
019: import junit.framework.TestCase;
020:
021: import javax.ejb.EJBMetaData;
022: import javax.ejb.Handle;
023: import javax.ejb.HomeHandle;
024: import java.util.Arrays;
025: import java.util.List;
026: import java.util.Stack;
027:
028: /**
029: * @version $Revision: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
030: */
031: public class CrossClassLoaderProxyTestObject extends TestCase {
032: public static final Class widgetClass = CrossClassLoaderProxyTest.Widget.class;
033: public static final Class widgetHomeClass = CrossClassLoaderProxyTest.WidgetHome.class;
034: public static final Class widgetRemoteClass = CrossClassLoaderProxyTest.WidgetRemote.class;
035: public static CrossClassLoaderProxyTest.WidgetHome widgetHome;
036:
037: public void testRemoteInterface() throws Exception {
038: assertNotNull("widgetHome", widgetHome);
039: // assertTrue("home should be an instance of WidgetHome", home instanceof CrossClassLoaderProxyTest.WidgetHome);
040: // CrossClassLoaderProxyTest.WidgetHome widgetHome = (CrossClassLoaderProxyTest.WidgetHome)home;
041:
042: Object object = widgetHome.create();
043: assertNotNull("widgetHome.create()", object);
044:
045: assertTrue(
046: "object should be an instance of WidgetRemote",
047: object instanceof CrossClassLoaderProxyTest.WidgetRemote);
048: CrossClassLoaderProxyTest.WidgetRemote widget = (CrossClassLoaderProxyTest.WidgetRemote) object;
049:
050: // Do a business method...
051: Stack<CrossClassLoaderProxyTest.Lifecycle> lifecycle = widget
052: .getLifecycle();
053: assertNotNull("lifecycle", lifecycle);
054: assertNotSame("is copy", lifecycle,
055: CrossClassLoaderProxyTest.WidgetBean.lifecycle);
056:
057: // Check the lifecycle of the bean
058: List expected = Arrays
059: .asList(CrossClassLoaderProxyTest.Lifecycle.values());
060:
061: assertEquals(join("\n", expected), join("\n", lifecycle));
062:
063: // verify home ejb meta data
064: EJBMetaData metaData = widgetHome.getEJBMetaData();
065: assertTrue(
066: "metaData.getEJBHome() should be an instance of WidgetHome",
067: metaData.getEJBHome() instanceof CrossClassLoaderProxyTest.WidgetHome);
068: assertEquals(CrossClassLoaderProxyTest.WidgetHome.class,
069: metaData.getHomeInterfaceClass());
070: assertEquals(CrossClassLoaderProxyTest.WidgetRemote.class,
071: metaData.getRemoteInterfaceClass());
072:
073: // verify home handle
074: HomeHandle homeHandle = widgetHome.getHomeHandle();
075: assertTrue(
076: "homeHandle.getEJBHome() should be an instance of WidgetHome",
077: homeHandle.getEJBHome() instanceof CrossClassLoaderProxyTest.WidgetHome);
078:
079: // verify ejb object getHome
080: assertTrue(
081: "widget.getEJBHome() should be an instance of WidgetHome",
082: widget.getEJBHome() instanceof CrossClassLoaderProxyTest.WidgetHome);
083:
084: // verify ejb object handle
085: Handle objectHandle = widget.getHandle();
086: assertTrue(
087: "objectHandle.getEJBObject() should be an instance of WidgetHome",
088: objectHandle.getEJBObject() instanceof CrossClassLoaderProxyTest.WidgetRemote);
089: }
090:
091: // public void testCrossClassLoaderRemoteInterface() throws Exception {
092: // CrossClassLoaderProxyTest.HackClassLoader loader = new CrossClassLoaderProxyTest.HackClassLoader(getClass().getClassLoader());
093: // ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
094: // Thread.currentThread().setContextClassLoader(loader);
095: // try {
096: // Class widgetClass = loader.loadClass(CrossClassLoaderProxyTest.Widget.class.getName());
097: // assertEquals(CrossClassLoaderProxyTest.Widget.class, widgetClass);
098: //
099: // Class widgetHomeClass = loader.loadClass(CrossClassLoaderProxyTest.WidgetHome.class.getName());
100: // assertFalse(CrossClassLoaderProxyTest.WidgetHome.class.equals(widgetHomeClass));
101: //
102: // Class widgetRemoteClass = loader.loadClass(CrossClassLoaderProxyTest.WidgetRemote.class.getName());
103: // assertFalse(CrossClassLoaderProxyTest.WidgetRemote.class.equals(widgetRemoteClass));
104: //
105: // CoreDeploymentInfo coreDeploymentInfo = (CoreDeploymentInfo) deploymentInfo;
106: // EJBHome home = (EJBHome) copy(coreDeploymentInfo.getEJBHome());
107: // assertNotNull("home", home);
108: // assertEquals(widgetHomeClass.getClassLoader(), home.getClass().getClassLoader());
109: // assertTrue(widgetHomeClass.isAssignableFrom(home.getClass()));
110: //
111: // Object object = widgetHomeClass.getMethod("create").invoke(home);
112: // assertNotNull("widgetHome.create()", home);
113: //
114: // assertTrue("object should be an instance of WidgetRemote", object instanceof CrossClassLoaderProxyTest.WidgetRemote);
115: // CrossClassLoaderProxyTest.WidgetRemote widget = (CrossClassLoaderProxyTest.WidgetRemote)object;
116: //
117: // // Do a business method...
118: // Stack<CrossClassLoaderProxyTest.Lifecycle> lifecycle = widget.getLifecycle();
119: // assertNotNull("lifecycle",lifecycle);
120: // assertNotSame("is copy", lifecycle, CrossClassLoaderProxyTest.WidgetBean.lifecycle);
121: //
122: // // Check the lifecycle of the bean
123: // List expected = Arrays.asList(CrossClassLoaderProxyTest.Lifecycle.values());
124: //
125: // assertEquals(join("\n", expected) , join("\n", lifecycle));
126: //
127: // // verify home ejb meta data
128: // EJBMetaData metaData = home.getEJBMetaData();
129: // assertTrue("metaData.getEJBHome() should be an instance of WidgetHome", metaData.getEJBHome() instanceof CrossClassLoaderProxyTest.WidgetHome);
130: // assertEquals(CrossClassLoaderProxyTest.WidgetHome.class, metaData.getHomeInterfaceClass());
131: // assertEquals(CrossClassLoaderProxyTest.WidgetRemote.class, metaData.getRemoteInterfaceClass());
132: //
133: // // verify home handle
134: // HomeHandle homeHandle = home.getHomeHandle();
135: // assertTrue("homeHandle.getEJBHome() should be an instance of WidgetHome", homeHandle.getEJBHome() instanceof CrossClassLoaderProxyTest.WidgetHome);
136: //
137: // // verify ejb object getHome
138: // assertTrue("widget.getEJBHome() should be an instance of WidgetHome", widget.getEJBHome() instanceof CrossClassLoaderProxyTest.WidgetHome);
139: //
140: // // verify ejb object handle
141: // Handle objectHandle = widget.getHandle();
142: // assertTrue("objectHandle.getEJBObject() should be an instance of WidgetHome", objectHandle.getEJBObject() instanceof CrossClassLoaderProxyTest.WidgetRemote);
143: // } finally {
144: // Thread.currentThread().setContextClassLoader(oldClassLoader);
145: // }
146: // }
147:
148: private static String join(String delimeter, List items) {
149: StringBuffer sb = new StringBuffer();
150: for (Object item : items) {
151: sb.append(item.toString()).append(delimeter);
152: }
153: return sb.toString();
154: }
155: }
|