|
7 | 7 | package org.hibernate.graalvm.internal; |
8 | 8 |
|
9 | 9 | import static org.assertj.core.api.Assertions.assertThat; |
| 10 | +import static org.hibernate.graalvm.internal.JandexTestUtils.findConcreteNamedImplementors; |
10 | 11 |
|
| 12 | +import java.io.IOException; |
11 | 13 | import java.lang.reflect.Array; |
12 | 14 | import java.lang.reflect.Constructor; |
| 15 | +import java.util.Arrays; |
13 | 16 | import java.util.stream.Collectors; |
14 | 17 | import java.util.stream.Stream; |
15 | 18 |
|
| 19 | +import org.hibernate.Session; |
16 | 20 | import org.hibernate.event.spi.EventType; |
17 | 21 | import org.hibernate.internal.util.ReflectHelper; |
| 22 | +import org.hibernate.persister.collection.CollectionPersister; |
| 23 | +import org.hibernate.persister.entity.EntityPersister; |
18 | 24 |
|
19 | 25 | import org.junit.Assert; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
20 | 27 | import org.junit.jupiter.api.Nested; |
21 | 28 | import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.EnumSource; |
| 31 | + |
| 32 | +import org.jboss.jandex.Index; |
22 | 33 |
|
23 | 34 | public class StaticClassListsTest { |
| 35 | + private static Index hibernateIndex; |
| 36 | + |
| 37 | + @BeforeAll |
| 38 | + public static void index() throws IOException { |
| 39 | + hibernateIndex = JandexTestUtils.indexJar( Session.class ); |
| 40 | + } |
24 | 41 |
|
25 | 42 | @Nested |
26 | | - class TypesNeedingArrayCopy { |
| 43 | + class TypesNeedingAllConstructorsAccessible { |
| 44 | + @ParameterizedTest |
| 45 | + @EnumSource(TypesNeedingAllConstructorsAccessible_Category.class) |
| 46 | + void containsAllExpectedClasses(TypesNeedingAllConstructorsAccessible_Category category) { |
| 47 | + assertThat( StaticClassLists.typesNeedingAllConstructorsAccessible() ) |
| 48 | + .containsAll( category.classes().collect( Collectors.toSet() ) ); |
| 49 | + } |
| 50 | + |
27 | 51 | @Test |
28 | | - void containsEventListenerInterfaces() { |
| 52 | + void meta_noMissingTestCategory() { |
| 53 | + assertThat( Arrays.stream( TypesNeedingAllConstructorsAccessible_Category.values() ).flatMap( TypesNeedingAllConstructorsAccessible_Category::classes ) ) |
| 54 | + .as( "If this fails, a category is missing in " + TypesNeedingAllConstructorsAccessible_Category.class ) |
| 55 | + .contains( StaticClassLists.typesNeedingAllConstructorsAccessible() ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // TODO ORM 7: Move this inside TypesNeedingAllConstructorsAccessible (requires JDK 17) and rename to simple Category |
| 60 | + enum TypesNeedingAllConstructorsAccessible_Category { |
| 61 | + PERSISTERS { |
| 62 | + @Override |
| 63 | + Stream<Class<?>> classes() { |
| 64 | + return findConcreteNamedImplementors( |
| 65 | + hibernateIndex, EntityPersister.class, CollectionPersister.class ) |
| 66 | + .stream(); |
| 67 | + } |
| 68 | + }, |
| 69 | + MISC { |
| 70 | + @Override |
| 71 | + Stream<Class<?>> classes() { |
| 72 | + // NOTE: Please avoid putting anything here, it's really a last resort. |
| 73 | + // Ideally you'd rather add new categories with their own way of listing classes, |
| 74 | + // like in PERSISTERS. |
| 75 | + // Putting anything here is running the risk of forgetting |
| 76 | + // why it was necessary in the first place... |
| 77 | + return Stream.of( |
| 78 | + // Logging - sometimes looked up without a static field |
| 79 | + org.hibernate.internal.CoreMessageLogger_$logger.class |
| 80 | + ); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + abstract Stream<Class<?>> classes(); |
| 85 | + } |
| 86 | + |
| 87 | + @Nested |
| 88 | + class TypesNeedingDefaultConstructorAccessible { |
| 89 | + @ParameterizedTest |
| 90 | + @EnumSource(TypesNeedingDefaultConstructorAccessible_Category.class) |
| 91 | + void containsAllExpectedClasses(TypesNeedingDefaultConstructorAccessible_Category category) { |
| 92 | + assertThat( StaticClassLists.typesNeedingDefaultConstructorAccessible() ) |
| 93 | + .containsAll( category.classes().collect( Collectors.toSet() ) ); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void meta_noMissingTestCategory() { |
| 98 | + assertThat( Arrays.stream( TypesNeedingDefaultConstructorAccessible_Category.values() ).flatMap( TypesNeedingDefaultConstructorAccessible_Category::classes ) ) |
| 99 | + .as( "If this fails, a category is missing in " + TypesNeedingDefaultConstructorAccessible_Category.class ) |
| 100 | + .contains( StaticClassLists.typesNeedingDefaultConstructorAccessible() ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // TODO ORM 7: Move this inside TypesNeedingDefaultConstructorAccessible (requires JDK 17) and rename to simple Category |
| 105 | + enum TypesNeedingDefaultConstructorAccessible_Category { |
| 106 | + MISC { |
| 107 | + @Override |
| 108 | + Stream<Class<?>> classes() { |
| 109 | + // NOTE: Please avoid putting anything here, it's really a last resort. |
| 110 | + // Ideally you'd rather add new categories with their own way of listing classes, |
| 111 | + // like in PERSISTERS. |
| 112 | + // Putting anything here is running the risk of forgetting |
| 113 | + // why it was necessary in the first place... |
| 114 | + return Stream.of( |
| 115 | + org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl.class, |
| 116 | + org.hibernate.id.enhanced.SequenceStyleGenerator.class, |
| 117 | + org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl.class, |
| 118 | + org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl.class, |
| 119 | + org.hibernate.type.EnumType.class, |
| 120 | + org.hibernate.tool.schema.internal.script.MultiLineSqlScriptExtractor.class |
| 121 | + ); |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + abstract Stream<Class<?>> classes(); |
| 126 | + } |
| 127 | + |
| 128 | + @Nested |
| 129 | + class TypesNeedingArrayCopy { |
| 130 | + @ParameterizedTest |
| 131 | + @EnumSource(TypesNeedingArrayCopy_Category.class) |
| 132 | + void containsAllExpectedClasses(TypesNeedingArrayCopy_Category category) { |
29 | 133 | assertThat( StaticClassLists.typesNeedingArrayCopy() ) |
30 | | - .containsAll( eventListenerInterfaces().collect( Collectors.toSet() ) ); |
| 134 | + .containsAll( category.classes().collect( Collectors.toSet() ) ); |
31 | 135 | } |
32 | 136 |
|
33 | | - static Stream<Class<?>> eventListenerInterfaces() { |
34 | | - return EventType.values().stream().map( EventType::baseListenerInterface ) |
35 | | - .map( c -> Array.newInstance( c, 0 ).getClass() ); |
| 137 | + @Test |
| 138 | + void meta_noMissingTestCategory() { |
| 139 | + assertThat( Arrays.stream( TypesNeedingArrayCopy_Category.values() ).flatMap( TypesNeedingArrayCopy_Category::classes ) ) |
| 140 | + .as( "If this fails, a category is missing in " + TypesNeedingArrayCopy_Category.class ) |
| 141 | + .contains( StaticClassLists.typesNeedingArrayCopy() ); |
36 | 142 | } |
37 | 143 | } |
38 | 144 |
|
| 145 | + // TODO ORM 7: Move this inside TypesNeedingArrayCopy (requires JDK 17) and rename to simple Category |
| 146 | + enum TypesNeedingArrayCopy_Category { |
| 147 | + EVENT_LISTENER_INTERFACES { |
| 148 | + @Override |
| 149 | + Stream<Class<?>> classes() { |
| 150 | + return EventType.values().stream().map( EventType::baseListenerInterface ) |
| 151 | + .map( c -> Array.newInstance( c, 0 ).getClass() ); |
| 152 | + } |
| 153 | + }, |
| 154 | + MISC { |
| 155 | + @Override |
| 156 | + Stream<Class<?>> classes() { |
| 157 | + // NOTE: Please avoid putting anything here, it's really a last resort. |
| 158 | + // Ideally you'd rather add new categories with their own way of listing classes, |
| 159 | + // like in EVENT_LISTENER_INTERFACES. |
| 160 | + // Putting anything here is running the risk of forgetting |
| 161 | + // why it was necessary in the first place... |
| 162 | + return Stream.of( |
| 163 | + // Java classes -- the why is lost to history |
| 164 | + java.util.function.Function[].class, |
| 165 | + java.util.List[].class, |
| 166 | + java.util.Map.Entry[].class, |
| 167 | + java.util.function.Supplier[].class, |
| 168 | + // Graphs -- the why is lost to history |
| 169 | + org.hibernate.graph.spi.AttributeNodeImplementor[].class, |
| 170 | + org.hibernate.sql.results.graph.FetchParent[].class, |
| 171 | + org.hibernate.graph.spi.GraphImplementor[].class, |
| 172 | + org.hibernate.graph.internal.parse.SubGraphGenerator[].class, |
| 173 | + // AST/parsing -- no way to detect this automatically, you just have to know. |
| 174 | + org.hibernate.sql.ast.Clause[].class, |
| 175 | + org.hibernate.query.hql.spi.DotIdentifierConsumer[].class, |
| 176 | + org.hibernate.query.sqm.sql.FromClauseIndex[].class, |
| 177 | + org.hibernate.query.sqm.spi.ParameterDeclarationContext[].class, |
| 178 | + org.hibernate.sql.ast.tree.select.QueryPart[].class, |
| 179 | + org.hibernate.sql.ast.spi.SqlAstProcessingState[].class, |
| 180 | + org.hibernate.query.hql.spi.SqmCreationProcessingState[].class, |
| 181 | + org.hibernate.sql.ast.tree.Statement[].class, |
| 182 | + // Various internals -- the why is lost to history |
| 183 | + org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingState[].class |
| 184 | + ); |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + abstract Stream<Class<?>> classes(); |
| 189 | + } |
| 190 | + |
39 | 191 | @Nested |
40 | 192 | class BasicConstructorsAvailable { |
41 | 193 |
|
|
0 commit comments