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