|
| 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.services; |
| 7 | + |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.TreeMap; |
| 14 | +import java.util.TreeSet; |
| 15 | + |
| 16 | +import org.hibernate.boot.registry.StandardServiceInitiator; |
| 17 | +import org.hibernate.reactive.provider.impl.ReactiveServiceInitiators; |
| 18 | +import org.hibernate.service.StandardServiceInitiators; |
| 19 | + |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +/** |
| 24 | + * Useful to spot inconsistencies in the default ServiceInitiator lists |
| 25 | + * used by Hibernate Reactive compared to Hibernate ORM. |
| 26 | + * N.B. Hibernate Reactive defines some additional services. |
| 27 | + */ |
| 28 | +public class ServiceInitiatorsTest { |
| 29 | + |
| 30 | + private static final Map<String,String> HR_SERVICES = toServicesMap( ReactiveServiceInitiators.LIST ); |
| 31 | + private static final Map<String,String> ORM_SERVICES = toServicesMap( StandardServiceInitiators.LIST ); |
| 32 | + |
| 33 | + // These services are NOT provided by the Hibernate Reactive default initiators, and that should be fine: |
| 34 | + private static final Set<String> HR_INTENTIONALLY_OMITTED = Set.of( "org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformResolver" ); |
| 35 | + |
| 36 | + @Test |
| 37 | + public void serviceInitiatorsAreUnique() { |
| 38 | + Assert.assertEquals( HR_SERVICES.size(), ReactiveServiceInitiators.LIST.size() ); |
| 39 | + Assert.assertEquals( ORM_SERVICES.size(), StandardServiceInitiators.LIST.size() ); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void allORMServicesAreDefined() { |
| 44 | + Set<String> sharedServiceImplementations = new TreeSet<>(); |
| 45 | + Set<String> notSharedServiceImplementations = new TreeSet<>(); |
| 46 | + reportDivider( "All Services in sorted order" ); |
| 47 | + int i = 1; |
| 48 | + for ( String key : ORM_SERVICES.keySet() ) { |
| 49 | + String error = "ORM service '" + key + "' is not defined by the HR services list"; |
| 50 | + Assert.assertTrue( error, HR_SERVICES.containsKey( key ) || HR_INTENTIONALLY_OMITTED.contains( key ) ); |
| 51 | + if ( Objects.equals( HR_SERVICES.get( key ), ORM_SERVICES.get( key ) ) ) { |
| 52 | + sharedServiceImplementations.add( key ); |
| 53 | + } |
| 54 | + else { |
| 55 | + notSharedServiceImplementations.add( key ); |
| 56 | + } |
| 57 | + StringBuilder entry = new StringBuilder(); |
| 58 | + entry.append( " #" ).append( i++ ).append( '\t' ).append( "service role " ).append( key ).append( '\n' ); |
| 59 | + entry.append( "\tORM: " ).append( ORM_SERVICES.get( key ) ).append( '\n' ); |
| 60 | + entry.append( "\tHR: " ).append( HR_SERVICES.get( key ) ).append( '\n' ); |
| 61 | + System.out.println( entry ); |
| 62 | + } |
| 63 | + reportDivider( "All Services which are shared among ORM and HR:" ); |
| 64 | + for ( String key : sharedServiceImplementations ) { |
| 65 | + System.out.println( key ); |
| 66 | + } |
| 67 | + reportDivider( "All Services which are overridden in HR:" ); |
| 68 | + for ( String key : notSharedServiceImplementations ) { |
| 69 | + StringBuilder entry = new StringBuilder(); |
| 70 | + entry.append( " service role " ).append( key ).append( '\n' ); |
| 71 | + entry.append( "\tORM: " ).append( ORM_SERVICES.get( key ) ).append( '\n' ); |
| 72 | + entry.append( "\tHR: " ).append( HR_SERVICES.get( key ) ).append( '\n' ); |
| 73 | + System.out.println( entry ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void reportDivider(String title) { |
| 78 | + System.out.println( "\n ########### " + title + "\n" ); |
| 79 | + } |
| 80 | + |
| 81 | + //N.B. service identity and class identity are handled by their name: |
| 82 | + //this is possibly more restrictive than strictly required in certain environments, |
| 83 | + //but we need to ensure safety in all classloading models. |
| 84 | + private static Map<String, String> toServicesMap(List<StandardServiceInitiator<?>> list) { |
| 85 | + TreeMap<String,String> rolesToImplMap = new TreeMap<>(); |
| 86 | + for ( StandardServiceInitiator<?> initiator : list ) { |
| 87 | + final String serviceRole = initiator.getServiceInitiated().getName(); |
| 88 | + rolesToImplMap.put( serviceRole, initiator.getClass().getName() ); |
| 89 | + } |
| 90 | + return Collections.unmodifiableMap( rolesToImplMap ); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments