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    
020    package org.apache.turbine.modules;
021    
022    import java.util.Vector;
023    
024    import javax.servlet.ServletConfig;
025    import javax.servlet.http.HttpServletResponse;
026    
027    import junit.framework.Assert;
028    
029    import org.apache.turbine.modules.layouts.TestVelocityOnlyLayout;
030    import org.apache.turbine.om.security.User;
031    import org.apache.turbine.pipeline.PipelineData;
032    import org.apache.turbine.services.template.TemplateService;
033    import org.apache.turbine.test.BaseTestCase;
034    import org.apache.turbine.test.EnhancedMockHttpServletRequest;
035    import org.apache.turbine.test.EnhancedMockHttpSession;
036    import org.apache.turbine.util.RunData;
037    import org.apache.turbine.util.TurbineConfig;
038    
039    import com.mockobjects.servlet.MockHttpServletResponse;
040    import com.mockobjects.servlet.MockServletConfig;
041    
042    
043    /**
044     * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
045     */
046    public class LayoutLoaderTest extends BaseTestCase {
047            private static TurbineConfig tc = null;
048            private static TemplateService ts = null;
049            private MockServletConfig config = null;
050            private EnhancedMockHttpServletRequest request = null;
051            private EnhancedMockHttpSession session = null;
052            private HttpServletResponse response = null;
053            private static ServletConfig sc = null;
054            /*
055             * @see TestCase#setUp()
056             */
057            protected void setUp() throws Exception {
058                    super.setUp();
059                    config = new MockServletConfig();
060                    config.setupNoParameters();
061                    request = new EnhancedMockHttpServletRequest();
062                    request.setupServerName("bob");
063                    request.setupGetProtocol("http");
064                    request.setupScheme("scheme");
065                    request.setupPathInfo("damn");
066                    request.setupGetServletPath("damn2");
067                    request.setupGetContextPath("wow");
068                    request.setupGetContentType("html/text");
069                    request.setupAddHeader("Content-type", "html/text");
070                    request.setupAddHeader("Accept-Language", "en-US");
071                    Vector v = new Vector();
072                    request.setupGetParameterNames(v.elements());
073                    session = new EnhancedMockHttpSession();
074                    response = new MockHttpServletResponse();
075                    session.setupGetAttribute(User.SESSION_KEY, null);
076                    request.setSession(session);
077                    sc = config;
078                    tc =
079                            new TurbineConfig(
080                                    ".",
081                                    "/conf/test/CompleteTurbineResources.properties");
082                    tc.initialize();
083            }
084            /*
085             * @see TestCase#tearDown()
086             */
087            protected void tearDown() throws Exception {
088                    super.tearDown();
089                    if (tc != null) {
090                            tc.dispose();
091                    }
092            }
093            /**
094             * Constructor for LayoutLoaderTest.
095             * @param arg0
096             */
097            public LayoutLoaderTest(String arg0) throws Exception {
098                    super(arg0);
099            }
100    
101            public void testPipelineDataContainsRunData()
102            {
103                try
104                {
105                        RunData data = getRunData(request,response,config);
106                PipelineData pipelineData = data;
107                            data.setLayout("TestVelocityOnlyLayout");
108                            int numberOfCalls = TestVelocityOnlyLayout.numberOfCalls;
109                            try {
110                                    LayoutLoader.getInstance().exec(pipelineData, data.getLayout());
111                            } catch (Exception e) {
112                                e.printStackTrace();
113                                Assert.fail("Should not have thrown an exception.");
114                            }
115                            assertEquals(numberOfCalls+1,TestVelocityOnlyLayout.numberOfCalls);
116                }
117                catch (Exception e)
118                {
119                    e.printStackTrace();
120                    Assert.fail("Should not have thrown an exception.");
121                }
122            }
123    
124            public void testDoBuildWithRunData()
125            {
126                try
127                {
128                        RunData data = getRunData(request,response,config);
129                            data.setLayout("TestVelocityOnlyLayout");
130                            int numberOfCalls = TestVelocityOnlyLayout.numberOfCalls;
131                            try {
132                                    LayoutLoader.getInstance().exec(data, data.getLayout());
133                            } catch (Exception e) {
134                                e.printStackTrace();
135                                Assert.fail("Should not have thrown an exception.");
136                            }
137                            assertEquals(numberOfCalls+1,TestVelocityOnlyLayout.numberOfCalls);
138                }
139                catch (Exception e)
140                {
141                    e.printStackTrace();
142                    Assert.fail("Should not have thrown an exception.");
143                }
144            }
145    
146    
147    }