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: import org.apache.openejb.assembler.classic.Assembler;
021: import org.apache.openejb.assembler.classic.ProxyFactoryInfo;
022: import org.apache.openejb.assembler.classic.SecurityServiceInfo;
023: import org.apache.openejb.assembler.classic.StatelessSessionContainerInfo;
024: import org.apache.openejb.assembler.classic.TransactionServiceInfo;
025: import org.apache.openejb.config.ConfigurationFactory;
026: import org.apache.openejb.core.ivm.naming.InitContextFactory;
027: import org.apache.openejb.jee.EjbJar;
028: import org.apache.openejb.jee.StatelessBean;
029:
030: import javax.ejb.SessionContext;
031: import javax.naming.InitialContext;
032: import java.util.Arrays;
033: import java.util.List;
034: import java.util.Stack;
035:
036: /**
037: * @version $Revision: 581127 $ $Date: 2007-10-01 19:13:16 -0700 $
038: */
039: public class StatelessContainerTest extends TestCase {
040:
041: public void testPojoStyleBean() throws Exception {
042: List expected = Arrays.asList(Lifecycle.values());
043: InitialContext ctx = new InitialContext();
044:
045: {
046: WidgetBean.lifecycle.clear();
047:
048: Object object = ctx.lookup("WidgetBeanLocal");
049:
050: assertTrue("instanceof widget", object instanceof Widget);
051:
052: Widget widget = (Widget) object;
053:
054: // Do a business method...
055: Stack<Lifecycle> lifecycle = widget.getLifecycle();
056: assertNotNull("lifecycle", lifecycle);
057: assertSame("lifecycle", lifecycle, WidgetBean.lifecycle);
058:
059: // Check the lifecycle of the bean
060: assertEquals(join("\n", expected), join("\n", lifecycle));
061: }
062: {
063:
064: WidgetBean.lifecycle.clear();
065:
066: Object object = ctx.lookup("WidgetBeanRemote");
067:
068: assertTrue("instanceof widget",
069: object instanceof RemoteWidget);
070:
071: RemoteWidget remoteWidget = (RemoteWidget) object;
072:
073: // Do a business method...
074: Stack<Lifecycle> lifecycle = remoteWidget.getLifecycle();
075: assertNotNull("lifecycle", lifecycle);
076: assertNotSame("lifecycle", lifecycle, WidgetBean.lifecycle);
077:
078: // Check the lifecycle of the bean
079: assertEquals(join("\n", expected), join("\n", lifecycle));
080: }
081: }
082:
083: protected void setUp() throws Exception {
084: super .setUp();
085:
086: System.setProperty(
087: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
088: InitContextFactory.class.getName());
089:
090: ConfigurationFactory config = new ConfigurationFactory();
091: Assembler assembler = new Assembler();
092:
093: assembler.createProxyFactory(config
094: .configureService(ProxyFactoryInfo.class));
095: assembler.createTransactionManager(config
096: .configureService(TransactionServiceInfo.class));
097: assembler.createSecurityService(config
098: .configureService(SecurityServiceInfo.class));
099:
100: // containers
101: StatelessSessionContainerInfo statelessContainerInfo = config
102: .configureService(StatelessSessionContainerInfo.class);
103: statelessContainerInfo.properties.setProperty("TimeOut", "10");
104: statelessContainerInfo.properties.setProperty("PoolSize", "0");
105: statelessContainerInfo.properties.setProperty("StrictPooling",
106: "false");
107: assembler.createContainer(statelessContainerInfo);
108:
109: // Setup the descriptor information
110:
111: StatelessBean bean = new StatelessBean(WidgetBean.class);
112: bean.addBusinessLocal(Widget.class.getName());
113: bean.addBusinessRemote(RemoteWidget.class.getName());
114: bean.addPostConstruct("init");
115: bean.addPreDestroy("destroy");
116:
117: EjbJar ejbJar = new EjbJar();
118: ejbJar.addEnterpriseBean(bean);
119:
120: assembler
121: .createApplication(config.configureApplication(ejbJar));
122:
123: }
124:
125: private static String join(String delimeter, List items) {
126: StringBuffer sb = new StringBuffer();
127: for (Object item : items) {
128: sb.append(item.toString()).append(delimeter);
129: }
130: return sb.toString();
131: }
132:
133: public static interface Widget {
134: Stack<Lifecycle> getLifecycle();
135: }
136:
137: public static interface RemoteWidget extends Widget {
138:
139: }
140:
141: public static enum Lifecycle {
142: CONSTRUCTOR, INJECTION, POST_CONSTRUCT, BUSINESS_METHOD, PRE_DESTROY
143: }
144:
145: public static class WidgetBean implements Widget, RemoteWidget {
146:
147: private static Stack<Lifecycle> lifecycle = new Stack<Lifecycle>();
148:
149: public WidgetBean() {
150: lifecycle.push(Lifecycle.CONSTRUCTOR);
151: }
152:
153: public void setSessionContext(SessionContext sessionContext) {
154: lifecycle.push(Lifecycle.INJECTION);
155: }
156:
157: public Stack<Lifecycle> getLifecycle() {
158: lifecycle.push(Lifecycle.BUSINESS_METHOD);
159: return lifecycle;
160: }
161:
162: public void init() {
163: lifecycle.push(Lifecycle.POST_CONSTRUCT);
164: }
165:
166: public void destroy() {
167: lifecycle.push(Lifecycle.PRE_DESTROY);
168: }
169: }
170: }
|