Files
alfresco-community-repo/source/test-java/org/alfresco/util/test/junitrules/ApplicationContextInit.java
Alan Davis 91eb2644ad Merged 5.2.N (5.2.1) to HEAD (5.2)
125781 rmunteanu: Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
      125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
         125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127808 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-06-03 16:40:56 +00:00

139 lines
4.8 KiB
Java

package org.alfresco.util.test.junitrules;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.alfresco.util.ApplicationContextHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.rules.ExternalResource;
import org.springframework.context.ApplicationContext;
/**
* This JUnit rule can be used to bring up a {@link ApplicationContext spring application context}.
* <p/>
* Example usage:
* <pre>
* public class YourTestClass
* {
* // Parameterless construction brings up the default Alfresco spring configuration.
* &#64;ClassRule public static final ApplicationContextInit APP_CONTEXT_RULE = new ApplicationContextInit();
* private static NodeService NODE_SERVICE;
*
* &#64;BeforeClass public static void initSpringServices() throws Exception
* {
* NODE_SERVICE = (NodeService) APP_CONTEXT_RULE.getApplicationContext().getBean("nodeService");
* }
* }
* </pre>
*
* @author Neil Mc Erlean
* @since Odin
*/
public class ApplicationContextInit extends ExternalResource
{
public static final String GLOBAL_INTEGRATION_TEST_CONFIG = "classpath:alfresco/test/global-integration-test-context.xml";
/**
* The locations for the application context configurations.
*/
private final String[] configLocations;
/**
* The initialised {@link ApplicationContext spring context}.
*/
private ApplicationContext appContext;
/**
* Construct a JUnit rule which will bring up a spring ApplicationContext based on the default Alfresco spring context.
*/
public ApplicationContextInit()
{
this(ApplicationContextHelper.CONFIG_LOCATIONS);
}
/**
* Construct a JUnit rule which will bring up an ApplicationContext based on the specified spring contexts.
*
* @param configLocations locations of spring contexts
*/
public ApplicationContextInit(String... configLocations)
{
List<String> requestedConfigs = new ArrayList<String>();
requestedConfigs.addAll(Arrays.asList(configLocations));
// No matter what spring contexts are provided in construction of this object, we always
// add the global test integration config to the end.
// Yes, this will mean that devs cannot override that context file, but it's almost empty anyway.
// We may have to change how this class handles this, but for now: keep it simple, s.
requestedConfigs.add(GLOBAL_INTEGRATION_TEST_CONFIG);
this.configLocations = requestedConfigs.toArray(new String[0]);
}
/**
* This factory method constructs a JUnit rule which will bring up an ApplicationContext consisting
* of the default Alfresco context with any additionConfigLocations appended. It is a convenient way to specify
* override contexts in test code.
*
* @param additionalConfigLocations addition config locations containing additional or overriding beans.
*/
public static ApplicationContextInit createStandardContextWithOverrides(String... additionalConfigLocations)
{
List<String> contexts = new ArrayList<String>();
// The defaults (currently only one)
for (String defaultConfigLocation: ApplicationContextHelper.CONFIG_LOCATIONS)
{
contexts.add(defaultConfigLocation);
}
// any user supplied extras
for (String additionalContext : additionalConfigLocations)
{
contexts.add(additionalContext);
}
String[] contextsAsArray = contexts.toArray(new String[0]);
return new ApplicationContextInit(contextsAsArray);
}
@Override protected void before()
{
log.debug("Initialising custom Spring Configuration: " + Arrays.asList(configLocations).toString());
appContext = ApplicationContextHelper.getApplicationContext(configLocations);
}
@Override protected void after()
{
// Intentionally empty. Do nothing.
}
/**
* Gets the configLocations as supplied to the code on construction.
*/
public List<String> getConfigLocations()
{
return Arrays.asList(configLocations);
}
/**
* Gets the ApplicationContext as initialised by the rule.
*/
public ApplicationContext getApplicationContext()
{
if (this.appContext == null)
{
// Chain order is wrong, try to help out by doing the @Before now
before();
}
return this.appContext;
}
private static final Log log = LogFactory.getLog(ApplicationContextInit.class);
}