View Javadoc
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          super();
44          // A functional test case that starts up the management context by default.
45          setStartContext(true);
46      }
47  
48      @Override
49      protected ConfigurationBuilder getBuilder() throws Exception
50      {
51          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          final FlowConstruct flowConstruct = muleContext.getRegistry().lookupObject(serviceName);
66  
67          if (flowConstruct != null)
68          {
69              return getComponent(flowConstruct);
70          }
71          else
72          {
73              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          if (flowConstruct instanceof Service)
88          {
89              return getComponentObject(((Service) flowConstruct).getComponent());
90          }
91          else if (flowConstruct instanceof SimpleService)
92          {
93              return getComponentObject(((SimpleService) flowConstruct).getComponent());
94          }
95          else if (flowConstruct instanceof SimpleFlowConstruct)
96          {
97              SimpleFlowConstruct flow = (SimpleFlowConstruct)flowConstruct;
98              //Retrieve the first component
99              for (MessageProcessor processor : flow.getMessageProcessors())
100             {
101                 if(processor instanceof Component)
102                 {
103                     return getComponentObject(((Component) processor));
104                 }
105             }
106 
107         }
108         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         return (FunctionalTestComponent) getComponent(serviceName);
124     }
125 
126     protected FlowConstruct getFlowConstruct(String name) throws Exception
127     {
128         return muleContext.getRegistry().lookupFlowConstruct(name);
129     }
130 
131     protected String loadResourceAsString(String name) throws IOException
132     {
133         return IOUtils.getResourceAsString(name, getClass());
134     }
135 
136     protected InputStream loadResource(String name) throws IOException
137     {
138         return IOUtils.getResourceAsStream(name, getClass());
139     }
140 
141     private Object getComponentObject(Component component) throws Exception
142     {
143         if (component instanceof JavaComponent)
144         {
145             return ((AbstractJavaComponent) component).getObjectFactory().getInstance(muleContext);
146         }
147         else
148         {
149             fail("Component is not a JavaComponent and therefore has no component object instance");
150             return null;
151         }
152     }
153     
154     protected void testFlow(String flowName) throws Exception
155     {
156         testFlow(flowName, getTestEvent("data", MessageExchangePattern.ONE_WAY));
157     }
158 
159     protected void testFlow(String flowName, MuleEvent event) throws Exception
160     {
161         SimpleFlowConstruct flow = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(flowName);
162         flow.process(event);
163         FlowAssert.verify(flowName);
164     }
165 }