ApplicationTest.java :  » EJB-Server » geronimo » plugins » org » apache » geronimo » jetty6 » Java Open Source

Java Open Source » EJB Server » geronimo 
geronimo » plugins » org » apache » geronimo » jetty6 » ApplicationTest.java
/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.geronimo.jetty6;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.servlet.SessionHandler;


/**
 * @version $Rev: 609571 $ $Date: 2008-01-07 03:11:35 -0800 (Mon, 07 Jan 2008) $
 */
public class ApplicationTest extends AbstractWebModuleTest {

    public void testApplication() throws Exception {
        JettyWebAppContext app = setUpAppContext(null, null, null, null, null, null, null, "war1/");

        setUpStaticContentServlet(app);

        HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:5678/test/hello.txt").openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
        assertEquals("Hello World", reader.readLine());
        connection.disconnect();
    }

    public void testApplicationWithSessionHandler() throws Exception {
        preHandlerFactory = new MockPreHandlerFactory();
        sessionHandlerFactory = new MockSessionHandlerFactory();
        JettyWebAppContext app = setUpAppContext(null, null, null, null, null, null, null, "war1/");

        setUpStaticContentServlet(app);

        HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:5678/test/hello.txt").openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
        assertEquals("Hello World", reader.readLine());
        connection.disconnect();
    }

    public class MockPreHandlerFactory implements PreHandlerFactory {
        public PreHandler createHandler() {
            return new AbstractPreHandler() {

                public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
                        throws IOException, ServletException {
                    next.handle(target, request, response, dispatch);
                }
                
            };
        }

    }

    public class MockSessionHandlerFactory implements SessionHandlerFactory {
        public SessionHandler createHandler(PreHandler preHandler) {
            return new SessionHandler();
        }
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.