View Javadoc

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