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.stateful;
017:
018: import java.util.ArrayList;
019: import java.util.Map;
020:
021: // import javax.ejb.EJB;
022:
023: /**
024: *
025: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
026: * @author <a href="mailto:goyathlay.geronimo@gmail.com">Prasad Kashyap</a>
027: */
028: // public class StatefulInterceptorTests extends AnnotatedFieldInjectionStatefulLocalTestClient {
029: public class StatefulInterceptorTests extends
030: BasicStatefulLocalTestClient {
031:
032: /*
033: * @EJB(name="BasicStatefulInterceptedBusinessRemote", beanInterface = BasicStatefulInterceptedRemote.class)
034: */
035: private BasicStatefulInterceptedRemote remoteInterceptor;
036:
037: public StatefulInterceptorTests() {
038: super ("BasicStatefulIntercepted.");
039: }
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043: Object obj = initialContext
044: .lookup("BasicStatefulInterceptedBusinessRemote");
045: assertNotNull("The object is null", obj);
046: remoteInterceptor = (BasicStatefulInterceptedRemote) javax.rmi.PortableRemoteObject
047: .narrow(obj, BasicStatefulInterceptedRemote.class);
048: assertNotNull("Remote interceptor is null", remoteInterceptor);
049: }
050:
051: /**
052: * Tears down the fixture, for example, close a network connection. This method is called after a test is executed.
053: */
054: protected void tearDown() throws Exception {
055: super .tearDown();
056: }
057:
058: /**
059: * Invokes a business method which is to be intercepted by class, in-bean and at method level.
060: */
061: public void test01_interceptorChaining() {
062: String reverseMe = "Intercept";
063: String reversedString = remoteInterceptor.reverse(reverseMe);
064: // verifying InvocationContext.procced()
065: assertEquals("tpecretnI", reversedString);
066:
067: Map contextData = remoteInterceptor.getContextData();
068: // verifying that inBeanInterceptor indeed intercepted this method. This cannot be excluded at all.
069: assertNotNull(contextData.containsKey("reverse"));
070:
071: Map innerMap = (Map) contextData.get("reverse");
072: ArrayList interceptorsList = (ArrayList) innerMap
073: .get("INTERCEPTORS");
074: // verifying interceptor chaining order
075: assertEquals("superClassInterceptor", interceptorsList.get(0)); //derived from class extension
076: assertEquals("classInterceptor", interceptorsList.get(1)); //specified by @
077: assertEquals("secondClassInterceptor", interceptorsList.get(2)); //specified by @
078: assertEquals("methodInterceptor", interceptorsList.get(3)); //specified by @ on method
079: assertEquals("ddInterceptor", interceptorsList.get(4)); //specified in DD on method
080: assertEquals("superBeanInterceptor", interceptorsList.get(5)); //derived from bean extension
081: assertEquals("inBeanInterceptor", interceptorsList.get(6)); //in bean
082: }
083:
084: /**
085: * Invokes just 1 business method on the bean. The interceptor method stores the intercepted method's name and
086: * params in a map that is returned by the <code>getContextData</code>
087: */
088: public void test02_methodProfile() {
089: String reverseMe = "Intercept";
090: String reversedString = remoteInterceptor.reverse(reverseMe);
091: // verifying InvocationContext.procced()
092: assertEquals("tpecretnI", reversedString);
093:
094: Map contextData = remoteInterceptor.getContextData();
095: // verifying InvocationContext.getMethod().getName()
096: assertTrue(contextData.containsKey("reverse"));
097:
098: Map innerMap = (Map) contextData.get("reverse");
099: Object[] params = (Object[]) innerMap.get("PARAMETERS");
100: // verifying that the parameters array was received from contextData and stored.
101: assertNotNull("value of PARAMETERS key is null", params);
102: // verifying InvocationContext.getParameters()
103: assertEquals(1, params.length);
104: assertEquals(reverseMe, params[0].toString());
105: }
106:
107: /**
108: * Invokes a business method which is annotated to be excluded from interception.
109: * <code>getContextData()</code> has been annotated with <code>@ExcludesClassInterceptors</code>
110: */
111: public void test03_excludeClassInterceptors() {
112: Map contextData = remoteInterceptor.getContextData();
113: // verifying that inBeanInterceptor indeed intercepted this method. This cannot be excluded at all.
114: assertNotNull(contextData.containsKey("getContextData"));
115:
116: Map innerMap = (Map) contextData.get("getContextData");
117: ArrayList interceptorsList = (ArrayList) innerMap
118: .get("INTERCEPTORS");
119: // verifying @ExcludeClassInterceptors annotated method was not intercepted by class interceptors
120: assertFalse(
121: "getContextData() should not have been intercepted by superClassInterceptor()",
122: interceptorsList.contains("superClassInterceptor"));
123: assertFalse(
124: "getContextData() should not have been intercepted by classInterceptor()",
125: interceptorsList.contains("classInterceptor"));
126: assertFalse(
127: "getContextData() should not have been intercepted by secondClassInterceptor()",
128: interceptorsList.contains("secondClassInterceptor"));
129: assertFalse(
130: "getContextData() should not have been intercepted by ddInterceptor()",
131: interceptorsList.contains("ddInterceptor"));
132: }
133:
134: /**
135: * Invokes a business method which is declared to be excluded from interception by the DD
136: * <code>getContextData()</code> has been annotated with <code>@ExcludesClassInterceptors</code>
137: */
138: public void test04_excludeClassInterceptors_02() {
139: String catString = remoteInterceptor.concat("Inter", "cept");
140: // verifying InvocationContext.procced()
141: assertEquals("Intercept", catString);
142:
143: Map contextData = remoteInterceptor.getContextData();
144: // verifying that inBeanInterceptor indeed intercepted this method. This cannot be excluded at all.
145: assertNotNull(contextData.containsKey("concat"));
146:
147: Map innerMap = (Map) contextData.get("concat");
148: ArrayList interceptorsList = (ArrayList) innerMap
149: .get("INTERCEPTORS");
150: // verifying @ExcludeClassInterceptors annotated method was not intercepted
151: assertFalse(
152: "concat() should not have been intercepted by superClassInterceptor()",
153: interceptorsList.contains("superClassInterceptor"));
154: assertFalse(
155: "concat() should not have been intercepted by classInterceptor()",
156: interceptorsList.contains("classInterceptor"));
157: assertFalse(
158: "concat() should not have been intercepted by ddInterceptor()",
159: interceptorsList.contains("ddInterceptor"));
160: assertFalse(
161: "concat() should not have been intercepted by secondClassInterceptor()",
162: interceptorsList.contains("secondClassInterceptor"));
163: }
164:
165: /**
166: * Invokes a business method which is annotated to be excluded from interception.
167: * <code>getContextData()</code> has been annotated with <code>@ExcludesClassInterceptors</code>
168: */
169: public void test05_PreDestroy() {
170: try {
171: tearDown();
172: Map contextData = remoteInterceptor.getContextData();
173: Map innerMap = (Map) contextData
174: .get("BasicStatefulInterceptedBean");
175: assertNotNull("InnerMap is null", innerMap);
176: ArrayList interceptorsList = (ArrayList) innerMap
177: .get("INTERCEPTORS");
178: // verifying interceptor chaining order
179: assertEquals("superClassInterceptorPreDestroy",
180: interceptorsList.get(0));
181: } catch (Exception e) {
182: // TODO Auto-generated catch block
183: e.printStackTrace();
184: }
185: }
186: }
|