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.stateful;
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.StatefulSessionContainerInfo;
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.StatefulBean;
029:
030: import javax.annotation.PostConstruct;
031: import javax.annotation.PreDestroy;
032: import javax.annotation.Resource;
033: import javax.ejb.Local;
034: import javax.ejb.PostActivate;
035: import javax.ejb.PrePassivate;
036: import javax.ejb.Remote;
037: import javax.ejb.Remove;
038: import javax.ejb.SessionContext;
039: import javax.naming.InitialContext;
040: import java.io.Serializable;
041: import java.util.Arrays;
042: import java.util.List;
043: import java.util.Stack;
044: import java.util.ArrayList;
045:
046: /**
047: * @version $Revision: 632525 $ $Date: 2008-02-29 17:16:47 -0800 $
048: */
049: public class StatefulContainerTest extends TestCase {
050:
051: public void testBusinessLocalInterface() throws Exception {
052:
053: // Do a create...
054:
055: InitialContext ctx = new InitialContext();
056: Object object = ctx.lookup("WidgetBeanLocal");
057:
058: assertTrue("instanceof widget", object instanceof Widget);
059:
060: Widget widget = (Widget) object;
061:
062: // Do a business method...
063: Stack<Lifecycle> lifecycle = widget.getLifecycle();
064: assertNotNull("lifecycle", lifecycle);
065:
066: // Do a remove...
067: widget.destroy();
068:
069: // Check the lifecycle of the bean
070: List expected = Arrays.asList(StatefulContainerTest.Lifecycle
071: .values());
072:
073: assertEquals(StatefulContainerTest.join("\n", expected), join(
074: "\n", WidgetBean.lifecycle));
075: }
076:
077: public void testBusinessRemoteInterface() throws Exception {
078: WidgetBean.lifecycle.clear();
079:
080: // Do a create...
081:
082: InitialContext ctx = new InitialContext();
083: Object object = ctx.lookup("WidgetBeanRemote");
084:
085: assertTrue("instanceof widget", object instanceof RemoteWidget);
086:
087: RemoteWidget widget = (RemoteWidget) object;
088:
089: // Do a business method...
090: Stack<Lifecycle> lifecycle = widget.getLifecycle();
091: assertNotNull("lifecycle", lifecycle);
092: assertNotSame("is copy", lifecycle, WidgetBean.lifecycle);
093:
094: // Do a remove...
095: widget.destroy();
096:
097: try {
098: widget.destroy();
099: fail("Calling a removed bean should not be possible");
100: } catch (Exception e) {
101: }
102:
103: // Check the lifecycle of the bean
104: List expected = Arrays.asList(StatefulContainerTest.Lifecycle
105: .values());
106:
107: assertEquals(StatefulContainerTest.join("\n", expected), join(
108: "\n", WidgetBean.lifecycle));
109: }
110:
111: protected void setUp() throws Exception {
112: super .setUp();
113:
114: System.setProperty(
115: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
116: InitContextFactory.class.getName());
117:
118: ConfigurationFactory config = new ConfigurationFactory();
119: Assembler assembler = new Assembler();
120:
121: assembler.createProxyFactory(config
122: .configureService(ProxyFactoryInfo.class));
123: assembler.createTransactionManager(config
124: .configureService(TransactionServiceInfo.class));
125: assembler.createSecurityService(config
126: .configureService(SecurityServiceInfo.class));
127:
128: // containers
129: StatefulSessionContainerInfo statefulContainerInfo = config
130: .configureService(StatefulSessionContainerInfo.class);
131: statefulContainerInfo.properties.setProperty("PoolSize", "0");
132: statefulContainerInfo.properties.setProperty("BulkPassivate",
133: "1");
134: assembler.createContainer(statefulContainerInfo);
135:
136: // Setup the descriptor information
137:
138: EjbJar ejbJar = new EjbJar();
139: ejbJar.addEnterpriseBean(new StatefulBean(WidgetBean.class));
140:
141: assembler
142: .createApplication(config.configureApplication(ejbJar));
143:
144: WidgetBean.lifecycle.clear();
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:
156: @Local
157: public static interface Widget {
158: Stack<Lifecycle> getLifecycle();
159:
160: void destroy();
161: }
162:
163: @Remote
164: public static interface RemoteWidget extends Widget {
165:
166: }
167:
168: public static enum Lifecycle {
169: CONSTRUCTOR, INJECTION, POST_CONSTRUCT, PRE_PASSIVATE1, POST_ACTIVATE1, BUSINESS_METHOD, PRE_PASSIVATE2, POST_ACTIVATE2, REMOVE, PRE_DESTROY,
170: }
171:
172: public static class WidgetBean implements Widget, RemoteWidget {
173: private static final long serialVersionUID = -8499745487520955081L;
174:
175: private int activates = 0;
176: private int passivates = 0;
177:
178: public static Stack<Lifecycle> lifecycle = new Stack<Lifecycle>();
179:
180: public WidgetBean() {
181: lifecycle.push(Lifecycle.CONSTRUCTOR);
182: }
183:
184: @Resource
185: public void setContext(SessionContext context) {
186: lifecycle.push(Lifecycle.INJECTION);
187: }
188:
189: public Stack<Lifecycle> getLifecycle() {
190: lifecycle.push(Lifecycle.BUSINESS_METHOD);
191: return lifecycle;
192: }
193:
194: @PostActivate
195: public void activate() {
196: lifecycle.push(Enum.valueOf(Lifecycle.class,
197: "POST_ACTIVATE" + (++activates)));
198: }
199:
200: @PrePassivate
201: public void passivate() {
202: lifecycle.push(Enum.valueOf(Lifecycle.class,
203: "PRE_PASSIVATE" + (++passivates)));
204: }
205:
206: @PostConstruct
207: public void init() {
208: lifecycle.push(Lifecycle.POST_CONSTRUCT);
209: }
210:
211: @PreDestroy
212: public void predestroy() {
213: lifecycle.push(Lifecycle.PRE_DESTROY);
214: }
215:
216: @Remove
217: public void destroy() {
218: lifecycle.push(Lifecycle.REMOVE);
219: }
220: }
221: }
|