001: /*
002: * ========================================================================
003: *
004: * Copyright 2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.server.runner;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024:
025: import javax.servlet.ServletConfig;
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletException;
028: import javax.servlet.UnavailableException;
029:
030: import com.mockobjects.dynamic.C;
031: import com.mockobjects.dynamic.Mock;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * Unit tests for {@link ServletTestRunner}.
037: *
038: * @version $Id: TestServletTestRunner.java 238991 2004-05-22 11:34:50Z vmassol $
039: */
040: public final class TestServletTestRunner extends TestCase {
041: /**
042: * Control mock for {@link ServletConfig}.
043: */
044: private Mock mockServletConfig;
045:
046: /**
047: * Mock for {@link ServletConfig}.
048: */
049: private ServletConfig servletConfig;
050:
051: /**
052: * Control mock for {@link ServletContext}.
053: */
054: private Mock mockServletContext;
055:
056: /**
057: * Mock for {@link ServletContext}.
058: */
059: private ServletContext servletContext;
060:
061: /**
062: * Object to unit test.
063: */
064: private ServletTestRunner runner;
065:
066: /**
067: * @see TestCase#setUp()
068: */
069: protected void setUp() {
070: mockServletConfig = new Mock(ServletConfig.class);
071: servletConfig = (ServletConfig) mockServletConfig.proxy();
072:
073: mockServletContext = new Mock(ServletContext.class);
074: servletContext = (ServletContext) mockServletContext.proxy();
075:
076: mockServletConfig.matchAndReturn("getServletContext",
077: servletContext);
078: mockServletConfig.matchAndReturn("getServletName",
079: "TestServlet");
080: mockServletContext.expect("log", C.ANY_ARGS);
081:
082: runner = new ServletTestRunner();
083: }
084:
085: /**
086: * Verify that the {@link ServletTestRunner#init()} method works when
087: * there are no user stylesheet defined.
088: *
089: * @throws ServletException in case of error
090: */
091: public void testInitWhenNoXslStylesheet() throws ServletException {
092: mockServletConfig.expectAndReturn("getInitParameter",
093: "xsl-stylesheet", null);
094:
095: runner.init(servletConfig);
096: }
097:
098: /**
099: * Verify that the {@link ServletTestRunner#init()} method works when
100: * there is a user stylesheet defined which points to an invalid
101: * file.
102: *
103: * @throws ServletException in case of error
104: */
105: public void testInitWhenXslStylesheetNotFound()
106: throws ServletException {
107: mockServletConfig.expectAndReturn("getInitParameter",
108: "xsl-stylesheet", "some-stylesheet.xsl");
109: mockServletContext.expectAndReturn("getResourceAsStream",
110: C.ANY_ARGS, null);
111:
112: try {
113: runner.init(servletConfig);
114: fail("Should have thrown an UnavailableException exception");
115: } catch (UnavailableException expected) {
116: assertEquals(
117: "The initialization parameter 'xsl-stylesheet' does "
118: + "not refer to an existing resource",
119: expected.getMessage());
120: }
121: }
122:
123: /**
124: * Verify that the {@link ServletTestRunner#init()} method works when
125: * there is a valid user stylesheet defined.
126: *
127: * @throws ServletException in case of error
128: */
129: public void testInitWithXslStylesheet() throws ServletException {
130: mockServletConfig.expectAndReturn("getInitParameter",
131: "xsl-stylesheet", "some-stylesheet.xsl");
132:
133: InputStream mockInputStream = new InputStream() {
134: private int counter = 0;
135:
136: private static final String CONTENT = ""
137: + "<xsl:stylesheet xmlns:xsl=\""
138: + "http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
139: + "</xsl:stylesheet>";
140:
141: public int read() throws IOException {
142: while (counter < CONTENT.length()) {
143: return CONTENT.charAt(counter++);
144: }
145: return -1;
146: }
147: };
148:
149: mockServletContext.expectAndReturn("getResourceAsStream",
150: C.ANY_ARGS, mockInputStream);
151:
152: // Note: There should be no call to log. If there is it means there
153: // has been an error...
154:
155: runner.init(servletConfig);
156: }
157:
158: }
|