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.lang.reflect.Method;
023: import java.net.URI;
024: import java.util.Collections;
025:
026: import javax.transaction.TransactionManager;
027:
028: import org.apache.geronimo.clustering.wadi.WADISessionManager;
029: import org.apache.geronimo.openejb.cluster.infra.NetworkConnectorTracker;
030: import org.apache.openejb.OpenEJBException;
031: import org.apache.openejb.core.CoreDeploymentInfo;
032: import org.apache.openejb.spi.SecurityService;
033: import org.codehaus.wadi.core.contextualiser.Invocation;
034: import org.codehaus.wadi.core.contextualiser.InvocationException;
035: import org.codehaus.wadi.core.manager.Manager;
036: import org.codehaus.wadi.servicespace.ServiceRegistry;
037: import org.codehaus.wadi.servicespace.ServiceSpace;
038:
039: import com.agical.rmock.core.Action;
040: import com.agical.rmock.core.MethodHandle;
041: import com.agical.rmock.extension.junit.RMockTestCase;
042:
043: /**
044: *
045: * @version $Rev:$ $Date:$
046: */
047: public class ClusteredStatefulContainerTest extends RMockTestCase {
048:
049: private WADISessionManager sessionManager;
050: private TransactionManager txManager;
051: private SecurityService securityService;
052: private ClusteredStatefulContainer container;
053: private CoreDeploymentInfo deploymentInfo;
054: private String deploymentId;
055: private Method callMethod;
056: private Manager wadiManager;
057: private NetworkConnectorTracker tracker;
058: private String primKey;
059: private Class<Runnable> callInterface;
060: private Object[] args;
061:
062: @Override
063: protected void setUp() throws Exception {
064: primKey = "primKey";
065: callInterface = Runnable.class;
066: callMethod = Runnable.class.getDeclaredMethod("run");
067: args = new Object[0];
068:
069: sessionManager = (WADISessionManager) mock(WADISessionManager.class);
070: wadiManager = sessionManager.getManager();
071: modify().multiplicity(expect.from(0));
072:
073: sessionManager.registerListener(null);
074: modify().multiplicity(expect.from(0)).args(is.NOT_NULL);
075:
076: ServiceSpace serviceSpace = sessionManager.getServiceSpace();
077: modify().multiplicity(expect.from(0));
078: ServiceRegistry serviceRegistry = serviceSpace
079: .getServiceRegistry();
080: modify().multiplicity(expect.from(0));
081: serviceRegistry.getStartedService(NetworkConnectorTracker.NAME);
082: modify().multiplicity(expect.from(0));
083: tracker = (NetworkConnectorTracker) mock(NetworkConnectorTracker.class);
084: modify().returnValue(tracker);
085:
086: txManager = (TransactionManager) mock(TransactionManager.class);
087: securityService = (SecurityService) mock(SecurityService.class);
088: container = (ClusteredStatefulContainer) intercept(
089: ClusteredStatefulContainer.class, new Object[] { "id",
090: txManager, securityService, null, 1, 1, 1 });
091: deploymentId = "deploymentId";
092: deploymentInfo = new CoreDeploymentInfo(null, SFSB.class, null,
093: null, null, null, null, null, null, null, null) {
094: @Override
095: public Object getDeploymentID() {
096: return deploymentId;
097: }
098: };
099: }
100:
101: public void testInvokeBusinessMethodForUnknownDeploymentThrowsOEJBE()
102: throws Exception {
103: startVerification();
104: try {
105: container.businessMethod(deploymentInfo, primKey,
106: callInterface, callMethod, args);
107: fail();
108: } catch (OpenEJBException e) {
109: }
110: }
111:
112: public void testInvokeRemoteMethodForUnknownDeploymentThrowsOEJBE()
113: throws Exception {
114: startVerification();
115: try {
116: container.removeEJBObject(deploymentInfo, primKey,
117: callInterface, callMethod, args);
118: fail();
119: } catch (OpenEJBException e) {
120: }
121: }
122:
123: public void testOEJBIsRethrownForBusinessMethod() throws Exception {
124: new OEJBIsRethrownTest() {
125: @Override
126: protected void executeMethod() throws OpenEJBException {
127: container.businessMethod(deploymentInfo, primKey,
128: callInterface, callMethod, args);
129: }
130:
131: @Override
132: protected void executeSuperMethod() throws OpenEJBException {
133: container.super BusinessMethod(deploymentInfo, primKey,
134: callInterface, callMethod, args);
135: }
136: }.executeTest();
137: }
138:
139: public void testOEJBIsRethrownForRemoveMethod() throws Exception {
140: new OEJBIsRethrownTest() {
141: @Override
142: protected void executeMethod() throws OpenEJBException {
143: container.removeEJBObject(deploymentInfo, primKey,
144: callInterface, callMethod, args);
145: }
146:
147: @Override
148: protected void executeSuperMethod() throws OpenEJBException {
149: container.super RemoveEJBObject(deploymentInfo, primKey,
150: callInterface, callMethod, args);
151: }
152: }.executeTest();
153: }
154:
155: protected abstract class OEJBIsRethrownTest {
156: public void executeTest() throws Exception {
157: recordContextualiseInvocation();
158:
159: executeSuperMethod();
160: OpenEJBException exception = new OpenEJBException();
161: modify().throwException(exception);
162:
163: startVerification();
164:
165: container.addSessionManager(deploymentId, sessionManager);
166:
167: try {
168: executeMethod();
169: fail();
170: } catch (OpenEJBException e) {
171: assertSame(exception, e);
172: }
173: }
174:
175: protected abstract void executeSuperMethod()
176: throws OpenEJBException;
177:
178: protected abstract void executeMethod() throws OpenEJBException;
179: }
180:
181: public void testBusinessMethodInvokationOK() throws Exception {
182: new MethodIsExecutedTest() {
183: @Override
184: protected Object executeMethod() throws OpenEJBException {
185: return container.businessMethod(deploymentInfo,
186: primKey, callInterface, callMethod, args);
187: }
188:
189: @Override
190: protected void executeSuperMethod() throws OpenEJBException {
191: container.super BusinessMethod(deploymentInfo, primKey,
192: callInterface, callMethod, args);
193: }
194: }.executeTest();
195: }
196:
197: public void testRemoveMethodInvokationOK() throws Exception {
198: new MethodIsExecutedTest() {
199: @Override
200: protected Object executeMethod() throws OpenEJBException {
201: return container.removeEJBObject(deploymentInfo,
202: primKey, callInterface, callMethod, args);
203: }
204:
205: @Override
206: protected void executeSuperMethod() throws OpenEJBException {
207: container.super RemoveEJBObject(deploymentInfo, primKey,
208: callInterface, callMethod, args);
209: }
210: }.executeTest();
211: }
212:
213: protected abstract class MethodIsExecutedTest {
214: public void executeTest() throws Exception {
215: recordContextualiseInvocation();
216:
217: executeSuperMethod();
218: Object result = new Object();
219: modify().returnValue(result);
220:
221: startVerification();
222:
223: container.addSessionManager(deploymentId, sessionManager);
224:
225: Object actualResult = executeMethod();
226: assertSame(result, actualResult);
227: }
228:
229: protected abstract void executeSuperMethod()
230: throws OpenEJBException;
231:
232: protected abstract Object executeMethod()
233: throws OpenEJBException;
234: }
235:
236: public void testGetLocationsRetunsNullWhenDeploymentIsNotRegistered()
237: throws Exception {
238: startVerification();
239:
240: assertNull(container.getLocations(deploymentInfo));
241: }
242:
243: public void testGetLocationsOK() throws Exception {
244: tracker.getConnectorURIs(deploymentId);
245: URI location = new URI("ejbd://host:1");
246: modify().returnValue(Collections.singleton(location));
247:
248: startVerification();
249:
250: container.addSessionManager(deploymentId, sessionManager);
251:
252: URI[] actualLocations = container.getLocations(deploymentInfo);
253: assertEquals(1, actualLocations.length);
254: assertSame(location, actualLocations[0]);
255: }
256:
257: public static class SFSB implements Runnable {
258: public void run() {
259: }
260: }
261:
262: protected void recordContextualiseInvocation()
263: throws InvocationException {
264: wadiManager.contextualise(null);
265: modify().args(is.NOT_NULL).perform(new Action() {
266: public Object invocation(Object[] arg0, MethodHandle arg1)
267: throws Throwable {
268: ((Invocation) arg0[0]).invoke();
269: return true;
270: }
271: });
272: }
273:
274: }
|