KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > TestSampleServletAspectWerkz


1 /*
2  * ========================================================================
3  *
4  * Copyright 2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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 JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
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     /**
41      * Intercepts Servlet's doXXX calls and instead redirect the flow of
42      * execution to the {@link SampleServlet#getRequestParameters} method to
43      * unit test.
44      */

45     public static class GetRequestParametersTestAdvice extends Aspect
46     {
47         /**
48          * @Execution * *..SampleServlet.do*(..)
49          */

50         Pointcut interceptServlet;
51         
52         /**
53          * @Around interceptServlet
54          */

55         public Object JavaDoc catchGetRequestParameters(JoinPoint joinPoint)
56             throws Throwable JavaDoc
57         {
58             MethodJoinPoint jp = (MethodJoinPoint) joinPoint;
59             SampleServlet servlet = (SampleServlet) jp.getTargetInstance();
60             Hashtable JavaDoc params = servlet.getRequestParameters(
61                 (HttpServletRequest JavaDoc) jp.getParameters()[0]);
62             assertNotNull(params.get("param1"));
63             assertNotNull(params.get("param2"));
64             assertEquals("value1", params.get("param1"));
65             assertEquals("value2", params.get("param2"));
66             return null;
67         }
68     }
69
70     /**
71      * Test {@link SampleServlet#getRequestParameters} by calling the server
72      * side using HttpUnit. On the server side, our aspect will kick in and
73      * the {@link GetRequestParametersTestAdvice#testGetRequestParameters(JoinPoint)} test method will
74      * be called to unit test our method.
75      */

76     public void testGetRequestParameters() throws Exception JavaDoc
77     {
78         WebConversation conversation = new WebConversation();
79         WebRequest request = new GetMethodWebRequest(
80             "http://localhost:8080/test/SampleServlet?param1=value1&param2=value2");
81         WebResponse response = conversation.getResponse(request);
82     }
83 }
84
Popular Tags