View Javadoc

1   /*
2    * $Id: AxisServletWithSecurityTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
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.transport.http.HttpConnector;
19  import org.mule.transport.servlet.MuleReceiverServlet;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.servlet.ServletContextEvent;
25  import javax.servlet.ServletContextListener;
26  
27  import org.mortbay.jetty.Server;
28  import org.mortbay.jetty.servlet.Context;
29  import org.mortbay.jetty.servlet.ServletHolder;
30  
31  public class AxisServletWithSecurityTestCase extends DynamicPortTestCase
32  {
33      public static int HTTP_PORT = -1;
34  
35      private Server httpServer;
36  
37      @Override
38      protected String getConfigResources()
39      {
40          return "axis-servlet-security-config.xml";
41      }
42  
43      @Override
44      protected void doSetUp() throws Exception
45      {
46          HTTP_PORT = getPorts().get(0);
47          httpServer = new Server(HTTP_PORT);
48  
49          Context c = new Context(httpServer, "/", Context.SESSIONS);
50          c.addServlet(new ServletHolder(new MuleReceiverServlet()), "/services/*");
51          c.addEventListener(new ServletContextListener() {
52              public void contextInitialized(ServletContextEvent sce)
53              {
54                  sce.getServletContext().setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
55              }
56  
57              public void contextDestroyed(ServletContextEvent sce) { }
58          });
59  
60          httpServer.start();
61      }
62  
63      @Override
64      protected void doTearDown() throws Exception
65      {
66          if (httpServer != null)
67          {
68              httpServer.stop();
69              httpServer.destroy();
70          }
71      }
72  
73      public void testSecurityWithServletsUsingGet() throws Exception
74      {
75          Map<String, Object> props = new HashMap<String, Object>();
76          props.put(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
77          MuleClient client = new MuleClient(muleContext);
78          MuleMessage result = client.send("http://ross:ross@localhost:" + HTTP_PORT
79                                          + "/services/mycomponent?method=echo", "test", props);
80          
81          MessagingExceptionHandler exceptionListener = 
82              muleContext.getRegistry().lookupService("mycomponent").getExceptionListener();
83          assertTrue(exceptionListener instanceof UnitTestExceptionStrategy);
84          
85          UnitTestExceptionStrategy utExStrat = (UnitTestExceptionStrategy)exceptionListener;
86          assertEquals(1, utExStrat.getMessagingExceptions().size());
87          
88          assertNotNull(result);
89          // assertTrue(result.getPayload() instanceof byte[]);
90      }
91  
92      @Override
93      protected int getNumPortsToFind()
94      {
95          return 1;
96      }
97  }