1   /**
2    * 
3    */
4   package org.melati.servlet.test;
5   
6   import javax.servlet.ServletException;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   import javax.servlet.http.HttpSession;
10  
11  import org.melati.util.HttpServletRequestParameters;
12  
13  import com.mockobjects.constraint.Constraint;
14  import com.mockobjects.constraint.IsEqual;
15  import com.mockobjects.dynamic.Mock;
16  
17  /**
18   * @author timp
19   *
20   */
21  public class TemplateServletTest extends PoemServletTest {
22  
23    /**
24     * Constructor for TemplateServletTest.
25     * @param name
26     */
27    public TemplateServletTest(String name) {
28      super(name);
29    }
30  
31    /**
32     * @see PoemServletTest#setUp()
33     */
34    protected void setUp()
35        throws Exception {
36      super.setUp();
37    }
38  
39    /**
40     * @see PoemServletTest#tearDown()
41     */
42    protected void tearDown()
43        throws Exception {
44      super.tearDown();
45    }
46  
47  
48    /**
49     * @throws ServletException 
50     * @see org.melati.servlet.PoemServlet#getSysAdminName()
51     */
52    public void testGetSysAdminName() throws ServletException {
53      super.testGetSysAdminName();
54    }
55  
56    /**
57     * @throws ServletException 
58     * @see org.melati.servlet.PoemServlet#getSysAdminEmail()
59     */
60    public void testGetSysAdminEmail() throws ServletException {
61      super.testGetSysAdminEmail();
62    }
63  
64    /**
65     * @see org.melati.servlet.ConfigServlet#doGet(HttpServletRequest, HttpServletResponse)
66     */
67    public void testDoGetHttpServletRequestHttpServletResponse() throws Exception {
68      //doGetPost(); 
69    }
70    /**
71     * @see org.melati.servlet.ConfigServlet#doPost(HttpServletRequest, HttpServletResponse)
72     */
73    public void testDoPostHttpServletRequestHttpServletResponse() throws Exception {
74      doGetPost(); 
75  
76    }
77    
78    /**
79     * 
80     */
81    public void doGetPost() throws Exception {
82      MockServletRequest mockHttpServletRequest = new MockServletRequest();
83      MockServletResponse mockHttpServletResponse = new MockServletResponse(); 
84      Mock mockSession = new Mock(HttpSession.class);
85      MockServletConfig mockServletConfig = new MockServletConfig();
86      MockServletContext mockServletContext = new MockServletContext();
87  
88      setupMocks(mockHttpServletRequest, mockSession,  
89              mockServletContext);
90      
91      
92      org.melati.test.TemplateServletTest aServlet = 
93            new org.melati.test.TemplateServletTest();
94      aServlet.init(mockServletConfig);
95      aServlet.doPost(mockHttpServletRequest,  
96                      mockHttpServletResponse);
97      aServlet.destroy();
98        
99      assertTrue(mockHttpServletResponse.getWritten().indexOf("TemplateServlet Test") > 1);
100 
101 
102   }
103 
104 
105   private void setupMocks(MockServletRequest mockHttpServletRequest, 
106           Mock mockSession,
107           MockServletContext mockServletContext) {
108 
109     mockSession.expectAndReturn("getId", "1");
110     mockSession.expectAndReturn("getAttribute", "org.melati.login.HttpSessionAccessHandler.overlayParameters", 
111        null); 
112     mockSession.expectAndReturn("getAttribute", "org.melati.login.HttpSessionAccessHandler.user", null);
113     mockHttpServletRequest.setSession(mockSession.proxy());
114 
115     mockServletContext.expectAndReturn("getMajorVersion", "2"); 
116     mockServletContext.expectAndReturn("getMinorVersion", "3"); 
117     mockServletContext.expectAndReturn("hashCode", "17"); 
118     mockServletContext.expectAndReturn("toString", "mockServletContext"); 
119     mockServletContext.expectAndReturn("log", "MelatiConfigTest: init");
120     mockServletContext.expectAndReturn("getResource", "/WEB-INF/WebMacro.properties"); 
121     mockServletContext.expectAndReturn("getResource", "/WebMacro.properties"); 
122     mockServletContext.expectAndReturn("getInitParameterNames", ""); 
123     mockServletContext.expectAndReturn("hashCode", "17"); 
124     mockServletContext.expectAndReturn("toString", "mockServletContext"); 
125     mockServletContext.expectAndReturn("log", "WebMacro:LogFile\tNOTICE\t--- Log Started ---"); 
126     mockServletContext.expectAndReturn("hashCode", "17"); 
127     mockServletContext.expectAndReturn("log", "WebMacro:broker\tNOTICE\tLoaded settings from WebMacro.defaults, WebMacro.properties, (WAR file), (System Properties)");
128     mockServletContext.expectAndReturn("hashCode", "17"); 
129     mockServletContext.expectAndReturn("log", "WebMacro:wm\tNOTICE\tnew WebMacro(mockServletContext) v2.0b1");
130     mockServletContext.expectAndReturn("getResource", "/org/melati/test/TemplateServletTest.wm"); 
131     mockServletContext.expectAndReturn("log", "MelatiConfigTest: destroy");
132   }
133 
134   /**
135    * @see org.melati.servlet.TemplateServlet#error(org.melati.Melati, Exception)
136    */
137   public void testError() throws Exception {
138     MockServletRequest mockHttpServletRequest = new MockServletRequest();
139     MockServletResponse mockHttpServletResponse = new MockServletResponse(); 
140                    
141     
142     Mock mockSession = new Mock(HttpSession.class);
143     mockSession.expectAndReturn("getId", "1");
144     mockSession.expectAndReturn("getId", "1");
145 
146 
147     mockSession.expect("removeAttribute", "org.melati.login.HttpSessionAccessHandler.overlayParameters"); 
148     mockSession.expectAndReturn("getId", "1");
149     mockSession.expectAndReturn("getId", "1");
150     mockSession.expectAndReturn("getAttribute", "org.melati.login.HttpSessionAccessHandler.user", null); 
151     mockSession.expectAndReturn("getId", "1");
152     
153     mockSession.expect("setAttribute", new Constraint []  {new IsEqual("org.melati.login.Login.triggeringRequestParameters"),
154         new IsInstanceOf(HttpServletRequestParameters.class)});
155 
156     mockSession.expect("setAttribute", new Constraint []  {new IsEqual("org.melati.login.Login.triggeringException"),
157         new IsInstanceOf(org.melati.poem.AccessPoemException.class)});
158     
159 
160     
161     mockSession.expectAndReturn("getId", "1");
162     
163            
164     MockServletConfig mockServletConfig = new MockServletConfig();
165     mockServletConfig.setInitParameter("pathInfo", "melatitest/user/1");
166     mockServletConfig.setServletName("MelatiConfigTest");
167 
168     mockHttpServletRequest.setSession(mockSession.proxy());
169     mockSession.expectAndReturn("getId", "1");
170     mockSession.expectAndReturn("getId", "1");
171 
172     mockSession.expectAndReturn("getAttribute", "org.melati.login.HttpSessionAccessHandler.overlayParameters",
173         new HttpServletRequestParameters(mockHttpServletRequest));
174         
175     
176     ExceptionTemplateServlet aServlet = 
177           new ExceptionTemplateServlet();
178     aServlet.init(mockServletConfig);
179     aServlet.doPost( mockHttpServletRequest,  
180                      mockHttpServletResponse);
181     assertTrue(mockHttpServletResponse.getWritten().indexOf("Melati Error Template") > 0);
182     assertTrue(mockHttpServletResponse.getWritten().indexOf("java.lang.Exception: A problem") > 0);
183     aServlet.destroy();
184   }
185   
186 
187   /**
188    * Test that a templet on the classpath is used.
189    * @see org.melati.servlet.TemplateServlet#error(org.melati.Melati, Exception)
190    */
191   public void testErrorUsesClasspathTemplet() throws Exception {
192     MockServletRequest mockHttpServletRequest = new MockServletRequest();
193     MockServletResponse mockHttpServletResponse = new MockServletResponse(); 
194                    
195     
196     Mock mockSession = new Mock(HttpSession.class);
197     mockSession.expectAndReturn("getId", "1");
198     mockSession.expectAndReturn("getId", "1");
199 
200 
201     mockSession.expect("removeAttribute", "org.melati.login.HttpSessionAccessHandler.overlayParameters"); 
202     mockSession.expectAndReturn("getId", "1");
203     mockSession.expectAndReturn("getId", "1");
204     mockSession.expectAndReturn("getAttribute", "org.melati.login.HttpSessionAccessHandler.user", null); 
205     mockSession.expectAndReturn("getId", "1");
206     
207     mockSession.expect("setAttribute", new Constraint []  {new IsEqual("org.melati.login.Login.triggeringRequestParameters"),
208         new IsInstanceOf(HttpServletRequestParameters.class)});
209 
210     mockSession.expect("setAttribute", new Constraint []  {new IsEqual("org.melati.login.Login.triggeringException"),
211         new IsInstanceOf(org.melati.poem.AccessPoemException.class)});
212     
213 
214     
215     mockSession.expectAndReturn("getId", "1");
216     
217            
218     MockServletConfig mockServletConfig = new MockServletConfig();
219     mockServletConfig.setInitParameter("pathInfo", "melatitest/user/1");
220     mockServletConfig.setServletName("MelatiConfigTest");
221 
222     mockHttpServletRequest.setSession(mockSession.proxy());
223     mockSession.expectAndReturn("getId", "1");
224     mockSession.expectAndReturn("getId", "1");
225 
226     mockSession.expectAndReturn("getAttribute", "org.melati.login.HttpSessionAccessHandler.overlayParameters",
227         new HttpServletRequestParameters(mockHttpServletRequest));
228         
229 
230     ClasspathRenderedExceptionTemplateServlet aServlet = 
231           new ClasspathRenderedExceptionTemplateServlet();
232     aServlet.init(mockServletConfig);
233     aServlet.doPost(mockHttpServletRequest,  
234                     mockHttpServletResponse);
235     System.err.println(mockHttpServletResponse.getWritten());
236     assertTrue(mockHttpServletResponse.getWritten().indexOf("org.melati.servlet.test.ClasspathRenderedException: A problem") > 0);
237     assertTrue(mockHttpServletResponse.getWritten().indexOf("Rendered using template from classpath") > 0);
238     aServlet.destroy();
239   }
240 
241   /**
242    * @see org.melati.servlet.ConfigServlet#writeConnectionPendingException(PrintWriter, Exception)
243    */
244   public void testWriteConnectionPendingException() {
245 
246   }
247 
248   /**
249    * Test passback AccessPoemException handling.
250    */
251   public void testPassbackAccessPoemExceptionHandling() throws Exception {
252     MockServletRequest mockHttpServletRequest = new MockServletRequest();
253     MockServletResponse mockHttpServletResponse = new MockServletResponse(); 
254     Mock mockSession = new Mock(HttpSession.class);
255     MockServletConfig mockServletConfig = new MockServletConfig();
256     MockServletContext mockServletContext = new MockServletContext();
257 
258     setupMocks(mockHttpServletRequest, mockSession,  
259             mockServletContext);
260 
261     
262     mockServletContext.expectAndReturn("getResource", 
263             "/org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm"); 
264     mockServletContext.expectAndReturn("log","WebMacro:resource WARNING BrokerTemplateProvider: Template not found: " + 
265             "org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm");
266     
267     
268     mockHttpServletRequest.setParameter("passback", "true");
269     org.melati.test.TemplateServletTest aServlet = 
270           new org.melati.test.TemplateServletTest();
271     aServlet.init(mockServletConfig);
272     aServlet.doPost(mockHttpServletRequest,  
273                     mockHttpServletResponse);
274     aServlet.destroy();
275       
276     assertTrue(mockHttpServletResponse.getWritten().indexOf("[Access denied to Melati guest user]") != -1);
277     
278   }
279   /**
280    * Test propagation of AccessPoemException handling.
281    */
282   public void testPropagateAccessPoemExceptionHandling() throws Exception {
283     MockServletRequest mockHttpServletRequest = new MockServletRequest();
284     MockServletResponse mockHttpServletResponse = new MockServletResponse(); 
285     Mock mockSession = new Mock(HttpSession.class);
286     MockServletConfig mockServletConfig = new MockServletConfig();
287     MockServletContext mockServletContext = new MockServletContext();
288 
289     setupMocks(mockHttpServletRequest, mockSession,  
290             mockServletContext);
291 
292     
293     mockServletContext.expectAndReturn("getResource", 
294             "/org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm"); 
295     mockServletContext.expectAndReturn("log","WebMacro:resource WARNING BrokerTemplateProvider: Template not found: " + 
296             "org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm");
297     
298     mockSession.expect("setAttribute", new Constraint []  {new IsEqual("org.melati.login.Login.triggeringRequestParameters"),
299             new IsInstanceOf(HttpServletRequestParameters.class)});
300 
301      mockSession.expect("setAttribute", new Constraint []  {new IsEqual("org.melati.login.Login.triggeringException"),
302             new IsInstanceOf(org.melati.poem.AccessPoemException.class)});
303         
304 
305     
306     mockHttpServletRequest.setParameter("propagate", "true");
307     org.melati.test.TemplateServletTest aServlet = 
308           new org.melati.test.TemplateServletTest();
309     aServlet.init(mockServletConfig);
310     aServlet.doPost(mockHttpServletRequest,  
311                     mockHttpServletResponse);
312     aServlet.destroy();
313       
314     assertEquals(mockHttpServletResponse.getWritten(), "");
315     
316   }
317 }