Coverage Report - org.mule.tck.MuleTestUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleTestUtils
0%
0/71
0%
0/17
1.148
 
 1  
 /*
 2  
  * $Id: MuleTestUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.tck;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.config.PoolingProfile;
 15  
 import org.mule.impl.DefaultExceptionStrategy;
 16  
 import org.mule.impl.MuleDescriptor;
 17  
 import org.mule.impl.MuleEvent;
 18  
 import org.mule.impl.MuleMessage;
 19  
 import org.mule.impl.MuleSession;
 20  
 import org.mule.impl.RequestContext;
 21  
 import org.mule.impl.endpoint.MuleEndpoint;
 22  
 import org.mule.impl.endpoint.MuleEndpointURI;
 23  
 import org.mule.impl.model.seda.SedaComponent;
 24  
 import org.mule.impl.model.seda.SedaModel;
 25  
 import org.mule.tck.testmodels.mule.TestAgent;
 26  
 import org.mule.tck.testmodels.mule.TestCompressionTransformer;
 27  
 import org.mule.tck.testmodels.mule.TestConnector;
 28  
 import org.mule.umo.UMOComponent;
 29  
 import org.mule.umo.UMODescriptor;
 30  
 import org.mule.umo.UMOEvent;
 31  
 import org.mule.umo.UMOEventContext;
 32  
 import org.mule.umo.UMOException;
 33  
 import org.mule.umo.UMOSession;
 34  
 import org.mule.umo.UMOTransaction;
 35  
 import org.mule.umo.UMOTransactionFactory;
 36  
 import org.mule.umo.endpoint.UMOEndpoint;
 37  
 import org.mule.umo.endpoint.UMOEndpointURI;
 38  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 39  
 import org.mule.umo.manager.UMOManager;
 40  
 import org.mule.umo.model.UMOModel;
 41  
 import org.mule.umo.provider.UMOConnector;
 42  
 import org.mule.umo.provider.UMOMessageDispatcher;
 43  
 import org.mule.umo.provider.UMOMessageDispatcherFactory;
 44  
 import org.mule.umo.transformer.UMOTransformer;
 45  
 import org.mule.util.ClassUtils;
 46  
 import org.mule.util.StringUtils;
 47  
 
 48  
 import com.mockobjects.dynamic.Mock;
 49  
 
 50  
 import java.util.HashMap;
 51  
 
 52  
 /**
 53  
  * Utilities for creating test and Mock Mule objects
 54  
  */
 55  
 public final class MuleTestUtils
 56  
 {
 57  
 
 58  
     public static final String DEFAULT_MODEL_NAME = "main";
 59  
 
 60  
     /** Do not instanciate. */
 61  
     private MuleTestUtils ()
 62  0
     {
 63  
         // no-op
 64  0
     }
 65  
 
 66  
     public static UMOManager getManager(boolean disableAdminService) throws Exception
 67  
     {
 68  0
         if (MuleManager.isInstanciated())
 69  
         {
 70  0
             MuleManager.getInstance().dispose();
 71  
         }
 72  
 
 73  0
         UMOManager manager = MuleManager.getInstance();
 74  
 
 75  0
         if (disableAdminService)
 76  
         {
 77  0
             MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY);
 78  
         }
 79  
 
 80  0
         UMOModel model = new SedaModel();
 81  0
         model.setName(DEFAULT_MODEL_NAME);
 82  0
         manager.registerModel(model);
 83  0
         MuleManager.getConfiguration().setSynchronous(true);
 84  0
         MuleManager.getConfiguration().getPoolingProfile().setInitialisationPolicy(
 85  
             PoolingProfile.INITIALISE_NONE);
 86  
 
 87  0
         return manager;
 88  
     }
 89  
 
 90  
     public static UMOModel getDefaultModel() throws UMOException
 91  
     {
 92  0
         UMOModel m = MuleManager.getInstance().lookupModel(DEFAULT_MODEL_NAME);
 93  0
         if (m == null)
 94  
         {
 95  0
             m = new SedaModel();
 96  0
             m.setName(DEFAULT_MODEL_NAME);
 97  0
             MuleManager.getInstance().registerModel(m);
 98  
         }
 99  0
         return m;
 100  
 
 101  
     }
 102  
 
 103  
     public static UMOEndpoint getTestEndpoint(String name, String type) throws Exception
 104  
     {
 105  0
         UMOEndpoint endpoint = new MuleEndpoint();
 106  
         // need to build endpoint this way to avoid depenency to any endpoint jars
 107  0
         UMOConnector connector = null;
 108  0
         connector = (UMOConnector) ClassUtils.loadClass("org.mule.tck.testmodels.mule.TestConnector",
 109  0
             AbstractMuleTestCase.class).newInstance();
 110  
 
 111  0
         connector.setName("testConnector");
 112  0
         endpoint.setConnector(connector);
 113  0
         endpoint.setEndpointURI(new MuleEndpointURI("test://test"));
 114  0
         endpoint.setName(name);
 115  0
         endpoint.setType(type);
 116  0
         return endpoint;
 117  
     }
 118  
 
 119  
     public static UMOEvent getTestEvent(Object data) throws Exception
 120  
     {
 121  0
         UMOComponent component = getTestComponent(getTestDescriptor("string", String.class.getName()));
 122  0
         UMOSession session = getTestSession(component);
 123  0
         return new MuleEvent(new MuleMessage(data, new HashMap()), getTestEndpoint("test1",
 124  
             UMOEndpoint.ENDPOINT_TYPE_SENDER), session, true);
 125  
     }
 126  
 
 127  
     public static UMOEventContext getTestEventContext(Object data) throws Exception
 128  
     {
 129  
         try
 130  
         {
 131  0
             UMOEvent event = getTestEvent(data);
 132  0
             RequestContext.setEvent(event);
 133  0
             return RequestContext.getEventContext();
 134  
         }
 135  
         finally
 136  
         {
 137  0
             RequestContext.setEvent(null);
 138  
         }
 139  
     }
 140  
 
 141  
     public static UMOTransformer getTestTransformer()
 142  
     {
 143  0
         return new TestCompressionTransformer();
 144  
     }
 145  
 
 146  
     public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor) throws Exception
 147  
     {
 148  0
         UMOComponent component = getTestComponent(descriptor);
 149  
 
 150  0
         UMOSession session = getTestSession(component);
 151  
 
 152  0
         UMOEndpoint endpoint = getTestEndpoint("test1", UMOEndpoint.ENDPOINT_TYPE_SENDER);
 153  
 
 154  0
         return new MuleEvent(new MuleMessage(data, new HashMap()), endpoint, session, true);
 155  
     }
 156  
 
 157  
     public static UMOEvent getTestEvent(Object data, UMOImmutableEndpoint endpoint) throws Exception
 158  
     {
 159  0
         UMOSession session = getTestSession(getTestComponent(getTestDescriptor("string", String.class
 160  
             .getName())));
 161  0
         return new MuleEvent(new MuleMessage(data, new HashMap()), endpoint, session, true);
 162  
     }
 163  
 
 164  
     public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor, UMOImmutableEndpoint endpoint)
 165  
         throws UMOException
 166  
     {
 167  0
         UMOSession session = getTestSession(getTestComponent(descriptor));
 168  0
         UMOEvent event = new MuleEvent(new MuleMessage(data, new HashMap()), endpoint, session, true);
 169  0
         return event;
 170  
     }
 171  
 
 172  
     public static UMOSession getTestSession(UMOComponent component)
 173  
     {
 174  0
         return new MuleSession(component);
 175  
     }
 176  
 
 177  
     public static UMOSession getTestSession()
 178  
     {
 179  0
         return new MuleSession(null);
 180  
     }
 181  
 
 182  
     public static TestConnector getTestConnector()
 183  
     {
 184  0
         TestConnector testConnector = new TestConnector();
 185  0
         testConnector.setName("testConnector");
 186  0
         return testConnector;
 187  
     }
 188  
 
 189  
     public static UMOComponent getTestComponent(MuleDescriptor descriptor)
 190  
     {
 191  0
         return new SedaComponent(descriptor, new SedaModel());
 192  
     }
 193  
 
 194  
     public static MuleDescriptor getTestDescriptor(String name, String implementation) throws Exception
 195  
     {
 196  0
         MuleDescriptor descriptor = new MuleDescriptor();
 197  0
         descriptor.setExceptionListener(new DefaultExceptionStrategy());
 198  0
         descriptor.setName(name);
 199  0
         descriptor.setImplementation(implementation);
 200  0
         descriptor.setOutboundEndpoint(getTestEndpoint("test1", UMOEndpoint.ENDPOINT_TYPE_SENDER));
 201  0
         descriptor.initialise();
 202  
 
 203  
         //TODO descriptor.getPoolingProfile().setInitialisationPolicy(PoolingProfile.POOL_INITIALISE_NO_COMPONENTS);
 204  
 
 205  0
         return descriptor;
 206  
     }
 207  
 
 208  
 
 209  
     public static TestAgent getTestAgent()
 210  
     {
 211  0
         return new TestAgent();
 212  
     }
 213  
 
 214  
     public static Mock getMockSession()
 215  
     {
 216  0
         return new Mock(UMOSession.class, "umoSession");
 217  
     }
 218  
 
 219  
     public static Mock getMockMessageDispatcher()
 220  
     {
 221  0
         return new Mock(UMOMessageDispatcher.class, "umoMessageDispatcher");
 222  
     }
 223  
 
 224  
     public static Mock getMockMessageDispatcherFactory()
 225  
     {
 226  0
         return new Mock(UMOMessageDispatcherFactory.class, "umoMessageDispatcherFactory");
 227  
     }
 228  
 
 229  
     public static Mock getMockConnector()
 230  
     {
 231  0
         return new Mock(UMOConnector.class, "umoConnector");
 232  
     }
 233  
 
 234  
     public static Mock getMockEvent()
 235  
     {
 236  0
         return new Mock(UMOEvent.class, "umoEvent");
 237  
     }
 238  
 
 239  
     public static Mock getMockManager()
 240  
     {
 241  0
         return new Mock(MuleManager.class, "muleManager");
 242  
     }
 243  
 
 244  
     public static Mock getMockEndpoint()
 245  
     {
 246  0
         return new Mock(UMOEndpoint.class, "umoEndpoint");
 247  
     }
 248  
 
 249  
     public static Mock getMockEndpointURI()
 250  
     {
 251  0
         return new Mock(UMOEndpointURI.class, "umoEndpointUri");
 252  
     }
 253  
 
 254  
     public static Mock getMockDescriptor()
 255  
     {
 256  0
         return new Mock(UMODescriptor.class, "umoDescriptor");
 257  
     }
 258  
 
 259  
     public static Mock getMockTransaction()
 260  
     {
 261  0
         return new Mock(UMOTransaction.class, "umoTransaction");
 262  
     }
 263  
 
 264  
     public static Mock getMockTransactionFactory()
 265  
     {
 266  0
         return new Mock(UMOTransactionFactory.class, "umoTransactionFactory");
 267  
     }
 268  
 
 269  
 }