View Javadoc
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   
10  /**
11   * @author timp
12   *
13   */
14  public class TemplateServletTest extends PoemServletTest {
15  
16    /**
17     * Constructor for TemplateServletTest.
18     * @param name
19     */
20    public TemplateServletTest(String name) {
21      super(name);
22    }
23  
24    /**
25     * @see PoemServletTest#setUp()
26     */
27    protected void setUp()
28        throws Exception {
29      super.setUp();
30    }
31  
32    /**
33     * @see PoemServletTest#tearDown()
34     */
35    protected void tearDown()
36        throws Exception {
37      super.tearDown();
38    }
39  
40  
41    /**
42     * @throws ServletException 
43     * @see org.melati.servlet.PoemServlet#getSysAdminName()
44     */
45    public void testGetSysAdminName() throws ServletException {
46      super.testGetSysAdminName();
47    }
48  
49    /**
50     * @throws ServletException 
51     * @see org.melati.servlet.PoemServlet#getSysAdminEmail()
52     */
53    public void testGetSysAdminEmail() throws ServletException {
54      super.testGetSysAdminEmail();
55    }
56  
57    /**
58     * @see org.melati.servlet.ConfigServlet#doGet(HttpServletRequest, HttpServletResponse)
59     */
60    public void testDoGetHttpServletRequestHttpServletResponse() throws Exception {
61      //doGetPost(); 
62    }
63    /**
64     * @see org.melati.servlet.ConfigServlet#doPost(HttpServletRequest, HttpServletResponse)
65     */
66    public void testDoPostHttpServletRequestHttpServletResponse() throws Exception {
67      doGetPost(); 
68  
69    }
70    
71    /**
72     * 
73     */
74    public void doGetPost() throws Exception {
75      MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
76      MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
77      MockServletConfig mockServletConfig = new MockServletConfig();
78  
79      org.melati.test.TemplateServletTest aServlet = 
80            new org.melati.test.TemplateServletTest();
81      aServlet.init(mockServletConfig);
82      aServlet.doPost(mockHttpServletRequest,  
83                      mockHttpServletResponse);
84      aServlet.destroy();
85        
86      assertTrue(mockHttpServletResponse.getWritten().indexOf("TemplateServlet Test") > 1);
87  
88  
89    }
90  
91  
92    /**
93     * @see org.melati.servlet.TemplateServlet#error(org.melati.Melati, Exception)
94     */
95    public void testError() throws Exception {
96      MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
97      MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
98                     
99      
100     MockHttpSession mockSession = new MockHttpSession();
101            
102     MockServletConfig mockServletConfig = new MockServletConfig();
103     mockServletConfig.setInitParameter("pathInfo", "melatitest/user/1");
104     mockServletConfig.setServletName("MelatiConfigTest");
105 
106     mockHttpServletRequest.setSession(mockSession);
107     
108     ExceptionTemplateServlet aServlet = 
109           new ExceptionTemplateServlet();
110     aServlet.init(mockServletConfig);
111     aServlet.doPost( mockHttpServletRequest,  
112                      mockHttpServletResponse);
113     assertTrue(mockHttpServletResponse.getWritten().indexOf("Melati Error Template") > 0);
114     assertTrue(mockHttpServletResponse.getWritten().indexOf("java.lang.Exception: A problem") > 0);
115     aServlet.destroy();
116   }
117   
118 
119   /**
120    * Test that an error templet from the classpath is used.
121    * @see org.melati.servlet.TemplateServlet#error(org.melati.Melati, Exception)
122    */
123   public void testErrorUsesClasspathTemplet() throws Exception {
124     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
125     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
126 
127     MockHttpSession mockSession = new MockHttpSession();
128            
129     MockServletConfig mockServletConfig = new MockServletConfig();
130     mockServletConfig.setInitParameter("pathInfo", "melatitest/user/1");
131     mockServletConfig.setServletName("MelatiConfigTest");
132 
133     mockHttpServletRequest.setSession(mockSession);
134     ClasspathRenderedExceptionTemplateServlet aServlet = 
135           new ClasspathRenderedExceptionTemplateServlet();
136     aServlet.init(mockServletConfig);
137     aServlet.doPost(mockHttpServletRequest,  
138                     mockHttpServletResponse);
139     assertTrue(mockHttpServletResponse.getWritten().indexOf("org.melati.servlet.test.ClasspathRenderedException: A problem") > 0);
140     assertTrue(mockHttpServletResponse.getWritten().indexOf("Rendered using template from classpath") > 0);
141     aServlet.destroy();
142   }
143 
144   /**
145    * Test passback AccessPoemException handling.
146    */
147   public void testPassbackAccessPoemExceptionHandling() throws Exception {
148     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
149     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
150     MockServletConfig mockServletConfig = new MockServletConfig();
151     MockServletContext mockServletContext = new MockServletContext();
152 
153     
154     mockServletContext.expectAndReturn("getResource", 
155             "/org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm"); 
156     mockServletContext.expectAndReturn("log","WebMacro:resource WARNING BrokerTemplateProvider: Template not found: " + 
157             "org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm");
158     
159     
160     mockHttpServletRequest.setParameter("passback", "true");
161     org.melati.test.TemplateServletTest aServlet = 
162           new org.melati.test.TemplateServletTest();
163     aServlet.init(mockServletConfig);
164     aServlet.doPost(mockHttpServletRequest,  
165                     mockHttpServletResponse);
166     aServlet.destroy();
167       
168     assertTrue(mockHttpServletResponse.getWritten().indexOf("[Access denied to Melati guest user]") != -1);
169     
170   }
171   /**
172    * Test propagation of AccessPoemException handling.
173    */
174   public void testPropagateAccessPoemExceptionHandling() throws Exception {
175     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
176     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
177     MockServletConfig mockServletConfig = new MockServletConfig();
178     MockServletContext mockServletContext = new MockServletContext();
179 
180     
181     mockServletContext.expectAndReturn("getResource", 
182             "/org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm"); 
183     mockServletContext.expectAndReturn("log","WebMacro:resource WARNING BrokerTemplateProvider: Template not found: " + 
184             "org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm");
185     
186     
187     mockHttpServletRequest.setParameter("propagate", "true");
188     org.melati.test.TemplateServletTest aServlet = 
189           new org.melati.test.TemplateServletTest();
190     aServlet.init(mockServletConfig);
191     aServlet.doPost(mockHttpServletRequest,  
192                     mockHttpServletResponse);
193     aServlet.destroy();
194       
195     assertEquals(mockHttpServletResponse.getWritten(), "");
196     
197   }
198 }