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 static org.junit.Assert.assertEquals;
023import static org.junit.Assert.fail;
024import static org.mockito.Mockito.mock;
025
026import javax.servlet.ServletConfig;
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029
030import org.apache.turbine.modules.layouts.TestVelocityOnlyLayout;
031import org.apache.turbine.pipeline.PipelineData;
032import org.apache.turbine.test.BaseTestCase;
033import org.apache.turbine.util.RunData;
034import org.apache.turbine.util.TurbineConfig;
035import org.junit.AfterClass;
036import org.junit.Before;
037import org.junit.BeforeClass;
038import org.junit.Test;
039
040/**
041 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
042 */
043public class LayoutLoaderTest extends BaseTestCase
044{
045    private static TurbineConfig tc = null;
046    private ServletConfig config = null;
047    private HttpServletRequest request = null;
048    private HttpServletResponse response = null;
049
050    @BeforeClass
051    public static void init()
052    {
053        tc = new TurbineConfig(
054                ".",
055                "/conf/test/CompleteTurbineResources.properties");
056        tc.initialize();
057    }
058
059    @Before
060    public void setUpBefore() throws Exception
061    {
062        config = mock(ServletConfig.class);
063        request = getMockRequest();
064        response = mock(HttpServletResponse.class);
065    }
066
067    /*
068     * @see TestCase#tearDown()
069     */
070    @AfterClass
071    public static void tearDown() throws Exception
072    {
073        if (tc != null)
074        {
075            tc.dispose();
076        }
077    }
078
079    @Test
080    public void testPipelineDataContainsRunData()
081    {
082        try
083        {
084            RunData data = getRunData(request, response, config);
085            PipelineData pipelineData = data;
086            data.setLayout("TestVelocityOnlyLayout");
087            int numberOfCalls = TestVelocityOnlyLayout.numberOfCalls;
088            try
089            {
090                LayoutLoader.getInstance().exec(pipelineData, data.getLayout());
091            }
092            catch (Exception e)
093            {
094                e.printStackTrace();
095                fail("Should not have thrown an exception.");
096            }
097            assertEquals(numberOfCalls + 1, TestVelocityOnlyLayout.numberOfCalls);
098        }
099        catch (Exception e)
100        {
101            e.printStackTrace();
102            fail("Should not have thrown an exception.");
103        }
104    }
105
106    @Test
107    public void testDoBuildWithRunData()
108    {
109        try
110        {
111            RunData data = getRunData(request, response, config);
112            data.setLayout("TestVelocityOnlyLayout");
113            int numberOfCalls = TestVelocityOnlyLayout.numberOfCalls;
114            try
115            {
116                LayoutLoader.getInstance().exec(data, data.getLayout());
117            }
118            catch (Exception e)
119            {
120                e.printStackTrace();
121                fail("Should not have thrown an exception.");
122            }
123            assertEquals(numberOfCalls + 1, TestVelocityOnlyLayout.numberOfCalls);
124        }
125        catch (Exception e)
126        {
127            e.printStackTrace();
128            fail("Should not have thrown an exception.");
129        }
130    }
131}