forked from gscokart/jbehave-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http://jira.codehaus.org/browse/JBEHAVE-754
- Loading branch information
1 parent
704fe07
commit d29353a
Showing
5 changed files
with
305 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,47 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jbehave</groupId> | ||
<artifactId>jbehave</artifactId> | ||
<version>3.7-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>jbehave-spring</artifactId> | ||
<name>JBehave Spring</name> | ||
<description>Extension of JBehave Core that supports Spring dependency injection</description> | ||
|
||
<properties> | ||
<spring.version>3.1.1.RELEASE</spring.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jbehave</groupId> | ||
<artifactId>jbehave-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${spring.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jbehave</groupId> | ||
<artifactId>jbehave</artifactId> | ||
<version>3.7-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>jbehave-spring</artifactId> | ||
<name>JBehave Spring</name> | ||
<description>Extension of JBehave Core that supports Spring dependency injection</description> | ||
|
||
<properties> | ||
<spring.version>3.1.1.RELEASE</spring.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jbehave</groupId> | ||
<artifactId>jbehave-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${spring.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib</artifactId> | ||
<version>2.2.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
117 changes: 69 additions & 48 deletions
117
...e-spring/src/main/java/org/jbehave/core/steps/spring/SpringApplicationContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,69 @@ | ||
package org.jbehave.core.steps.spring; | ||
|
||
import org.springframework.beans.factory.support.BeanDefinitionReader; | ||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.core.io.DefaultResourceLoader; | ||
import org.springframework.core.io.ResourceLoader; | ||
|
||
/** | ||
* Factory for Spring {@link ApplicationContext} using the specified resources | ||
*/ | ||
public class SpringApplicationContextFactory { | ||
|
||
private final ApplicationContext parent; | ||
private final ClassLoader classLoader; | ||
private final String[] resources; | ||
|
||
public SpringApplicationContextFactory(String... resources) { | ||
this(SpringApplicationContextFactory.class.getClassLoader(), resources); | ||
} | ||
|
||
public SpringApplicationContextFactory(ClassLoader classLoader, String... resources) { | ||
this(null, classLoader, resources); | ||
} | ||
|
||
public SpringApplicationContextFactory(ApplicationContext parent, ClassLoader classLoader, String... resources) { | ||
this.parent = parent; | ||
this.classLoader = classLoader; | ||
this.resources = resources; | ||
} | ||
|
||
public ConfigurableApplicationContext createApplicationContext() { | ||
// create application context | ||
GenericApplicationContext context = new GenericApplicationContext(parent); | ||
context.setClassLoader(classLoader); | ||
ResourceLoader resourceLoader = new DefaultResourceLoader(classLoader); | ||
context.setResourceLoader(resourceLoader); | ||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); | ||
for (String resource : resources) { | ||
reader.loadBeanDefinitions(resourceLoader.getResource(resource)); | ||
} | ||
context.refresh(); | ||
return context; | ||
} | ||
|
||
} | ||
package org.jbehave.core.steps.spring; | ||
|
||
import org.springframework.beans.factory.support.BeanDefinitionReader; | ||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.core.io.DefaultResourceLoader; | ||
import org.springframework.core.io.ResourceLoader; | ||
|
||
/** | ||
* Factory for Spring {@link ApplicationContext} using the specified resources | ||
*/ | ||
public class SpringApplicationContextFactory { | ||
|
||
private final ApplicationContext parent; | ||
private final ClassLoader classLoader; | ||
private final String[] resources; | ||
|
||
public SpringApplicationContextFactory(String... resources) { | ||
|
||
this(SpringApplicationContextFactory.class.getClassLoader(), resources); | ||
} | ||
|
||
public SpringApplicationContextFactory(ClassLoader classLoader, String... resources) { | ||
|
||
this(null, classLoader, resources); | ||
} | ||
|
||
public SpringApplicationContextFactory(ApplicationContext parent, ClassLoader classLoader, String... resources) { | ||
|
||
this.parent = parent; | ||
this.classLoader = classLoader; | ||
this.resources = resources; | ||
} | ||
|
||
public ConfigurableApplicationContext createApplicationContext() { | ||
|
||
|
||
try { | ||
|
||
Class<?>[] classes = new Class<?>[resources.length]; | ||
|
||
for (int i = 0; i < resources.length; i++) { | ||
classes[i] = this.classLoader.loadClass(resources[i]); | ||
} | ||
|
||
AnnotationConfigApplicationContext result = new AnnotationConfigApplicationContext(classes); | ||
result.setParent(parent); | ||
result.setClassLoader(classLoader); | ||
return result; | ||
} catch (ClassNotFoundException e) { | ||
// create application context | ||
GenericApplicationContext context = new GenericApplicationContext(parent); | ||
context.setClassLoader(classLoader); | ||
ResourceLoader resourceLoader = new DefaultResourceLoader(classLoader); | ||
context.setResourceLoader(resourceLoader); | ||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); | ||
for (String resource : resources) { | ||
reader.loadBeanDefinitions(resourceLoader.getResource(resource)); | ||
} | ||
context.refresh(); | ||
return context; | ||
} | ||
|
||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
jbehave-spring/src/test/java/org/jbehave/core/steps/spring/AnnotationStepsConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.jbehave.core.steps.spring; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AnnotationStepsConfiguration { | ||
|
||
@Bean | ||
public SpringStepsFactoryBehaviour.FooSteps fooSteps() { | ||
return new SpringStepsFactoryBehaviour.FooSteps(); | ||
} | ||
|
||
@Bean | ||
public SpringStepsFactoryBehaviour.AbstractSteps abstractSteps () { | ||
return new SpringStepsFactoryBehaviour.AbstractSteps() { | ||
|
||
}; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...c/test/java/org/jbehave/core/steps/spring/AnnotationStepsWithDependencyConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.jbehave.core.steps.spring; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AnnotationStepsWithDependencyConfiguration { | ||
|
||
@Bean | ||
public SpringStepsFactoryBehaviour.FooStepsWithDependency fooSteps() { | ||
return new SpringStepsFactoryBehaviour.FooStepsWithDependency(42); | ||
} | ||
} |
Oops, something went wrong.