Skip to content

Commit 9ff2bb9

Browse files
author
Daniel Behrwind
committed
added method in TestDataLoader to clear the entity cache without clearing the database
1 parent 290788c commit 9ff2bb9

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/main/groovy/de/triology/blog/testdataloader/TestDataLoader.groovy

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TestDataLoader {
5757

5858
// TODO: javadoc -> hint that client needs to take care of transaction management when JTA is specified
5959
TestDataLoader(EntityManager entityManager, TransactionType transactionType) {
60-
if(transactionType == null) throw new IllegalArgumentException("transactionType must not be null")
60+
if (transactionType == null) throw new IllegalArgumentException("transactionType must not be null")
6161
checkTransactionType(entityManager, transactionType)
6262
this.entityManager = entityManager
6363
entityBuilder = EntityBuilder.instance()
@@ -66,13 +66,13 @@ class TestDataLoader {
6666
}
6767

6868
private void checkTransactionType(EntityManager entityManager, TransactionType transactionType) {
69-
if(transactionType == TransactionType.RESOURCE_LOCAL) {
69+
if (transactionType == TransactionType.RESOURCE_LOCAL) {
7070
makeSureTransactionsAreResourceLocal(entityManager)
7171
}
7272
}
7373

7474
private static void makeSureTransactionsAreResourceLocal(EntityManager entityManager) {
75-
try{
75+
try {
7676
entityManager.getTransaction()
7777
} catch (IllegalStateException e) {
7878
throw new IllegalStateException(
@@ -128,13 +128,21 @@ class TestDataLoader {
128128
* Clears all previously built entities so that they are no longer available through the {@code getEntityByName}
129129
* method and deletes all data from the database.
130130
*/
131-
void clear() {
131+
void clearEntityCacheAndDatabase() {
132132
withTransaction {
133133
entityDeleter.deleteAllEntities()
134-
entityBuilder.clear();
134+
clearEntityCache()
135135
}
136136
}
137137

138+
/**
139+
* Clears all previously built entities so that they are no longer available through the {@code getEntityByName}
140+
* method.
141+
*/
142+
void clearEntityCache() {
143+
entityBuilder.clear();
144+
}
145+
138146
private void withTransaction(Closure doWithinTransaction) {
139147
if (newTransactionRequired()) {
140148
withNewTransaction(doWithinTransaction)

src/test/java/de/triology/blog/testdataloader/EntityBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void clearsEntities() throws Exception {
217217
builder.getEntityByName("basicEntity", BasicTestEntity.class);
218218
} catch (NoSuchElementException e) {
219219
e.printStackTrace();
220-
fail("basicEntity already was not available before calling clear");
220+
fail("basicEntity already was not available before calling clearEntityCacheAndDatabase");
221221
}
222222

223223
builder.clear();

src/test/java/de/triology/blog/testdataloader/TestDataLoaderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ public void clearsEntitiesFromMemory() throws Exception {
8787
testDataLoader.getEntityByName("basicEntity", BasicTestEntity.class);
8888
} catch (NoSuchElementException e) {
8989
e.printStackTrace();
90-
fail("basicEntity already was not available before calling clear");
90+
fail("basicEntity already was not available before calling clearEntityCacheAndDatabase");
9191
}
9292

93-
testDataLoader.clear();
93+
testDataLoader.clearEntityCacheAndDatabase();
9494
testDataLoader.getEntityByName("basicEntity", BasicTestEntity.class);
9595
}
9696

9797
@Test
9898
public void clearsEntitiesFromDatabase() throws Exception {
9999
when(entityManagerMock.merge(any())).then(returnsFirstArg());
100100
testDataLoader.loadTestData(Collections.singletonList("tests/testEntityDefinitions.groovy"));
101-
testDataLoader.clear();
101+
testDataLoader.clearEntityCacheAndDatabase();
102102
verify(entityManagerMock, times(12)).remove(any());
103103
}
104104

src/test/java/de/triology/blog/testdataloader/demo/Demo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void setUp() throws Exception {
5151

5252
@After
5353
public void tearDown() throws Exception {
54-
testDataLoader.clear();
54+
testDataLoader.clearEntityCacheAndDatabase();
5555
assertEquals(0L, entityManager.createQuery("select count(u) from User u").getSingleResult());
5656
assertEquals(0L, entityManager.createQuery("select count(d) from Department d").getSingleResult());
5757
}

0 commit comments

Comments
 (0)