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  import org.melati.Melati;
11  
12  import junit.framework.TestCase;
13  
14  
15  /**
16   * @author timp
17   *
18   */
19  public class PoemServletTest extends TestCase {
20  
21    /**
22     * Constructor for PoemServletTest.
23     * @param name
24     */
25    public PoemServletTest(String name) {
26      super(name);
27    }
28  
29    /**
30     * @see PoemTestCase#setUp()
31     */
32    protected void setUp()
33        throws Exception {
34      super.setUp();
35    }
36  
37    /**
38     * @see TestCase#tearDown()
39     */
40    protected void tearDown()
41        throws Exception {
42      super.tearDown();
43    }
44  
45    /**
46     * @throws ServletException 
47     * @see org.melati.servlet.PoemServlet#getSysAdminName()
48     */
49    public void testGetSysAdminName() throws ServletException {
50                     
51      MockServletConfig mockServletConfig = new MockServletConfig();
52      
53      org.melati.test.PoemServletTest aServlet = 
54            new org.melati.test.PoemServletTest();
55      aServlet.init(mockServletConfig);
56      assertEquals("nobody", aServlet.getSysAdminName());
57  
58      aServlet.destroy();
59    }
60  
61    /**
62     * @throws ServletException 
63     * @see org.melati.servlet.PoemServlet#getSysAdminEmail()
64     */
65    public void testGetSysAdminEmail() throws ServletException {
66      MockServletConfig mockServletConfig = new MockServletConfig();
67      org.melati.test.PoemServletTest aServlet = 
68            new org.melati.test.PoemServletTest();
69      aServlet.init(mockServletConfig);
70      assertEquals("nobody@nobody.com", aServlet.getSysAdminEmail());
71                     
72      aServlet.destroy();
73  
74    }
75  
76  
77    /**
78     * @see org.melati.servlet.PoemServlet#prePoemSession(Melati)
79     */
80    public void testPrePoemSession() {
81  
82    }
83  
84    /**
85     * @throws Exception 
86     * @see org.melati.servlet.ConfigServlet#doGet(HttpServletRequest, HttpServletResponse)
87     */
88    public void testDoGetHttpServletRequestHttpServletResponse() throws Exception {
89      MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
90      MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
91                     
92      mockHttpServletRequest.setPathInfo("/melatitest/user/1");
93      
94      mockHttpServletRequest.setHeader("Accept-Charset", "ISO-8859-1"); 
95      
96  
97      MockServletConfig mockServletConfig = new MockServletConfig();
98  
99      org.melati.test.HttpAuthenticationPoemServletTest aServlet = 
100           new org.melati.test.HttpAuthenticationPoemServletTest();
101     aServlet.init(mockServletConfig);
102     aServlet.doGet(mockHttpServletRequest,  
103                    mockHttpServletResponse);
104     String output = mockHttpServletResponse.getWritten();
105     assertTrue("Unexpected output (check org.melati.LogicalDatabase properties): " + output.toString(), 
106                 output.toString().indexOf("<h2>PoemServlet Test</h2>") != -1); 
107 
108     aServlet.destroy();
109   }
110 
111   /**
112    * @see org.melati.servlet.ConfigServlet#doPost(HttpServletRequest, HttpServletResponse)
113    */
114   public void testDoPostHttpServletRequestHttpServletResponse() throws Exception {
115     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
116     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
117                    
118     mockHttpServletRequest.setCharacterEncoding("ISO-8859-1"); 
119     mockHttpServletRequest.setPathInfo("/melatitest/user/1"); 
120     mockHttpServletRequest.setHeader("Accept-Charset", "ISO-8859-1"); 
121     
122            
123     MockServletConfig mockServletConfig = new MockServletConfig();
124 
125     org.melati.test.HttpAuthenticationPoemServletTest aServlet = 
126       new org.melati.test.HttpAuthenticationPoemServletTest();
127     aServlet.init(mockServletConfig);
128     aServlet.doPost(mockHttpServletRequest,  
129                     mockHttpServletResponse);
130     String output = mockHttpServletResponse.getWritten();
131     assertTrue("Unexpected output (check org.melati.LogicalDatabase properties): " + 
132             output.toString(), output.toString().indexOf("<h2>PoemServlet Test</h2>") != -1); 
133     aServlet.destroy();
134 
135   }
136 
137   /**
138    * @throws Exception 
139    * @see org.melati.servlet.PoemServlet#error(Melati, Exception)
140    */
141   public void testError() throws Exception {
142     MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
143     MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); 
144                    
145     MockHttpSession mockSession = new MockHttpSession();
146 
147     MockServletConfig mockServletConfig = new MockServletConfig();
148     mockServletConfig.setInitParameter("pathInfo", "melatitest/user/1");
149 
150     mockHttpServletRequest.setSession(mockSession);
151     
152 
153     ExceptionPoemServlet aServlet = 
154           new ExceptionPoemServlet();
155     aServlet.init(mockServletConfig);
156     aServlet.doPost(mockHttpServletRequest,  
157                     mockHttpServletResponse);
158     aServlet.destroy();
159                    
160     String output = mockHttpServletResponse.getWritten();
161     // Request gets redirected to login
162     assertTrue(output.toString().equals("")); 
163 
164   }
165 
166   /**
167    * Test logical database.
168    * 
169    */
170   public void testLDB() throws Exception {
171     MockHttpServletResponse response = new MockHttpServletResponse();
172     MockHttpServletRequest request = new MockHttpServletRequest();
173     MockServletConfig mockServletConfig = new MockServletConfig();
174     LDBPoemServlet aServlet = 
175       new LDBPoemServlet();
176     aServlet.init(mockServletConfig);
177     aServlet.doPost(request, response);
178     System.out.println(response.getWritten().toString());
179     aServlet.destroy();
180     assertTrue(response.getWritten().toString().indexOf("logicalDatabase = melatijunit") != -1);
181   }
182 
183 }