Coverage Report - org.mule.tck.junit4.FunctionalTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
FunctionalTestCase
0%
0/32
0%
0/14
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.tck.junit4;
 8  
 
 9  
 import static org.junit.Assert.fail;
 10  
 
 11  
 import org.mule.MessageExchangePattern;
 12  
 import org.mule.api.MuleEvent;
 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.FlowAssert;
 26  
 import org.mule.tck.functional.FunctionalTestComponent;
 27  
 import org.mule.util.IOUtils;
 28  
 
 29  
 import java.io.IOException;
 30  
 import java.io.InputStream;
 31  
 
 32  
 /**
 33  
  * A base test case for tests that initialize Mule using a configuration file. The
 34  
  * default configuration builder used is SpringXmlConfigurationBuilder. To use this
 35  
  * test case, ensure you have the mule-modules-builders JAR file on your classpath.
 36  
  * To use a different builder, just overload the <code>getBuilder()</code> method of
 37  
  * this class to return the type of builder you want to use with your test.
 38  
  */
 39  
 public abstract class FunctionalTestCase extends AbstractMuleContextTestCase
 40  
 {
 41  
     public FunctionalTestCase()
 42  
     {
 43  0
         super();
 44  
         // A functional test case that starts up the management context by default.
 45  0
         setStartContext(true);
 46  0
     }
 47  
 
 48  
     @Override
 49  
     protected ConfigurationBuilder getBuilder() throws Exception
 50  
     {
 51  0
         return new SpringXmlConfigurationBuilder(getConfigResources());
 52  
     }
 53  
 
 54  
     protected abstract String getConfigResources();
 55  
 
 56  
     /**
 57  
      * Returns an instance of the service's component object. Note that depending on
 58  
      * the type of ObjectFactory used for the component, this may create a new
 59  
      * instance of the object. If you plan to set properties on the returned object,
 60  
      * make sure your component is declared as a singleton, otherwise this will not
 61  
      * work.
 62  
      */
 63  
     protected Object getComponent(String serviceName) throws Exception
 64  
     {
 65  0
         final FlowConstruct flowConstruct = muleContext.getRegistry().lookupObject(serviceName);
 66  
 
 67  0
         if (flowConstruct != null)
 68  
         {
 69  0
             return getComponent(flowConstruct);
 70  
         }
 71  
         else
 72  
         {
 73  0
             throw new RegistrationException(MessageFactory.createStaticMessage("Service " + serviceName
 74  
                                                                                + " not found in Registry"));
 75  
         }
 76  
     }
 77  
 
 78  
     /**
 79  
      * Returns an instance of the service's component object. Note that depending on
 80  
      * the type of ObjectFactory used for the component, this may create a new
 81  
      * instance of the object. If you plan to set properties on the returned object,
 82  
      * make sure your component is declared as a singleton, otherwise this will not
 83  
      * work.
 84  
      */
 85  
     protected Object getComponent(FlowConstruct flowConstruct) throws Exception
 86  
     {
 87  0
         if (flowConstruct instanceof Service)
 88  
         {
 89  0
             return getComponentObject(((Service) flowConstruct).getComponent());
 90  
         }
 91  0
         else if (flowConstruct instanceof SimpleService)
 92  
         {
 93  0
             return getComponentObject(((SimpleService) flowConstruct).getComponent());
 94  
         }
 95  0
         else if (flowConstruct instanceof SimpleFlowConstruct)
 96  
         {
 97  0
             SimpleFlowConstruct flow = (SimpleFlowConstruct)flowConstruct;
 98  
             //Retrieve the first component
 99  0
             for (MessageProcessor processor : flow.getMessageProcessors())
 100  
             {
 101  0
                 if(processor instanceof Component)
 102  
                 {
 103  0
                     return getComponentObject(((Component) processor));
 104  
                 }
 105  
             }
 106  
 
 107  
         }
 108  0
         throw new RegistrationException(
 109  
                 MessageFactory.createStaticMessage("Can't get component from flow construct "
 110  
                                                    + flowConstruct.getName()));
 111  
     }
 112  
 
 113  
     /**
 114  
      * A convenience method to get a type-safe reference to the FunctionTestComponent
 115  
      *
 116  
      * @param serviceName service name as declared in the config
 117  
      * @return test component
 118  
      * @since 2.2
 119  
      * @see org.mule.tck.functional.FunctionalTestComponent
 120  
      */
 121  
     protected FunctionalTestComponent getFunctionalTestComponent(String serviceName) throws Exception
 122  
     {
 123  0
         return (FunctionalTestComponent) getComponent(serviceName);
 124  
     }
 125  
 
 126  
     protected FlowConstruct getFlowConstruct(String name) throws Exception
 127  
     {
 128  0
         return muleContext.getRegistry().lookupFlowConstruct(name);
 129  
     }
 130  
 
 131  
     protected String loadResourceAsString(String name) throws IOException
 132  
     {
 133  0
         return IOUtils.getResourceAsString(name, getClass());
 134  
     }
 135  
 
 136  
     protected InputStream loadResource(String name) throws IOException
 137  
     {
 138  0
         return IOUtils.getResourceAsStream(name, getClass());
 139  
     }
 140  
 
 141  
     private Object getComponentObject(Component component) throws Exception
 142  
     {
 143  0
         if (component instanceof JavaComponent)
 144  
         {
 145  0
             return ((AbstractJavaComponent) component).getObjectFactory().getInstance(muleContext);
 146  
         }
 147  
         else
 148  
         {
 149  0
             fail("Component is not a JavaComponent and therefore has no component object instance");
 150  0
             return null;
 151  
         }
 152  
     }
 153  
     
 154  
     protected void testFlow(String flowName) throws Exception
 155  
     {
 156  0
         testFlow(flowName, getTestEvent("data", MessageExchangePattern.ONE_WAY));
 157  0
     }
 158  
 
 159  
     protected void testFlow(String flowName, MuleEvent event) throws Exception
 160  
     {
 161  0
         SimpleFlowConstruct flow = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(flowName);
 162  0
         flow.process(event);
 163  0
         FlowAssert.verify(flowName);
 164  0
     }
 165  
 }