View Javadoc

1   /*
2    * $Id: FunctionalTestCase.java 22046 2011-05-31 03:00:11Z dfeist $
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.Flow;
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          super();
43          // A functional test case that starts up the management context by default.
44          setStartContext(true);
45      }
46  
47      @Override
48      protected ConfigurationBuilder getBuilder() throws Exception
49      {
50          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          final FlowConstruct flowConstruct = muleContext.getRegistry().lookupObject(serviceName);
65  
66          if (flowConstruct != null)
67          {
68              return getComponent(flowConstruct);
69          }
70          else
71          {
72              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          if (flowConstruct instanceof Service)
87          {
88              return getComponentObject(((Service) flowConstruct).getComponent());
89          }
90          else if (flowConstruct instanceof SimpleService)
91          {
92              return getComponentObject(((SimpleService) flowConstruct).getComponent());
93          }
94          else if (flowConstruct instanceof Flow)
95          {
96              Flow flow = (Flow)flowConstruct;
97              //Retrieve the first component
98              for (MessageProcessor processor : flow.getMessageProcessors())
99              {
100                 if(processor instanceof Component)
101                 {
102                     return getComponentObject(((Component) processor));
103                 }
104             }
105 
106         }
107             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         return (FunctionalTestComponent) getComponent(serviceName);
123     }
124 
125     protected FlowConstruct getFlowConstruct(String name) throws Exception
126     {
127         return muleContext.getRegistry().lookupFlowConstruct(name);
128     }
129     
130     protected String loadResourceAsString(String name) throws IOException
131     {
132         return IOUtils.getResourceAsString(name, getClass());
133     }
134 
135     protected InputStream loadResource(String name) throws IOException
136     {
137         return IOUtils.getResourceAsStream(name, getClass());
138     }
139 
140     private Object getComponentObject(Component component) throws Exception
141     {
142         if (component instanceof JavaComponent)
143         {
144             return ((AbstractJavaComponent) component).getObjectFactory().getInstance(muleContext);
145         }
146         else
147         {
148             fail("Component is not a JavaComponent and therefore has no component object instance");
149             return null;
150         }
151     }
152 }