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.openejb.core.stateful;
017:
018: import junit.framework.TestCase;
019: import org.apache.openejb.core.ivm.naming.InitContextFactory;
020: import org.apache.openejb.config.ConfigurationFactory;
021: import org.apache.openejb.config.EjbModule;
022: import org.apache.openejb.assembler.classic.Assembler;
023: import org.apache.openejb.assembler.classic.ProxyFactoryInfo;
024: import org.apache.openejb.assembler.classic.TransactionServiceInfo;
025: import org.apache.openejb.assembler.classic.SecurityServiceInfo;
026: import org.apache.openejb.assembler.classic.StatefulSessionContainerInfo;
027: import org.apache.openejb.jee.EjbJar;
028: import org.apache.openejb.jee.StatefulBean;
029:
030: import javax.naming.InitialContext;
031: import javax.ejb.EJBException;
032: import javax.ejb.SessionBean;
033: import javax.ejb.SessionContext;
034: import javax.ejb.CreateException;
035: import javax.ejb.EJBObject;
036: import javax.ejb.EJBHome;
037: import java.util.ArrayList;
038: import java.util.List;
039: import java.util.Arrays;
040: import java.rmi.RemoteException;
041:
042: /**
043: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
044: */
045: public class StatefulSessionBeanTest extends TestCase {
046:
047: public void test() throws Exception {
048: System.setProperty(
049: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
050: InitContextFactory.class.getName());
051:
052: ConfigurationFactory config = new ConfigurationFactory();
053: Assembler assembler = new Assembler();
054:
055: assembler.createProxyFactory(config
056: .configureService(ProxyFactoryInfo.class));
057: assembler.createTransactionManager(config
058: .configureService(TransactionServiceInfo.class));
059: assembler.createSecurityService(config
060: .configureService(SecurityServiceInfo.class));
061:
062: // containers
063: StatefulSessionContainerInfo statefulContainerInfo = config
064: .configureService(StatefulSessionContainerInfo.class);
065: statefulContainerInfo.properties.setProperty("PoolSize", "0");
066: statefulContainerInfo.properties.setProperty("BulkPassivate",
067: "1");
068: assembler.createContainer(statefulContainerInfo);
069:
070: assembler.createApplication(config
071: .configureApplication(buildTestApp()));
072:
073: StatefulSessionBeanTest.calls.clear();
074:
075: InitialContext ctx = new InitialContext();
076: TargetHome home = (TargetHome) ctx
077: .lookup("TargetBeanRemoteHome");
078: assertNotNull(home);
079:
080: Target target = home.create("Fuzz");
081: assertNotNull(target);
082:
083: String name = target.getName();
084: assertEquals("Fuzz", name);
085:
086: target.remove();
087:
088: assertCalls(Call.values());
089:
090: }
091:
092: private void assertCalls(Call... expectedCalls) {
093: List expected = Arrays.asList(expectedCalls);
094: assertEquals(StatefulSessionBeanTest.join("\n", expected),
095: StatefulSessionBeanTest.join("\n",
096: StatefulSessionBeanTest.calls));
097: }
098:
099: public EjbModule buildTestApp() {
100: EjbJar ejbJar = new EjbJar();
101:
102: StatefulBean bean = ejbJar.addEnterpriseBean(new StatefulBean(
103: StatefulSessionBeanTest.TargetBean.class));
104: bean.setHomeAndRemote(TargetHome.class, Target.class);
105:
106: return new EjbModule(this .getClass().getClassLoader(), this
107: .getClass().getSimpleName(), "test", ejbJar, null);
108: }
109:
110: public static List<Call> calls = new ArrayList<Call>();
111:
112: public static enum Call {
113: Constructor, SetSessionContext, EjbCreate, EjbPassivate1, EjbActivate1, BusinessMethod, EjbPassivate2, EjbActivate2, EjbPassivate3, EjbActivate3, EjbRemove
114: }
115:
116: public static class TargetBean implements SessionBean {
117:
118: private int activates = 0;
119: private int passivates = 0;
120:
121: private String name;
122:
123: public TargetBean() {
124: calls.add(Call.Constructor);
125: }
126:
127: public void setSessionContext(SessionContext sessionContext) {
128: calls.add(Call.SetSessionContext);
129: }
130:
131: public void ejbCreate(String name) throws CreateException {
132: calls.add(Call.EjbCreate);
133: this .name = name;
134: }
135:
136: public String getName() {
137: calls.add(Call.BusinessMethod);
138: return name;
139: }
140:
141: public void ejbActivate() throws EJBException, RemoteException {
142: calls.add(Enum.valueOf(Call.class, "EjbActivate"
143: + (++activates)));
144: }
145:
146: public void ejbPassivate() throws EJBException, RemoteException {
147: calls.add(Enum.valueOf(Call.class, "EjbPassivate"
148: + (++passivates)));
149: }
150:
151: public void ejbRemove() throws EJBException, RemoteException {
152: calls.add(Call.EjbRemove);
153: }
154: }
155:
156: public static interface TargetHome extends EJBHome {
157: Target create(String name) throws RemoteException,
158: CreateException;
159: }
160:
161: public static interface Target extends EJBObject {
162: String getName();
163: }
164:
165: private static String join(String delimeter, List items) {
166: StringBuffer sb = new StringBuffer();
167: for (Object item : items) {
168: sb.append(item.toString()).append(delimeter);
169: }
170: return sb.toString();
171: }
172: }
|