- Revision
- 949
- Author
- mauro
- Date
- 2008-09-27 12:27:10 -0500 (Sat, 27 Sep 2008)
Log Message
Added Spring-based container implementation.
Modified Paths
- trunk/jbehave-core/pom.xml
- trunk/jbehave-core/src/behaviour/org/jbehave/container/pico/XMLPicoContainerBehaviour.java
- trunk/jbehave-core/src/java/org/jbehave/container/pico/AbstractPicoContainer.java
- trunk/pom.xml
Added Paths
- trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/
- trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/SpringContainerBehaviour.java
- trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/components.xml
- trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/no-components.xml
- trunk/jbehave-core/src/java/org/jbehave/container/InvalidContainerException.java
- trunk/jbehave-core/src/java/org/jbehave/container/spring/
- trunk/jbehave-core/src/java/org/jbehave/container/spring/SpringContainer.java
Diff
Modified: trunk/jbehave-core/pom.xml (948 => 949)
--- trunk/jbehave-core/pom.xml 2008-09-26 07:51:47 UTC (rev 948) +++ trunk/jbehave-core/pom.xml 2008-09-27 17:27:10 UTC (rev 949) @@ -20,6 +20,10 @@ <groupId>org.picocontainer.script</groupId> <artifactId>picocontainer-script-core</artifactId> </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-beans</artifactId> + </dependency> </dependencies> </project>
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/container/pico/XMLPicoContainerBehaviour.java (948 => 949)
--- trunk/jbehave-core/src/behaviour/org/jbehave/container/pico/XMLPicoContainerBehaviour.java 2008-09-26 07:51:47 UTC (rev 948) +++ trunk/jbehave-core/src/behaviour/org/jbehave/container/pico/XMLPicoContainerBehaviour.java 2008-09-27 17:27:10 UTC (rev 949) @@ -6,12 +6,12 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; -import java.util.NoSuchElementException; import org.jbehave.container.AComponent; import org.jbehave.container.AnotherComponent; import org.jbehave.container.ComponentNotFoundException; import org.jbehave.container.Container; +import org.jbehave.container.InvalidContainerException; import org.junit.Test; /** @@ -45,7 +45,7 @@ assertNotNull(container.getComponent(AnotherComponent.class)); } - @Test(expected = NoSuchElementException.class) + @Test(expected = InvalidContainerException.class) public void cannotGetComponentsWithInvalidClassLoader() throws MalformedURLException { new XMLPicoContainer("org/jbehave/container/pico/components.xml", new InvalidClassLoader()); } @@ -56,7 +56,7 @@ container.getComponent(AComponent.class); } - @Test(expected = NoSuchElementException.class) + @Test(expected = InvalidContainerException.class) public void cannotGetResourceWhenNotFound() { new XMLPicoContainer("inexistent-resource.xml"); }
Added: trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/SpringContainerBehaviour.java (0 => 949)
--- trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/SpringContainerBehaviour.java (rev 0) +++ trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/SpringContainerBehaviour.java 2008-09-27 17:27:10 UTC (rev 949) @@ -0,0 +1,74 @@ +package org.jbehave.container.spring; + +import static org.junit.Assert.assertNotNull; + +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; + +import org.jbehave.container.AComponent; +import org.jbehave.container.AnotherComponent; +import org.jbehave.container.ComponentNotFoundException; +import org.jbehave.container.Container; +import org.jbehave.container.InvalidContainerException; +import org.junit.Test; + +/** + * @author Mauro Talevi + */ +public class SpringContainerBehaviour { + + @Test + public void canGetComponentByKey() { + Container container = new SpringContainer("/org/jbehave/container/spring/components.xml"); + assertNotNull(container.getComponent(AComponent.class, "a-component")); + } + + @Test(expected = ComponentNotFoundException.class) + public void cannotGetComponentByInexistentKey() { + Container container = new SpringContainer("/org/jbehave/container/spring/components.xml"); + container.getComponent(AComponent.class, "inexistent-key"); + } + + @Test + public void canGetComponentByType() { + Container container = new SpringContainer("/org/jbehave/container/spring/components.xml"); + assertNotNull(container.getComponent(AnotherComponent.class)); + } + + //[EMAIL PROTECTED] + public void canGetComponentsWithCustomClassLoader() { + Container container = new SpringContainer("/org/jbehave/container/spring/components.xml", Thread + .currentThread().getContextClassLoader()); + assertNotNull(container.getComponent(AComponent.class)); + assertNotNull(container.getComponent(AnotherComponent.class)); + } + + @Test(expected = InvalidContainerException.class) + public void cannotGetComponentsWithInvalidClassLoader() throws MalformedURLException { + new SpringContainer("/org/jbehave/container/spring/components.xml", new InvalidClassLoader()); + } + + @Test(expected = ComponentNotFoundException.class) + public void cannotGetComponentWithNoneConfigured() { + Container container = new SpringContainer("/org/jbehave/container/spring/no-components.xml"); + container.getComponent(AComponent.class); + } + + @Test(expected = InvalidContainerException.class) + public void cannotGetResourceWhenNotFound() { + new SpringContainer("inexistent-resource.xml"); + } + + class InvalidClassLoader extends URLClassLoader { + public InvalidClassLoader() throws MalformedURLException { + super(new URL[] {}); + } + + public InputStream getResourceAsStream(String resource) { + return null; + } + } + +}
Added: trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/components.xml (0 => 949)
--- trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/components.xml (rev 0) +++ trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/components.xml 2008-09-27 17:27:10 UTC (rev 949) @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> + + <bean id="a-component" class="org.jbehave.container.AComponent"> + </bean> + + <bean id="org.jbehave.container.AnotherComponent" class="org.jbehave.container.AnotherComponent"> + </bean> + +</beans>
Added: trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/no-components.xml (0 => 949)
--- trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/no-components.xml (rev 0) +++ trunk/jbehave-core/src/behaviour/org/jbehave/container/spring/no-components.xml 2008-09-27 17:27:10 UTC (rev 949) @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> + +</beans>
Added: trunk/jbehave-core/src/java/org/jbehave/container/InvalidContainerException.java (0 => 949)
--- trunk/jbehave-core/src/java/org/jbehave/container/InvalidContainerException.java (rev 0) +++ trunk/jbehave-core/src/java/org/jbehave/container/InvalidContainerException.java 2008-09-27 17:27:10 UTC (rev 949) @@ -0,0 +1,15 @@ +package org.jbehave.container; + +/** + * Thrown when a container cannot be created + * + * @author Mauro Talevi + */ [EMAIL PROTECTED]("serial") +public class InvalidContainerException extends RuntimeException { + + public InvalidContainerException(String message) { + super(message); + } + +}
Modified: trunk/jbehave-core/src/java/org/jbehave/container/pico/AbstractPicoContainer.java (948 => 949)
--- trunk/jbehave-core/src/java/org/jbehave/container/pico/AbstractPicoContainer.java 2008-09-26 07:51:47 UTC (rev 948) +++ trunk/jbehave-core/src/java/org/jbehave/container/pico/AbstractPicoContainer.java 2008-09-27 17:27:10 UTC (rev 949) @@ -6,10 +6,10 @@ import java.io.InputStreamReader; import java.io.Reader; import java.util.List; -import java.util.NoSuchElementException; import org.jbehave.container.ComponentNotFoundException; import org.jbehave.container.Container; +import org.jbehave.container.InvalidContainerException; import org.picocontainer.ComponentAdapter; import org.picocontainer.PicoContainer; import org.picocontainer.script.ScriptedContainerBuilder; @@ -73,7 +73,7 @@ if (is == null) { String message = format("Resource {0} not found in ClassLoader {1}", resource, classLoader.getClass(), classLoader); - throw new NoSuchElementException(message); + throw new InvalidContainerException(message); } return new InputStreamReader(is); }
Added: trunk/jbehave-core/src/java/org/jbehave/container/spring/SpringContainer.java (0 => 949)
--- trunk/jbehave-core/src/java/org/jbehave/container/spring/SpringContainer.java (rev 0) +++ trunk/jbehave-core/src/java/org/jbehave/container/spring/SpringContainer.java 2008-09-27 17:27:10 UTC (rev 949) @@ -0,0 +1,67 @@ +package org.jbehave.container.spring; + +import static java.text.MessageFormat.format; + +import org.jbehave.container.ComponentNotFoundException; +import org.jbehave.container.Container; +import org.jbehave.container.InvalidContainerException; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.factory.xml.XmlBeanFactory; +import org.springframework.core.io.ClassPathResource; + +/** + * <p> + * Abstract implementation of Container which uses a Spring's BeanFactory as + * delegate container. + * </p> + * + * @author Mauro Talevi + */ +public class SpringContainer implements Container { + + private BeanFactory factory; + + public SpringContainer(String resource) { + this(resource, Thread.currentThread().getContextClassLoader()); + } + + public SpringContainer(String resource, ClassLoader classLoader) { + this.factory = buildBeanFactory(resource, classLoader); + } + + private BeanFactory buildBeanFactory(String resource, ClassLoader classLoader) { + try { + XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new XmlBeanFactory(new ClassPathResource( + resource, classLoader))); + reader.setBeanClassLoader(classLoader); + reader.loadBeanDefinitions(resource); + return (BeanFactory) reader.getBeanFactory(); + } catch (BeansException e) { + String message = format("Failed to create container for resource {0}", resource); + throw new InvalidContainerException(message); + } + } + + @SuppressWarnings("unchecked") + public <T> T getComponent(Class<T> type) { + try { + return (T) factory.getBean(type.getName()); + } catch (BeansException e) { + String message = format("No component registered in container of type {0}", type); + throw new ComponentNotFoundException(message); + } + } + + @SuppressWarnings("unchecked") + public <T> T getComponent(Class<T> type, Object key) { + try { + return (T) factory.getBean(key.toString()); + } catch (BeansException e) { + String message = format("No component registered in container of key {0}", key); + throw new ComponentNotFoundException(message); + } + } + +}
Modified: trunk/pom.xml (948 => 949)
--- trunk/pom.xml 2008-09-26 07:51:47 UTC (rev 948) +++ trunk/pom.xml 2008-09-27 17:27:10 UTC (rev 949) @@ -27,7 +27,6 @@ <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.7.0</version> - <optional>true</optional> <scope>provided</scope> </dependency> <dependency> @@ -35,11 +34,15 @@ <groupId>org.picocontainer.script</groupId> <artifactId>picocontainer-script-core</artifactId> <version>2.0</version> - <optional>true</optional> <scope>provided</scope> </dependency> - <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-beans</artifactId> + <version>2.5.5</version> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.2</version>
To unsubscribe from this list please visit:
