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.test.stateless;
017:
018: import java.util.ArrayList;
019: import java.util.Map;
020: import java.util.Arrays;
021:
022: /**
023: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
024: * @author <a href="mailto:goyathlay.geronimo@gmail.com">Prasad Kashyap</a>
025: *
026: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
027: */
028: public class StatelessInterceptorTests extends
029: BasicStatelessLocalTestClient {
030:
031: /*
032: * @EJB(name="BasicStatelessInterceptedBusinessRemote", beanInterface = BasicStatelessInterceptedRemote.class)
033: */
034: private BasicStatelessInterceptedRemote remoteInterceptor;
035:
036: public StatelessInterceptorTests() {
037: super ("BasicStatelessIntercepted.");
038: }
039:
040: protected void setUp() throws Exception {
041: super .setUp();
042: Object obj = initialContext
043: .lookup("BasicStatelessInterceptedBusinessRemote");
044: assertNotNull(
045: "The BasicStatelessInterceptedBusinessRemote object is null",
046: obj);
047: remoteInterceptor = (BasicStatelessInterceptedRemote) javax.rmi.PortableRemoteObject
048: .narrow(obj, BasicStatelessInterceptedRemote.class);
049: assertNotNull("Remote interceptor is null", remoteInterceptor);
050: }
051:
052: /**
053: * Invokes a business method which is to be intercepted from a class, in-bean and at method level.
054: */
055: public void test01_interceptorChaining() {
056: String reverseMe = "Intercept";
057: String reversedString = remoteInterceptor.reverse(reverseMe);
058: // verifying InvocationContext.procced()
059: assertEquals("tpecretnI", reversedString);
060:
061: Map contextData = remoteInterceptor.getContextData();
062: // verifying that inBeanInterceptor indeed intercepted this method. This cannot be excluded at all.
063: assertNotNull(contextData.containsKey("reverse"));
064:
065: Map innerMap = (Map) contextData.get("reverse");
066: ArrayList interceptorsList = (ArrayList) innerMap
067: .get("INTERCEPTORS");
068:
069: assertEquals("ddInterceptor", interceptorsList.get(0)); //specified in DD
070: assertEquals("secondClassInterceptor", interceptorsList.get(1)); //specified in DD
071: assertEquals("superClassInterceptor", interceptorsList.get(2)); //derived from class extension
072: assertEquals("classInterceptor", interceptorsList.get(3)); //specified by @
073: assertEquals("methodInterceptor", interceptorsList.get(4)); //specified by @ on method
074: assertEquals("superBeanInterceptor", interceptorsList.get(5)); //derived from bean extension
075: assertEquals("inBeanInterceptor", interceptorsList.get(6)); //in bean
076: }
077:
078: /**
079: * Invokes just 1 business method on the bean. The interceptor method stores the intercepted method's name and
080: * params in a map that is returned by the <code>getContextData</code>
081: */
082: public void test02_methodProfile() {
083: String reverseMe = "Intercept";
084: String reversedString = remoteInterceptor.reverse(reverseMe);
085: // verifying InvocationContext.procced()
086: assertEquals("tpecretnI", reversedString);
087:
088: Map contextData = remoteInterceptor.getContextData();
089: // verifying InvocationContext.getMethod().getName()
090: assertTrue(contextData.containsKey("reverse"));
091:
092: Map innerMap = (Map) contextData.get("reverse");
093: Object[] params = (Object[]) innerMap.get("PARAMETERS");
094: // verifying that the parameters array was received from contextData and stored.
095: assertNotNull("value of PARAMETERS key is null", params);
096: // verifying InvocationContext.getParameters()
097: assertEquals(1, params.length);
098: assertEquals(reverseMe, params[0].toString());
099: }
100:
101: /**
102: * Invokes a business method which is annotated to be excluded from interception.
103: * <code>getContextData()</code> has been annotated with <code>@ExcludesClassInterceptors</code>
104: */
105: public void test03_excludeClassInterceptors() {
106: Map contextData = remoteInterceptor.getContextData();
107: // verifying that inBeanInterceptor indeed intercepted this method. This cannot be excluded at all.
108: assertNotNull(contextData.containsKey("getContextData"));
109:
110: Map innerMap = (Map) contextData.get("getContextData");
111: ArrayList interceptorsList = (ArrayList) innerMap
112: .get("INTERCEPTORS");
113: // verifying @ExcludeClassInterceptors annotated method was not intercepted
114: assertFalse(
115: "getContextData() should not have been intercepted by classInterceptor()",
116: interceptorsList.contains("classInterceptor"));
117: assertFalse(
118: "getContextData() should not have been intercepted by classInterceptor()",
119: interceptorsList.contains("classInterceptor"));
120: assertFalse(
121: "getContextData() should not have been intercepted by secondClassInterceptor()",
122: interceptorsList.contains("secondClassInterceptor"));
123: assertFalse(
124: "getContextData() should not have been intercepted by ddInterceptor()",
125: interceptorsList.contains("ddInterceptor"));
126: }
127:
128: /**
129: * Invokes a business method which is declared to be excluded from interception by the DD
130: * <code>concat()</code> has been annotated with <code>exclude-class-interceptors</code>
131: */
132: public void test04_excludeClassInterceptors_02() {
133: String catString = remoteInterceptor.concat("Inter", "cept");
134: // verifying InvocationContext.procced()
135: assertEquals("Intercept", catString);
136:
137: Map contextData = remoteInterceptor.getContextData();
138: // verifying that inBeanInterceptor indeed intercepted this method. This cannot be excluded at all.
139: assertNotNull(contextData.containsKey("concat"));
140:
141: Map innerMap = (Map) contextData.get("concat");
142: ArrayList interceptorsList = (ArrayList) innerMap
143: .get("INTERCEPTORS");
144: // verifying @ExcludeClassInterceptors annotated method was not intercepted
145: assertFalse(
146: "concat() should not have been intercepted by superClassInterceptor()",
147: interceptorsList.contains("superClassInterceptor"));
148: assertFalse(
149: "concat() should not have been intercepted by classInterceptor()",
150: interceptorsList.contains("classInterceptor"));
151: assertFalse(
152: "concat() should not have been intercepted by ddInterceptor()",
153: interceptorsList.contains("ddInterceptor"));
154: assertFalse(
155: "concat() should not have been intercepted by secondClassInterceptor()",
156: interceptorsList.contains("secondClassInterceptor"));
157: }
158:
159: }
|