View Javadoc

1   /*
2    * $Id: FunctionalTestCase.java 12182 2008-06-26 21:14:14Z jwheeler $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.JavaComponent;
14  import org.mule.api.config.ConfigurationBuilder;
15  import org.mule.api.registry.RegistrationException;
16  import org.mule.api.service.Service;
17  import org.mule.component.AbstractJavaComponent;
18  import org.mule.config.spring.SpringXmlConfigurationBuilder;
19  import org.mule.util.IOUtils;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  /**
25   * A base tast case for tests that initialize Mule using a configuration file. The
26   * default configuration builder used is SpringXmlConfigurationBuilder. To use this
27   * test case, ensure you have the mule-modules-builders JAR file on your classpath. 
28   * To use a different builder, just overload the <code>getBuilder()</code> method
29   * of this class to return the type of builder you want to use with your test. 
30   */
31  public abstract class FunctionalTestCase extends AbstractMuleTestCase
32  {
33      public FunctionalTestCase()
34      {
35          super();
36          // A functional test case that starts up the management context by default.
37          setStartContext(true);
38      }
39      
40      protected ConfigurationBuilder getBuilder() throws Exception
41      {
42          return new SpringXmlConfigurationBuilder(getConfigurationResources());
43      }
44  
45      //Delegate to an abstract method to ensure that FunctionalTestCases know they need to pass in config resources
46      protected String getConfigurationResources()
47      {
48          return getConfigResources();
49      }
50  
51      protected abstract String getConfigResources();
52      
53      protected Object getComponent(String serviceName) throws Exception
54      {
55          Service service = muleContext.getRegistry().lookupService(serviceName);
56          if (service != null)
57          {
58              return getComponent(service);
59          }
60          else
61          {
62              throw new RegistrationException("Service " + serviceName + " not found in Registry");
63          }
64      }
65      
66      protected Object getComponent(Service service) throws Exception
67      {
68          if (service.getComponent() instanceof JavaComponent)
69          {
70              return ((AbstractJavaComponent) service.getComponent()).getObjectFactory().getInstance();
71          }
72          else
73          {
74              fail("Componnent is not a JavaComponent and therefore has no component object instance");
75              return null;
76          }
77      }
78  
79      protected String loadResourceAsString(String name) throws IOException
80      {
81          return IOUtils.getResourceAsString(name, getClass());
82      }
83  
84      protected InputStream loadResource(String name) throws IOException
85      {
86          return IOUtils.getResourceAsStream(name, getClass());
87      }
88  }