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 a templet on 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     
128     MockHttpSession mockSession = new MockHttpSession();
129            
130     MockServletConfig mockServletConfig = new MockServletConfig();
131     mockServletConfig.setInitParameter("pathInfo", "melatitest/user/1");
132     mockServletConfig.setServletName("MelatiConfigTest");
133 
134     mockHttpServletRequest.setSession(mockSession);
135     ClasspathRenderedExceptionTemplateServlet aServlet = 
136           new ClasspathRenderedExceptionTemplateServlet();
137     aServlet.init(mockServletConfig);
138     aServlet.doPost(mockHttpServletRequest,  
139                     mockHttpServletResponse);
140     System.err.println(mockHttpServletResponse.getWritten());
141     assertTrue(mockHttpServletResponse.getWritten().indexOf("org.melati.servlet.test.ClasspathRenderedException: A problem") > 0);
142     assertTrue(mockHttpServletResponse.getWritten().indexOf("Rendered using template from classpath") > 0);
143     aServlet.destroy();
144   }
145 
146   /**
147    * @see org.melati.servlet.ConfigServlet#writeConnectionPendingException(PrintWriter, Exception)
148    */
149   public void testWriteConnectionPendingException() {
150 
151   }
152 
153   /**
154    * Test passback AccessPoemException handling.
155    */
156   public void testPassbackAccessPoemExceptionHandling() throws Exception {
157     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
158     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
159     MockServletConfig mockServletConfig = new MockServletConfig();
160     MockServletContext mockServletContext = new MockServletContext();
161 
162     
163     mockServletContext.expectAndReturn("getResource", 
164             "/org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm"); 
165     mockServletContext.expectAndReturn("log","WebMacro:resource WARNING BrokerTemplateProvider: Template not found: " + 
166             "org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm");
167     
168     
169     mockHttpServletRequest.setParameter("passback", "true");
170     org.melati.test.TemplateServletTest aServlet = 
171           new org.melati.test.TemplateServletTest();
172     aServlet.init(mockServletConfig);
173     aServlet.doPost(mockHttpServletRequest,  
174                     mockHttpServletResponse);
175     aServlet.destroy();
176       
177     assertTrue(mockHttpServletResponse.getWritten().indexOf("[Access denied to Melati guest user]") != -1);
178     
179   }
180   /**
181    * Test propagation of AccessPoemException handling.
182    */
183   public void testPropagateAccessPoemExceptionHandling() throws Exception {
184     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
185     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
186     MockServletConfig mockServletConfig = new MockServletConfig();
187     MockServletContext mockServletContext = new MockServletContext();
188 
189     
190     mockServletContext.expectAndReturn("getResource", 
191             "/org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm"); 
192     mockServletContext.expectAndReturn("log","WebMacro:resource WARNING BrokerTemplateProvider: Template not found: " + 
193             "org/melati/template/webmacro/templets/html/error/org.melati.template.TemplateEngineException.wm");
194     
195     
196     mockHttpServletRequest.setParameter("propagate", "true");
197     org.melati.test.TemplateServletTest aServlet = 
198           new org.melati.test.TemplateServletTest();
199     aServlet.init(mockServletConfig);
200     aServlet.doPost(mockHttpServletRequest,  
201                     mockHttpServletResponse);
202     aServlet.destroy();
203       
204     assertEquals(mockHttpServletResponse.getWritten(), "");
205     
206   }
207 }