/
BaseCoreFunctionalTestCase.java
497 lines (421 loc) · 14.1 KB
/
BaseCoreFunctionalTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.testing.junit4;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Consumer;
import javax.persistence.SharedCacheMode;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.jdbc.AbstractReturningWork;
import org.hibernate.jdbc.Work;
import org.hibernate.resource.transaction.spi.TransactionCoordinator;
import org.hibernate.testing.AfterClassOnce;
import org.hibernate.testing.BeforeClassOnce;
import org.hibernate.testing.OnExpectedFailure;
import org.hibernate.testing.OnFailure;
import org.hibernate.testing.SkipLog;
import org.hibernate.testing.cache.CachingRegionFactory;
import org.junit.After;
import org.junit.Before;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.fail;
/**
* Applies functional testing logic for core Hibernate testing on top of {@link BaseUnitTestCase}
*
* @author Steve Ebersole
*/
@SuppressWarnings( {"deprecation"} )
public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
public static final String VALIDATE_DATA_CLEANUP = "hibernate.test.validateDataCleanup";
public static final Dialect DIALECT = Dialect.getDialect();
private Configuration configuration;
private StandardServiceRegistryImpl serviceRegistry;
private SessionFactoryImplementor sessionFactory;
protected Session session;
protected static Dialect getDialect() {
return DIALECT;
}
protected Configuration configuration() {
return configuration;
}
protected StandardServiceRegistryImpl serviceRegistry() {
return serviceRegistry;
}
protected SessionFactoryImplementor sessionFactory() {
return sessionFactory;
}
protected Session openSession() throws HibernateException {
session = sessionFactory().openSession();
return session;
}
protected Session openSession(Interceptor interceptor) throws HibernateException {
session = sessionFactory().withOptions().interceptor( interceptor ).openSession();
return session;
}
// beforeQuery/afterQuery test class ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@BeforeClassOnce
@SuppressWarnings( {"UnusedDeclaration"})
protected void buildSessionFactory() {
buildSessionFactory( null );
}
protected void buildSessionFactory(Consumer<Configuration> configurationAdapter) {
// for now, build the configuration to get all the property settings
configuration = constructAndConfigureConfiguration();
if ( configurationAdapter != null ) {
configurationAdapter.accept(configuration);
}
BootstrapServiceRegistry bootRegistry = buildBootstrapServiceRegistry();
serviceRegistry = buildServiceRegistry( bootRegistry, configuration );
// this is done here because Configuration does not currently support 4.0 xsd
afterConstructAndConfigureConfiguration( configuration );
sessionFactory = ( SessionFactoryImplementor ) configuration.buildSessionFactory( serviceRegistry );
afterSessionFactoryBuilt();
}
protected void rebuildSessionFactory() {
rebuildSessionFactory( null );
}
protected void rebuildSessionFactory(Consumer<Configuration> configurationAdapter) {
if ( sessionFactory == null ) {
return;
}
try {
sessionFactory.close();
sessionFactory = null;
configuration = null;
serviceRegistry.destroy();
serviceRegistry = null;
}
catch (Exception ignore) {
}
buildSessionFactory( configurationAdapter );
}
protected Configuration buildConfiguration() {
Configuration cfg = constructAndConfigureConfiguration();
afterConstructAndConfigureConfiguration( cfg );
return cfg;
}
protected Configuration constructAndConfigureConfiguration() {
Configuration cfg = constructConfiguration();
configure( cfg );
return cfg;
}
private void afterConstructAndConfigureConfiguration(Configuration cfg) {
addMappings( cfg );
applyCacheSettings( cfg );
afterConfigurationBuilt( cfg );
}
protected Configuration constructConfiguration() {
Configuration configuration = new Configuration();
configuration.setProperty( AvailableSettings.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() );
configuration.setProperty( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true" );
if ( createSchema() ) {
configuration.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
final String secondSchemaName = createSecondSchema();
if ( StringHelper.isNotEmpty( secondSchemaName ) ) {
if ( !( getDialect() instanceof H2Dialect ) ) {
throw new UnsupportedOperationException( "Only H2 dialect supports creation of second schema." );
}
Helper.createH2Schema( secondSchemaName, configuration );
}
}
configuration.setImplicitNamingStrategy( ImplicitNamingStrategyLegacyJpaImpl.INSTANCE );
configuration.setProperty( Environment.DIALECT, getDialect().getClass().getName() );
return configuration;
}
protected void configure(Configuration configuration) {
}
protected void addMappings(Configuration configuration) {
String[] mappings = getMappings();
if ( mappings != null ) {
for ( String mapping : mappings ) {
configuration.addResource(
getBaseForMappings() + mapping,
getClass().getClassLoader()
);
}
}
Class<?>[] annotatedClasses = getAnnotatedClasses();
if ( annotatedClasses != null ) {
for ( Class<?> annotatedClass : annotatedClasses ) {
configuration.addAnnotatedClass( annotatedClass );
}
}
String[] annotatedPackages = getAnnotatedPackages();
if ( annotatedPackages != null ) {
for ( String annotatedPackage : annotatedPackages ) {
configuration.addPackage( annotatedPackage );
}
}
String[] xmlFiles = getXmlFiles();
if ( xmlFiles != null ) {
for ( String xmlFile : xmlFiles ) {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFile );
configuration.addInputStream( is );
}
}
}
protected static final String[] NO_MAPPINGS = new String[0];
protected String[] getMappings() {
return NO_MAPPINGS;
}
protected String getBaseForMappings() {
return "org/hibernate/test/";
}
protected static final Class<?>[] NO_CLASSES = new Class[0];
protected Class<?>[] getAnnotatedClasses() {
return NO_CLASSES;
}
protected String[] getAnnotatedPackages() {
return NO_MAPPINGS;
}
protected String[] getXmlFiles() {
// todo : rename to getOrmXmlFiles()
return NO_MAPPINGS;
}
protected void applyCacheSettings(Configuration configuration) {
if ( getCacheConcurrencyStrategy() != null ) {
configuration.setProperty( AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, getCacheConcurrencyStrategy() );
configuration.setSharedCacheMode( SharedCacheMode.ALL );
}
}
protected String getCacheConcurrencyStrategy() {
return null;
}
protected void afterConfigurationBuilt(Configuration configuration) {
}
protected BootstrapServiceRegistry buildBootstrapServiceRegistry() {
final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
prepareBootstrapRegistryBuilder( builder );
return builder.build();
}
protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder builder) {
}
protected StandardServiceRegistryImpl buildServiceRegistry(BootstrapServiceRegistry bootRegistry, Configuration configuration) {
Properties properties = new Properties();
properties.putAll( configuration.getProperties() );
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
StandardServiceRegistryBuilder cfgRegistryBuilder = configuration.getStandardServiceRegistryBuilder();
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder( bootRegistry, cfgRegistryBuilder.getAggregatedCfgXml() )
.applySettings( properties );
prepareBasicRegistryBuilder( registryBuilder );
return (StandardServiceRegistryImpl) registryBuilder.build();
}
protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) {
}
protected void afterSessionFactoryBuilt() {
}
protected boolean createSchema() {
return true;
}
/**
* Feature supported only by H2 dialect.
* @return Provide not empty name to create second schema.
*/
protected String createSecondSchema() {
return null;
}
protected boolean rebuildSessionFactoryOnError() {
return true;
}
@AfterClassOnce
@SuppressWarnings( {"UnusedDeclaration"})
protected void releaseSessionFactory() {
if ( sessionFactory == null ) {
return;
}
sessionFactory.close();
sessionFactory = null;
configuration = null;
if ( serviceRegistry != null ) {
if ( serviceRegistry.isActive() ) {
try {
serviceRegistry.destroy();
}
catch (Exception ignore) {
}
fail( "StandardServiceRegistry was not closed down as expected" );
}
}
serviceRegistry=null;
}
@OnFailure
@OnExpectedFailure
@SuppressWarnings( {"UnusedDeclaration"})
public void onFailure() {
if ( rebuildSessionFactoryOnError() ) {
rebuildSessionFactory();
}
}
// beforeQuery/afterQuery each test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Before
public final void beforeTest() throws Exception {
prepareTest();
}
protected void prepareTest() throws Exception {
}
@After
public final void afterTest() throws Exception {
completeStrayTransaction();
if ( isCleanupTestDataRequired() ) {
cleanupTestData();
}
cleanupTest();
cleanupSession();
assertAllDataRemoved();
}
private void completeStrayTransaction() {
if ( session == null ) {
// nothing to do
return;
}
if ( ( (SessionImplementor) session ).isClosed() ) {
// nothing to do
return;
}
if ( !session.isConnected() ) {
// nothing to do
return;
}
final TransactionCoordinator.TransactionDriver tdc =
( (SessionImplementor) session ).getTransactionCoordinator().getTransactionDriverControl();
if ( tdc.getStatus().canRollback() ) {
session.getTransaction().rollback();
}
session.close();
}
protected void cleanupCache() {
if ( sessionFactory != null ) {
sessionFactory.getCache().evictAllRegions();
}
}
protected boolean isCleanupTestDataRequired() {
return false;
}
protected boolean isCleanupTestDataUsingBulkDelete() {
return false;
}
protected void cleanupTestData() throws Exception {
if(isCleanupTestDataUsingBulkDelete()) {
doInHibernate( this::sessionFactory, s -> {
s.createQuery( "delete from java.lang.Object" ).executeUpdate();
} );
}
else {
// Because of https://hibernate.atlassian.net/browse/HHH-5529,
// we can'trely on a Bulk Delete query which will not clear the link tables in @ElementCollection or unidirectional collections
doInHibernate( this::sessionFactory, s -> {
s.createQuery( "from java.lang.Object" ).list().forEach( s::remove );
} );
}
}
private void cleanupSession() {
if ( session != null && ! ( (SessionImplementor) session ).isClosed() ) {
session.close();
}
session = null;
}
public static class RollbackWork implements Work {
public void execute(Connection connection) throws SQLException {
connection.rollback();
}
}
protected void cleanupTest() throws Exception {
}
@SuppressWarnings( {"UnnecessaryBoxing", "UnnecessaryUnboxing"})
protected void assertAllDataRemoved() {
if ( !createSchema() ) {
return; // no tables were created...
}
if ( !Boolean.getBoolean( VALIDATE_DATA_CLEANUP ) ) {
return;
}
Session tmpSession = sessionFactory.openSession();
Transaction transaction = tmpSession.beginTransaction();
try {
List list = tmpSession.createQuery( "select o from java.lang.Object o" ).list();
Map<String,Integer> items = new HashMap<String,Integer>();
if ( !list.isEmpty() ) {
for ( Object element : list ) {
Integer l = items.get( tmpSession.getEntityName( element ) );
if ( l == null ) {
l = 0;
}
l = l + 1 ;
items.put( tmpSession.getEntityName( element ), l );
System.out.println( "Data left: " + element );
}
transaction.rollback();
fail( "Data is left in the database: " + items.toString() );
}
transaction.rollback();
}
finally {
try {
if(transaction.getStatus().canRollback()){
transaction.rollback();
}
tmpSession.close();
}
catch( Throwable t ) {
// intentionally empty
}
}
}
protected boolean readCommittedIsolationMaintained(String scenario) {
int isolation = java.sql.Connection.TRANSACTION_READ_UNCOMMITTED;
Session testSession = null;
try {
testSession = openSession();
isolation = testSession.doReturningWork(
new AbstractReturningWork<Integer>() {
@Override
public Integer execute(Connection connection) throws SQLException {
return connection.getTransactionIsolation();
}
}
);
}
catch( Throwable ignore ) {
}
finally {
if ( testSession != null ) {
try {
testSession.close();
}
catch( Throwable ignore ) {
}
}
}
if ( isolation < java.sql.Connection.TRANSACTION_READ_COMMITTED ) {
SkipLog.reportSkip( "environment does not support at least read committed isolation", scenario );
return false;
}
else {
return true;
}
}
}