KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tomcat > ContainerTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.tomcat;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.net.HttpURLConnection JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import org.apache.geronimo.tomcat.app.MockWebServiceContainer;
26 import org.apache.geronimo.util.encoders.Base64;
27
28
29 /**
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class ContainerTest extends AbstractWebModuleTest {
33
34     public void testWebServiceHandler() throws Exception JavaDoc {
35
36         String JavaDoc contextPath = "/foo/webservice.ws";
37         MockWebServiceContainer webServiceInvoker = new MockWebServiceContainer();
38         container.addWebService(contextPath, null, webServiceInvoker, null, null, null,null, cl);
39         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:8181" + contextPath).openConnection();
40         try {
41             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(connection.getInputStream()));
42             assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
43             assertEquals("Hello World", reader.readLine());
44         } finally {
45             connection.disconnect();
46         }
47         container.removeWebService(contextPath);
48         connection = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:8181" + contextPath).openConnection();
49         try {
50             connection.getInputStream();
51             fail();
52         } catch (Exception JavaDoc e) {
53             // see if we removed the ws.
54
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection.getResponseCode());
55             connection.disconnect();
56         }
57
58     }
59
60     public void testSecureWebServiceHandler() throws Exception JavaDoc {
61
62         setUpSecurity();
63
64         String JavaDoc contextPath = "/foo/webservice.ws";
65         MockWebServiceContainer webServiceInvoker = new MockWebServiceContainer();
66         container.addWebService(contextPath, null, webServiceInvoker, securityRealmName, securityRealmName, "NONE", "BASIC", cl);
67
68         //Veryify its secured
69
HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:8181" + contextPath).openConnection();
70         try {
71             connection.getInputStream();
72             fail();
73         } catch (Exception JavaDoc e) {
74             assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, connection.getResponseCode());
75         } finally {
76             connection.disconnect();
77         }
78
79         //Authenticate
80
connection = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:8181" + contextPath).openConnection();
81         String JavaDoc authentication = new String JavaDoc(Base64.encode("alan:starcraft".getBytes()));
82         connection.setRequestProperty("Authorization", "Basic " + authentication);
83         try {
84             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(connection.getInputStream()));
85             assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
86             assertEquals("Hello World", reader.readLine());
87         } finally {
88             connection.disconnect();
89         }
90         container.removeWebService(contextPath);
91         connection = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:8181" + contextPath).openConnection();
92         try {
93             connection.getInputStream();
94             fail();
95         } catch (Exception JavaDoc e) {
96             // see if we removed the ws.
97
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection.getResponseCode());
98             connection.disconnect();
99         }
100
101     }
102
103
104     protected void setUp() throws Exception JavaDoc {
105         super.setUp();
106         super.init(null);
107     }
108
109 }
110
Popular Tags