|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.models.annotations.target; |
| 6 | + |
| 7 | +import org.hibernate.models.annotations.target.sub.SubNoGeneratorEntity; |
| 8 | +import org.hibernate.models.spi.ClassDetails; |
| 9 | +import org.hibernate.models.spi.ClassDetailsRegistry; |
| 10 | +import org.hibernate.models.spi.FieldDetails; |
| 11 | +import org.hibernate.models.spi.SourceModelBuildingContext; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import org.jboss.jandex.Index; |
| 16 | + |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.hibernate.models.SourceModelTestHelper.buildJandexIndex; |
| 20 | +import static org.hibernate.models.SourceModelTestHelper.createBuildingContext; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Steve Ebersole |
| 24 | + */ |
| 25 | +public class FromContainersTests { |
| 26 | + @Test |
| 27 | + void testNoGeneratorWithJandex() { |
| 28 | + testNoGenerator( buildJandexIndex( NoGeneratorEntity.class ) ); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testNoGeneratorWithoutJandex() { |
| 33 | + testNoGenerator( null ); |
| 34 | + } |
| 35 | + |
| 36 | + private void testNoGenerator(Index jandexIndex) { |
| 37 | + final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, NoGeneratorEntity.class ); |
| 38 | + final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry(); |
| 39 | + |
| 40 | + final ClassDetails entityClass = classDetailsRegistry.getClassDetails( NoGeneratorEntity.class.getName() ); |
| 41 | + { |
| 42 | + final GeneratorAnnotation found = entityClass.fromSelfAndContainers( |
| 43 | + false, |
| 44 | + buildingContext, |
| 45 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 46 | + ); |
| 47 | + assertThat( found ).isNotNull(); |
| 48 | + assertThat( found.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE ); |
| 49 | + } |
| 50 | + |
| 51 | + { |
| 52 | + final GeneratorAnnotation found = entityClass.findFieldByName( "id" ).fromSelfAndContainers( |
| 53 | + false, |
| 54 | + buildingContext, |
| 55 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 56 | + ); |
| 57 | + assertThat( found ).isNotNull(); |
| 58 | + assertThat( found.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE ); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testClassDefinedWithJandex() { |
| 64 | + testClassDefined( buildJandexIndex( ClassGeneratorEntity.class ) ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testClassDefinedWithoutJandex() { |
| 69 | + testClassDefined( null ); |
| 70 | + } |
| 71 | + |
| 72 | + private void testClassDefined(Index jandexIndex) { |
| 73 | + final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, ClassGeneratorEntity.class ); |
| 74 | + final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry(); |
| 75 | + |
| 76 | + final ClassDetails entityClass = classDetailsRegistry.getClassDetails( ClassGeneratorEntity.class.getName() ); |
| 77 | + final FieldDetails idMember = entityClass.findFieldByName( "id" ); |
| 78 | + |
| 79 | + final GeneratorAnnotation fromClass = entityClass.fromSelfAndContainers( |
| 80 | + false, |
| 81 | + buildingContext, |
| 82 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 83 | + ); |
| 84 | + assertThat( fromClass ).isNotNull(); |
| 85 | + assertThat( fromClass.value() ).isEqualTo( GeneratorAnnotation.Source.TYPE ); |
| 86 | + |
| 87 | + final GeneratorAnnotation fromMember = idMember.fromSelfAndContainers( |
| 88 | + false, |
| 89 | + buildingContext, |
| 90 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 91 | + ); |
| 92 | + assertThat( fromMember ).isNotNull(); |
| 93 | + assertThat( fromMember.value() ).isEqualTo( GeneratorAnnotation.Source.TYPE ); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testMemberDefinedWithJandex() { |
| 98 | + testMemberDefined( buildJandexIndex( MemberGeneratorEntity.class ) ); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testMemberDefinedWithoutJandex() { |
| 103 | + testMemberDefined( null ); |
| 104 | + } |
| 105 | + |
| 106 | + private void testMemberDefined(Index jandexIndex) { |
| 107 | + final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, MemberGeneratorEntity.class ); |
| 108 | + final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry(); |
| 109 | + |
| 110 | + final ClassDetails entityClass = classDetailsRegistry.getClassDetails( MemberGeneratorEntity.class.getName() ); |
| 111 | + final FieldDetails idMember = entityClass.findFieldByName( "id" ); |
| 112 | + |
| 113 | + final GeneratorAnnotation fromClass = entityClass.fromSelfAndContainers( |
| 114 | + false, |
| 115 | + buildingContext, |
| 116 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 117 | + ); |
| 118 | + assertThat( fromClass ).isNotNull(); |
| 119 | + assertThat( fromClass.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE ); |
| 120 | + |
| 121 | + final GeneratorAnnotation fromMember = idMember.fromSelfAndContainers( |
| 122 | + false, |
| 123 | + buildingContext, |
| 124 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 125 | + ); |
| 126 | + assertThat( fromMember ).isNotNull(); |
| 127 | + assertThat( fromMember.value() ).isEqualTo( GeneratorAnnotation.Source.MEMBER ); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void testUpPackageDefinedWithJandex() { |
| 132 | + testUpPackageDefined( buildJandexIndex( SubNoGeneratorEntity.class ) ); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void testUpPackageDefinedWithoutJandex() { |
| 137 | + testUpPackageDefined( null ); |
| 138 | + } |
| 139 | + |
| 140 | + private void testUpPackageDefined(Index jandexIndex) { |
| 141 | + final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, SubNoGeneratorEntity.class ); |
| 142 | + final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry(); |
| 143 | + |
| 144 | + final ClassDetails entityClass = classDetailsRegistry.getClassDetails( SubNoGeneratorEntity.class.getName() ); |
| 145 | + final FieldDetails idMember = entityClass.findFieldByName( "id" ); |
| 146 | + |
| 147 | + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 148 | + // cross package boundary - false |
| 149 | + |
| 150 | + final GeneratorAnnotation fromClassScoped = entityClass.fromSelfAndContainers( |
| 151 | + false, |
| 152 | + buildingContext, |
| 153 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 154 | + ); |
| 155 | + assertThat( fromClassScoped ).isNull(); |
| 156 | + |
| 157 | + final GeneratorAnnotation fromMemberScoped = idMember.fromSelfAndContainers( |
| 158 | + false, |
| 159 | + buildingContext, |
| 160 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 161 | + ); |
| 162 | + assertThat( fromMemberScoped ).isNull(); |
| 163 | + |
| 164 | + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 165 | + // cross package boundary - true |
| 166 | + |
| 167 | + final GeneratorAnnotation fromClassUnScoped = entityClass.fromSelfAndContainers( |
| 168 | + true, |
| 169 | + buildingContext, |
| 170 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 171 | + ); |
| 172 | + assertThat( fromClassUnScoped ).isNotNull(); |
| 173 | + assertThat( fromClassUnScoped.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE ); |
| 174 | + |
| 175 | + final GeneratorAnnotation fromMemberUnScoped = idMember.fromSelfAndContainers( |
| 176 | + true, |
| 177 | + buildingContext, |
| 178 | + (classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class ) |
| 179 | + ); |
| 180 | + assertThat( fromMemberUnScoped ).isNotNull(); |
| 181 | + assertThat( fromMemberUnScoped.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE ); |
| 182 | + } |
| 183 | +} |
0 commit comments