KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > extension > jetty > TestJettyTestSetup


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.extension.jetty;
21
22 import java.net.URL JavaDoc;
23
24 import org.apache.cactus.internal.configuration.Configuration;
25 import org.apache.cactus.internal.configuration.FilterConfiguration;
26 import org.apache.cactus.internal.configuration.ServletConfiguration;
27
28 import com.mockobjects.dynamic.Mock;
29
30 import junit.framework.TestCase;
31
32 /**
33  * Unit tests of the {@link JettyTestSetup} class.
34  *
35  * Note: For this test to work, it must be passed the <code>cactus.port</code>
36  * system property. If not it will default to 8080.
37  *
38  * @version $Id: TestJettyTestSetup.java,v 1.1 2004/05/22 11:34:49 vmassol Exp $
39  */

40 public class TestJettyTestSetup extends TestCase
41 {
42     /**
43      * Control mock for {@link Configuration}.
44      */

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

50     private Configuration configuration;
51
52     /**
53      * Control mock for {@link ServletConfiguration}.
54      */

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

60     private ServletConfiguration servletConfiguration;
61
62     /**
63      * Control mock for {@link FilterConfiguration}.
64      */

65     private Mock mockFilterConfiguration;
66
67     /**
68      * Mock for {@link FilterConfiguration}.
69      */

70     private FilterConfiguration filterConfiguration;
71     
72     /**
73      * URL pointing to the test context. Note that if the port is not
74      * passed as a system property it defaults to 8080.
75      */

76     private static final String JavaDoc CONTEXT_URL =
77         "http://localhost:" + System.getProperty("cactus.port", "8080");
78
79     /**
80      * Object to unit test.
81      */

82     private JettyTestSetup jettyTestSetup;
83
84     /**
85      * Fake test case object used only to construct an instance of
86      * {@link JettyTestSetup}.
87      */

88     public class SampleTestCase extends TestCase
89     {
90     }
91
92     /**
93      * @see TestCase#setUp()
94      */

95     protected void setUp() throws Exception JavaDoc
96     {
97         mockConfiguration = new Mock(Configuration.class);
98         configuration = (Configuration) mockConfiguration.proxy();
99
100         mockServletConfiguration = new Mock(ServletConfiguration.class);
101         servletConfiguration =
102             (ServletConfiguration) mockServletConfiguration.proxy();
103         mockFilterConfiguration = new Mock(FilterConfiguration.class);
104         filterConfiguration =
105             (FilterConfiguration) mockFilterConfiguration.proxy();
106
107         mockConfiguration.matchAndReturn("getContextURL", CONTEXT_URL);
108         mockServletConfiguration.matchAndReturn("getDefaultRedirectorName",
109             "ServletRedirector");
110
111         URL JavaDoc testURL = new URL JavaDoc(CONTEXT_URL + "/"
112             + servletConfiguration.getDefaultRedirectorName());
113             
114         mockServletConfiguration.matchAndReturn("getDefaultRedirectorURL",
115             testURL.getPath());
116         
117         jettyTestSetup = new JettyTestSetup(new SampleTestCase(),
118             configuration, servletConfiguration, filterConfiguration);
119         
120         // Ensure that the Jetty server is not already started.
121
if (jettyTestSetup.isAvailable(
122             jettyTestSetup.testConnectivity(testURL)))
123         {
124             fail("No server serving the [" + testURL.getPath()
125                 + "] URL should be started.");
126         }
127     }
128
129     /**
130      * @see TestCase#tearDown()
131      */

132     protected void tearDown() throws Exception JavaDoc
133     {
134         // Ensure that the server is stopped after each test
135
jettyTestSetup.tearDown();
136         
137         mockConfiguration.verify();
138         mockServletConfiguration.verify();
139         mockFilterConfiguration.verify();
140     }
141
142     /**
143      * Verify that calling the {@link JettyTestSetup#setUp()} method
144      * works when Jetty is not already started.
145      *
146      * @throws Exception in case of error
147      */

148     public void testSetUpWhenServerNotAlreadyStarted() throws Exception JavaDoc
149     {
150         jettyTestSetup.setUp();
151         assertTrue(jettyTestSetup.isRunning());
152     }
153
154     /**
155      * Verify that calling the {@link JettyTestSetup#setUp()} method
156      * does not start Jetty a second time if Jetty is already started.
157      *
158      * @throws Exception in case of error
159      */

160     public void testSetUpWhenServerIsAlreadyStarted() throws Exception JavaDoc
161     {
162         jettyTestSetup.setUp();
163
164         // If we succeed calling a second time setUp(), it means that it's
165
// not starting the container again as otherwise it would fail to
166
// bind to an already bound port.
167
jettyTestSetup.setUp();
168
169         // Ensure that tearDown will shutdown the container
170
jettyTestSetup.setForceShutdown(true);
171     }
172 }
173
Popular Tags