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