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