|
| 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.annotations; |
| 7 | + |
| 8 | +import java.lang.annotation.Annotation; |
| 9 | +import java.lang.reflect.AnnotatedElement; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.LinkedHashMap; |
| 13 | + |
| 14 | +import org.hibernate.reactive.containers.DatabaseConfiguration; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 17 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 18 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 19 | + |
| 20 | +import org.jboss.logging.Logger; |
| 21 | + |
| 22 | + |
| 23 | +public class DBTypeFilterExtension implements ExecutionCondition { |
| 24 | + private static final Logger LOG = Logger.getLogger( DBTypeFilterExtension.class ); |
| 25 | + |
| 26 | + @Override |
| 27 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 28 | + // Tests are run on a single DB, so capture the current DB being tested. |
| 29 | + final DatabaseConfiguration.DBType dbType = DatabaseConfiguration.dbType(); |
| 30 | + if ( dbType == null ) { |
| 31 | + throw new RuntimeException( "#getDbType() returned null" ); |
| 32 | + } |
| 33 | + |
| 34 | + LOG.info( "Checking DBType [" + dbType + "] - context = " + context.getDisplayName() ); |
| 35 | + |
| 36 | + ConditionEvaluationResult enableResult = evaluateEnableConditions( context, dbType ); |
| 37 | + if( enableResult == null ) { |
| 38 | + return evaluateDisableConditions( context, dbType ); |
| 39 | + } |
| 40 | + if( enableResult.isDisabled() ) { |
| 41 | + LOG.info( enableResult.getReason() ); |
| 42 | + } |
| 43 | + return enableResult; |
| 44 | + } |
| 45 | + |
| 46 | + private ConditionEvaluationResult evaluateDisableConditions(ExtensionContext context, DatabaseConfiguration.DBType dbType) { |
| 47 | + final Collection<DisableFor> disabledDBTypesList = collectAnnotations( |
| 48 | + context, |
| 49 | + DisableFor.class, |
| 50 | + DisableForGroup.class, |
| 51 | + (methodAnnotation, methodAnnotations, classAnnotation, classAnnotations) -> { |
| 52 | + final LinkedHashMap<DatabaseConfiguration.DBType, DisableFor> map = new LinkedHashMap<>(); |
| 53 | + if ( classAnnotation != null ) { |
| 54 | + map.put( classAnnotation.value(), classAnnotation ); |
| 55 | + } |
| 56 | + if ( classAnnotations != null ) { |
| 57 | + for ( DisableFor annotation : classAnnotations ) { |
| 58 | + map.put( annotation.value(), annotation ); |
| 59 | + } |
| 60 | + } |
| 61 | + if ( methodAnnotation != null ) { |
| 62 | + map.put( methodAnnotation.value(), methodAnnotation ); |
| 63 | + } |
| 64 | + if ( methodAnnotations != null ) { |
| 65 | + for ( DisableFor annotation : methodAnnotations ) { |
| 66 | + map.put( annotation.value(), annotation ); |
| 67 | + } |
| 68 | + } |
| 69 | + return map.values(); |
| 70 | + } |
| 71 | + ); |
| 72 | + |
| 73 | + for ( DisableFor disableForDBType : disabledDBTypesList ) { |
| 74 | + DatabaseConfiguration.DBType currentDbType = disableForDBType.value(); |
| 75 | + if ( dbType == currentDbType ) { |
| 76 | + String skipMessage = " Skipping test for DB: " + dbType.toString() + |
| 77 | + " REASON: " + disableForDBType.reason(); |
| 78 | + LOG.info( skipMessage ); |
| 79 | + return ConditionEvaluationResult.disabled( skipMessage ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + LOG.info(" Enabling test for DB: " + dbType.toString() ); |
| 84 | + return ConditionEvaluationResult.enabled( "" ); |
| 85 | + } |
| 86 | + |
| 87 | + private ConditionEvaluationResult evaluateEnableConditions(ExtensionContext context, DatabaseConfiguration.DBType dbType) { |
| 88 | + final Collection<EnableFor> enabledDBTypesList = collectAnnotations( |
| 89 | + context, |
| 90 | + EnableFor.class, |
| 91 | + EnableForGroup.class, |
| 92 | + (methodAnnotation, methodAnnotations, classAnnotation, classAnnotations) -> { |
| 93 | + final LinkedHashMap<DatabaseConfiguration.DBType, EnableFor> map = new LinkedHashMap<>(); |
| 94 | + if ( classAnnotation != null ) { |
| 95 | + map.put( classAnnotation.value(), classAnnotation ); |
| 96 | + } |
| 97 | + if ( classAnnotations != null ) { |
| 98 | + for ( EnableFor annotation : classAnnotations ) { |
| 99 | + map.put( annotation.value(), annotation ); |
| 100 | + } |
| 101 | + } |
| 102 | + if ( methodAnnotation != null ) { |
| 103 | + map.put( methodAnnotation.value(), methodAnnotation ); |
| 104 | + } |
| 105 | + if ( methodAnnotations != null ) { |
| 106 | + for ( EnableFor annotation : methodAnnotations ) { |
| 107 | + map.put( annotation.value(), annotation ); |
| 108 | + } |
| 109 | + } |
| 110 | + return map.values(); |
| 111 | + } |
| 112 | + ); |
| 113 | + |
| 114 | + if( !enabledDBTypesList.isEmpty() ) { |
| 115 | + for ( EnableFor enableForDBType : enabledDBTypesList ) { |
| 116 | + DatabaseConfiguration.DBType currentDbType = enableForDBType.value(); |
| 117 | + if( dbType == currentDbType ) { |
| 118 | + LOG.info(" Enabling test for DB: " + dbType.toString() ); |
| 119 | + return ConditionEvaluationResult.enabled( "" ); |
| 120 | + } |
| 121 | + } |
| 122 | + return ConditionEvaluationResult.disabled( getMessageForNotAnEnabledDBType(enabledDBTypesList) ); |
| 123 | + } |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + private String getMessageForNotAnEnabledDBType(Collection<EnableFor> allowedDBTypes) { |
| 128 | + StringBuilder sb = new StringBuilder(); |
| 129 | + sb.append( "The test method or test class currently supports only these DBTypes: [ "); |
| 130 | + int i=1; |
| 131 | + for ( EnableFor enableForDBType : allowedDBTypes ) { |
| 132 | + DatabaseConfiguration.DBType currentDbType = enableForDBType.value(); |
| 133 | + sb.append( currentDbType.toString() ); |
| 134 | + if( i++ < allowedDBTypes.size() ) { |
| 135 | + sb.append(", "); |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + sb.append(" ] "); |
| 140 | + return sb.toString(); |
| 141 | + } |
| 142 | + |
| 143 | + private static <A extends Annotation> Collection<A> collectAnnotations( |
| 144 | + ExtensionContext context, |
| 145 | + Class<A> annotationType, |
| 146 | + Class<? extends Annotation> groupAnnotationType, |
| 147 | + TestAnnotationCollector<A> collector) { |
| 148 | + if ( !context.getElement().isPresent() ) { |
| 149 | + return Collections.emptyList(); |
| 150 | + } |
| 151 | + |
| 152 | + final AnnotatedElement annotatedElement = context.getElement().get(); |
| 153 | + final A methodAnnotation = annotatedElement.getAnnotation( annotationType ); |
| 154 | + final A classAnnotation = context.getTestInstance().map( i -> i.getClass().getAnnotation( annotationType ) ).orElse( null ); |
| 155 | + final Annotation methodPluralAnn = annotatedElement.getAnnotation( groupAnnotationType ); |
| 156 | + final Annotation classPluralAnn = context.getTestInstance().map( i -> i.getClass().getAnnotation( groupAnnotationType ) ).orElse( null ); |
| 157 | + final A[] methodAnnotations; |
| 158 | + final A[] classAnnotations; |
| 159 | + if ( methodPluralAnn != null ) { |
| 160 | + try { |
| 161 | + methodAnnotations = (A[]) groupAnnotationType.getDeclaredMethod( "value", null ).invoke( methodPluralAnn ); |
| 162 | + } |
| 163 | + catch (Exception e) { |
| 164 | + throw new RuntimeException( e ); |
| 165 | + } |
| 166 | + } |
| 167 | + else { |
| 168 | + methodAnnotations = null; |
| 169 | + } |
| 170 | + if ( classPluralAnn != null ) { |
| 171 | + try { |
| 172 | + classAnnotations = (A[]) groupAnnotationType.getDeclaredMethod( "value", null ).invoke( classPluralAnn ); |
| 173 | + } |
| 174 | + catch (Exception e) { |
| 175 | + throw new RuntimeException( e ); |
| 176 | + } |
| 177 | + } |
| 178 | + else { |
| 179 | + classAnnotations = null; |
| 180 | + } |
| 181 | + |
| 182 | + return collector.collect( methodAnnotation, methodAnnotations, classAnnotation, classAnnotations ); |
| 183 | + } |
| 184 | + |
| 185 | + public interface TestAnnotationCollector<S> { |
| 186 | + Collection<S> collect(S methodAnnotation, S[] methodAnnotations, S classAnnotation, S[] classAnnotations); |
| 187 | + } |
| 188 | +} |
0 commit comments