Skip to content

Commit b09e201

Browse files
committed
[#1952] Tests clean up
Some refactoring of existing tests and I made some improvements to OrphanRemovalTest
1 parent d64f1e1 commit b09e201

12 files changed

+344
-249
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ protected void addEntities(Configuration configuration) {
165165
}
166166
}
167167

168+
/**
169+
* This method works for most common cases, but some tests might need to overrides it
170+
*/
168171
public CompletionStage<Void> deleteEntities(Class<?>... entities) {
169172
return getSessionFactory()
170173
.withTransaction( s -> loop( entities, entityClass -> s

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

+72-73
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ private static String nameFromResult(List<String> rowSet) {
6767
}
6868

6969
private CompletionStage<Double> selectWeightFromId(Integer id) {
70-
return getSessionFactory().withSession(
71-
session -> session.createSelectionQuery("SELECT weight FROM GuineaPig WHERE id = " + id, Double.class )
72-
.getResultList()
73-
.thenApply( CompositeIdTest::weightFromResult )
70+
return getSessionFactory().withSession( session -> session
71+
.createSelectionQuery( "SELECT weight FROM GuineaPig WHERE id = " + id, Double.class )
72+
.getResultList()
73+
.thenApply( CompositeIdTest::weightFromResult )
7474
);
7575
}
7676

@@ -79,93 +79,85 @@ private static Double weightFromResult(List<Double> rowSet) {
7979
case 0:
8080
return null;
8181
case 1:
82-
return rowSet.get(0);
82+
return rowSet.get( 0 );
8383
default:
84-
throw new AssertionError("More than one result returned: " + rowSet.size());
84+
throw new AssertionError( "More than one result returned: " + rowSet.size() );
8585
}
8686
}
8787

8888
@Test
8989
public void reactiveFind(VertxTestContext context) {
9090
final GuineaPig expectedPig = new GuineaPig( 5, "Aloi" );
91-
test(
92-
context,
93-
populateDB()
94-
.thenCompose( v -> openSession() )
95-
.thenCompose( session -> session.find( GuineaPig.class, new Pig(5, "Aloi") ) )
96-
.thenAccept( actualPig -> assertThatPigsAreEqual( context, expectedPig, actualPig ) )
91+
test( context, populateDB()
92+
.thenCompose( v -> openSession() )
93+
.thenCompose( session -> session.find( GuineaPig.class, new Pig( 5, "Aloi" ) ) )
94+
.thenAccept( actualPig -> assertThatPigsAreEqual( context, expectedPig, actualPig ) )
9795
);
9896
}
9997

10098
@Test
10199
public void reactivePersist(VertxTestContext context) {
102-
test(
103-
context,
104-
openSession()
105-
.thenCompose( s -> s.persist( new GuineaPig( 10, "Tulip" ) )
106-
.thenCompose( v -> s.flush() )
107-
)
108-
.thenCompose( v -> selectNameFromId( 10 ) )
109-
.thenAccept( selectRes -> assertEquals( "Tulip", selectRes ) )
100+
test( context, openSession()
101+
.thenCompose( s -> s
102+
.persist( new GuineaPig( 10, "Tulip" ) )
103+
.thenCompose( v -> s.flush() )
104+
)
105+
.thenCompose( v -> selectNameFromId( 10 ) )
106+
.thenAccept( selectRes -> assertEquals( "Tulip", selectRes ) )
110107
);
111108
}
112109

113110
@Test
114111
public void reactiveRemoveTransientEntity(VertxTestContext context) {
115-
test(
116-
context,
117-
populateDB()
118-
.thenCompose( v -> selectNameFromId( 5 ) )
119-
.thenAccept( Assertions::assertNotNull )
120-
.thenCompose( v -> openSession() )
121-
.thenCompose( session -> session.remove( new GuineaPig( 5, "Aloi" ) )
122-
.thenCompose( v -> session.flush() )
123-
.thenCompose( v -> session.close() )
124-
)
125-
.thenCompose( v -> selectNameFromId( 5 ) )
126-
.thenAccept( Assertions::assertNull )
127-
.handle( (r, e) -> {
128-
assertNotNull( e );
129-
return r;
130-
} )
112+
test( context, populateDB()
113+
.thenCompose( v -> selectNameFromId( 5 ) )
114+
.thenAccept( Assertions::assertNotNull )
115+
.thenCompose( v -> openSession() )
116+
.thenCompose( session -> session
117+
.remove( new GuineaPig( 5, "Aloi" ) )
118+
.thenCompose( v -> session.flush() )
119+
.thenCompose( v -> session.close() )
120+
)
121+
.thenCompose( v -> selectNameFromId( 5 ) )
122+
.thenAccept( Assertions::assertNull )
123+
.handle( (r, e) -> {
124+
assertNotNull( e );
125+
return r;
126+
} )
131127
);
132128
}
133129

134130
@Test
135131
public void reactiveRemoveManagedEntity(VertxTestContext context) {
136-
test(
137-
context,
138-
populateDB()
139-
.thenCompose( v -> openSession() )
140-
.thenCompose( session ->
141-
session.find( GuineaPig.class, new Pig(5, "Aloi") )
142-
.thenCompose( session::remove )
143-
.thenCompose( v -> session.flush() )
144-
.thenCompose( v -> selectNameFromId( session,5 ) )
145-
.thenAccept( Assertions::assertNull )
146-
)
132+
test( context, populateDB()
133+
.thenCompose( v -> openSession() )
134+
.thenCompose( session -> session
135+
.find( GuineaPig.class, new Pig( 5, "Aloi" ) )
136+
.thenCompose( session::remove )
137+
.thenCompose( v -> session.flush() )
138+
.thenCompose( v -> selectNameFromId( session, 5 ) )
139+
.thenAccept( Assertions::assertNull )
140+
)
147141
);
148142
}
149143

150144
@Test
151145
public void reactiveUpdate(VertxTestContext context) {
152146
final double NEW_WEIGHT = 200.0;
153-
test(
154-
context,
155-
populateDB()
156-
.thenCompose( v -> openSession() )
157-
.thenCompose( session ->
158-
session.find( GuineaPig.class, new Pig(5, "Aloi") )
159-
.thenAccept( pig -> {
160-
assertNotNull( pig );
161-
// Checking we are actually changing the name
162-
assertNotEquals( pig.getWeight(), NEW_WEIGHT );
163-
pig.setWeight( NEW_WEIGHT );
164-
} )
165-
.thenCompose( v -> session.flush() )
166-
.thenCompose( v -> session.close() )
167-
.thenCompose( v -> selectWeightFromId( 5 ) )
168-
.thenAccept( w -> assertEquals( NEW_WEIGHT, w ) ) )
147+
test( context, populateDB()
148+
.thenCompose( v -> openSession() )
149+
.thenCompose( session -> session
150+
.find( GuineaPig.class, new Pig( 5, "Aloi" ) )
151+
.thenAccept( pig -> {
152+
assertNotNull( pig );
153+
// Checking we are actually changing the name
154+
assertNotEquals( pig.getWeight(), NEW_WEIGHT );
155+
pig.setWeight( NEW_WEIGHT );
156+
} )
157+
.thenCompose( v -> session.flush() )
158+
.thenCompose( v -> session.close() )
159+
.thenCompose( v -> selectWeightFromId( 5 ) )
160+
.thenAccept( w -> assertEquals( NEW_WEIGHT, w ) ) )
169161
);
170162
}
171163

@@ -185,7 +177,8 @@ public Pig(Integer id, String name) {
185177
this.name = name;
186178
}
187179

188-
Pig() {}
180+
Pig() {
181+
}
189182

190183
public Integer getId() {
191184
return id;
@@ -197,25 +190,31 @@ public String getName() {
197190

198191
@Override
199192
public boolean equals(Object o) {
200-
if (this == o) return true;
201-
if (o == null || getClass() != o.getClass()) return false;
193+
if ( this == o ) {
194+
return true;
195+
}
196+
if ( o == null || getClass() != o.getClass() ) {
197+
return false;
198+
}
202199
Pig pig = (Pig) o;
203-
return id.equals(pig.id) &&
204-
name.equals(pig.name);
200+
return id.equals( pig.id ) &&
201+
name.equals( pig.name );
205202
}
206203

207204
@Override
208205
public int hashCode() {
209-
return Objects.hash(id, name);
206+
return Objects.hash( id, name );
210207
}
211208
}
212209

213-
@Entity(name="GuineaPig")
214-
@Table(name="Pig")
210+
@Entity(name = "GuineaPig")
211+
@Table(name = "Pig")
215212
@IdClass(Pig.class)
216213
public static class GuineaPig implements Serializable {
217-
@Id private Integer id;
218-
@Id private String name;
214+
@Id
215+
private Integer id;
216+
@Id
217+
private String name;
219218

220219
private double weight = 100.0;
221220

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ public void testPersistWithReference(VertxTestContext context) {
9090
Bar bar = new Bar( "uniquePersist" );
9191
test( context, getSessionFactory()
9292
.withTransaction( session -> session.persist( bar ) )
93-
.thenCompose( i -> getSessionFactory()
94-
.withTransaction( session -> {
95-
Foo foo = new Foo( session.getReference( Bar.class, bar.getId() ) );
96-
return session.persist( foo ).thenApply( v -> foo );
97-
} ) )
93+
.thenCompose( i -> getSessionFactory().withTransaction( session -> {
94+
Foo foo = new Foo( session.getReference( Bar.class, bar.getId() ) );
95+
return session.persist( foo ).thenApply( v -> foo );
96+
} ) )
9897
.thenCompose( result -> getSessionFactory().withTransaction( session -> session.fetch( result.getBar() ) ) )
9998
.thenAccept( b -> assertEquals( "uniquePersist", b.getKey() ) )
10099
);

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import java.util.Collection;
1111
import java.util.List;
1212
import java.util.UUID;
13+
import java.util.concurrent.CompletionStage;
1314

1415
import org.hibernate.reactive.annotations.DisabledFor;
16+
import org.hibernate.reactive.util.impl.CompletionStages;
1517

1618
import org.junit.jupiter.api.BeforeEach;
1719
import org.junit.jupiter.api.Test;
@@ -50,7 +52,7 @@ protected Collection<Class<?>> annotatedEntities() {
5052
@BeforeEach
5153
public void populateDb(VertxTestContext context) {
5254
theCampaign = new Campaign();
53-
theCampaign.setSchedule( new ExecutionDate(OffsetDateTime.now(), "ALPHA") );
55+
theCampaign.setSchedule( new ExecutionDate( OffsetDateTime.now(), "ALPHA" ) );
5456

5557
test( context, getMutinySessionFactory().withTransaction( (s, t) -> s.persist( theCampaign ) ) );
5658
}
@@ -70,6 +72,11 @@ public void testUpdateScheduleChange(VertxTestContext context) {
7072
);
7173
}
7274

75+
@Override
76+
protected CompletionStage<Void> cleanDb() {
77+
return CompletionStages.voidFuture();
78+
}
79+
7380
@Test
7481
public void testUpdateWithMultipleScheduleChanges(VertxTestContext context) {
7582
test( context, getMutinySessionFactory()
@@ -94,13 +101,14 @@ public void testUpdateWithMultipleScheduleChanges(VertxTestContext context) {
94101
);
95102
}
96103

97-
@Entity (name="Campaign")
104+
@Entity(name = "Campaign")
98105
public static class Campaign implements Serializable {
99106

100-
@Id @GeneratedValue
107+
@Id
108+
@GeneratedValue
101109
private Integer id;
102110

103-
@OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
111+
@OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
104112
public Schedule schedule;
105113

106114
public Campaign() {
@@ -123,7 +131,7 @@ public Integer getId() {
123131
}
124132
}
125133

126-
@Entity (name="Schedule")
134+
@Entity(name = "Schedule")
127135
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
128136
@DiscriminatorColumn(name = "schedule_type", discriminatorType = DiscriminatorType.STRING)
129137
public static abstract class Schedule implements Serializable {
@@ -160,7 +168,7 @@ public String getCodeName() {
160168
}
161169
}
162170

163-
@Entity (name="ExecutionDate")
171+
@Entity(name = "ExecutionDate")
164172
@DiscriminatorValue("EXECUTION_DATE")
165173
public static class ExecutionDate extends Schedule {
166174

@@ -170,7 +178,7 @@ public static class ExecutionDate extends Schedule {
170178
public ExecutionDate() {
171179
}
172180

173-
public ExecutionDate( OffsetDateTime start, String code_name ) {
181+
public ExecutionDate(OffsetDateTime start, String code_name) {
174182
this.start = start;
175183
setCodeName( code_name );
176184
}

0 commit comments

Comments
 (0)