01: /*
02: * ========================================================================
03: *
04: * Copyright 2004 The Apache Software Foundation.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: * ========================================================================
19: */
20: package org.apache.cactus.sample.servlet;
21:
22: import java.util.Hashtable;
23:
24: import javax.servlet.http.HttpServletRequest;
25:
26: import org.codehaus.aspectwerkz.attribdef.Pointcut;
27: import org.codehaus.aspectwerkz.attribdef.aspect.Aspect;
28: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
29: import org.codehaus.aspectwerkz.joinpoint.MethodJoinPoint;
30:
31: import com.meterware.httpunit.GetMethodWebRequest;
32: import com.meterware.httpunit.WebConversation;
33: import com.meterware.httpunit.WebRequest;
34: import com.meterware.httpunit.WebResponse;
35:
36: import junit.framework.TestCase;
37:
38: public class TestSampleServletAspectWerkz extends TestCase {
39: /**
40: * Intercepts Servlet's doXXX calls and instead redirect the flow of
41: * execution to the {@link SampleServlet#getRequestParameters} method to
42: * unit test.
43: */
44: public static class GetRequestParametersTestAdvice extends Aspect {
45: /**
46: * @Execution * *..SampleServlet.do*(..)
47: */
48: Pointcut interceptServlet;
49:
50: /**
51: * @Around interceptServlet
52: */
53: public Object catchGetRequestParameters(JoinPoint joinPoint)
54: throws Throwable {
55: MethodJoinPoint jp = (MethodJoinPoint) joinPoint;
56: SampleServlet servlet = (SampleServlet) jp
57: .getTargetInstance();
58: Hashtable params = servlet
59: .getRequestParameters((HttpServletRequest) jp
60: .getParameters()[0]);
61: assertNotNull(params.get("param1"));
62: assertNotNull(params.get("param2"));
63: assertEquals("value1", params.get("param1"));
64: assertEquals("value2", params.get("param2"));
65: return null;
66: }
67: }
68:
69: /**
70: * Test {@link SampleServlet#getRequestParameters} by calling the server
71: * side using HttpUnit. On the server side, our aspect will kick in and
72: * the {@link GetRequestParametersTestAdvice#testGetRequestParameters(JoinPoint)} test method will
73: * be called to unit test our method.
74: */
75: public void testGetRequestParameters() throws Exception {
76: WebConversation conversation = new WebConversation();
77: WebRequest request = new GetMethodWebRequest(
78: "http://localhost:8080/test/SampleServlet?param1=value1¶m2=value2");
79: WebResponse response = conversation.getResponse(request);
80: }
81: }
|