KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > HttpManagedTest


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 package org.apache.servicemix.http;
18
19 import java.util.EventListener JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.apache.commons.httpclient.HttpClient;
26 import org.apache.commons.httpclient.methods.PostMethod;
27 import org.apache.commons.httpclient.methods.StringRequestEntity;
28 import org.apache.servicemix.components.http.InvalidStatusResponseException;
29 import org.apache.xbean.spring.context.XmlWebApplicationContext;
30 import org.mortbay.jetty.Connector;
31 import org.mortbay.jetty.Handler;
32 import org.mortbay.jetty.Server;
33 import org.mortbay.jetty.handler.ContextHandler;
34 import org.mortbay.jetty.handler.ContextHandlerCollection;
35 import org.mortbay.jetty.handler.HandlerCollection;
36 import org.mortbay.jetty.nio.SelectChannelConnector;
37 import org.mortbay.jetty.servlet.ServletHandler;
38 import org.mortbay.jetty.servlet.ServletHolder;
39 import org.mortbay.jetty.servlet.ServletMapping;
40 import org.springframework.web.context.ContextLoaderListener;
41
42 public class HttpManagedTest extends TestCase {
43
44     public void test() throws Exception JavaDoc {
45         ContextHandler context = new ContextHandler();
46         context.setContextPath("/test");
47         context.setEventListeners(new EventListener JavaDoc[] { new ContextLoaderListener() });
48         Map JavaDoc initParams = new HashMap JavaDoc();
49         initParams.put("contextConfigLocation", "classpath:org/apache/servicemix/http/spring-web.xml");
50         initParams.put("contextClass", XmlWebApplicationContext.class.getName());
51         context.setInitParams(initParams);
52         ServletHolder holder = new ServletHolder();
53         holder.setName("jbiServlet");
54         holder.setClassName(HttpManagedServlet.class.getName());
55         ServletHandler handler = new ServletHandler();
56         handler.setServlets(new ServletHolder[] { holder });
57         ServletMapping mapping = new ServletMapping();
58         mapping.setServletName("jbiServlet");
59         mapping.setPathSpec("/*");
60         handler.setServletMappings(new ServletMapping[] { mapping });
61         context.setHandler(handler);
62
63         ContextHandlerCollection contexts = new ContextHandlerCollection();
64         HandlerCollection handlers = new HandlerCollection();
65         handlers.setHandlers(new Handler[] { contexts });
66         contexts.addHandler(context);
67
68         SelectChannelConnector connector = new SelectChannelConnector();
69         connector.setHost("localhost");
70         connector.setPort(8080);
71         
72         Server server = new Server();
73         server.setConnectors(new Connector[] { connector });
74         server.setHandler(handlers);
75         server.start();
76         
77         System.err.println("Started");
78         
79         PostMethod post = new PostMethod("http://localhost:8080/test/jbi/Service/");
80         post.setRequestEntity(new StringRequestEntity("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><hello>world</hello></soap:Body></soap:Envelope>"));
81         new HttpClient().executeMethod(post);
82         if (post.getStatusCode() != 200) {
83             throw new InvalidStatusResponseException(post.getStatusCode());
84         }
85         System.err.println(post.getResponseBodyAsString());
86         
87     }
88 }
89
Popular Tags