Skip to content

Commit

Permalink
refs #38
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Aug 27, 2016
1 parent 8ffeeb1 commit 63adb5c
Show file tree
Hide file tree
Showing 20 changed files with 592 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/dbunit-tomee-appcomposer-sample/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= DBUnit / ApplicationComposer Sample

This sample shows how to mix `ApplicationComposer` and `DBUnit Rules` to test efficiently and quickly your persistence layer.
59 changes: 59 additions & 0 deletions examples/dbunit-tomee-appcomposer-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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>com.github.dbunit-rules</groupId>
<artifactId>examples</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>dbunit-tomee-appcomposer-sample</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>openejb-core</artifactId>
<version>7.0.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.dbunit-rules</groupId>
<artifactId>core</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.rmannibucau.sample;


import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {
@Id
private long id;

private String name;

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.rmannibucau.sample;

import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import static javax.transaction.Transactional.TxType.SUPPORTS;

@ApplicationScoped
public class UserRepository {
@PersistenceContext
private EntityManager em;

@Transactional(SUPPORTS)
public User find(final long id) {
return em.find(User.class, id);
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="user">
<jta-data-source>jdbc/user</jta-data-source>
<non-jta-data-source>jdbc/user-unmanaged</non-jta-data-source>
<class>com.github.rmannibucau.sample.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="openejb.jpa.init-entitymanager" value="true"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.rmannibucau.sample;

import com.github.dbunit.rules.DBUnitRule;
import com.github.dbunit.rules.api.dataset.DataSet;
import org.apache.openejb.junit.ApplicationComposerRule;
import org.apache.openejb.testing.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.sql.DataSource;
import java.sql.SQLException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.rules.RuleChain.outerRule;

@Default
@Classes(cdi = true)
@Descriptors(@Descriptor(name = "persistence.xml", path = "META-INF/persistence.xml"))
@ContainerProperties({
@ContainerProperties.Property(name = "jdbc/user", value = "new://Resource?type=DataSource"),
@ContainerProperties.Property(name = "jdbc/user.LogSql", value = "true")
})
@DataSet(cleanBefore = true)
public class UserRepositoryTest {

@Resource
private DataSource dataSource;

@Rule
public final TestRule rules = outerRule(new ApplicationComposerRule(this))
.around(DBUnitRule.instance(() -> {
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}));


@Inject
private UserRepository repository;


@Test
@DataSet("datasets/users.yml")
public void find1() {
assertEquals("John Smith", repository.find(1L).getName());
assertEquals("Clark Kent", repository.find(2L).getName());
}

@Test
public void find2() { // ensure we didn't leak previous dataset
assertNull(repository.find(1L));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
user:
- id: 1
name: John Smith
- id: 2
name: Clark Kent
22 changes: 22 additions & 0 deletions examples/jOOQ-DBUnit-flyway-example/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
All rights reserved.

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.

Other licenses:
-----------------------------------------------------------------------------
Commercial licenses for this work are available. These replace the above
ASL 2.0 and offer limited warranties, support, maintenance, and commercial
database integrations.

For more information, please visit: http://www.jooq.org/licenses
12 changes: 12 additions & 0 deletions examples/jOOQ-DBUnit-flyway-example/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
= jOOQ DBUnit Flyway Example

Modified example from https://github.com/jOOQ/jOOQ/tree/master/jOOQ-examples/jOOQ-flyway-example[jOOQ repository^] to add DBUnit via DBUnit rules to test efficiently and quickly your persistence layer.

Thanks for downloading jOOQ.
Please visit http://www.jooq.org for more information.

To install and run this example, simply check it out and run the following Maven command

----
mvn clean install
----
152 changes: 152 additions & 0 deletions examples/jOOQ-DBUnit-flyway-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<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>com.github.dbunit-rules</groupId>
<artifactId>examples</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>jooq-dbunit-flyway-example</artifactId>
<name>jOOQ DBUnit Flyway Example</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>3.2.6.RELEASE</org.springframework.version>
<org.jooq.version>3.8.4</org.jooq.version>
<org.h2.version>1.4.192</org.h2.version>
<db.url>jdbc:h2:${project.build.directory}/flyway-test</db.url>
<db.username>sa</db.username>
</properties>

<dependencies>

<!-- Database access -->
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${org.jooq.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${org.h2.version}</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>3.2.1</version>
<scope>test</scope>
</dependency>


<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.dbunit-rules</groupId>
<artifactId>core</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<fork>true</fork>
<maxmem>1024m</maxmem>
<meminitial>256m</meminitial>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<debug>true</debug>
<debuglevel>lines,vars,source</debuglevel>
</configuration>
</plugin>

<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions>
<configuration>
<url>${db.url}</url>
<user>${db.username}</user>
<locations>
<location>filesystem:src/main/resources/db/migration</location>
</locations>
</configuration>
</plugin>

<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>${org.jooq.version}</version>

<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>

<configuration>
<jdbc>
<url>${db.url}</url>
<user>${db.username}</user>
</jdbc>
<generator>
<database>
<includes>.*</includes>
<inputSchema>FLYWAY_TEST</inputSchema>
<catalogVersionProvider>SELECT 'DEFAULT_CATALOG_' || TO_CHAR(current_timestamp,
'YYYYMMDDHHMISS')
</catalogVersionProvider>
<schemaVersionProvider>SELECT :schema_name || '_' || MAX("version") FROM "schema_version"
</schemaVersionProvider>
</database>
<target>
<packageName>org.jooq.example.flyway.db.h2</packageName>
<directory>target/generated-sources/jooq-h2</directory>
</target>
</generator>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP SCHEMA flyway_test IF EXISTS;

CREATE SCHEMA flyway_test;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE SEQUENCE flyway_test.s_author_id START WITH 1;

CREATE TABLE flyway_test.author (
id INT NOT NULL,
first_name VARCHAR(50),
last_name VARCHAR(50) NOT NULL,
date_of_birth DATE,
year_of_birth INT,
address VARCHAR(50),

CONSTRAINT pk_t_author PRIMARY KEY (ID)
);
Loading

0 comments on commit 63adb5c

Please sign in to comment.