Skip to content

Commit baafbe2

Browse files
jmnarlochaslakknutsen
authored andcommitted
ARQ-1107 Add support for Arquillian Persistence Extension
arquillian-persistence-spring implements the Persistence Extension SPI DataSourceProvider which allow users to use a Spring Context defined DataSource for Persistence Extension operations like @UsingDataSet and @ShouldMatchDataSet.
1 parent 86427f1 commit baafbe2

File tree

11 files changed

+696
-0
lines changed

11 files changed

+696
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0"?>
2+
<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/xsd/maven-4.0.0.xsd">
3+
4+
<!-- Parent -->
5+
<parent>
6+
<artifactId>arquillian-spring-parent</artifactId>
7+
<groupId>org.jboss.arquillian.extension</groupId>
8+
<version>1.0.0.Final-SNAPSHOT</version>
9+
</parent>
10+
11+
<!-- Model Version -->
12+
<modelVersion>4.0.0</modelVersion>
13+
14+
<artifactId>arquillian-persistence-spring</artifactId>
15+
<packaging>jar</packaging>
16+
17+
<name>Arquillian Persistence Extension: Spring</name>
18+
<description>Spring Framework persistence extension for Arquillian</description>
19+
20+
<!-- Properties -->
21+
<properties>
22+
23+
</properties>
24+
25+
<!-- Dependencies -->
26+
<dependencies>
27+
28+
<dependency>
29+
<groupId>org.jboss.arquillian.extension</groupId>
30+
<artifactId>arquillian-service-integration-spring</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.jboss.arquillian.extension</groupId>
35+
<artifactId>arquillian-persistence-spi</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.jboss.arquillian.container</groupId>
40+
<artifactId>arquillian-container-test-spi</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework</groupId>
45+
<artifactId>spring-context</artifactId>
46+
<version>${version.spring_framework_2_5}</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.jboss.shrinkwrap</groupId>
52+
<artifactId>shrinkwrap-impl-base</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.jboss.arquillian.core</groupId>
58+
<artifactId>arquillian-core-impl-base</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.jboss.arquillian.core</groupId>
64+
<artifactId>arquillian-core-impl-base</artifactId>
65+
<scope>test</scope>
66+
<classifier>tests</classifier>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>org.jboss.arquillian.test</groupId>
71+
<artifactId>arquillian-test-impl-base</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>org.jboss.arquillian.test</groupId>
77+
<artifactId>arquillian-test-impl-base</artifactId>
78+
<scope>test</scope>
79+
<classifier>tests</classifier>
80+
</dependency>
81+
82+
<dependency>
83+
<groupId>junit</groupId>
84+
<artifactId>junit</artifactId>
85+
</dependency>
86+
87+
<dependency>
88+
<groupId>org.mockito</groupId>
89+
<artifactId>mockito-all</artifactId>
90+
</dependency>
91+
92+
</dependencies>
93+
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jboss.arquillian.spring.persistence.client;
18+
19+
import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
20+
import org.jboss.arquillian.container.test.spi.client.deployment.AuxiliaryArchiveAppender;
21+
import org.jboss.arquillian.spring.persistence.container.SpringPersistenceRemoteExtension;
22+
import org.jboss.arquillian.spring.persistence.datasource.ApplicationContextDataSourceProvider;
23+
import org.jboss.shrinkwrap.api.Archive;
24+
import org.jboss.shrinkwrap.api.ShrinkWrap;
25+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
26+
27+
/**
28+
* <p>Packages the spring persistence extension classes into a archive deployed into the container.</p>
29+
*
30+
* @author <a href="mailto:jmnarloch@gmail.com">Jakub Narloch</a>
31+
*/
32+
public class SpringPersistenceArchiveAppender implements AuxiliaryArchiveAppender {
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
@Override
38+
public Archive<?> createAuxiliaryArchive() {
39+
40+
// creates the archive
41+
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "arquillian-persistence-spring.jar");
42+
43+
// adds the container classes
44+
archive.addPackage(ApplicationContextDataSourceProvider.class.getPackage());
45+
archive.addClass(SpringPersistenceRemoteExtension.class);
46+
47+
// registers the remote extension
48+
archive.addAsServiceProvider(RemoteLoadableExtension.class, SpringPersistenceRemoteExtension.class);
49+
50+
return archive;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jboss.arquillian.spring.persistence.client;
18+
19+
import org.jboss.arquillian.container.test.spi.client.deployment.AuxiliaryArchiveAppender;
20+
import org.jboss.arquillian.core.spi.LoadableExtension;
21+
import org.jboss.arquillian.persistence.spi.datasource.DataSourceProvider;
22+
import org.jboss.arquillian.spring.persistence.datasource.ApplicationContextDataSourceProvider;
23+
24+
/**
25+
* <p>Registers the Spring persistence extension.</p>
26+
*
27+
* @author <a href="mailto:jmnarloch@gmail.com">Jakub Narloch</a>
28+
*/
29+
public class SpringPersistenceExtension implements LoadableExtension {
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
@Override
35+
public void register(ExtensionBuilder builder) {
36+
37+
// registers the spring archive appender
38+
builder.service(AuxiliaryArchiveAppender.class, SpringPersistenceArchiveAppender.class);
39+
40+
// registers the data source provider on the client side for local protocol, embedded containers and so on
41+
builder.service(DataSourceProvider.class, ApplicationContextDataSourceProvider.class);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jboss.arquillian.spring.persistence.container;
18+
19+
import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
20+
import org.jboss.arquillian.persistence.spi.datasource.DataSourceProvider;
21+
import org.jboss.arquillian.spring.persistence.datasource.ApplicationContextDataSourceProvider;
22+
23+
/**
24+
* <p>Registers the Spring persistence extension in the container.</p>
25+
*
26+
* @author <a href="mailto:jmnarloch@gmail.com">Jakub Narloch</a>
27+
*/
28+
public class SpringPersistenceRemoteExtension implements RemoteLoadableExtension {
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
@Override
34+
public void register(ExtensionBuilder builder) {
35+
36+
// registers the data source provider in the container
37+
builder.service(DataSourceProvider.class, ApplicationContextDataSourceProvider.class);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jboss.arquillian.spring.persistence.datasource;
18+
19+
import java.util.logging.Level;
20+
import java.util.logging.Logger;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.jboss.arquillian.core.api.Instance;
25+
import org.jboss.arquillian.core.api.annotation.Inject;
26+
import org.jboss.arquillian.persistence.spi.datasource.DataSourceProvider;
27+
import org.jboss.arquillian.spring.integration.context.RemoteTestScopeApplicationContext;
28+
import org.springframework.beans.BeansException;
29+
import org.springframework.context.ApplicationContext;
30+
31+
/**
32+
* <p>An implementation of {@link DataSourceProvider} that .</p>
33+
*
34+
* @author <a href="mailto:jmnarloch@gmail.com">Jakub Narloch</a>
35+
*/
36+
public class ApplicationContextDataSourceProvider implements DataSourceProvider {
37+
38+
/**
39+
* The logger instance used by this class.
40+
*/
41+
private static final Logger log = Logger.getLogger(ApplicationContextDataSourceProvider.class.getName());
42+
43+
/**
44+
* <p>Instance of application context.</p>
45+
*/
46+
@Inject
47+
private Instance<RemoteTestScopeApplicationContext> applicationContextInstance;
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
@Override
53+
public DataSource lookupDataSource(String dataSourceName) {
54+
55+
try {
56+
57+
// retrieves the application context
58+
return (DataSource) getApplicationContext().getBean(dataSourceName, DataSource.class);
59+
} catch (BeansException e) {
60+
61+
log.log(Level.WARNING, String.format(
62+
"The data source with name '%s' could not be lookup due to an unexpected error.", dataSourceName),
63+
e);
64+
65+
return null;
66+
}
67+
}
68+
69+
/**
70+
* Retrieves the instance of the {@link ApplicationContext}
71+
*/
72+
private ApplicationContext getApplicationContext() {
73+
74+
return applicationContextInstance.get().getApplicationContext();
75+
}
76+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.jboss.arquillian.spring.persistence.client.SpringPersistenceExtension

0 commit comments

Comments
 (0)