|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.bytecode.enhancement.access; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.metamodel.Attribute; |
| 10 | +import jakarta.persistence.metamodel.ManagedType; |
| 11 | +import jakarta.persistence.metamodel.Metamodel; |
| 12 | +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import java.lang.reflect.Member; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Scott Marlow |
| 24 | + */ |
| 25 | +@SessionFactory |
| 26 | +@DomainModel(annotatedClasses = { |
| 27 | + PropertyAccessMemberTest.PropertyEntity.class, |
| 28 | + PropertyAccessMemberTest.FieldEntity.class |
| 29 | +}) |
| 30 | +@BytecodeEnhanced |
| 31 | +public class PropertyAccessMemberTest { |
| 32 | + @Test |
| 33 | + public void testPropertyAccessMember(SessionFactoryScope scope) { |
| 34 | + scope.inTransaction( entityManager -> { |
| 35 | + final Metamodel metaModel = entityManager.getMetamodel(); |
| 36 | + final ManagedType<PropertyEntity> managedType = metaModel.managedType( PropertyEntity.class ); |
| 37 | + final Attribute<PropertyEntity, ?> attribute = managedType.getDeclaredAttribute( "total" ); |
| 38 | + final Member member = attribute.getJavaMember(); |
| 39 | + assertEquals( "getTotal", member.getName() ); |
| 40 | + } ); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testFieldAccessMember(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( entityManager -> { |
| 46 | + final Metamodel metaModel = entityManager.getMetamodel(); |
| 47 | + final ManagedType<FieldEntity> managedType = metaModel.managedType( FieldEntity.class ); |
| 48 | + final Attribute<FieldEntity, ?> attribute = managedType.getDeclaredAttribute( "total" ); |
| 49 | + final Member member = attribute.getJavaMember(); |
| 50 | + assertEquals( "total", member.getName() ); |
| 51 | + } ); |
| 52 | + } |
| 53 | + |
| 54 | + @Entity(name = "PropertyEntity") |
| 55 | + static class PropertyEntity { |
| 56 | + private int id; |
| 57 | + private int total; |
| 58 | + |
| 59 | + @Id |
| 60 | + public int getId() { |
| 61 | + return id; |
| 62 | + } |
| 63 | + |
| 64 | + public void setId(int id) { |
| 65 | + this.id = id; |
| 66 | + } |
| 67 | + |
| 68 | + public int getTotal() { |
| 69 | + return total; |
| 70 | + } |
| 71 | + |
| 72 | + public void setTotal(int total) { |
| 73 | + this.total = total; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Entity(name = "FieldEntity") |
| 78 | + static class FieldEntity { |
| 79 | + @Id |
| 80 | + private int id; |
| 81 | + private int total; |
| 82 | + |
| 83 | + public int getId() { |
| 84 | + return id; |
| 85 | + } |
| 86 | + |
| 87 | + public void setId(int id) { |
| 88 | + this.id = id; |
| 89 | + } |
| 90 | + |
| 91 | + public int getTotal() { |
| 92 | + return total; |
| 93 | + } |
| 94 | + |
| 95 | + public void setTotal(int total) { |
| 96 | + this.total = total; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments