Coverage Report - org.mule.tck.FunctionalTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
FunctionalTestCase
0%
0/25
0%
0/14
0
 
 1  
 /*
 2  
  * $Id: FunctionalTestCase.java 19350 2010-09-03 17:02:09Z rossmason $
 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.tck;
 12  
 
 13  
 import org.mule.api.component.Component;
 14  
 import org.mule.api.component.JavaComponent;
 15  
 import org.mule.api.config.ConfigurationBuilder;
 16  
 import org.mule.api.construct.FlowConstruct;
 17  
 import org.mule.api.processor.MessageProcessor;
 18  
 import org.mule.api.registry.RegistrationException;
 19  
 import org.mule.api.service.Service;
 20  
 import org.mule.component.AbstractJavaComponent;
 21  
 import org.mule.config.i18n.MessageFactory;
 22  
 import org.mule.config.spring.SpringXmlConfigurationBuilder;
 23  
 import org.mule.construct.SimpleFlowConstruct;
 24  
 import org.mule.construct.SimpleService;
 25  
 import org.mule.tck.functional.FunctionalTestComponent;
 26  
 import org.mule.util.IOUtils;
 27  
 
 28  
 import java.io.IOException;
 29  
 import java.io.InputStream;
 30  
 
 31  
 /**
 32  
  * A base tast case for tests that initialize Mule using a configuration file. The
 33  
  * default configuration builder used is SpringXmlConfigurationBuilder. To use this
 34  
  * test case, ensure you have the mule-modules-builders JAR file on your classpath.
 35  
  * To use a different builder, just overload the <code>getBuilder()</code> method of
 36  
  * this class to return the type of builder you want to use with your test.
 37  
  */
 38  
 public abstract class FunctionalTestCase extends AbstractMuleTestCase
 39  
 {
 40  
     public FunctionalTestCase()
 41  
     {
 42  0
         super();
 43  
         // A functional test case that starts up the management context by default.
 44  0
         setStartContext(true);
 45  0
     }
 46  
 
 47  
     @Override
 48  
     protected ConfigurationBuilder getBuilder() throws Exception
 49  
     {
 50  0
         return new SpringXmlConfigurationBuilder(getConfigResources());
 51  
     }
 52  
 
 53  
     protected abstract String getConfigResources();
 54  
 
 55  
     /**
 56  
      * Returns an instance of the service's component object. Note that depending on
 57  
      * the type of ObjectFactory used for the component, this may create a new
 58  
      * instance of the object. If you plan to set properties on the returned object,
 59  
      * make sure your component is declared as a singleton, otherwise this will not
 60  
      * work.
 61  
      */
 62  
     protected Object getComponent(String serviceName) throws Exception
 63  
     {
 64  0
         final FlowConstruct flowConstruct = muleContext.getRegistry().lookupObject(serviceName);
 65  
 
 66  0
         if (flowConstruct != null)
 67  
         {
 68  0
             return getComponent(flowConstruct);
 69  
         }
 70  
         else
 71  
         {
 72  0
             throw new RegistrationException(MessageFactory.createStaticMessage("Service " + serviceName
 73  
                                                                                + " not found in Registry"));
 74  
         }
 75  
     }
 76  
 
 77  
     /**
 78  
      * Returns an instance of the service's component object. Note that depending on
 79  
      * the type of ObjectFactory used for the component, this may create a new
 80  
      * instance of the object. If you plan to set properties on the returned object,
 81  
      * make sure your component is declared as a singleton, otherwise this will not
 82  
      * work.
 83  
      */
 84  
     protected Object getComponent(FlowConstruct flowConstruct) throws Exception
 85  
     {
 86  0
         if (flowConstruct instanceof Service)
 87  
         {
 88  0
             return getComponentObject(((Service) flowConstruct).getComponent());
 89  
         }
 90  0
         else if (flowConstruct instanceof SimpleService)
 91  
         {
 92  0
             return getComponentObject(((SimpleService) flowConstruct).getComponent());
 93  
         }
 94  0
         else if (flowConstruct instanceof SimpleFlowConstruct)
 95  
         {
 96  0
             SimpleFlowConstruct flow = (SimpleFlowConstruct)flowConstruct;
 97  
             //Retrieve the first component
 98  0
             for (MessageProcessor processor : flow.getMessageProcessors())
 99  
             {
 100  0
                 if(processor instanceof Component)
 101  
                 {
 102  0
                     return getComponentObject(((Component) processor));
 103  
                 }
 104  
             }
 105  
 
 106  
         }
 107  0
             throw new RegistrationException(
 108  
                 MessageFactory.createStaticMessage("Can't get component from flow construct "
 109  
                                                    + flowConstruct.getName()));
 110  
     }
 111  
 
 112  
     /**
 113  
      * A convenience method to get a type-safe reference to the FunctionTestComponent
 114  
      * 
 115  
      * @param serviceName service name as declared in the config
 116  
      * @return test component
 117  
      * @since 2.2
 118  
      * @see org.mule.tck.functional.FunctionalTestComponent
 119  
      */
 120  
     protected FunctionalTestComponent getFunctionalTestComponent(String serviceName) throws Exception
 121  
     {
 122  0
         return (FunctionalTestComponent) getComponent(serviceName);
 123  
     }
 124  
 
 125  
     protected String loadResourceAsString(String name) throws IOException
 126  
     {
 127  0
         return IOUtils.getResourceAsString(name, getClass());
 128  
     }
 129  
 
 130  
     protected InputStream loadResource(String name) throws IOException
 131  
     {
 132  0
         return IOUtils.getResourceAsStream(name, getClass());
 133  
     }
 134  
 
 135  
     private Object getComponentObject(Component component) throws Exception
 136  
     {
 137  0
         if (component instanceof JavaComponent)
 138  
         {
 139  0
             return ((AbstractJavaComponent) component).getObjectFactory().getInstance(muleContext);
 140  
         }
 141  
         else
 142  
         {
 143  0
             fail("Component is not a JavaComponent and therefore has no component object instance");
 144  0
             return null;
 145  
         }
 146  
     }
 147  
 }