Index: spring-testsuite/src/test/resources/applicationContext.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/test/resources/applicationContext.xml (revision ) +++ spring-testsuite/src/test/resources/applicationContext.xml (revision ) @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file Index: pom.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- pom.xml (revision ) +++ pom.xml (revision ) @@ -0,0 +1,218 @@ + + + + + 4.0.0 + + + org.jboss.arquillian.container.spring + arquillian-spring-parent + pom + Arquillian Container Spring + http://www.jboss.org + Arquillian Container Spring TestEnricher Aggregator + 1.0.0.Final-SNAPSHOT + + + arquillian-testenricher-spring + spring-testsuite + + + + + + 1.5 + 1.5 + + + + 1.0.0-cr-1 + 1.0.0-beta-5 + 1.1.0-beta-1 + + + 1 + 2.1.0.GA + 6.1.9 + 4.8.1 + 1.8.3 + 5.14.6 + + 3.1.1.RELEASE + + + + + jira + http://jira.jboss.com/jira/browse/ARQ + + + + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + scm:git:git://git@github.com:arquillian/arquillian-core.git + scm:git:ssh://github.com/arquillian/arquillian-core.git + git://github.com/arquillian/arquillian-core.git + + + + + + + javax.inject + javax.inject + ${version.javax.inject_javax.inject} + provided + + + org.jboss.shrinkwrap + shrinkwrap-api + ${version.shrinkwrap_shrinkwrap} + + + org.jboss.shrinkwrap + shrinkwrap-spi + ${version.shrinkwrap_shrinkwrap} + + + org.jboss.shrinkwrap + shrinkwrap-impl-base + ${version.shrinkwrap_shrinkwrap} + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-api + ${version.shrinkwrap_descriptors} + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-spi + ${version.shrinkwrap_descriptors} + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-impl + ${version.shrinkwrap_descriptors} + + + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-api-maven + ${version.shrinkwrap_resolver} + + + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-impl-maven + ${version.shrinkwrap_resolver} + + + + junit + junit + ${version.junit_junit} + test + + + org.testng + testng + ${version.testng_testng} + test + + + + org.mortbay.jetty + jetty-embedded + ${version.jetty_jetty} + test + + + org.mockito + mockito-all + ${version.mockito_all} + test + + + + org.springframework + spring-context + ${version.spring_framework} + + + + + + + + + maven-compiler-plugin + + ${maven.compiler.argument.source} + ${maven.compiler.argument.target} + + + + + maven-surefire-plugin + + true + false + true + + **/*TestCase.java + **/*TestSuite.java + + true + + + + maven-jar-plugin + + + + test-jar + + + + + + + org.codehaus.mojo + animal-sniffer-maven-plugin + 1.7 + + + signature-check + verify + + check + + + + + + org.codehaus.mojo.signature + java15 + 1.0 + + + + + + + + + SpringSource Repository + SpringSource Repository + http://repo.springsource.org/release + + + + + \ No newline at end of file Index: arquillian-testenricher-spring/src/main/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension (revision ) +++ arquillian-testenricher-spring/src/main/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension (revision ) @@ -0,0 +1,1 @@ +org.jboss.arquillian.testenricher.spring.client.SpringEnricherExtension \ No newline at end of file Index: arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/container/ApplicationContextProducer.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/container/ApplicationContextProducer.java (revision ) +++ arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/container/ApplicationContextProducer.java (revision ) @@ -0,0 +1,69 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.testenricher.spring.container; + +import org.jboss.arquillian.core.api.InstanceProducer; +import org.jboss.arquillian.core.api.annotation.ApplicationScoped; +import org.jboss.arquillian.core.api.annotation.Inject; +import org.jboss.arquillian.core.api.annotation.Observes; +import org.jboss.arquillian.test.spi.event.suite.BeforeSuite; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.logging.Logger; + +/** + * A producer that creates the instance of {@link ApplicationContext}. + * + * @author Jakub Narloch + * @version $Revision: $ + */ +public class ApplicationContextProducer { + + /** + * The logger used by this class. + */ + private static final Logger log = Logger.getLogger(ApplicationContextProducer.class.getName()); + + /** + * Producer proxy for {@link ApplicationContext}. + */ + @Inject + @ApplicationScoped + private InstanceProducer applicationContextProducer; + + /** + * Builds the application context before the test suite is being executed. + * + * @param suite the event + */ + public void buildApplicationContext(@Observes BeforeSuite suite) { + + // creates the application context + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); + + // set the application context to be shared among all tests + if (applicationContext != null) { + applicationContextProducer.set(applicationContext); + + log.fine("Successfully created application context"); + } else { + + log.warning("The application context could not be created"); + } + } +} Index: spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/impl/DefaultEmployeeRepository.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/impl/DefaultEmployeeRepository.java (revision ) +++ spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/impl/DefaultEmployeeRepository.java (revision ) @@ -0,0 +1,45 @@ +package org.jboss.arquillian.container.spring.repository.impl; + +import org.jboss.arquillian.container.spring.Employee; +import org.jboss.arquillian.container.spring.repository.EmployeeRepository; +import org.springframework.stereotype.Repository; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.List; + +/** + * The default implementation of {@link EmployeeRepository}. + * + * @author Jakub Narloch + */ +@Repository(value = "defaultEmployeeRepository") +public class DefaultEmployeeRepository implements EmployeeRepository { + + /** + *

Represents the list of employees.

+ */ + private List employees = new ArrayList(); + + @PostConstruct + private void init() { + + Employee employee; + + employee = new Employee(); + employee.setName("John Smith"); + employees.add(employee); + + employee = new Employee(); + employee.setName("Marty Smith"); + employees.add(employee); + } + + /** + * {@inheritDoc} + */ + public List getEmployees() { + + return employees; + } +} Index: spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/service/impl/DefaultEmployeeService.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/service/impl/DefaultEmployeeService.java (revision ) +++ spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/service/impl/DefaultEmployeeService.java (revision ) @@ -0,0 +1,34 @@ +package org.jboss.arquillian.container.spring.service.impl; + +import org.jboss.arquillian.container.spring.Employee; +import org.jboss.arquillian.container.spring.repository.EmployeeRepository; +import org.jboss.arquillian.container.spring.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * Default employee service. + * + * @author Jakub Narloch + */ +@Service +public class DefaultEmployeeService implements EmployeeService { + + /** + * Instance of {@link EmployeeRepository}. + */ + @Autowired + @Qualifier("defaultEmployeeRepository") + EmployeeRepository employeeRepository; + + /** + * {@inheritDoc} + */ + public List getEmployees() { + + return employeeRepository.getEmployees(); + } +} Index: arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringProtocolArchiveProcessor.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringProtocolArchiveProcessor.java (revision ) +++ arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringProtocolArchiveProcessor.java (revision ) @@ -0,0 +1,148 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.testenricher.spring.client; + +import org.jboss.arquillian.container.test.spi.TestDeployment; +import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor; +import org.jboss.arquillian.testenricher.spring.SpringEnricherConsts; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.DependencyResolvers; +import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyResolver; + +import java.io.File; + +/** + * SpringProtocolArchiveProcessor + * + * @author Jakub Narloch + * @version $Revision: $ + */ +public class SpringProtocolArchiveProcessor implements ProtocolArchiveProcessor { + + /** + * {@inheritDoc} + */ + public void process(TestDeployment testDeployment, Archive protocolArchive) { + + // if the application archive is an war or ear then add the spring dependencies into it + if (isEnterpriseArchive(testDeployment.getApplicationArchive()) || + isWebArchive(testDeployment.getApplicationArchive())) { + addSpringLibraries(testDeployment.getApplicationArchive()); + } else if (isEnterpriseArchive(protocolArchive) || isWebArchive(protocolArchive)) { + // otherwise try to add the required dependencies into the protocol archive + addSpringLibraries(protocolArchive); + } + } + + /** + * Returns whether the passed archive is an enterprise archive (EAR) + * + * @param archive the archive + * + * @return true if passed archive is an enterprise archive, false otherwise + */ + private boolean isEnterpriseArchive(Archive archive) { + return archive instanceof EnterpriseArchive; + } + + /** + * Returns whether the passed archive is an web archive (WAR) + * + * @param archive the archive + * + * @return true if passed archive is an web archive, false otherwise + */ + private boolean isWebArchive(Archive archive) { + return archive instanceof WebArchive; + } + + /** + * Adds the spring dependencies into the passed archive + * + * @param archive the archive + */ + private void addSpringLibraries(Archive archive) { + + File[] springLibraries = resolveSpringDependencies(); + + if (archive instanceof EnterpriseArchive) { + ((EnterpriseArchive) archive).addAsModules(springLibraries); + } else if (archive instanceof WebArchive) { + ((WebArchive) archive).addAsLibraries(springLibraries); + } else { + throw new RuntimeException("Unsupported archive format[" + archive.getClass().getSimpleName() + + ", " + archive.getName() + "] for Spring application. Please use WAR or EAR."); + } + } + + /** + * Resolves the spring dependencies using maven. + * + * @return the spring dependencies + */ + private File[] resolveSpringDependencies() { + + return resolveArtifact(SpringEnricherConsts.SPRING_ARTIFACT_NAME, SpringEnricherConsts.SPRING_ARTIFACT_VERSION); + } + + /** + * Resolves the given artifact in specified version with help of maven build system. + * + * @param artifact the artifact name + * @param version the artifact version + * + * @return the resolved files + */ + private File[] resolveArtifact(String artifact, String version) { + File[] artifacts = null; + try { + artifacts = resolveArtifact(artifact); + } catch (Exception e) { + artifacts = resolveArtifact(artifact + ":" + version); + } + return artifacts; + } + + /** + * Resolves the given artifact by it's name with help of maven build system. + * + * @param artifact the fully qualified artifact name + * + * @return the resolved files + */ + private File[] resolveArtifact(String artifact) { + MavenDependencyResolver mvnResolver = DependencyResolvers.use(MavenDependencyResolver.class); + + if (isMavenUsed()) { + mvnResolver.loadMetadataFromPom(SpringEnricherConsts.POM_XML); + } + + return mvnResolver.artifacts(artifact) + .resolveAsFiles(); + } + + /** + * Returns whether maven is being used in project. + * + * @return true if maven is being used in project, false otherwise + */ + private boolean isMavenUsed() { + return new File(SpringEnricherConsts.POM_XML).exists(); + } +} Index: spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/service/EmployeeService.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/service/EmployeeService.java (revision ) +++ spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/service/EmployeeService.java (revision ) @@ -0,0 +1,20 @@ +package org.jboss.arquillian.container.spring.service; + +import org.jboss.arquillian.container.spring.Employee; + +import java.util.List; + +/** + * Represents an employee service. + * + * @author Jakub Narloch + */ +public interface EmployeeService { + + /** + * Retrieves all employees. + * + * @return list of employees + */ + List getEmployees(); +} Index: arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/container/SpringEnricherRemoteExtension.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/container/SpringEnricherRemoteExtension.java (revision ) +++ arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/container/SpringEnricherRemoteExtension.java (revision ) @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.testenricher.spring.container; + +import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension; +import org.jboss.arquillian.test.spi.TestEnricher; +import org.jboss.arquillian.testenricher.spring.SpringEnricherConsts; +import org.jboss.arquillian.testenricher.spring.SpringInjectionEnricher; + +/** + * SpringEnricherRemoteExtension + * + * @author Jakub Narloch + * @version $Revision: $ + */ +public class SpringEnricherRemoteExtension implements RemoteLoadableExtension { + + /** + * {@inheritDoc} + */ + @Override + public void register(ExtensionBuilder builder) { + + // only load if Spring Application Context is in ClassPath + if (Validate.classExists(SpringEnricherConsts.APPLICATION_CONTEXT)) { + builder.service(TestEnricher.class, SpringInjectionEnricher.class) + .observer(ApplicationContextProducer.class); + } + } +} Index: spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/repository/impl/DefaultEmployeeRepositoryTestCase.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/repository/impl/DefaultEmployeeRepositoryTestCase.java (revision ) +++ spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/repository/impl/DefaultEmployeeRepositoryTestCase.java (revision ) @@ -0,0 +1,48 @@ +package org.jboss.arquillian.container.spring.repository.impl; + +import org.jboss.arquillian.container.spring.Employee; +import org.jboss.arquillian.container.spring.repository.EmployeeRepository; +import org.jboss.arquillian.container.spring.service.EmployeeService; +import org.jboss.arquillian.container.spring.service.impl.DefaultEmployeeService; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Tests the {@link DefaultEmployeeRepository} class. + */ +@RunWith(Arquillian.class) +public class DefaultEmployeeRepositoryTestCase { + + @Deployment + public static JavaArchive createTestArchive() { + return ShrinkWrap.create(JavaArchive.class, "spring-test.jar") + .addClasses(Employee.class, + EmployeeRepository.class, DefaultEmployeeRepository.class, NullEmployeeRepository.class) + .addAsResource(DefaultEmployeeRepositoryTestCase.class.getResource("/applicationContext.xml"), + "applicationContext.xml"); + } + + @Autowired + @Qualifier("defaultEmployeeRepository") + private EmployeeRepository employeeRepository; + + @Test + public void testGetEmployees() throws Exception { + + List result = employeeRepository.getEmployees(); + + assertNotNull("Method returned null list as result.", result); + assertEquals("Two employees were expected.", 2, result.size()); + } +} Index: arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringEnricherExtension.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringEnricherExtension.java (revision ) +++ arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringEnricherExtension.java (revision ) @@ -0,0 +1,48 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.testenricher.spring.client; + +import org.jboss.arquillian.container.test.spi.client.deployment.AuxiliaryArchiveAppender; +import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor; +import org.jboss.arquillian.core.spi.LoadableExtension; +import org.jboss.arquillian.test.spi.TestEnricher; +import org.jboss.arquillian.testenricher.spring.SpringEnricherConsts; +import org.jboss.arquillian.testenricher.spring.SpringInjectionEnricher; + +/** + * SpringEnricherExtension + * + * @author Jakub Narloch + * @version $Revision: $ + */ +public class SpringEnricherExtension implements LoadableExtension { + + /** + * {@inheritDoc} + */ + @Override + public void register(ExtensionBuilder builder) { + + // only load if Spring Application Context is in ClassPath + if (Validate.classExists(SpringEnricherConsts.APPLICATION_CONTEXT)) { + builder.service(AuxiliaryArchiveAppender.class, SpringEnricherArchiveAppender.class) + .service(ProtocolArchiveProcessor.class, SpringProtocolArchiveProcessor.class); + + builder.service(TestEnricher.class, SpringInjectionEnricher.class); + } + } +} Index: spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/EmployeeRepository.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/EmployeeRepository.java (revision ) +++ spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/EmployeeRepository.java (revision ) @@ -0,0 +1,20 @@ +package org.jboss.arquillian.container.spring.repository; + +import org.jboss.arquillian.container.spring.Employee; + +import java.util.List; + +/** + * Represents an employee repository. + * + * @author Jakub Narloch + */ +public interface EmployeeRepository { + + /** + * Retrieves all employees. + * + * @return list of employees + */ + List getEmployees(); +} Index: arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringEnricherArchiveAppender.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringEnricherArchiveAppender.java (revision ) +++ arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/client/SpringEnricherArchiveAppender.java (revision ) @@ -0,0 +1,45 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.testenricher.spring.client; + +import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension; +import org.jboss.arquillian.container.test.spi.client.deployment.CachedAuxilliaryArchiveAppender; +import org.jboss.arquillian.testenricher.spring.SpringInjectionEnricher; +import org.jboss.arquillian.testenricher.spring.container.SpringEnricherRemoteExtension; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; + +/** + * Spring Enricher Archive Appender. Adds all the required classes by this extension into a test archive. + * + * @author Jakub Narloch + * @version $Revision: $ + */ +public class SpringEnricherArchiveAppender extends CachedAuxilliaryArchiveAppender { + + /** + * {@inheritDoc} + */ + @Override + protected Archive buildArchive() { + return ShrinkWrap.create(JavaArchive.class, "arquillian-testenricher-spring.jar") + .addPackage(SpringInjectionEnricher.class.getPackage()) + .addPackage(SpringEnricherRemoteExtension.class.getPackage()) + .addAsServiceProvider(RemoteLoadableExtension.class, SpringEnricherRemoteExtension.class); + } +} Index: spring-testsuite/pom.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/pom.xml (revision ) +++ spring-testsuite/pom.xml (revision ) @@ -0,0 +1,90 @@ + + + + + org.jboss.arquillian.container.spring + arquillian-spring-parent + 1.0.0.Final-SNAPSHOT + + + + 4.0.0 + + + org.jboss.arquillian.container-spring + spring-testsuite + Arquillian Spring Test Suite + Spring Test Suite for the Arquillian Project + + + + + + + + + + + + + + + org.jboss.spec + jboss-javaee-6.0 + 1.0.0.Final + pom + provided + + + + org.jboss.arquillian.junit + arquillian-junit-container + ${project.version} + test + + + + org.jboss.arquillian.container + arquillian-container-spi + ${project.version} + test + + + + org.jboss.arquillian.container-spring + arquillian-testenricher-spring + ${project.version} + test + + + + org.springframework + spring-context + + + + junit + junit + + + + + + jbossas-remote-6 + + + org.jboss.arquillian.container + arquillian-jbossas-remote-6 + 1.0.0.CR2 + + + org.jboss.jbossas + jboss-as-client + 6.0.0.Final + pom + + + + + Index: spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/repository/impl/NullEmployeeRepositoryTestCase.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/repository/impl/NullEmployeeRepositoryTestCase.java (revision ) +++ spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/repository/impl/NullEmployeeRepositoryTestCase.java (revision ) @@ -0,0 +1,48 @@ +package org.jboss.arquillian.container.spring.repository.impl; + +import org.jboss.arquillian.container.spring.Employee; +import org.jboss.arquillian.container.spring.repository.EmployeeRepository; +import org.jboss.arquillian.container.spring.service.EmployeeService; +import org.jboss.arquillian.container.spring.service.impl.DefaultEmployeeService; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +/** + * Tests the {@link NullEmployeeRepository} class. + */ +@RunWith(Arquillian.class) +public class NullEmployeeRepositoryTestCase { + + @Deployment + public static JavaArchive createTestArchive() { + return ShrinkWrap.create(JavaArchive.class, "spring-test.jar") + .addClasses(Employee.class, + EmployeeRepository.class, DefaultEmployeeRepository.class, NullEmployeeRepository.class) + .addAsResource(NullEmployeeRepositoryTestCase.class.getResource("/applicationContext.xml"), + "applicationContext.xml"); + } + + @Autowired + @Qualifier("nullEmployeeRepository") + private EmployeeRepository employeeRepository; + + @Test + public void testGetEmployees() throws Exception { + + List result = employeeRepository.getEmployees(); + + assertNull("Null was expected.", result); + } +} Index: arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SecurityActions.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SecurityActions.java (revision ) +++ arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SecurityActions.java (revision ) @@ -0,0 +1,48 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.testenricher.spring; + +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * Defines a set of operations that are mend to be executed within security context. + * + * @author Jakub Narloch + * @version $Revision: $ + */ +class SecurityActions { + + static boolean isClassPresent(String name) { + try { + ClassLoader classLoader = getThreadContextClassLoader(); + classLoader.loadClass(name); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + + static ClassLoader getThreadContextClassLoader() { + return AccessController.doPrivileged(new PrivilegedAction() { + + public ClassLoader run() { + return Thread.currentThread().getContextClassLoader(); + } + }); + } +} Index: spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/Employee.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/Employee.java (revision ) +++ spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/Employee.java (revision ) @@ -0,0 +1,39 @@ +package org.jboss.arquillian.container.spring; + +/** + * POJO representing an employee. + * + * @author Jakub Narloch + */ +public class Employee { + + /** + * Represents the employee name. + */ + private String name; + + /** + * Creates new instance of {@link Employee} class. + */ + public Employee() { + // empty constructor + } + + /** + * Retrieves the employee name. + * + * @return the employee name + */ + public String getName() { + return name; + } + + /** + * Sets the employee name. + * + * @param name the employee name + */ + public void setName(String name) { + this.name = name; + } +} Index: arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SpringInjectionEnricher.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SpringInjectionEnricher.java (revision ) +++ arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SpringInjectionEnricher.java (revision ) @@ -0,0 +1,104 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.testenricher.spring; + +import org.jboss.arquillian.core.api.Instance; +import org.jboss.arquillian.core.api.annotation.Inject; +import org.jboss.arquillian.test.spi.TestEnricher; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.context.ApplicationContext; + +import java.lang.reflect.Method; +import java.util.logging.Logger; + +/** + * Spring test enricher, injects spring dependencies. + * + * @author Jakub Narloch + * @version $Revision: $ + */ +public class SpringInjectionEnricher implements TestEnricher { + + /** + * Logger used by this class. + */ + private static final Logger log = Logger.getLogger(SpringInjectionEnricher.class.getName()); + + /** + * Instance of Spring {@link ApplicationContext}. + */ + @Inject + private Instance applicationContext; + + /** + * {@inheritDoc} + */ + @Override + public void enrich(Object testCase) { + + if(SecurityActions.isClassPresent(SpringEnricherConsts.APPLICATION_CONTEXT)) { + injectClass(testCase); + } + } + + /** + * {@inheritDoc} + */ + @Override + public Object[] resolve(Method method) { + + return new Object[method.getParameterTypes().length]; + } + + /** + * Injects the tests case. + * + * @param testCase the test case + */ + private void injectClass(Object testCase) { + + ApplicationContext applicationContext = getApplicationContext(); + + if (applicationContext != null) { + log.fine("Injecting dependencies into bean."); + injectDependencies(applicationContext, testCase); + } + } + + /** + * Injects dependencies into the test bean. + * + * @param applicationContext the {@link ApplicationContext} + * @param testCase the test case for which the beans will be injected + */ + private void injectDependencies(ApplicationContext applicationContext, Object testCase) { + + AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory(); + beanFactory.autowireBeanProperties(testCase, AutowireCapableBeanFactory.AUTOWIRE_NO, false); + beanFactory.initializeBean(testCase, testCase.getClass().getName()); + } + + /** + * Retrieves the {@link ApplicationContext}. + * + * @return the {@link ApplicationContext} + */ + private ApplicationContext getApplicationContext() { + + return applicationContext.get(); + } +} Index: spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/service/impl/DefaultEmployeeServiceTestCase.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/service/impl/DefaultEmployeeServiceTestCase.java (revision ) +++ spring-testsuite/src/test/java/org/jboss/arquillian/container/spring/service/impl/DefaultEmployeeServiceTestCase.java (revision ) @@ -0,0 +1,50 @@ +package org.jboss.arquillian.container.spring.service.impl; + +import org.jboss.arquillian.container.spring.Employee; +import org.jboss.arquillian.container.spring.repository.EmployeeRepository; +import org.jboss.arquillian.container.spring.repository.impl.DefaultEmployeeRepository; +import org.jboss.arquillian.container.spring.repository.impl.NullEmployeeRepository; +import org.jboss.arquillian.container.spring.service.EmployeeService; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Required; + +import javax.inject.Inject; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Tests the {@link DefaultEmployeeService} class. + */ +@RunWith(Arquillian.class) +public class DefaultEmployeeServiceTestCase { + + @Deployment + public static JavaArchive createTestArchive() { + return ShrinkWrap.create(JavaArchive.class, "spring-test.jar") + .addClasses(Employee.class, + EmployeeService.class, DefaultEmployeeService.class, + EmployeeRepository.class, DefaultEmployeeRepository.class, NullEmployeeRepository.class) + .addAsResource(DefaultEmployeeServiceTestCase.class.getResource("/applicationContext.xml"), + "applicationContext.xml"); + } + + @Inject + private EmployeeService employeeService; + + @Test + public void testGetEmployees() throws Exception { + + List result = employeeService.getEmployees(); + + assertNotNull("Method returned null list as result.", result); + assertEquals("Two employees were expected.", 2, result.size()); + } +} Index: arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SpringEnricherConsts.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SpringEnricherConsts.java (revision ) +++ arquillian-testenricher-spring/src/main/java/org/jboss/arquillian/testenricher/spring/SpringEnricherConsts.java (revision ) @@ -0,0 +1,43 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.testenricher.spring; + +/** + * Aggreagates all consts used by this extension. + * + * @author Jakub Narloch + * @version $Revision: $ + */ +public final class SpringEnricherConsts { + + /** + *

Creates new instance of {@link SpringEnricherConsts}.

+ * + *

Private constructor prevents from instantiation outside this class.

+ */ + private SpringEnricherConsts() { + // empty constructor + } + + public static final String POM_XML = "pom.xml"; + + public static final String APPLICATION_CONTEXT = "org.springframework.context.ApplicationContext"; + + public static final String SPRING_ARTIFACT_NAME = "org.springframework:spring-context"; + + public static final String SPRING_ARTIFACT_VERSION = "3.1.1.RELEASE"; +} Index: arquillian-testenricher-spring/pom.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- arquillian-testenricher-spring/pom.xml (revision ) +++ arquillian-testenricher-spring/pom.xml (revision ) @@ -0,0 +1,69 @@ + + + + org.jboss.arquillian.container.spring + arquillian-spring-parent + 1.0.0.Final-SNAPSHOT + + + + 4.0.0 + + + org.jboss.arquillian.container-spring + arquillian-testenricher-spring + Arquillian Spring TestEnricher + Spring TestEnricher for the Arquillian Project + + + + + + + + + + + + + + + org.jboss.arquillian.test + arquillian-test-spi + ${project.version} + + + org.jboss.arquillian.container + arquillian-container-test-spi + ${project.version} + + + + org.jboss.shrinkwrap + shrinkwrap-impl-base + test + + + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-api-maven + + + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-impl-maven + + + + junit + junit + test + + + + org.springframework + spring-context + + + Index: spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/impl/NullEmployeeRepository.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/impl/NullEmployeeRepository.java (revision ) +++ spring-testsuite/src/main/java/org/jboss/arquillian/container/spring/repository/impl/NullEmployeeRepository.java (revision ) @@ -0,0 +1,23 @@ +package org.jboss.arquillian.container.spring.repository.impl; + +import org.jboss.arquillian.container.spring.Employee; +import org.jboss.arquillian.container.spring.repository.EmployeeRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +/** + * A {@code null} repository. + * + * @author Jakub Narloch + */ +@Repository(value = "nullEmployeeRepository") +public class NullEmployeeRepository implements EmployeeRepository { + + /** + * {@inheritDoc} + */ + public List getEmployees() { + return null; + } +}