KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > server > runner > TestServletTestRunner


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.server.runner;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 import javax.servlet.ServletConfig JavaDoc;
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.UnavailableException JavaDoc;
29
30 import com.mockobjects.dynamic.C;
31 import com.mockobjects.dynamic.Mock;
32
33 import junit.framework.TestCase;
34
35 /**
36  * Unit tests for {@link ServletTestRunner}.
37  *
38  * @version $Id: TestServletTestRunner.java,v 1.1 2004/05/22 11:34:50 vmassol Exp $
39  */

40 public final class TestServletTestRunner extends TestCase
41 {
42     /**
43      * Control mock for {@link ServletConfig}.
44      */

45     private Mock mockServletConfig;
46
47     /**
48      * Mock for {@link ServletConfig}.
49      */

50     private ServletConfig JavaDoc servletConfig;
51
52     /**
53      * Control mock for {@link ServletContext}.
54      */

55     private Mock mockServletContext;
56
57     /**
58      * Mock for {@link ServletContext}.
59      */

60     private ServletContext JavaDoc servletContext;
61     
62     /**
63      * Object to unit test.
64      */

65     private ServletTestRunner runner;
66
67     /**
68      * @see TestCase#setUp()
69      */

70     protected void setUp()
71     {
72         mockServletConfig = new Mock(ServletConfig JavaDoc.class);
73         servletConfig = (ServletConfig JavaDoc) mockServletConfig.proxy();
74
75         mockServletContext = new Mock(ServletContext JavaDoc.class);
76         servletContext = (ServletContext JavaDoc) mockServletContext.proxy();
77
78         mockServletConfig.matchAndReturn("getServletContext", servletContext);
79         mockServletConfig.matchAndReturn("getServletName", "TestServlet");
80         mockServletContext.expect("log", C.ANY_ARGS);
81         
82         runner = new ServletTestRunner();
83     }
84
85     /**
86      * Verify that the {@link ServletTestRunner#init()} method works when
87      * there are no user stylesheet defined.
88      *
89      * @throws ServletException in case of error
90      */

91     public void testInitWhenNoXslStylesheet() throws ServletException JavaDoc
92     {
93         mockServletConfig.expectAndReturn("getInitParameter",
94             "xsl-stylesheet", null);
95
96         runner.init(servletConfig);
97     }
98
99     /**
100      * Verify that the {@link ServletTestRunner#init()} method works when
101      * there is a user stylesheet defined which points to an invalid
102      * file.
103      *
104      * @throws ServletException in case of error
105      */

106     public void testInitWhenXslStylesheetNotFound() throws ServletException JavaDoc
107     {
108         mockServletConfig.expectAndReturn("getInitParameter",
109             "xsl-stylesheet", "some-stylesheet.xsl");
110         mockServletContext.expectAndReturn("getResourceAsStream", C.ANY_ARGS,
111             null);
112         
113         try
114         {
115             runner.init(servletConfig);
116             fail("Should have thrown an UnavailableException exception");
117         }
118         catch (UnavailableException JavaDoc expected)
119         {
120             assertEquals("The initialization parameter 'xsl-stylesheet' does "
121                 + "not refer to an existing resource", expected.getMessage());
122         }
123     }
124
125     /**
126      * Verify that the {@link ServletTestRunner#init()} method works when
127      * there is a valid user stylesheet defined.
128      *
129      * @throws ServletException in case of error
130      */

131     public void testInitWithXslStylesheet() throws ServletException JavaDoc
132     {
133         mockServletConfig.expectAndReturn("getInitParameter",
134             "xsl-stylesheet", "some-stylesheet.xsl");
135
136         InputStream JavaDoc mockInputStream = new InputStream JavaDoc()
137         {
138             private int counter = 0;
139             
140             private static final String JavaDoc CONTENT = ""
141                 + "<xsl:stylesheet xmlns:xsl=\""
142                 + "http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
143                 + "</xsl:stylesheet>";
144             
145             public int read() throws IOException JavaDoc
146             {
147                 while (counter < CONTENT.length())
148                 {
149                     return CONTENT.charAt(counter++);
150                 }
151                 return -1;
152             }
153         };
154
155         mockServletContext.expectAndReturn("getResourceAsStream", C.ANY_ARGS,
156             mockInputStream);
157
158         // Note: There should be no call to log. If there is it means there
159
// has been an error...
160

161         runner.init(servletConfig);
162     }
163
164 }
165
Popular Tags