|
| 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.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import org.hibernate.models.internal.SourceModelBuildingContextImpl; |
| 14 | +import org.hibernate.models.orm.JpaAnnotations; |
| 15 | +import org.hibernate.models.spi.AnnotationDescriptor; |
| 16 | +import org.hibernate.models.spi.AnnotationDescriptorRegistry; |
| 17 | +import org.hibernate.models.spi.AnnotationUsage; |
| 18 | +import org.hibernate.models.spi.ClassDetails; |
| 19 | +import org.hibernate.models.spi.MutableAnnotationUsage; |
| 20 | +import org.hibernate.models.spi.MutableClassDetails; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.jboss.jandex.Index; |
| 25 | + |
| 26 | +import jakarta.persistence.SecondaryTable; |
| 27 | +import jakarta.persistence.SecondaryTables; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.hibernate.models.SourceModelTestHelper.buildJandexIndex; |
| 31 | +import static org.hibernate.models.SourceModelTestHelper.createBuildingContext; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Steve Ebersole |
| 35 | + */ |
| 36 | +public class AnnotationReplacementTests { |
| 37 | + @Test |
| 38 | + void testBasicReplacementWithJandex() { |
| 39 | + basicReplacementChecks( buildJandexIndex( SimpleEntity.class ) ); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testBasicReplacementWithoutJandex() { |
| 44 | + basicReplacementChecks( null ); |
| 45 | + } |
| 46 | + |
| 47 | + void basicReplacementChecks(Index index) { |
| 48 | + final SourceModelBuildingContextImpl buildingContext = createBuildingContext( index, SimpleEntity.class ); |
| 49 | + |
| 50 | + final MutableClassDetails classDetails = (MutableClassDetails) buildingContext.getClassDetailsRegistry().getClassDetails( SimpleEntity.class.getName() ); |
| 51 | + assertThat( classDetails.hasAnnotationUsage( SecondaryTable.class ) ).isTrue(); |
| 52 | + assertThat( classDetails.hasAnnotationUsage( SecondaryTables.class ) ).isFalse(); |
| 53 | + |
| 54 | + final MutableAnnotationUsage<SecondaryTables> replacement = classDetails.replaceAnnotationUsage( |
| 55 | + JpaAnnotations.SECONDARY_TABLE, |
| 56 | + JpaAnnotations.SECONDARY_TABLES, |
| 57 | + buildingContext |
| 58 | + ); |
| 59 | + |
| 60 | + assertThat( classDetails.hasAnnotationUsage( SecondaryTable.class ) ).isTrue(); |
| 61 | + assertThat( classDetails.hasAnnotationUsage( SecondaryTables.class ) ).isTrue(); |
| 62 | + |
| 63 | + List<MutableAnnotationUsage<SecondaryTable>> valueList = replacement.getList( "value" ); |
| 64 | + // because it is required |
| 65 | + assertThat( valueList ).isNull(); |
| 66 | + valueList = new ArrayList<>(); |
| 67 | + replacement.setAttributeValue( "value", valueList ); |
| 68 | + |
| 69 | + final MutableAnnotationUsage<SecondaryTable> fromXml = JpaAnnotations.SECONDARY_TABLE.createUsage( buildingContext ); |
| 70 | + valueList.add( fromXml ); |
| 71 | + fromXml.setAttributeValue( "name", "from_xml" ); |
| 72 | + |
| 73 | + final AnnotationUsage<SecondaryTable> annotationUsage = classDetails.getAnnotationUsage( SecondaryTable.class ); |
| 74 | + assertThat( annotationUsage.getString( "name" ) ).isEqualTo( "from_xml" ); |
| 75 | + |
| 76 | + final AnnotationUsage<SecondaryTables> annotationUsage1 = classDetails.getAnnotationUsage( SecondaryTables.class ); |
| 77 | + assertThat( annotationUsage1.getList( "value" ) ).isSameAs( valueList ); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments