-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rmpestano
committed
Jul 29, 2015
1 parent
b41830b
commit b108d2c
Showing
17 changed files
with
520 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
core/src/test/java/com/github/dbunit/rules/EntityManagerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
jpa/src/main/java/com/github/dbunit/rules/jpa/JPADataSetExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
jpa/src/test/java/com/github/dbunit/rules/JPADatasetExecutorIt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.