Skip to content

Commit

Permalink
refs #23
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Jun 17, 2016
1 parent 926dcb3 commit f704941
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,32 +95,73 @@ private void init(String unitName) {
}


public Connection getConnection() {
/**
*
* @param puName
* @return jdbc connection of provider instance represented by given puName
*/
public Connection connection(String puName) {
return instance(puName).conn;
}

/**
*
* @return jdbc conection of current provider instance
*/
public Connection connection() {
checkInstance();
return instance.conn;
}

/**
*
* @param puName
* @return entityManager represented by given puName
*/
public static EntityManager em(String puName) {
return instance(puName).em;
}

/**
*
* @return entityManager of current instance of this provider
*/
public static EntityManager em() {
checkInstance();
return instance.em;
}

private static void checkInstance() {
if(instance == null){
throw new IllegalStateException("Call instance('PU_NAME') before calling em()");
}
/**
* @param puName
* clears entityManager (represented by given puName) persistence context
* @return provider represented by puName
*/
public static EntityManagerProvider clear(String puName){
em(puName).clear();
return instance;
}

/**
* clear entityManager persistence context of this provider
* @return
* clears entityManager persistence context of current instance of this provider
* @return current provider
*/
public static EntityManagerProvider clear(){
em().clear();
return instance;
}

/**
* @param puName
* @return transaction of entityManager represented by given puName
*/
public static EntityTransaction tx(String puName) {
return em(puName).getTransaction();
}

/**
*
* @return transaction of entityManager of current instance of this provider
*/
public static EntityTransaction tx() {
checkInstance();
return instance.tx;
Expand All @@ -147,4 +188,10 @@ private boolean isHibernateOnClasspath() {
return false;
}
}

private static void checkInstance() {
if(instance == null){
throw new IllegalStateException("Call instance('PU_NAME') before calling em()");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class CleanBeforeAfterIt {

@Rule
public DBUnitRule dbUnitRule = DBUnitRule.instance(EntityManagerProvider.instance("rules-it").getConnection());
public DBUnitRule dbUnitRule = DBUnitRule.instance(EntityManagerProvider.instance("rules-it").connection());


@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Connection getConnection() {
});

private Connection initConnection() {
return emProvider.getConnection();
return emProvider.connection();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DBUnitClassLevelAnnotationIt {
public EntityManagerProvider emProvider = EntityManagerProvider.instance("rules-it");

@Rule
public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.getConnection());
public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.connection());


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DBUnitRulesIt {
public EntityManagerProvider emProvider = EntityManagerProvider.instance("rules-it"); //<1>

@Rule
public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.getConnection()); //<2>
public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.connection()); //<2>
// end::rules[]

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DataSetExecutorIt {

@BeforeClass
public static void setup() {
executor = DataSetExecutorImpl.instance("executor-name", new ConnectionHolderImpl(instance("executor-it").getConnection()));
executor = DataSetExecutorImpl.instance("executor-name", new ConnectionHolderImpl(instance("executor-it").connection()));
}

@AfterClass
Expand Down Expand Up @@ -78,7 +78,7 @@ public void shouldSeedDataSetUsingTableCreationOrder() {
tableOrdering(new String[]{"USER","TWEET","FOLLOWER"}).
executeStatementsBefore(new String[]{"DELETE FROM FOLLOWER","DELETE FROM TWEET","DELETE FROM USER"}).//needed because other tests created user dataset
useSequenceFiltering(false);
DataSetExecutorImpl.instance(new ConnectionHolderImpl(emProvider.getConnection())).createDataSet(dataSetModel);
DataSetExecutorImpl.instance(new ConnectionHolderImpl(emProvider.connection())).createDataSet(dataSetModel);
List<User> users = emProvider.em().createQuery("select u from User u").getResultList();
assertThat(users).hasSize(2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DateReplacementsIt {
public EntityManagerProvider emProvider = EntityManagerProvider.instance("scripts-it");

@Rule
public DBUnitRule dbUnitRule = DBUnitRule.instance("scripts-it",emProvider.getConnection());
public DBUnitRule dbUnitRule = DBUnitRule.instance("scripts-it",emProvider.connection());

@Before
public void setup(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ExpectedDataSetIt {
public EntityManagerProvider emProvider = EntityManagerProvider.instance("rules-it");

@Rule
public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.getConnection());
public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.connection());


// end::expectedDeclaration[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public class MultipleDataSetsIt {
public EntityManagerProvider emProvider2 = EntityManagerProvider.instance("dataset2-pu");

@Rule
public DBUnitRule exec1Rule = DBUnitRule.instance("exec1",emProvider1.getConnection());
public DBUnitRule exec1Rule = DBUnitRule.instance("exec1",emProvider1.connection());

@Rule
public DBUnitRule exec2Rule = DBUnitRule.instance("exec2",emProvider2.getConnection());
public DBUnitRule exec2Rule = DBUnitRule.instance("exec2",emProvider2.connection());


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class MultipleExecutorsIt {

@BeforeClass
public static void setup() {
executors.add(DataSetExecutorImpl.instance("executor1", new ConnectionHolderImpl(instance("executor1-pu").getConnection())));
executors.add(DataSetExecutorImpl.instance("executor2", new ConnectionHolderImpl(instance("executor2-pu").getConnection())));
executors.add(DataSetExecutorImpl.instance("executor3", new ConnectionHolderImpl(instance("executor3-pu").getConnection())));
executors.add(DataSetExecutorImpl.instance("executor1", new ConnectionHolderImpl(instance("executor1-pu").connection())));
executors.add(DataSetExecutorImpl.instance("executor2", new ConnectionHolderImpl(instance("executor2-pu").connection())));
executors.add(DataSetExecutorImpl.instance("executor3", new ConnectionHolderImpl(instance("executor3-pu").connection())));
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ScriptReplacementsIt {
public EntityManagerProvider emProvider = EntityManagerProvider.instance("scripts-it");

@Rule
public DBUnitRule dbUnitRule = DBUnitRule.instance("scripts-it",emProvider.getConnection());
public DBUnitRule dbUnitRule = DBUnitRule.instance("scripts-it",emProvider.connection());

@Before
public void setup(){
Expand Down
3 changes: 1 addition & 2 deletions core/src/test/java/com/github/dbunit/rules/ScriptsIt.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.persistence.EntityManager;
import java.util.List;

import static com.github.dbunit.rules.util.EntityManagerProvider.em;
Expand All @@ -29,7 +28,7 @@ public class ScriptsIt {
public EntityManagerProvider emProvider = EntityManagerProvider.instance("scripts-it");

@Rule
public DBUnitRule dbUnitRule = DBUnitRule.instance("scripts-it",emProvider.getConnection());
public DBUnitRule dbUnitRule = DBUnitRule.instance("scripts-it",emProvider.connection());

@BeforeClass
public static void before() {
Expand Down

0 comments on commit f704941

Please sign in to comment.