Skip to content

Commit

Permalink
fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Jul 29, 2015
1 parent b41830b commit b108d2c
Show file tree
Hide file tree
Showing 17 changed files with 520 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ cache:

script:
- cd core && mvn clean install -Pcoverage
- cd ../examples && mvn clean install
- cd ../examples && mvn clean package
- cd ../jpa && mvn clean package
23 changes: 23 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,26 @@ this JPA configuration depends on hsqldb (an in memory database) and eclipse lin
<scope>test</scope>
</dependency>
----

JPA module also provides a JPA based dataset executor based on Entity Manager:

[source,java]
----
@RunWith(JUnit4.class)
public class JPADatasetExecutorIt {
@Rule
public EntityManagerProvider emProvider = EntityManagerProvider.instance("rules-it");
@Test
public void shouldSeedUserDataSetUsing() {
DataSetModel dataModel = new DataSetModel("datasets/yml/users.yml");
JPADataSetExecutor.instance(emProvider.em()).execute(dataModel);
User user = (User) emProvider.em().createQuery("select u from User u where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
}
}
----
13 changes: 10 additions & 3 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@
</dependency>

<dependency>
<groupId>com.github.dbunit-rules</groupId>
<artifactId>jpa</artifactId>
<version>${project.parent.version}</version>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<scope>test</scope>
</dependency>



</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.sql.SQLException;

/**
* COPIED from JPA module because of maven cyclic dependencies (even with test scope)
* Created by pestano on 26/07/15.
*/
public class DataSetExecutor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public class DataSetModel {
private String[] executeStatementsBefore = {};
private String[] executeStatementsAfter = {};


public DataSetModel() {
}

public DataSetModel(String name) {
this.name = name;
}

public DataSetModel name(String name) {
this.name = name;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* processor. <br/>
* <br/>
* The format looks like this: <br/>
*
*
* <pre>
* {
* "&lt;table_name&gt;": [
Expand All @@ -30,10 +30,10 @@
* ...
* }
* </pre>
*
*
* <br/>
* I.e.: <br/>
*
*
* <pre>
* {
* "test_table": [
Expand All @@ -59,7 +59,7 @@
* ...
* }
* </pre>
*
*
* @author Lieven DOCLO
*/
public class JSONDataSet extends AbstractDataSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.dbunit.rules.connection.ConnectionHolder;
import com.github.dbunit.rules.dataset.DataSet;
import com.github.dbunit.rules.jpa.EntityManagerProvider;
import com.github.dbunit.rules.model.Follower;
import com.github.dbunit.rules.model.User;
import org.junit.Rule;
Expand All @@ -11,7 +10,6 @@
import org.junit.runners.JUnit4;

import java.sql.Connection;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.dbunit.rules;

import com.github.dbunit.rules.dataset.DataSet;
import com.github.dbunit.rules.jpa.EntityManagerProvider;
import com.github.dbunit.rules.model.Follower;
import com.github.dbunit.rules.model.User;
import org.junit.Rule;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.github.dbunit.rules;

/**
* from https://github.com/AdamBien/rulz/tree/master/em/
* only difference is is that we need jdbc connection to create dataset
*/

import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.sql.Connection;
import java.util.Map;

public class EntityManagerProvider implements TestRule {

private EntityManagerFactory emf;
private EntityManager em;
private EntityTransaction tx;
private Connection conn;
private Map<String,Object> emfProps;
private static Logger log = LoggerFactory.getLogger(EntityManagerProvider.class);

private static EntityManagerProvider instance;

private EntityManagerProvider() {
}

public static EntityManagerProvider instance(String unitName){
if(instance == null){
instance = new EntityManagerProvider();
}

try {
instance.init(unitName);
}catch (Exception e){
log.error("Could not initialize persistence unit " + unitName, e);
}

return instance;
}

private void init(String unitName) {
if(emf == null){
emf = Persistence.createEntityManagerFactory(unitName);
em = emf.createEntityManager();
this.tx = this.em.getTransaction();
if(em.getDelegate() instanceof Session){
conn = ((SessionImpl) em.unwrap(Session.class)).connection();
} else{
/**
* see here:http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI#Getting_a_JDBC_Connection_from_an_EntityManager
*/
tx.begin();
conn = em.unwrap(Connection.class);
tx.commit();
}

}
emf.getCache().evictAll();

}


public static Connection getConnection() {
return instance.conn;
}

public static EntityManager em() {
return instance.em;
}

public static EntityTransaction tx() {
return instance.tx;
}

@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {

@Override
public void evaluate() throws Throwable {
base.evaluate();
em.clear();
}

};
}

}
5 changes: 5 additions & 0 deletions jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<artifactId>jpa</artifactId>

<dependencies>
<dependency>
<groupId>com.github.dbunit-rules</groupId>
<artifactId>core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.dbunit.rules.jpa;

import com.github.dbunit.rules.connection.ConnectionHolder;
import com.github.dbunit.rules.connection.ConnectionHolderImpl;
import com.github.dbunit.rules.dataset.DataSetExecutor;
import com.github.dbunit.rules.dataset.DataSetModel;
import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import java.sql.Connection;
import java.sql.SQLException;

/**
* Created by pestano on 28/07/15.
*/
public class JPADataSetExecutor {

private EntityManager entityManager;
private DataSetExecutor executor;
private static JPADataSetExecutor instance;
private ConnectionHolder connection;

public static JPADataSetExecutor instance(EntityManager entityManager){
if(instance == null){
instance = new JPADataSetExecutor();
}
instance.setEntityManager(entityManager);
if(instance.executor == null){
try {
instance.executor = DataSetExecutor.instance(instance.getConnectionHolder());
} catch (SQLException e) {
LoggerFactory.getLogger(JPADataSetExecutor.class).error("Could not create JPA connection", e);
}
}
return instance;
}

public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

public EntityManager getEntityManager() {
return entityManager;
}

public ConnectionHolder getConnectionHolder() throws SQLException {
if(entityManager == null || !entityManager.isOpen()){
throw new RuntimeException("Could not get jdbc connection. Entity manager is null or is closed");
}
if(connection == null || connection.getConnection().isClosed()){
connection = new ConnectionHolderImpl(this.getJdbcConnection());
}
return connection;
}

public Connection getJdbcConnection() {
if (entityManager.getDelegate() instanceof Session){
return ((SessionImpl)entityManager.getDelegate()).connection();
}else{
entityManager.getTransaction().begin();
Connection connection = entityManager.unwrap(Connection.class);
entityManager.getTransaction().commit();
return connection;
}
}

public void execute(DataSetModel model){
executor.execute(model);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.dbunit.rules;

import com.github.dbunit.rules.dataset.DataSet;
import com.github.dbunit.rules.dataset.DataSetModel;
import com.github.dbunit.rules.jpa.EntityManagerProvider;
import com.github.dbunit.rules.jpa.JPADataSetExecutor;
import com.github.dbunit.rules.model.Follower;
import com.github.dbunit.rules.model.User;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

/**
* Created by pestano on 23/07/15.
*/

@RunWith(JUnit4.class)
public class JPADatasetExecutorIt {

@Rule
public EntityManagerProvider emProvider = EntityManagerProvider.instance("rules-it");


@Test
public void shouldSeedUserDataSetUsing() {
DataSetModel dataModel = new DataSetModel("datasets/yml/users.yml");
JPADataSetExecutor.instance(emProvider.em()).execute(dataModel);
User user = (User) emProvider.em().createQuery("select u from User u where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
}

}
Loading

0 comments on commit b108d2c

Please sign in to comment.