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