001package org.apache.turbine.pipeline;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import java.util.Vector;
025
026import javax.servlet.http.HttpServletResponse;
027
028import org.apache.turbine.om.security.User;
029import org.apache.turbine.test.BaseTestCase;
030import org.apache.turbine.test.EnhancedMockHttpServletRequest;
031import org.apache.turbine.test.EnhancedMockHttpSession;
032import org.apache.turbine.util.RunData;
033import org.apache.turbine.util.TurbineConfig;
034import org.apache.turbine.util.uri.URIConstants;
035import org.junit.AfterClass;
036import org.junit.Before;
037import org.junit.BeforeClass;
038import org.junit.Test;
039
040import com.mockobjects.servlet.MockHttpServletResponse;
041import com.mockobjects.servlet.MockServletConfig;
042
043import static org.junit.Assert.*;
044
045/**
046 * Tests TurbinePipeline.
047 *
048 * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
049 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
050 * @version $Id: DetermineTargetValveTest.java 1606111 2014-06-27 14:46:47Z gk $
051 */
052public class DetermineTargetValveTest extends BaseTestCase
053{
054    private static TurbineConfig tc = null;
055    private MockServletConfig config = null;
056    private EnhancedMockHttpServletRequest request = null;
057    private EnhancedMockHttpSession session = null;
058    private HttpServletResponse response = null;
059
060    
061    @BeforeClass
062    public static void init() {
063        tc = new TurbineConfig(
064                            ".",
065                            "/conf/test/CompleteTurbineResources.properties");
066        tc.initialize();
067    }
068
069    @Before
070    public void setUpBefore() throws Exception {
071        config = new MockServletConfig();
072        config.setupNoParameters();
073        request = new EnhancedMockHttpServletRequest();
074        request.setupServerName("bob");
075        request.setupGetProtocol("http");
076        request.setupScheme("scheme");
077        request.setupPathInfo("damn");
078        request.setupGetServletPath("damn2");
079        request.setupGetContextPath("wow");
080        request.setupGetContentType("html/text");
081        request.setupAddHeader("Content-type", "html/text");
082        request.setupAddHeader("Accept-Language", "en-US");
083
084        session = new EnhancedMockHttpSession();
085        response = new MockHttpServletResponse();
086
087        session.setupGetAttribute(User.SESSION_KEY, null);
088        request.setSession(session);
089    }
090
091    /**
092     * Tests the Valve.
093     */
094    @Test public void testScreenSet() throws Exception
095    {
096        Vector<String> v = new Vector<String>();
097        v.add(URIConstants.CGI_SCREEN_PARAM);
098        request.setupGetParameterNames(v.elements());
099
100        request.setupAddParameter(URIConstants.CGI_SCREEN_PARAM,"TestScreen");
101
102        RunData runData = getRunData(request,response,config);
103
104        Pipeline pipeline = new TurbinePipeline();
105        PipelineData pipelineData = runData;
106        DetermineTargetValve valve = new DetermineTargetValve();
107        pipeline.addValve(valve);
108
109        pipeline.invoke(pipelineData);
110        assertEquals("TestScreen",runData.getScreen());
111    }
112
113    @Test public void testScreenNotSet() throws Exception
114    {
115        Vector<String> v = new Vector<String>();
116        v.add(URIConstants.CGI_SCREEN_PARAM);
117        request.setupGetParameterNames(v.elements());
118
119        String screens[] = new String[1];
120        screens[0]=null;
121        request.setupAddParameter(URIConstants.CGI_SCREEN_PARAM,screens);
122
123        RunData runData = getRunData(request,response,config);
124
125        Pipeline pipeline = new TurbinePipeline();
126        PipelineData pipelineData = runData;
127
128        DetermineTargetValve valve = new DetermineTargetValve();
129        pipeline.addValve(valve);
130
131        pipeline.invoke(pipelineData);
132        assertEquals("",runData.getScreen());
133    }
134    
135    @AfterClass
136    public static void destroy() {
137        tc.dispose();
138    }
139}