001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.openejb.cluster.stateful.container;
021:
022: import java.io.IOException;
023: import java.rmi.dgc.VMID;
024: import java.util.concurrent.Callable;
025: import java.util.concurrent.FutureTask;
026:
027: import javax.transaction.TransactionManager;
028: import javax.transaction.TransactionSynchronizationRegistry;
029:
030: import org.apache.geronimo.clustering.Session;
031: import org.apache.geronimo.clustering.SessionListener;
032: import org.apache.geronimo.clustering.wadi.WADISessionManager;
033: import org.apache.geronimo.openejb.cluster.stateful.container.ClusteredStatefulContainerTest.SFSB;
034: import org.apache.openejb.core.CoreDeploymentInfo;
035: import org.apache.openejb.core.ThreadContext;
036: import org.apache.openejb.core.stateful.BeanEntry;
037: import org.apache.openejb.persistence.JtaEntityManagerRegistry;
038: import org.apache.openejb.spi.SecurityService;
039:
040: import com.agical.rmock.core.Action;
041: import com.agical.rmock.core.MethodHandle;
042: import com.agical.rmock.core.describe.ExpressionDescriber;
043: import com.agical.rmock.core.match.operator.AbstractExpression;
044: import com.agical.rmock.extension.junit.RMockTestCase;
045:
046: /**
047: *
048: * @version $Rev:$ $Date:$
049: */
050: public class ClusteredStatefulInstanceManagerTest extends RMockTestCase {
051:
052: private ClusteredStatefulInstanceManager manager;
053: private String deploymentId;
054: private CoreDeploymentInfo deploymentInfo;
055: private VMID primKey;
056: private ThreadContext threadContext;
057: private WADISessionManager sessionManager;
058: private SessionListener sessionListener;
059:
060: @Override
061: protected void setUp() throws Exception {
062: TransactionManager txManager = (TransactionManager) mock(TransactionManager.class);
063: SecurityService securityService = (SecurityService) mock(SecurityService.class);
064: TransactionSynchronizationRegistry txSynchRegistry = (TransactionSynchronizationRegistry) mock(TransactionSynchronizationRegistry.class);
065: JtaEntityManagerRegistry jtaEntityManagerRegistry = new JtaEntityManagerRegistry(
066: txSynchRegistry);
067:
068: manager = (ClusteredStatefulInstanceManager) intercept(
069: ClusteredStatefulInstanceManager.class, new Object[] {
070: txManager, securityService,
071: jtaEntityManagerRegistry, null, 1, 1, 1 });
072:
073: deploymentId = "deploymentId";
074: deploymentInfo = new CoreDeploymentInfo(null, SFSB.class, null,
075: null, null, null, null, null, null, null, null) {
076: @Override
077: public ClassLoader getClassLoader() {
078: return getClass().getClassLoader();
079: }
080:
081: @Override
082: public Object getDeploymentID() {
083: return deploymentId;
084: }
085: };
086:
087: primKey = new VMID();
088:
089: sessionManager = (WADISessionManager) mock(WADISessionManager.class);
090:
091: threadContext = new ThreadContext(deploymentInfo, primKey);
092: ThreadContext.enter(threadContext);
093: }
094:
095: @Override
096: protected void tearDown() throws Exception {
097: ThreadContext.exit(threadContext);
098: }
099:
100: public void testNewBeanEntryForUnknownDeploymentThrowsISE()
101: throws Exception {
102: startVerification();
103:
104: try {
105: manager.newBeanEntry("primaryKey", new Object());
106: fail();
107: } catch (IllegalStateException e) {
108: }
109: }
110:
111: public void testNewBeanEntryOK() throws Exception {
112: VMID primaryKey = new VMID();
113:
114: recordAddSessionManagerAndCreateSession(primaryKey);
115:
116: startVerification();
117:
118: manager.addSessionManager(deploymentId, sessionManager);
119: manager.newBeanEntry(primaryKey, new Object());
120: }
121:
122: public void testSessionDestructionFreeInstance() throws Exception {
123: Session session = recordAddSessionManagerAndCreateSession(primKey);
124: FutureTask<BeanEntry> newBeanEntryTask = newBeanEntryTask(
125: primKey, session);
126:
127: manager.freeInstance(null);
128: modify().args(new ThreadContextArgAssertion());
129:
130: startVerification();
131:
132: manager.deploy(deploymentInfo, null);
133: manager.addSessionManager(deploymentId, sessionManager);
134:
135: newBeanEntryTask.run();
136: sessionListener.notifySessionDestruction(session);
137: }
138:
139: public void testInboundSessionMigrationActivateAndPoolBeanEntry()
140: throws Exception {
141: Session session = recordAddSessionManagerAndCreateSession(primKey);
142: FutureTask<BeanEntry> newBeanEntryTask = newBeanEntryTask(
143: primKey, session);
144:
145: manager.activateInstance(null, null);
146: modify().args(new ThreadContextArgAssertion(), is.NOT_NULL);
147: manager.poolInstance(null, null);
148: modify().args(new ThreadContextArgAssertion(), is.NOT_NULL);
149:
150: startVerification();
151:
152: manager.deploy(deploymentInfo, null);
153: manager.addSessionManager(deploymentId, sessionManager);
154:
155: newBeanEntryTask.run();
156: sessionListener.notifyInboundSessionMigration(session);
157: }
158:
159: public void testOutboundSessionMigrationPassivateBeanEntry()
160: throws Exception {
161: Session session = recordAddSessionManagerAndCreateSession(primKey);
162: FutureTask<BeanEntry> newBeanEntryTask = newBeanEntryTask(
163: primKey, session);
164:
165: manager.passivate(null, null);
166: modify().args(new ThreadContextArgAssertion(), is.NOT_NULL);
167:
168: manager.freeInstance(null);
169: modify().args(new ThreadContextArgAssertion());
170:
171: startVerification();
172:
173: manager.deploy(deploymentInfo, null);
174: manager.addSessionManager(deploymentId, sessionManager);
175:
176: newBeanEntryTask.run();
177: sessionListener.notifyOutboundSessionMigration(session);
178: }
179:
180: public void testOnFreeBeanEntryReleaseSession() throws Exception {
181: Session session = (Session) mock(Session.class);
182: session.addState(ClusteredBeanEntry.SESSION_KEY_ENTRY, null);
183: modify().args(is.AS_RECORDED, is.NOT_NULL);
184:
185: session.release();
186:
187: startVerification();
188:
189: manager.onFreeBeanEntry(threadContext, new ClusteredBeanEntry(
190: session, deploymentId, new Object(), primKey, 0));
191: }
192:
193: public void testOnPoolInstanceWithoutTransactionTriggersSessionOnEndAccess()
194: throws Exception {
195: Session session = (Session) mock(Session.class);
196: session.addState(ClusteredBeanEntry.SESSION_KEY_ENTRY, null);
197: modify().args(is.AS_RECORDED, is.NOT_NULL);
198:
199: session.addState(ClusteredBeanEntry.SESSION_KEY_ENTRY, null);
200: modify().args(is.AS_RECORDED, is.NOT_NULL);
201: session.onEndAccess();
202:
203: startVerification();
204:
205: manager.onPoolInstanceWithoutTransaction(threadContext,
206: new ClusteredBeanEntry(session, deploymentId,
207: new Object(), primKey, 0));
208: }
209:
210: private FutureTask<BeanEntry> newBeanEntryTask(
211: final VMID primaryKey, Session session) {
212: final FutureTask<BeanEntry> newBeanEntryTask = new FutureTask<BeanEntry>(
213: new Callable<BeanEntry>() {
214: public BeanEntry call() throws Exception {
215: return manager.newBeanEntry(primaryKey,
216: new Object());
217: }
218: });
219: session.getState(ClusteredBeanEntry.SESSION_KEY_ENTRY);
220: modify().multiplicity(expect.from(0)).perform(new Action() {
221: public Object invocation(Object[] arg0, MethodHandle arg1)
222: throws Throwable {
223: return newBeanEntryTask.get();
224: }
225: });
226: return newBeanEntryTask;
227: }
228:
229: private Session recordAddSessionManagerAndCreateSession(
230: VMID primaryKey) throws Exception {
231: sessionManager.registerListener(null);
232: modify().args(new AbstractExpression() {
233: public void describeWith(ExpressionDescriber arg0)
234: throws IOException {
235: }
236:
237: public boolean passes(Object arg0) {
238: sessionListener = (SessionListener) arg0;
239: return true;
240: }
241: });
242:
243: Session session = sessionManager.createSession(primaryKey
244: .toString());
245:
246: session.addState(ClusteredBeanEntry.SESSION_KEY_ENTRY, null);
247: modify().args(is.AS_RECORDED, is.NOT_NULL);
248:
249: return session;
250: }
251:
252: protected final class ThreadContextArgAssertion extends
253: AbstractExpression {
254: public void describeWith(ExpressionDescriber arg0)
255: throws IOException {
256: }
257:
258: public boolean passes(Object arg0) {
259: ThreadContext context = (ThreadContext) arg0;
260: assertSame(deploymentInfo, context.getDeploymentInfo());
261: assertSame(primKey, context.getPrimaryKey());
262: return true;
263: }
264: }
265:
266: }
|