View Javadoc

1   /*
2    * $Id: AxisServletWithSecurityTestCase.java 19841 2010-10-05 23:17:20Z dzapata $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.soap.axis;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.exception.MessagingExceptionHandler;
16  import org.mule.module.client.MuleClient;
17  import org.mule.tck.DynamicPortTestCase;
18  import org.mule.tck.FunctionalTestCase;
19  import org.mule.transport.http.HttpConnector;
20  import org.mule.transport.servlet.MuleReceiverServlet;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.servlet.ServletContextEvent;
26  import javax.servlet.ServletContextListener;
27  
28  import org.mortbay.jetty.Server;
29  import org.mortbay.jetty.servlet.Context;
30  import org.mortbay.jetty.servlet.ServletHolder;
31  
32  public class AxisServletWithSecurityTestCase extends DynamicPortTestCase
33  {
34      public static int HTTP_PORT = -1;
35  
36      private Server httpServer;
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "axis-servlet-security-config.xml";
42      }
43  
44      @Override
45      protected void doSetUp() throws Exception
46      {
47          HTTP_PORT = getPorts().get(0);
48          httpServer = new Server(HTTP_PORT);
49  
50          Context c = new Context(httpServer, "/", Context.SESSIONS);
51          c.addServlet(new ServletHolder(new MuleReceiverServlet()), "/services/*");
52          c.addEventListener(new ServletContextListener() {
53              public void contextInitialized(ServletContextEvent sce)
54              {
55                  sce.getServletContext().setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
56              }
57  
58              public void contextDestroyed(ServletContextEvent sce) { }
59          });
60  
61          httpServer.start();
62      }
63  
64      @Override
65      protected void doTearDown() throws Exception
66      {
67          if (httpServer != null)
68          {
69              httpServer.stop();
70              httpServer.destroy();
71          }
72      }
73  
74      public void testSecurityWithServletsUsingGet() throws Exception
75      {
76          Map<String, Object> props = new HashMap<String, Object>();
77          props.put(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
78          MuleClient client = new MuleClient(muleContext);
79          MuleMessage result = client.send("http://ross:ross@localhost:" + HTTP_PORT
80                                          + "/services/mycomponent?method=echo", "test", props);
81          
82          MessagingExceptionHandler exceptionListener = 
83              muleContext.getRegistry().lookupService("mycomponent").getExceptionListener();
84          assertTrue(exceptionListener instanceof UnitTestExceptionStrategy);
85          
86          UnitTestExceptionStrategy utExStrat = (UnitTestExceptionStrategy)exceptionListener;
87          assertEquals(1, utExStrat.getMessagingExceptions().size());
88          
89          assertNotNull(result);
90          // assertTrue(result.getPayload() instanceof byte[]);
91      }
92  
93      @Override
94      protected int getNumPortsToFind()
95      {
96          return 1;
97      }
98  }