Coverage Report - org.jbehave.core.steps.spring.SpringApplicationContextFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringApplicationContextFactory
100%
26/26
100%
4/4
2
 
 1  
 package org.jbehave.core.steps.spring;
 2  
 
 3  
 import org.springframework.beans.factory.support.BeanDefinitionReader;
 4  
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
 5  
 import org.springframework.context.ApplicationContext;
 6  
 import org.springframework.context.ConfigurableApplicationContext;
 7  
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 8  
 import org.springframework.context.support.GenericApplicationContext;
 9  
 import org.springframework.core.io.DefaultResourceLoader;
 10  
 import org.springframework.core.io.ResourceLoader;
 11  
 
 12  
 /**
 13  
  * Factory for Spring {@link ApplicationContext} using the specified resources.
 14  
  * The resources can be expressed as:
 15  
  * <ol>
 16  
  * <li>Annotated class names</li>
 17  
  * <li>XML location paths</li>
 18  
  * </ol>
 19  
  * The context will be an instance of {@link AnnotationConfigApplicationContext}, 
 20  
  * if the resources are annotated class names, or
 21  
  * {@link GenericApplicationContext} otherwise.
 22  
  */
 23  
 public class SpringApplicationContextFactory {
 24  
 
 25  
     private final ApplicationContext parent;
 26  
     private final ClassLoader classLoader;
 27  
     private final String[] resources;
 28  
 
 29  
     public SpringApplicationContextFactory(String... resources) {
 30  5
         this(SpringApplicationContextFactory.class.getClassLoader(), resources);
 31  5
     }
 32  
 
 33  
     public SpringApplicationContextFactory(ClassLoader classLoader, String... resources) {
 34  11
         this(null, classLoader, resources);
 35  11
     }
 36  
 
 37  11
     public SpringApplicationContextFactory(ApplicationContext parent, ClassLoader classLoader, String... resources) {
 38  11
         this.parent = parent;
 39  11
         this.classLoader = classLoader;
 40  11
         this.resources = resources;
 41  11
     }
 42  
 
 43  
     /**
 44  
      * Creates a configurable application context from the resources provided.
 45  
      * The context will be an instance of
 46  
      * {@link AnnotationConfigApplicationContext}, if the resources are
 47  
      * annotated class names, or {@link GenericApplicationContext} otherwise.
 48  
      * 
 49  
      * @return A ConfigurableApplicationContext
 50  
      */
 51  
     public ConfigurableApplicationContext createApplicationContext() {
 52  
         try {
 53  
             // first try to create annotation config application context
 54  11
             Class<?>[] annotatedClasses = new Class<?>[resources.length];
 55  13
             for (int i = 0; i < resources.length; i++) {
 56  11
                 annotatedClasses[i] = this.classLoader.loadClass(resources[i]);
 57  
             }
 58  2
             AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(annotatedClasses);
 59  2
             context.setParent(parent);
 60  2
             context.setClassLoader(classLoader);
 61  2
             return context;
 62  9
         } catch (ClassNotFoundException e) {
 63  
             // create generic application context
 64  9
             GenericApplicationContext context = new GenericApplicationContext(parent);
 65  9
             context.setClassLoader(classLoader);
 66  9
             ResourceLoader resourceLoader = new DefaultResourceLoader(classLoader);
 67  9
             context.setResourceLoader(resourceLoader);
 68  9
             BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
 69  20
             for (String resource : resources) {
 70  13
                 reader.loadBeanDefinitions(resourceLoader.getResource(resource));
 71  
             }
 72  7
             context.refresh();
 73  7
             return context;
 74  
         }
 75  
 
 76  
     }
 77  
 
 78  
 }