Skip to content

Commit 968dc36

Browse files
committed
[#2926] Test validation of open transaction
1 parent 4a3015f commit 968dc36

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.reactive;
6+
7+
import java.util.Collection;
8+
import java.util.List;
9+
10+
import org.hibernate.reactive.pool.ReactiveConnection;
11+
import org.hibernate.reactive.stage.Stage;
12+
import org.hibernate.reactive.stage.impl.StageSessionImpl;
13+
14+
import org.junit.jupiter.api.Test;
15+
16+
import io.vertx.junit5.VertxTestContext;
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.Id;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.hibernate.reactive.testing.ReactiveAssertions.assertThrown;
22+
23+
public class TransactionValidationErrorTest extends BaseReactiveTest {
24+
25+
@Override
26+
protected Collection<Class<?>> annotatedEntities() {
27+
return List.of( Comic.class );
28+
}
29+
30+
@Test
31+
public void beginTransactionError(VertxTestContext context) {
32+
test( context, assertThrown( IllegalStateException.class, openSession()
33+
.thenCompose( s -> {
34+
ReactiveConnection connection = reactiveConnection( s );
35+
return connection.beginTransaction()
36+
.thenCompose( t1 -> connection.beginTransaction() );
37+
} )
38+
)
39+
.thenAccept( e -> {
40+
assertThat( e )
41+
.hasMessageContaining( "HR000090" )
42+
.hasMessageContaining( "starting a new transaction" );
43+
} )
44+
);
45+
}
46+
47+
private static ReactiveConnection reactiveConnection(Stage.Session s) {
48+
return ( (StageSessionImpl) s ).getReactiveConnection();
49+
}
50+
51+
@Test
52+
public void rollbackOnCloseWithStage(VertxTestContext context) {
53+
Comic beneath = new Comic( "979-8887241081", "Beneath The Trees Where Nobody Sees" );
54+
55+
test(
56+
context,
57+
assertThrown( IllegalStateException.class, getSessionFactory()
58+
.withTransaction( s -> s
59+
.persist( beneath )
60+
.thenCompose( v -> s.flush() )
61+
// Close the connection before committing
62+
.thenCompose( v -> s.close() )
63+
)
64+
)
65+
.thenAccept( e -> assertThat( e )
66+
.hasMessageContaining( "HR000090" )
67+
.hasMessageContaining( "closing the connection" )
68+
)
69+
.thenCompose( v -> getSessionFactory()
70+
.withTransaction( s -> s.find( Comic.class, beneath.isbn ) )
71+
)
72+
.thenAccept( result -> assertThat( result )
73+
.as( "The persist should have been roll backed" )
74+
.isNull() )
75+
);
76+
}
77+
78+
@Test
79+
public void rollbackOnErrorWithStage(VertxTestContext context) {
80+
Comic beneath = new Comic( "979-8887241081", "Beneath The Trees Where Nobody Sees" );
81+
final RuntimeException ohNo = new RuntimeException( "Oh, no!" );
82+
test(
83+
context,
84+
assertThrown( RuntimeException.class, getSessionFactory()
85+
.withTransaction( s -> s
86+
.persist( beneath )
87+
.thenCompose( v -> s.flush() )
88+
// Close the connection before committing
89+
.thenAccept( v -> {
90+
throw ohNo;
91+
} )
92+
)
93+
)
94+
.thenAccept( e -> assertThat( e ).hasMessageContaining( ohNo.getMessage() ) )
95+
.thenCompose( v -> getSessionFactory()
96+
.withTransaction( s -> s.find( Comic.class, beneath.isbn ) )
97+
)
98+
.thenAccept( result -> assertThat( result )
99+
.as( "The persist should have been roll backed" )
100+
.isNull() )
101+
);
102+
}
103+
104+
@Test
105+
public void rollbackOnClose(VertxTestContext context) {
106+
Comic beneath = new Comic( "979-8887241081", "Beneath The Trees Where Nobody Sees" );
107+
108+
test(
109+
context,
110+
assertThrown( IllegalStateException.class, getMutinySessionFactory()
111+
.withTransaction( s -> s
112+
.persist( beneath )
113+
.call( s::flush )
114+
// Close the connection before committing
115+
.call( s::close )
116+
)
117+
)
118+
.invoke( e -> assertThat( e )
119+
.hasMessageContaining( "HR000090" )
120+
.hasMessageContaining( "closing the connection" )
121+
)
122+
.chain( () -> getMutinySessionFactory()
123+
.withTransaction( s -> s.find( Comic.class, beneath.isbn ) )
124+
)
125+
.invoke( result -> assertThat( result )
126+
.as( "The persist should have been roll backed" )
127+
.isNull() )
128+
);
129+
}
130+
131+
@Test
132+
public void rollbackOnError(VertxTestContext context) {
133+
Comic beneath = new Comic( "979-8887241081", "Beneath The Trees Where Nobody Sees" );
134+
final RuntimeException ohNo = new RuntimeException( "Oh, no!" );
135+
test(
136+
context,
137+
assertThrown( RuntimeException.class, getMutinySessionFactory()
138+
.withTransaction( s -> s
139+
.persist( beneath )
140+
.call( s::flush )
141+
.chain( () -> {
142+
throw ohNo;
143+
} )
144+
)
145+
)
146+
.invoke( e -> assertThat( e ).hasMessageContaining( ohNo.getMessage() ) )
147+
.chain( () -> getMutinySessionFactory()
148+
.withTransaction( s -> s.find( Comic.class, beneath.isbn ) )
149+
)
150+
.invoke( result -> assertThat( result )
151+
.as( "The persist should have been roll backed" )
152+
.isNull() )
153+
);
154+
}
155+
156+
@Entity
157+
public static class Comic {
158+
@Id
159+
public String isbn;
160+
public String title;
161+
162+
public Comic() {
163+
}
164+
165+
public Comic(String iban, String title) {
166+
this.isbn = iban;
167+
this.title = title;
168+
}
169+
170+
@Override
171+
public String toString() {
172+
return isbn + ":" + title;
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)