Skip to content

Commit ed22492

Browse files
committed
[hibernate#1906] Clean up tests
1 parent 4eefa06 commit ed22492

File tree

3 files changed

+116
-175
lines changed

3 files changed

+116
-175
lines changed

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

Lines changed: 69 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
package org.hibernate.reactive;
77

8+
import java.util.Collection;
9+
import java.util.Date;
10+
import java.util.List;
11+
812
import org.hibernate.annotations.ColumnDefault;
913
import org.hibernate.annotations.Generated;
1014
import org.hibernate.annotations.NaturalId;
@@ -37,9 +41,6 @@
3741
import jakarta.persistence.GeneratedValue;
3842
import jakarta.persistence.GenerationType;
3943
import jakarta.persistence.Id;
40-
import java.util.Collection;
41-
import java.util.Date;
42-
import java.util.List;
4344

4445
import static org.assertj.core.api.Assertions.assertThat;
4546
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
@@ -67,11 +68,11 @@ protected Collection<Class<?>> annotatedEntities() {
6768
protected Configuration constructConfiguration() {
6869
Configuration configuration = super.constructConfiguration();
6970
// Batch size is only enabled to make sure it's ignored when using mutation delegates
70-
configuration.setProperty( AvailableSettings.STATEMENT_BATCH_SIZE, "5");
71+
configuration.setProperty( AvailableSettings.STATEMENT_BATCH_SIZE, "5" );
7172

7273
// Construct a tracker that collects query statements via the SqlStatementLogger framework.
7374
// Pass in configuration properties to hand off any actual logging properties
74-
sqlTracker = new SqlStatementTracker( MutationDelegateIdentityTest::filter, configuration.getProperties() );
75+
sqlTracker = new SqlStatementTracker( s -> true, configuration.getProperties() );
7576
return configuration;
7677
}
7778

@@ -85,20 +86,10 @@ protected void addServices(StandardServiceRegistryBuilder builder) {
8586
sqlTracker.registerService( builder );
8687
}
8788

88-
private static boolean filter(String s) {
89-
String[] accepted = { "insert ", "update ", "delete ", "select " };
90-
for ( String valid : accepted ) {
91-
if ( s.toLowerCase().startsWith( valid ) ) {
92-
return true;
93-
}
94-
}
95-
return false;
96-
}
97-
9889
@Test
9990
public void testInsertGeneratedIdentityOnly(VertxTestContext context) {
10091
final GeneratedValuesMutationDelegate delegate = getDelegate( IdentityOnly.class, MutationType.INSERT );
101-
92+
final int expectedQueriesSize = delegate instanceof AbstractReturningDelegate ? 1 : 2;
10293
final IdentityOnly entity = new IdentityOnly();
10394

10495
test( context, getMutinySessionFactory().withTransaction( s -> s
@@ -107,16 +98,18 @@ public void testInsertGeneratedIdentityOnly(VertxTestContext context) {
10798
.invoke( () -> {
10899
assertThat( entity.getId() ).isNotNull();
109100
assertThat( entity.getName() ).isNull();
101+
assertThat( sqlTracker.getLoggedQueries() ).hasSize( expectedQueriesSize );
110102
assertThat( sqlTracker.getLoggedQueries().get( 0 ) ).contains( "insert" );
111-
assertExecutedQueriesCount( delegate instanceof AbstractReturningDelegate ? 1 : 2 );
112103
} )
113-
));
104+
) );
114105
}
115106

116107
@Test
117108
public void testInsertGeneratedValuesAndIdentity(VertxTestContext context) {
118109
final GeneratedValuesMutationDelegate delegate = getDelegate( IdentityAndValues.class, MutationType.INSERT );
119-
110+
final int expectedQueriesSize = delegate instanceof AbstractSelectingDelegate
111+
? 3
112+
: delegate != null && delegate.supportsArbitraryValues() ? 1 : 2;
120113
final IdentityAndValues entity = new IdentityAndValues();
121114

122115
test( context, getMutinySessionFactory().withTransaction( s -> s
@@ -125,118 +118,107 @@ public void testInsertGeneratedValuesAndIdentity(VertxTestContext context) {
125118
.invoke( () -> {
126119
assertThat( entity.getId() ).isNotNull();
127120
assertThat( entity.getName() ).isEqualTo( "default_name" );
121+
assertThat( sqlTracker.getLoggedQueries() ).hasSize( expectedQueriesSize );
128122
assertThat( sqlTracker.getLoggedQueries().get( 0 ) ).contains( "insert" );
129-
assertExecutedQueriesCount(
130-
delegate instanceof AbstractSelectingDelegate
131-
? 3
132-
: delegate != null && delegate.supportsArbitraryValues() ? 1 : 2
133-
);
134-
})
135-
));
123+
} )
124+
) );
136125
}
137126

138127
@Test
139128
public void testUpdateGeneratedValuesAndIdentity(VertxTestContext context) {
140129
final GeneratedValuesMutationDelegate delegate = getDelegate( IdentityAndValues.class, MutationType.UPDATE );
141130
final IdentityAndValues entity = new IdentityAndValues();
131+
final int expectedQuerySize = delegate != null && delegate.supportsArbitraryValues() ? 3 : 4;
142132

143-
test( context, getMutinySessionFactory().withTransaction( s -> s
144-
.persist( entity ) )
145-
.invoke( () -> sqlTracker.clear() )
133+
test( context, getMutinySessionFactory()
134+
.withTransaction( s -> s.persist( entity ) )
135+
.invoke( sqlTracker::clear )
146136
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
147137
.find( IdentityAndValues.class, entity.getId() )
148-
.invoke( identityAndValues -> identityAndValues.setData( "changed" ) )
149-
).chain( () -> getMutinySessionFactory().withTransaction( s -> s
138+
.invoke( identityAndValues -> identityAndValues.setData( "changed" ) )
139+
) )
140+
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
150141
.find( IdentityAndValues.class, entity.getId() )
151142
.invoke( identityAndValues -> {
152143
assertThat( entity.getUpdateDate() ).isNotNull();
153-
sqlTracker.getLoggedQueries().get( 0 ).startsWith( "select" );
154-
assertThat( sqlTracker.getLoggedQueries().get( 0 ) ).contains( "update" );
155-
assertExecutedQueriesCount(
156-
delegate != null && delegate.supportsArbitraryValues() ? 3 : 4
157-
);
158-
})
159-
) ) )
144+
assertThat( sqlTracker.getLoggedQueries().size() ).isEqualTo( expectedQuerySize );
145+
assertThat( sqlTracker.getLoggedQueries().get( 0 ) )
146+
.startsWith( "select" ).contains( "update" );
147+
} )
148+
) )
160149
);
161150
}
162151

163152
@Test
164153
@DisabledFor(value = ORACLE, reason = "Vert.x driver doesn't support RowId type parameters")
165154
public void testInsertGeneratedValuesAndIdentityAndRowId(VertxTestContext context) {
166155
final GeneratedValuesMutationDelegate delegate = getDelegate( IdentityAndValuesAndRowId.class, MutationType.INSERT );
167-
168-
final boolean shouldHaveRowId = delegate != null && delegate.supportsRowId()
169-
&& getDialect().rowId( "" ) != null;
156+
final int expectedQueriesSize = delegate instanceof AbstractSelectingDelegate
157+
? 3
158+
: delegate != null && delegate.supportsArbitraryValues() ? 1 : 2;
159+
final boolean shouldHaveRowId = delegate != null && delegate.supportsRowId() && getDialect().rowId( "" ) != null;
170160
final IdentityAndValuesAndRowId entity = new IdentityAndValuesAndRowId();
171161

172-
test(context, getMutinySessionFactory().withTransaction( s -> s
173-
.persist( entity )
174-
.call( s::flush )
175-
.invoke( () -> {
176-
assertThat( entity.getId() ).isNotNull();
177-
assertThat( entity.getName() ).isEqualTo( "default_name" );
178-
179-
assertThat( sqlTracker.getLoggedQueries().get( 0 ) ).contains( "insert" );
180-
assertExecutedQueriesCount(
181-
delegate instanceof AbstractSelectingDelegate
182-
? 3
183-
: delegate != null && delegate.supportsArbitraryValues() ? 1 : 2
184-
);
185-
186-
if ( shouldHaveRowId ) {
187-
// assert row-id was populated in entity entry
188-
final PersistenceContext pc = ( (MutinySessionImpl) s ).unwrap( ReactiveSession.class )
189-
.getPersistenceContext();
190-
final EntityEntry entry = pc.getEntry( entity );
191-
assertThat( entry.getRowId() ).isNotNull();
192-
}
193-
sqlTracker.clear();
194-
entity.setData( "changed" );
195-
} )
196-
.call( s::flush )
197-
.invoke( () -> {
198-
assertThat( entity.getUpdateDate() ).isNotNull();
199-
assertThat( sqlTracker.getLoggedQueries().get( 0 ) ).contains( "update" );
200-
assertNumberOfOccurrenceInQueryNoSpace( 0, "id_column", shouldHaveRowId ? 0 : 1 );
201-
} )
202-
).chain( () -> getMutinySessionFactory().withTransaction( s -> s
203-
.find( IdentityAndValuesAndRowId.class, entity.getId() )
204-
.invoke( identityAndValuesAndRowId -> assertThat( identityAndValuesAndRowId.getUpdateDate() ).isNotNull() )
162+
test( context, getMutinySessionFactory()
163+
.withTransaction( s -> s
164+
.persist( entity )
165+
.call( s::flush )
166+
.invoke( () -> {
167+
assertThat( entity.getId() ).isNotNull();
168+
assertThat( entity.getName() ).isEqualTo( "default_name" );
169+
assertThat( sqlTracker.getLoggedQueries() ).hasSize( expectedQueriesSize );
170+
assertThat( sqlTracker.getLoggedQueries().get( 0 ) ).contains( "insert" );
171+
if ( shouldHaveRowId ) {
172+
// assert row-id was populated in entity entry
173+
final PersistenceContext pc = ( (MutinySessionImpl) s )
174+
.unwrap( ReactiveSession.class )
175+
.getPersistenceContext();
176+
final EntityEntry entry = pc.getEntry( entity );
177+
assertThat( entry.getRowId() ).isNotNull();
178+
}
179+
sqlTracker.clear();
180+
entity.setData( "changed" );
181+
} )
182+
.call( s::flush )
183+
.invoke( () -> {
184+
assertThat( entity.getUpdateDate() ).isNotNull();
185+
assertThat( sqlTracker.getLoggedQueries().get( 0 ) ).contains( "update" );
186+
assertNumberOfOccurrenceInQueryNoSpace( 0, "id_column", shouldHaveRowId ? 0 : 1 );
187+
} )
188+
)
189+
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
190+
.find( IdentityAndValuesAndRowId.class, entity.getId() )
191+
.invoke( identityAndValuesAndRowId -> assertThat( identityAndValuesAndRowId.getUpdateDate() ).isNotNull() )
205192
) )
206193
);
207194
}
208195

209196
@Test
210197
@DisabledFor(value = ORACLE, reason = "Vert.x driver doesn't support RowId type parameters")
211198
public void testInsertGeneratedValuesAndIdentityAndRowIdAndNaturalId(VertxTestContext context) {
212-
final GeneratedValuesMutationDelegate delegate = getDelegate(
213-
IdentityAndValuesAndRowIdAndNaturalId.class,
214-
MutationType.INSERT
215-
);
199+
final GeneratedValuesMutationDelegate delegate = getDelegate( IdentityAndValuesAndRowIdAndNaturalId.class, MutationType.INSERT );
216200
final IdentityAndValuesAndRowIdAndNaturalId entity = new IdentityAndValuesAndRowIdAndNaturalId( "naturalid_1" );
217-
201+
final boolean isUniqueKeyDelegate = delegate instanceof UniqueKeySelectingDelegate;
202+
final int expectedQueriesSize = delegate == null || !delegate.supportsArbitraryValues() || isUniqueKeyDelegate ? 2 : 1;
218203
test(context, getMutinySessionFactory().withTransaction( s -> s
219204
.persist( entity )
220205
.call( s::flush )
221206
.invoke( () -> {
222207
assertThat( entity.getId() ).isNotNull();
223208
assertThat( entity.getName() ).isEqualTo( "default_name" );
224-
209+
assertThat( sqlTracker.getLoggedQueries() ).hasSize( expectedQueriesSize );
225210
assertThat( sqlTracker.getLoggedQueries().get( 0 ) ).contains( "insert" );
226-
final boolean isUniqueKeyDelegate = delegate instanceof UniqueKeySelectingDelegate;
227-
assertExecutedQueriesCount(
228-
delegate == null || !delegate.supportsArbitraryValues() || isUniqueKeyDelegate ? 2 : 1
229-
);
211+
230212
if ( isUniqueKeyDelegate ) {
231213
assertNumberOfOccurrenceInQueryNoSpace( 1, "data", 1 );
232214
assertNumberOfOccurrenceInQueryNoSpace( 1, "id_column", 1 );
233215
}
234216

235-
final boolean shouldHaveRowId = delegate != null && delegate.supportsRowId()
236-
&& getDialect().rowId( "" ) != null;
217+
final boolean shouldHaveRowId = delegate != null && delegate.supportsRowId() && getDialect().rowId( "" ) != null;
237218
if ( shouldHaveRowId ) {
238219
// assert row-id was populated in entity entry
239-
final PersistenceContext pc = ( (MutinySessionImpl) s ).unwrap( ReactiveSession.class )
220+
final PersistenceContext pc = ( (MutinySessionImpl) s )
221+
.unwrap( ReactiveSession.class )
240222
.getPersistenceContext();
241223
final EntityEntry entry = pc.getEntry( entity );
242224
assertThat( entry.getRowId() ).isNotNull();
@@ -245,10 +227,6 @@ public void testInsertGeneratedValuesAndIdentityAndRowIdAndNaturalId(VertxTestCo
245227
);
246228
}
247229

248-
private static void assertExecutedQueriesCount(int expected) {
249-
assertThat( sqlTracker.getLoggedQueries().size() ).isEqualTo( expected );
250-
}
251-
252230
private static void assertNumberOfOccurrenceInQueryNoSpace(int queryNumber, String toCheck, int expectedNumberOfOccurrences) {
253231
String query = sqlTracker.getLoggedQueries().get( queryNumber );
254232
int actual = query.split( toCheck, -1 ).length - 1;

0 commit comments

Comments
 (0)