1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.turbine.modules;
21  
22  import java.util.Vector;
23  
24  import javax.servlet.ServletConfig;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import junit.framework.Assert;
28  
29  import org.apache.turbine.modules.layouts.TestVelocityOnlyLayout;
30  import org.apache.turbine.om.security.User;
31  import org.apache.turbine.pipeline.PipelineData;
32  import org.apache.turbine.services.template.TemplateService;
33  import org.apache.turbine.test.BaseTestCase;
34  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
35  import org.apache.turbine.test.EnhancedMockHttpSession;
36  import org.apache.turbine.util.RunData;
37  import org.apache.turbine.util.TurbineConfig;
38  
39  import com.mockobjects.servlet.MockHttpServletResponse;
40  import com.mockobjects.servlet.MockServletConfig;
41  
42  
43  /**
44   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
45   */
46  public class LayoutLoaderTest extends BaseTestCase {
47  	private static TurbineConfig tc = null;
48  	private static TemplateService ts = null;
49  	private MockServletConfig config = null;
50  	private EnhancedMockHttpServletRequest request = null;
51  	private EnhancedMockHttpSession session = null;
52  	private HttpServletResponse response = null;
53  	private static ServletConfig sc = null;
54  	/*
55  	 * @see TestCase#setUp()
56  	 */
57  	protected void setUp() throws Exception {
58  		super.setUp();
59  		config = new MockServletConfig();
60  		config.setupNoParameters();
61  		request = new EnhancedMockHttpServletRequest();
62  		request.setupServerName("bob");
63  		request.setupGetProtocol("http");
64  		request.setupScheme("scheme");
65  		request.setupPathInfo("damn");
66  		request.setupGetServletPath("damn2");
67  		request.setupGetContextPath("wow");
68  		request.setupGetContentType("html/text");
69  		request.setupAddHeader("Content-type", "html/text");
70  		request.setupAddHeader("Accept-Language", "en-US");
71  		Vector v = new Vector();
72  		request.setupGetParameterNames(v.elements());
73  		session = new EnhancedMockHttpSession();
74  		response = new MockHttpServletResponse();
75  		session.setupGetAttribute(User.SESSION_KEY, null);
76  		request.setSession(session);
77  		sc = config;
78  		tc =
79  			new TurbineConfig(
80  				".",
81  				"/conf/test/CompleteTurbineResources.properties");
82  		tc.initialize();
83  	}
84  	/*
85  	 * @see TestCase#tearDown()
86  	 */
87  	protected void tearDown() throws Exception {
88  		super.tearDown();
89  		if (tc != null) {
90  			tc.dispose();
91  		}
92  	}
93  	/**
94  	 * Constructor for LayoutLoaderTest.
95  	 * @param arg0
96  	 */
97  	public LayoutLoaderTest(String arg0) throws Exception {
98  		super(arg0);
99  	}
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 }