Skip to content

Commit 7b50c90

Browse files
committed
[#2330] Test collection events with StatelessSession
Add test from ORM issue HHH-19523
1 parent bace0f4 commit 7b50c90

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import java.util.ArrayList;
9+
import java.util.Collection;
10+
import java.util.List;
11+
import java.util.Set;
12+
13+
import org.hibernate.engine.spi.SessionFactoryImplementor;
14+
import org.hibernate.event.service.spi.EventListenerRegistry;
15+
import org.hibernate.event.spi.AbstractCollectionEvent;
16+
import org.hibernate.event.spi.EventType;
17+
import org.hibernate.event.spi.PostCollectionRecreateEvent;
18+
import org.hibernate.event.spi.PostCollectionRecreateEventListener;
19+
import org.hibernate.event.spi.PostCollectionRemoveEvent;
20+
import org.hibernate.event.spi.PostCollectionRemoveEventListener;
21+
import org.hibernate.event.spi.PreCollectionRecreateEvent;
22+
import org.hibernate.event.spi.PreCollectionRecreateEventListener;
23+
import org.hibernate.event.spi.PreCollectionRemoveEvent;
24+
import org.hibernate.event.spi.PreCollectionRemoveEventListener;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import io.vertx.junit5.VertxTestContext;
29+
import jakarta.persistence.Column;
30+
import jakarta.persistence.Entity;
31+
import jakarta.persistence.GeneratedValue;
32+
import jakarta.persistence.GenerationType;
33+
import jakarta.persistence.Id;
34+
import jakarta.persistence.JoinColumn;
35+
import jakarta.persistence.OneToMany;
36+
import jakarta.persistence.Table;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
40+
/**
41+
* Adapt test in ORM: CollectionStatelessSessionListenerTest
42+
*/
43+
public class CollectionStatelessSessionListenerTest extends BaseReactiveTest {
44+
45+
@Override
46+
protected Collection<Class<?>> annotatedEntities() {
47+
return Set.of( EntityA.class, EntityB.class );
48+
}
49+
50+
51+
@Test
52+
public void statelessInsert(VertxTestContext context) {
53+
final List<AbstractCollectionEvent> events = new ArrayList<>();
54+
initializeListeners( events );
55+
56+
EntityA a = new EntityA();
57+
EntityB b = new EntityB();
58+
a.children.add( b );
59+
test( context, getMutinySessionFactory()
60+
.withStatelessTransaction( statelessSession -> statelessSession
61+
.insert( b )
62+
.chain( () -> statelessSession.insert( a ) )
63+
.chain( () -> statelessSession.delete( a ) )
64+
.chain( () -> statelessSession.delete( b ) )
65+
)
66+
.invoke( () -> assertEvents( events ) )
67+
);
68+
}
69+
70+
@Test
71+
public void statelessInsertAll(VertxTestContext context) {
72+
final List<AbstractCollectionEvent> events = new ArrayList<>();
73+
initializeListeners( events );
74+
75+
EntityA a = new EntityA();
76+
EntityB b = new EntityB();
77+
a.children.add( b );
78+
test( context, getMutinySessionFactory()
79+
.withStatelessTransaction( statelessSession -> statelessSession
80+
.insertAll( b, a )
81+
.chain( () -> statelessSession.deleteAll( a, b ) )
82+
)
83+
.invoke( () -> assertEvents( events ) )
84+
);
85+
}
86+
87+
private static void assertEvents(List<AbstractCollectionEvent> events) {
88+
assertThat( events ).hasSize( 4 );
89+
assertThat( events.get( 0 ) )
90+
.isInstanceOf( PreCollectionRecreateEvent.class )
91+
.extracting( AbstractCollectionEvent::getAffectedOwnerEntityName ).isEqualTo( EntityA.class.getName() );
92+
assertThat( events.get( 1 ) )
93+
.isInstanceOf( PostCollectionRecreateEvent.class )
94+
.extracting( AbstractCollectionEvent::getAffectedOwnerEntityName ).isEqualTo( EntityA.class.getName() );
95+
assertThat( events.get( 2 ) )
96+
.isInstanceOf( PreCollectionRemoveEvent.class )
97+
.extracting( AbstractCollectionEvent::getAffectedOwnerEntityName ).isEqualTo( EntityA.class.getName() );
98+
assertThat( events.get( 3 ) )
99+
.isInstanceOf( PostCollectionRemoveEvent.class )
100+
.extracting( AbstractCollectionEvent::getAffectedOwnerEntityName ).isEqualTo( EntityA.class.getName() );
101+
}
102+
103+
private void initializeListeners(List<AbstractCollectionEvent> events) {
104+
final EventListenerRegistry registry = ( (SessionFactoryImplementor) factoryManager
105+
.getHibernateSessionFactory() )
106+
.getEventListenerRegistry();
107+
108+
// Clear previous listeners
109+
registry.getEventListenerGroup( EventType.PRE_COLLECTION_RECREATE )
110+
.clearListeners();
111+
registry.getEventListenerGroup( EventType.PRE_COLLECTION_REMOVE )
112+
.clearListeners();
113+
registry.getEventListenerGroup( EventType.POST_COLLECTION_RECREATE )
114+
.clearListeners();
115+
registry.getEventListenerGroup( EventType.POST_COLLECTION_REMOVE )
116+
.clearListeners();
117+
118+
var preRecreate = new MyPreCollectionRecreateEventListener( events );
119+
var preRemove = new MyPreCollectionRemoveEventListener( events );
120+
var postRecreate = new MyPostCollectionRecreateEventListener( events );
121+
var postRemove = new MyPostCollectionRemoveEventListener( events );
122+
123+
// Add new listeners
124+
registry.getEventListenerGroup( EventType.PRE_COLLECTION_RECREATE )
125+
.appendListener( preRecreate );
126+
registry.getEventListenerGroup( EventType.PRE_COLLECTION_REMOVE )
127+
.appendListener( preRemove );
128+
registry.getEventListenerGroup( EventType.POST_COLLECTION_RECREATE )
129+
.appendListener( postRecreate );
130+
registry.getEventListenerGroup( EventType.POST_COLLECTION_REMOVE )
131+
.appendListener( postRemove );
132+
}
133+
134+
@Entity
135+
@Table(name = "ENTITY_A")
136+
public static class EntityA {
137+
138+
@Id
139+
@GeneratedValue(strategy = GenerationType.AUTO)
140+
@Column(name = "ID")
141+
Integer id;
142+
143+
@OneToMany
144+
@JoinColumn(name = "ENTITY_A")
145+
Collection<EntityB> children = new ArrayList<>();
146+
}
147+
148+
@Entity
149+
@Table(name = "ENTITY_B")
150+
public class EntityB {
151+
@Id
152+
@GeneratedValue(strategy = GenerationType.AUTO)
153+
@Column(name = "ID")
154+
Integer id;
155+
}
156+
157+
public static class MyPreCollectionRecreateEventListener implements PreCollectionRecreateEventListener {
158+
159+
private final List<AbstractCollectionEvent> events;
160+
161+
162+
public MyPreCollectionRecreateEventListener(List<AbstractCollectionEvent> events) {
163+
this.events = events;
164+
}
165+
166+
@Override
167+
public void onPreRecreateCollection(PreCollectionRecreateEvent event) {
168+
events.add( event );
169+
}
170+
171+
}
172+
173+
public static class MyPreCollectionRemoveEventListener implements PreCollectionRemoveEventListener {
174+
175+
private final List<AbstractCollectionEvent> events;
176+
177+
public MyPreCollectionRemoveEventListener(List<AbstractCollectionEvent> events) {
178+
this.events = events;
179+
}
180+
181+
@Override
182+
public void onPreRemoveCollection(PreCollectionRemoveEvent event) {
183+
events.add( event );
184+
}
185+
186+
}
187+
188+
public static class MyPostCollectionRecreateEventListener implements PostCollectionRecreateEventListener {
189+
190+
private final List<AbstractCollectionEvent> events;
191+
192+
public MyPostCollectionRecreateEventListener(List<AbstractCollectionEvent> events) {
193+
this.events = events;
194+
}
195+
196+
@Override
197+
public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
198+
events.add( event );
199+
}
200+
}
201+
202+
public static class MyPostCollectionRemoveEventListener implements PostCollectionRemoveEventListener {
203+
204+
private final List<AbstractCollectionEvent> events;
205+
206+
public MyPostCollectionRemoveEventListener(List<AbstractCollectionEvent> events) {
207+
this.events = events;
208+
}
209+
210+
@Override
211+
public void onPostRemoveCollection(PostCollectionRemoveEvent event) {
212+
events.add( event );
213+
}
214+
}
215+
}

0 commit comments

Comments
 (0)