View Javadoc

1   package com.atlassian.johnson.filters;
2   
3   import com.atlassian.johnson.JohnsonEventContainer;
4   import junit.framework.TestCase;
5   import org.easymock.MockControl;
6   
7   import javax.servlet.http.HttpServletResponse;
8   import java.io.ByteArrayOutputStream;
9   import java.io.IOException;
10  import java.io.PrintWriter;
11  import java.util.Collection;
12  import java.util.Collections;
13  
14  /**
15   * Tests both the JohnsonSoapFilter and JohnsonXmlRpcFilter classes.
16   */
17  public class TestJohnsonXmlRpcAndSoapFilter extends TestCase
18  {
19  
20      private static final String ERROR_MSG = "TEST ERROR MESSAGE";
21      private static final int ERROR_CODE = 0;
22      private static final String NOT_SETUP_ERROR_MESG = "The application has not yet been setup.";
23  
24      protected void setUp() throws Exception
25      {
26          super.setUp();
27      }
28  
29      public void testHandleErrorSoapJohnsonFilter() throws IOException
30      {
31          JohnsonSoapFilter johnsonSoapFilter = new JohnsonSoapFilter()
32          {
33              protected String getStringForEvents(Collection events)
34              {
35                  return null;
36              }
37  
38              String buildSoapFault(String errorMessage)
39              {
40                  return getSoapFaultMessage(ERROR_MSG);
41              }
42          };
43          testHandleError(johnsonSoapFilter, getSoapFaultMessage(ERROR_MSG));
44      }
45  
46      public void testHandleErrorXmlRpcJohnsonFilter() throws IOException
47      {
48          JohnsonXmlRpcFilter johnsonXmlRpcFilter = new JohnsonXmlRpcFilter()
49          {
50  
51              protected String getStringForEvents(Collection events)
52              {
53                  return null;
54              }
55  
56              String buildXmlRpcErrorMessage(String error, int faultCode)
57              {
58                  return getXmlRpcFaultMessage(ERROR_MSG);
59              }
60          };
61          testHandleError(johnsonXmlRpcFilter, getXmlRpcFaultMessage(ERROR_MSG));
62      }
63  
64      public void testHandleError(AbstractJohnsonFilter filter, String message) throws IOException
65      {
66          MockControl responseMockCtrl = MockControl.createStrictControl(HttpServletResponse.class);
67  
68          HttpServletResponse response = (HttpServletResponse) responseMockCtrl.getMock();
69          response.setContentType(AbstractJohnsonFilter.TEXT_XML_UTF8_CONTENT_TYPE);
70          responseMockCtrl.setVoidCallable();
71          response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
72          responseMockCtrl.setVoidCallable();
73          response.getWriter();
74          ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
75          PrintWriter writer = new PrintWriter(baos);
76          responseMockCtrl.setReturnValue(writer);
77          responseMockCtrl.replay();
78  
79  
80          filter.handleError(new JohnsonEventContainer(){
81              public Collection getEvents()
82              {
83                  return Collections.EMPTY_LIST;
84              }
85          }, null, response);
86          responseMockCtrl.verify();
87          writer.flush();
88          assertEquals(message, baos.toString());
89      }
90  
91      public void testBuildXmlRpcErrorMessage()
92      {
93          JohnsonXmlRpcFilter johnsonXmlRpcFilter = new JohnsonXmlRpcFilter();
94          assertEquals(getXmlRpcFaultMessage(ERROR_MSG), johnsonXmlRpcFilter.buildXmlRpcErrorMessage(ERROR_MSG, ERROR_CODE));
95      }
96  
97      public void testHandleNotSetupXmlRpcJohnsonFilter() throws IOException
98      {
99          JohnsonXmlRpcFilter johnsonXmlRpcFilter = new JohnsonXmlRpcFilter();
100         testHandleNotSetup(johnsonXmlRpcFilter, getXmlRpcFaultMessage(NOT_SETUP_ERROR_MESG));
101     }
102 
103     public void testHandleNotSetupSoapJohnsonFilter() throws IOException
104     {
105         JohnsonSoapFilter filter = new JohnsonSoapFilter();
106         testHandleNotSetup(filter, getSoapFaultMessage(NOT_SETUP_ERROR_MESG));
107     }
108 
109     public void testHandleNotSetup(AbstractJohnsonFilter filter, String message) throws IOException
110     {
111         MockControl responseMockCtrl = MockControl.createStrictControl(HttpServletResponse.class);
112 
113         HttpServletResponse response = (HttpServletResponse) responseMockCtrl.getMock();
114         response.setContentType(AbstractJohnsonFilter.TEXT_XML_UTF8_CONTENT_TYPE);
115         responseMockCtrl.setVoidCallable();
116         response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
117         responseMockCtrl.setVoidCallable();
118         response.getWriter();
119         ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
120         PrintWriter writer = new PrintWriter(baos);
121         responseMockCtrl.setReturnValue(writer);
122         responseMockCtrl.replay();
123 
124         filter.handleNotSetup(null, response);
125         responseMockCtrl.verify();
126         writer.flush();
127         assertEquals(message, baos.toString());
128 
129     }
130 
131     private String getXmlRpcFaultMessage(String errorMessage)
132     {
133         return "<?xml version=\"1.0\"?>\n" +
134 
135                 "<methodResponse>\n" +
136                 "    <fault>\n" +
137                 "        <value>\n" +
138                 "            <struct>\n" +
139                 "                <member>\n" +
140                 "                    <name>faultString</name>\n" +
141                 "                    <value>" +
142                 errorMessage +
143                 "</value>\n" +
144                 "                </member>\n" +
145                 "                <member>\n" +
146                 "                    <name>faultCode</name>\n" +
147                 "                    <value>\n" +
148                 "                        <int>" +
149                 ERROR_CODE +
150                 "</int>\n" +
151                 "                    </value>\n" +
152                 "                </member>\n" +
153                 "            </struct>\n" +
154                 "        </value>\n" +
155                 "    </fault>\n" +
156                 "</methodResponse>";
157     }
158 
159     private String getSoapFaultMessage(String notSetupErrorMesg)
160     {
161         return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
162                 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
163                 "                  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
164                 "    <soapenv:Body>\n" +
165                 "        <soapenv:Fault>\n" +
166                 "            <faultcode>soapenv:Server</faultcode>\n" +
167                 "            <faultstring>" +
168                 notSetupErrorMesg +
169                 "            </faultstring>\n" +
170                 "        </soapenv:Fault>\n" +
171                 "    </soapenv:Body>\n" +
172                 "</soapenv:Envelope>";
173     }
174 }