|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 6 | + */ |
| 7 | + |
| 8 | +package org.hibernate.models.annotations; |
| 9 | + |
| 10 | +import java.lang.annotation.ElementType; |
| 11 | +import java.lang.annotation.Retention; |
| 12 | +import java.lang.annotation.RetentionPolicy; |
| 13 | +import java.lang.annotation.Target; |
| 14 | +import java.util.List; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +import org.hibernate.models.internal.SourceModelBuildingContextImpl; |
| 18 | +import org.hibernate.models.spi.AnnotationDescriptor; |
| 19 | +import org.hibernate.models.spi.AnnotationDescriptorRegistry; |
| 20 | +import org.hibernate.models.spi.AnnotationUsage; |
| 21 | +import org.hibernate.models.spi.AttributeDescriptor; |
| 22 | +import org.hibernate.models.spi.ClassDetails; |
| 23 | +import org.hibernate.models.spi.ClassDetailsRegistry; |
| 24 | +import org.hibernate.models.spi.MutableAnnotationUsage; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import org.jboss.jandex.Index; |
| 29 | + |
| 30 | +import jakarta.persistence.Entity; |
| 31 | +import jakarta.persistence.Id; |
| 32 | +import jakarta.persistence.Table; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.hibernate.models.SourceModelTestHelper.buildJandexIndex; |
| 36 | +import static org.hibernate.models.SourceModelTestHelper.createBuildingContext; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Steve Ebersole |
| 40 | + */ |
| 41 | +public class DefaultValueTests { |
| 42 | + @Test |
| 43 | + void testWithJandex() { |
| 44 | + doTest( buildJandexIndex( SimpleEntity.class ) ); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testWithoutJandex() { |
| 49 | + doTest( null ); |
| 50 | + } |
| 51 | + |
| 52 | + private void doTest(Index index) { |
| 53 | + final SourceModelBuildingContextImpl buildingContext = createBuildingContext( index, TestEntity.class ); |
| 54 | + final AnnotationDescriptorRegistry descriptorRegistry = buildingContext.getAnnotationDescriptorRegistry(); |
| 55 | + |
| 56 | + final AnnotationDescriptor<CustomAnnotation> descriptor = descriptorRegistry.getDescriptor( CustomAnnotation.class ); |
| 57 | + final List<String> attributeNames = descriptor |
| 58 | + .getAttributes() |
| 59 | + .stream() |
| 60 | + .map( AttributeDescriptor::getName ) |
| 61 | + .toList(); |
| 62 | + assertThat( attributeNames ).contains( "name", "someClassValue" ); |
| 63 | + assertThat( descriptor.findAttribute( "someClassValue" ) ).isNotNull(); |
| 64 | + |
| 65 | + final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry(); |
| 66 | + final ClassDetails entityClassDetails = classDetailsRegistry.getClassDetails( TestEntity.class.getName() ); |
| 67 | + final AnnotationUsage<CustomAnnotation> annotationUsage = entityClassDetails.getAnnotationUsage( CustomAnnotation.class ); |
| 68 | + assertThat( annotationUsage.getClassDetails( "someClassValue" ) ).isNotNull(); |
| 69 | + assertThat( annotationUsage.getClassDetails( "someClassValue" ).toJavaClass() ).isEqualTo( Entity.class ); |
| 70 | + |
| 71 | + final MutableAnnotationUsage<CustomAnnotation> created = descriptor.createUsage( null, buildingContext ); |
| 72 | + } |
| 73 | + |
| 74 | + @Target( ElementType.TYPE ) |
| 75 | + @Retention( RetentionPolicy.RUNTIME ) |
| 76 | + public @interface CustomAnnotation { |
| 77 | + String name(); |
| 78 | + Class someClassValue(); |
| 79 | + } |
| 80 | + |
| 81 | + @Entity(name="TestEntity") |
| 82 | + @Table(name="TestEntity") |
| 83 | + @CustomAnnotation( name="tester", someClassValue = Entity.class ) |
| 84 | + public static class TestEntity { |
| 85 | + @Id |
| 86 | + private Integer id; |
| 87 | + private String name; |
| 88 | + } |
| 89 | +} |
0 commit comments