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.extension.jetty;
021:
022: import java.net.URL;
023:
024: import org.apache.cactus.internal.configuration.Configuration;
025: import org.apache.cactus.internal.configuration.FilterConfiguration;
026: import org.apache.cactus.internal.configuration.ServletConfiguration;
027:
028: import com.mockobjects.dynamic.Mock;
029:
030: import junit.framework.TestCase;
031:
032: /**
033: * Unit tests of the {@link JettyTestSetup} class.
034: *
035: * Note: For this test to work, it must be passed the <code>cactus.port</code>
036: * system property. If not it will default to 8080.
037: *
038: * @version $Id: TestJettyTestSetup.java 238991 2004-05-22 11:34:50Z vmassol $
039: */
040: public class TestJettyTestSetup extends TestCase {
041: /**
042: * Control mock for {@link Configuration}.
043: */
044: private Mock mockConfiguration;
045:
046: /**
047: * Mock for {@link Configuration}.
048: */
049: private Configuration configuration;
050:
051: /**
052: * Control mock for {@link ServletConfiguration}.
053: */
054: private Mock mockServletConfiguration;
055:
056: /**
057: * Mock for {@link ServletConfiguration}.
058: */
059: private ServletConfiguration servletConfiguration;
060:
061: /**
062: * Control mock for {@link FilterConfiguration}.
063: */
064: private Mock mockFilterConfiguration;
065:
066: /**
067: * Mock for {@link FilterConfiguration}.
068: */
069: private FilterConfiguration filterConfiguration;
070:
071: /**
072: * URL pointing to the test context. Note that if the port is not
073: * passed as a system property it defaults to 8080.
074: */
075: private static final String CONTEXT_URL = "http://localhost:"
076: + System.getProperty("cactus.port", "8080");
077:
078: /**
079: * Object to unit test.
080: */
081: private JettyTestSetup jettyTestSetup;
082:
083: /**
084: * Fake test case object used only to construct an instance of
085: * {@link JettyTestSetup}.
086: */
087: public class SampleTestCase extends TestCase {
088: }
089:
090: /**
091: * @see TestCase#setUp()
092: */
093: protected void setUp() throws Exception {
094: mockConfiguration = new Mock(Configuration.class);
095: configuration = (Configuration) mockConfiguration.proxy();
096:
097: mockServletConfiguration = new Mock(ServletConfiguration.class);
098: servletConfiguration = (ServletConfiguration) mockServletConfiguration
099: .proxy();
100: mockFilterConfiguration = new Mock(FilterConfiguration.class);
101: filterConfiguration = (FilterConfiguration) mockFilterConfiguration
102: .proxy();
103:
104: mockConfiguration.matchAndReturn("getContextURL", CONTEXT_URL);
105: mockServletConfiguration.matchAndReturn(
106: "getDefaultRedirectorName", "ServletRedirector");
107:
108: URL testURL = new URL(CONTEXT_URL + "/"
109: + servletConfiguration.getDefaultRedirectorName());
110:
111: mockServletConfiguration.matchAndReturn(
112: "getDefaultRedirectorURL", testURL.getPath());
113:
114: jettyTestSetup = new JettyTestSetup(new SampleTestCase(),
115: configuration, servletConfiguration,
116: filterConfiguration);
117:
118: // Ensure that the Jetty server is not already started.
119: if (jettyTestSetup.isAvailable(jettyTestSetup
120: .testConnectivity(testURL))) {
121: fail("No server serving the [" + testURL.getPath()
122: + "] URL should be started.");
123: }
124: }
125:
126: /**
127: * @see TestCase#tearDown()
128: */
129: protected void tearDown() throws Exception {
130: // Ensure that the server is stopped after each test
131: jettyTestSetup.tearDown();
132:
133: mockConfiguration.verify();
134: mockServletConfiguration.verify();
135: mockFilterConfiguration.verify();
136: }
137:
138: /**
139: * Verify that calling the {@link JettyTestSetup#setUp()} method
140: * works when Jetty is not already started.
141: *
142: * @throws Exception in case of error
143: */
144: public void testSetUpWhenServerNotAlreadyStarted() throws Exception {
145: jettyTestSetup.setUp();
146: assertTrue(jettyTestSetup.isRunning());
147: }
148:
149: /**
150: * Verify that calling the {@link JettyTestSetup#setUp()} method
151: * does not start Jetty a second time if Jetty is already started.
152: *
153: * @throws Exception in case of error
154: */
155: public void testSetUpWhenServerIsAlreadyStarted() throws Exception {
156: jettyTestSetup.setUp();
157:
158: // If we succeed calling a second time setUp(), it means that it's
159: // not starting the container again as otherwise it would fail to
160: // bind to an already bound port.
161: jettyTestSetup.setUp();
162:
163: // Ensure that tearDown will shutdown the container
164: jettyTestSetup.setForceShutdown(true);
165: }
166: }
|