如何在web.xml或Arquillian中的其他地方指定便携式数据源?
我正在开始使用Arquillian,并注意到必须指定要在服务器特定文件(jboss-ds.xml
,glassfish-resources.xml
等)中的JPA中与JTA一起使用的数据源,但是在Java EE> 6中,应该可以在web.xml
(或ejb-jar.xml
,application.xml
或application-client.xml
)中指定它,例如如何在web.xml或Arquillian中的其他地方指定便携式数据源?
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
[...]
<data-source>
<name>jdbc/project1</name>
<class-name>org.apache.derby.jdbc.EmbeddedDataSource</class-name>
<server-name>localhost</server-name>
<database-name>project1</database-name>
<user>project1</user>
<password>project1</password>
<property>
<name>connectionAttributes</name>
<value>create=true</value>
</property>
<transactional>true</transactional>
<isolation-level>TRANSACTION_READ_COMMITTED</isolation-level>
<initial-pool-size>2</initial-pool-size>
<max-pool-size>10</max-pool-size>
<min-pool-size>5</min-pool-size>
<max-statements>0</max-statements>
</data-source>
</web-app>
和按如下方式使用它:
@Deployment
public static Archive<?> createDeployment() {
WebArchive retValue = ShrinkWrap.create(WebArchive.class)
.addClasses(MyManagedBean.class, SaveController.class, DefaultSaveController.class)
.setWebXML("web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
Stream.of(Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().resolve().withTransitivity().as(JavaArchive.class)).forEach(archive -> retValue.addAsLibrary(archive));
return retValue;
}
这将避免重复。但是数据源不可用,例如,由于Caused by: com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : jdbc/project1__pm
,GlassFish失败。
MCVE位于https://github.com/krichter722/arquillian-data-source-in-web-xml。
Arquillian似乎创建了一个可用的数据源,但我想测试我的生产环境的确切类型,因为这些测试已经非常接近集成。
我会建议按照Arquillian JPA tutorial和使用glassfish-resourcess.xml
。如果你按照你会看到maven项目的以下文件结构。
,一旦你有这样的包,然后文件结构中加入<testResources>
标签指向您的测试资源pom.xml
。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>project-service</artifactId>
<packaging>jar</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.0.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>infohub-entities</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-web</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<name>project-service</name>
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/resources-glassfish-embedded</directory>
</testResource>
</testResources>
</build>
</project>
然后在glassfish-resources.xml
您可以创建您的便携式数据源配置。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/datasource" object-type="user" pool-name="connectionPool">
<description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false"
associate-with-thread="false"
connection-creation-retry-attempts="0"
connection-creation-retry-interval-in-seconds="10"
connection-leak-reclaim="false"
connection-leak-timeout-in-seconds="0"
connection-validation-method="auto-commit"
datasource-classname="oracle.jdbc.pool.OracleDataSource"
fail-all-connections="false"
idle-timeout-in-seconds="300"
is-connection-validation-required="false"
is-isolation-level-guaranteed="true"
lazy-connection-association="false"
lazy-connection-enlistment="false"
match-connections="false"
max-connection-usage-count="0"
max-pool-size="32" max-wait-time-in-millis="60000"
name="connectionPool"
non-transactional-connections="false"
pool-resize-quantity="2"
res-type="javax.sql.DataSource"
statement-timeout-in-seconds="-1"
steady-pool-size="8"
validate-atmost-once-period-in-seconds="0"
wrap-jdbc-objects="false">
<property name="URL" value="jdbc:oracle:thin:@//url:port"/>
<property name="User" value="username"/>
<property name="Password" value="password"/>
</jdbc-connection-pool>
</resources>
,并指出该数据源arquillian.xml
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<container qualifier="glassfish-embedded" default="true">
<configuration>
<property name="resourcesXml">
src/test/resources-glassfish-embedded/glassfish-resources.xml
</property>
</configuration>
</container>
</arquillian>
使您的数据源是便携式的,这将提供给每一次测试开始。 有关完整的解决方案,请按照Arquillian JPA Tutorial。或Arquillian Blog