View Javadoc

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.http.HttpServletResponse;
25  
26  import static org.junit.Assert.*;
27  
28  import org.apache.turbine.modules.layouts.TestVelocityOnlyLayout;
29  import org.apache.turbine.om.security.User;
30  import org.apache.turbine.pipeline.PipelineData;
31  import org.apache.turbine.test.BaseTestCase;
32  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
33  import org.apache.turbine.test.EnhancedMockHttpSession;
34  import org.apache.turbine.util.RunData;
35  import org.apache.turbine.util.TurbineConfig;
36  import org.junit.AfterClass;
37  import org.junit.Before;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  
41  import com.mockobjects.servlet.MockHttpServletResponse;
42  import com.mockobjects.servlet.MockServletConfig;
43  
44  
45  /**
46   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
47   */
48  public class LayoutLoaderTest extends BaseTestCase {
49  	private static TurbineConfig tc = null;
50  	private MockServletConfig config = null;
51  	private EnhancedMockHttpServletRequest request = null;
52  	private EnhancedMockHttpSession session = null;
53  	private HttpServletResponse response = null;
54  
55      @BeforeClass
56      public static void init() {
57          tc = new TurbineConfig(
58                              ".",
59                              "/conf/test/CompleteTurbineResources.properties");
60          tc.initialize();
61      }
62  
63  	@Before
64  	public void setUpBefore() throws Exception {
65  		config = new MockServletConfig();
66  		config.setupNoParameters();
67  		request = new EnhancedMockHttpServletRequest();
68  		request.setupServerName("bob");
69  		request.setupGetProtocol("http");
70  		request.setupScheme("scheme");
71  		request.setupPathInfo("damn");
72  		request.setupGetServletPath("damn2");
73  		request.setupGetContextPath("wow");
74  		request.setupGetContentType("html/text");
75  		request.setupAddHeader("Content-type", "html/text");
76  		request.setupAddHeader("Accept-Language", "en-US");
77  		Vector<String> v = new Vector<String>();
78  		request.setupGetParameterNames(v.elements());
79  		session = new EnhancedMockHttpSession();
80  		response = new MockHttpServletResponse();
81  		session.setupGetAttribute(User.SESSION_KEY, null);
82  		request.setSession(session);
83  
84  	}
85  
86  	/*
87  	 * @see TestCase#tearDown()
88  	 */
89  	@AfterClass
90  	public static void tearDown() throws Exception {
91  		if (tc != null) {
92  			tc.dispose();
93  		}
94  	}
95  
96  	@Test
97  	public void testPipelineDataContainsRunData()
98  	{
99  	    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 }