1   
2   /*
3    * Copyright 2001-2004 The Apache Software Foundation.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License")
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.turbine.modules;
19  
20  import java.util.Vector;
21  
22  import javax.servlet.ServletConfig;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import junit.framework.Assert;
26  
27  import org.apache.turbine.modules.layouts.TestVelocityOnlyLayout;
28  import org.apache.turbine.om.security.User;
29  import org.apache.turbine.pipeline.PipelineData;
30  import org.apache.turbine.services.template.TemplateService;
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  
37  import com.mockobjects.servlet.MockHttpServletResponse;
38  import com.mockobjects.servlet.MockServletConfig;
39  
40  
41  /***
42   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
43   */
44  public class LayoutLoaderTest extends BaseTestCase {
45  	private static TurbineConfig tc = null;
46  	private static TemplateService ts = null;
47  	private MockServletConfig config = null;
48  	private EnhancedMockHttpServletRequest request = null;
49  	private EnhancedMockHttpSession session = null;
50  	private HttpServletResponse response = null;
51  	private static ServletConfig sc = null;
52  	/*
53  	 * @see TestCase#setUp()
54  	 */
55  	protected void setUp() throws Exception {
56  		super.setUp();
57  		config = new MockServletConfig();
58  		config.setupNoParameters();
59  		request = new EnhancedMockHttpServletRequest();
60  		request.setupServerName("bob");
61  		request.setupGetProtocol("http");
62  		request.setupScheme("scheme");
63  		request.setupPathInfo("damn");
64  		request.setupGetServletPath("damn2");
65  		request.setupGetContextPath("wow");
66  		request.setupGetContentType("html/text");
67  		request.setupAddHeader("Content-type", "html/text");
68  		request.setupAddHeader("Accept-Language", "en-US");
69  		Vector v = new Vector();
70  		request.setupGetParameterNames(v.elements());
71  		session = new EnhancedMockHttpSession();
72  		response = new MockHttpServletResponse();
73  		session.setupGetAttribute(User.SESSION_KEY, null);
74  		request.setSession(session);
75  		sc = config;
76  		tc =
77  			new TurbineConfig(
78  				".",
79  				"/conf/test/CompleteTurbineResources.properties");
80  		tc.initialize();
81  	}
82  	/*
83  	 * @see TestCase#tearDown()
84  	 */
85  	protected void tearDown() throws Exception {
86  		super.tearDown();
87  		if (tc != null) {
88  			tc.dispose();
89  		}
90  	}
91  	/***
92  	 * Constructor for LayoutLoaderTest.
93  	 * @param arg0
94  	 */
95  	public LayoutLoaderTest(String arg0) throws Exception {
96  		super(arg0);
97  	}
98  
99  	public void testPipelineDataContainsRunData()
100 	{
101 	    try
102 	    {
103 		    RunData data = getRunData(request,response,config);
104             PipelineData pipelineData = data;
105 			data.setLayout("TestVelocityOnlyLayout");
106 			int numberOfCalls = TestVelocityOnlyLayout.numberOfCalls;
107 			try {
108 				LayoutLoader.getInstance().exec(pipelineData, data.getLayout());
109 			} catch (Exception e) {
110 			    e.printStackTrace();
111 			    Assert.fail("Should not have thrown an exception.");
112 			}
113 			assertEquals(numberOfCalls+1,TestVelocityOnlyLayout.numberOfCalls);
114 	    }
115 	    catch (Exception e)
116 	    {
117 	        e.printStackTrace();
118 	        Assert.fail("Should not have thrown an exception.");
119 	    }
120 	}
121 
122 	public void testDoBuildWithRunData()
123 	{
124 	    try
125 	    {
126 		    RunData data = getRunData(request,response,config);
127 			data.setLayout("TestVelocityOnlyLayout");
128 			int numberOfCalls = TestVelocityOnlyLayout.numberOfCalls;
129 			try {
130 				LayoutLoader.getInstance().exec(data, data.getLayout());
131 			} catch (Exception e) {
132 			    e.printStackTrace();
133 			    Assert.fail("Should not have thrown an exception.");
134 			}
135 			assertEquals(numberOfCalls+1,TestVelocityOnlyLayout.numberOfCalls);
136 	    }
137 	    catch (Exception e)
138 	    {
139 	        e.printStackTrace();
140 	        Assert.fail("Should not have thrown an exception.");
141 	    }
142 	}
143 
144 	
145 }