View Javadoc
1   package org.melati.servlet.test;
2   
3   import java.io.PrintWriter;
4   
5   import javax.servlet.ServletConfig;
6   import javax.servlet.ServletException;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.melati.util.ConfigException;
11  
12  import junit.framework.TestCase;
13  
14  /**
15   * @author timp
16   */
17  public class ConfigServletTest extends TestCase {
18  
19    /**
20     * Constructor for ConfigServletTest.
21     * @param name
22     */
23    public ConfigServletTest(String name) {
24      super(name);
25      
26    }
27  
28    /**
29     * @see TestCase#setUp()
30     */
31    protected void setUp()
32        throws Exception {
33      super.setUp();
34    }
35  
36    /**
37     * @see TestCase#tearDown()
38     */
39    protected void tearDown()
40        throws Exception {
41      super.tearDown();
42    }
43  
44    /**
45     * @see org.melati.servlet.ConfigServlet#init(ServletConfig)
46     */
47    public void testInitServletConfig() {
48  
49    }
50  
51    /**
52     * @see org.melati.servlet.ConfigServlet#doGet(HttpServletRequest, HttpServletResponse)
53     */
54    public void testDoGetHttpServletRequestHttpServletResponse() {
55      MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); 
56      MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
57                     
58      mockHttpServletRequest.setPathInfo("melatitest/user/1"); 
59  
60      MockServletConfig mockServletConfig = new MockServletConfig();
61      org.melati.test.ConfigServletTest aServlet = 
62            new org.melati.test.ConfigServletTest();
63      try {
64        aServlet.init(mockServletConfig);
65        aServlet.doGet(mockHttpServletRequest,  
66                       mockHttpServletResponse);
67      } catch (ServletException e) {
68        e.printStackTrace();
69        fail(e.toString());
70      } 
71  
72      aServlet.destroy();
73  
74      String output = mockHttpServletResponse.getWritten();
75      assertTrue(output.toString().indexOf("<h2>ConfigServlet Test</h2>") != -1); 
76    }
77  
78    /**
79     * @see org.melati.servlet.ConfigServlet#doPost(HttpServletRequest, HttpServletResponse)
80     */
81    public void testDoPostHttpServletRequestHttpServletResponse() {
82      MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); 
83      MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
84                     
85      mockHttpServletRequest.setPathInfo("melatitest/user/1"); 
86      
87      MockServletConfig mockServletConfig = new MockServletConfig();
88      org.melati.test.ConfigServletTest aServlet = 
89            new org.melati.test.ConfigServletTest();
90      try {
91        aServlet.init(mockServletConfig);
92        aServlet.doPost(mockHttpServletRequest,  
93                       mockHttpServletResponse);
94      } catch (Exception e) {
95        e.printStackTrace();
96        fail();
97      } 
98                     
99      aServlet.destroy();
100 
101     String output = mockHttpServletResponse.getWritten();
102     assertTrue(output.toString().indexOf("<h2>ConfigServlet Test</h2>") != -1); 
103 
104   }
105 
106   /**
107    * @see org.melati.servlet.ConfigServlet#error(Melati, Exception)
108    */
109   public void testError() {
110     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); 
111     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
112                    
113     mockHttpServletRequest.setPathInfo("melatitest/user/1"); 
114     
115     MockServletConfig mockServletConfig = new MockServletConfig();
116     ErrorConfigServlet aServlet = 
117           new ErrorConfigServlet();
118     try {
119       aServlet.init(mockServletConfig);
120       aServlet.doPost(mockHttpServletRequest,  
121                      mockHttpServletResponse);
122     } catch (Exception e) {
123       e.printStackTrace();
124       fail();
125     } 
126                    
127     aServlet.destroy();
128 
129     String output = mockHttpServletResponse.getWritten();
130     assertTrue(output.toString().indexOf("You need the capability") != -1); 
131 
132 
133   }
134 
135   /**
136    * @see org.melati.servlet.ConfigServlet#error(Melati, Exception)
137    */
138   public void testConnectionPendingError() throws Exception {
139     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); 
140     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
141                    
142     mockHttpServletRequest.setPathInfo("melatitest/user/1"); 
143     
144     MockServletConfig mockServletConfig = new MockServletConfig();
145     DbPendingErrorConfigServlet aServlet = 
146           new DbPendingErrorConfigServlet();
147     aServlet.init(mockServletConfig);
148     aServlet.doPost(mockHttpServletRequest,  
149                    mockHttpServletResponse);
150     aServlet.destroy();
151                    
152     String output = mockHttpServletResponse.getWritten();
153     assertTrue(output.toString().indexOf("The database `testdb' is in the process of being initialized") != -1); 
154 
155   }
156   
157   /**
158    * Test expection thrown during initialisation.
159    */
160   public void testExceptionDuringInit() throws Exception {
161     MockHttpServletResponse response = new MockHttpServletResponse();
162     MockHttpServletRequest request = new MockHttpServletRequest();
163     MockServletConfig mockServletConfig = new MockServletConfig();
164     MelatiConfigExceptionConfigServlet aServlet = 
165       new MelatiConfigExceptionConfigServlet();
166     try {
167       aServlet.init(mockServletConfig);
168       aServlet.doPost(request,  
169                      response);
170       fail("Should have blown up");
171     } catch (ConfigException e) {
172       assertEquals("Pretend bug", e.getMessage());
173     }
174     aServlet.destroy();
175   }
176   
177 
178   /**
179    * @see org.melati.servlet.ConfigServlet#writeError(PrintWriter, Exception)
180    */
181   public void testWriteError() {
182 
183   }
184 
185   /**
186    * @see org.melati.servlet.ConfigServlet#writeConnectionPendingException(PrintWriter, Exception)
187    */
188   public void testWriteConnectionPendingException() {
189 
190   }
191 
192   /**
193    * @see org.melati.servlet.ConfigServlet#getSysAdminName()
194    */
195   public void testGetSysAdminName() {
196     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); 
197     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
198                    
199     mockHttpServletRequest.setPathInfo("melatitest/user/1"); 
200            
201     MockServletConfig mockServletConfig = new MockServletConfig();
202 
203     org.melati.test.ConfigServletTest aServlet = 
204           new org.melati.test.ConfigServletTest();
205     try {
206       aServlet.init(mockServletConfig);
207       assertEquals("nobody", aServlet.getSysAdminName());
208       aServlet.doPost(mockHttpServletRequest,  
209           mockHttpServletResponse);
210       aServlet.destroy();               
211     } catch (Exception e) {
212       e.printStackTrace();
213       fail();
214     } 
215   }
216 
217   /**
218    * @see org.melati.servlet.ConfigServlet#getSysAdminEmail()
219    */
220   public void testGetSysAdminEmail() {
221     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); 
222     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
223 
224     mockHttpServletRequest.setPathInfo("melatitest/user/1"); 
225 
226     MockServletConfig mockServletConfig = new MockServletConfig();
227 
228     org.melati.test.ConfigServletTest aServlet = 
229           new org.melati.test.ConfigServletTest();
230     try {
231       aServlet.init(mockServletConfig);
232       assertEquals("nobody@nobody.com", aServlet.getSysAdminEmail());
233       aServlet.doPost(mockHttpServletRequest,  
234           mockHttpServletResponse);
235       aServlet.destroy();               
236     } catch (Exception e) {
237       e.printStackTrace();
238       fail();
239     } 
240   }
241 
242 }