Skip to content

Commit 06873a7

Browse files
committed
[#487] Add utility methods for cleaning up entities
1 parent 1443f9a commit 06873a7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/BaseReactiveTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@
3636
import org.junit.Rule;
3737
import org.junit.runner.RunWith;
3838

39+
import java.util.Arrays;
3940
import java.util.concurrent.CompletionStage;
4041
import java.util.concurrent.TimeUnit;
42+
import java.util.stream.Collectors;
4143

4244
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
45+
import static org.hibernate.reactive.util.impl.CompletionStages.loop;
4346

4447
/**
4548
* Base class for unit tests that need a connection to the selected db and
@@ -127,6 +130,26 @@ protected Configuration constructConfiguration() {
127130
return configuration;
128131
}
129132

133+
public CompletionStage<Void> deleteEntities(Class<?>... entities) {
134+
return deleteEntities( Arrays.stream( entities )
135+
.map( BaseReactiveTest::defaultEntityName )
136+
.collect( Collectors.toList() )
137+
.toArray( new String[entities.length] ) );
138+
}
139+
140+
private static String defaultEntityName(Class<?> aClass) {
141+
int index = aClass.getName().lastIndexOf( '.' );
142+
index = index > -1 ? index + 1 : 0;
143+
return aClass.getName().substring( index );
144+
}
145+
146+
public CompletionStage<Void> deleteEntities(String... entities) {
147+
return getSessionFactory()
148+
.withTransaction( (s, tx) -> loop( entities, name -> s
149+
.createQuery( "from " + name ).getResultList()
150+
.thenCompose( list -> s.remove( list.toArray( new Object[list.size()] ) ) ) ) );
151+
}
152+
130153
@Before
131154
public void before(TestContext context) {
132155
Configuration configuration = constructConfiguration();

0 commit comments

Comments
 (0)