|
| 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.Access; |
| 8 | +import jakarta.persistence.AccessType; |
| 9 | +import jakarta.persistence.Basic; |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import jakarta.persistence.Table; |
| 13 | +import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl; |
| 14 | +import org.hibernate.bytecode.enhance.spi.EnhancementContext; |
| 15 | +import org.hibernate.bytecode.enhance.spi.EnhancementException; |
| 16 | +import org.hibernate.bytecode.enhance.spi.Enhancer; |
| 17 | +import org.hibernate.bytecode.enhance.spi.UnsupportedEnhancementStrategy; |
| 18 | +import org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState; |
| 19 | +import org.hibernate.bytecode.spi.ByteCodeHelper; |
| 20 | +import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; |
| 21 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 29 | + |
| 30 | +@JiraKey("HHH-18833") |
| 31 | +public class UnsupportedEnhancementStrategyTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void skip() throws IOException { |
| 35 | + var context = new EnhancerTestContext() { |
| 36 | + @Override |
| 37 | + public UnsupportedEnhancementStrategy getUnsupportedEnhancementStrategy() { |
| 38 | + // This is currently the default, but we don't care about what's the default here |
| 39 | + return UnsupportedEnhancementStrategy.SKIP; |
| 40 | + } |
| 41 | + }; |
| 42 | + byte[] originalBytes = getAsBytes( SomeEntity.class ); |
| 43 | + byte[] enhancedBytes = doEnhance( SomeEntity.class, originalBytes, context ); |
| 44 | + assertThat( enhancedBytes ).isNull(); // null means "not enhanced" |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void fail() throws IOException { |
| 49 | + var context = new EnhancerTestContext() { |
| 50 | + @Override |
| 51 | + public UnsupportedEnhancementStrategy getUnsupportedEnhancementStrategy() { |
| 52 | + return UnsupportedEnhancementStrategy.FAIL; |
| 53 | + } |
| 54 | + }; |
| 55 | + byte[] originalBytes = getAsBytes( SomeEntity.class ); |
| 56 | + assertThatThrownBy( () -> doEnhance( SomeEntity.class, originalBytes, context ) ) |
| 57 | + .isInstanceOf( EnhancementException.class ) |
| 58 | + .hasMessageContainingAll( |
| 59 | + String.format( |
| 60 | + "Enhancement of [%s] failed because no field named [%s] could be found for property accessor method [%s].", |
| 61 | + SomeEntity.class.getName(), "propertyMethod", "getPropertyMethod" ), |
| 62 | + "To fix this, make sure all property accessor methods have a matching field." |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @SuppressWarnings("deprecation") |
| 68 | + public void legacy() throws IOException { |
| 69 | + var context = new EnhancerTestContext() { |
| 70 | + @Override |
| 71 | + public UnsupportedEnhancementStrategy getUnsupportedEnhancementStrategy() { |
| 72 | + // This is currently the default, but we don't care about what's the default here |
| 73 | + return UnsupportedEnhancementStrategy.LEGACY; |
| 74 | + } |
| 75 | + }; |
| 76 | + byte[] originalBytes = getAsBytes( SomeEntity.class ); |
| 77 | + byte[] enhancedBytes = doEnhance( SomeEntity.class, originalBytes, context ); |
| 78 | + assertThat( enhancedBytes ).isNotNull(); // non-null means enhancement _was_ performed |
| 79 | + } |
| 80 | + |
| 81 | + private byte[] doEnhance(Class<SomeEntity> someEntityClass, byte[] originalBytes, EnhancementContext context) { |
| 82 | + final ByteBuddyState byteBuddyState = new ByteBuddyState(); |
| 83 | + final Enhancer enhancer = new EnhancerImpl( context, byteBuddyState ); |
| 84 | + return enhancer.enhance( someEntityClass.getName(), originalBytes ); |
| 85 | + } |
| 86 | + |
| 87 | + private byte[] getAsBytes(Class<?> clazz) throws IOException { |
| 88 | + final String classFile = clazz.getName().replace( '.', '/' ) + ".class"; |
| 89 | + try (InputStream classFileStream = clazz.getClassLoader().getResourceAsStream( classFile )) { |
| 90 | + return ByteCodeHelper.readByteCode( classFileStream ); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Entity |
| 95 | + @Table(name = "SOME_ENTITY") |
| 96 | + static class SomeEntity { |
| 97 | + @Id |
| 98 | + Long id; |
| 99 | + |
| 100 | + @Basic |
| 101 | + String field; |
| 102 | + |
| 103 | + String property; |
| 104 | + |
| 105 | + public SomeEntity() { |
| 106 | + } |
| 107 | + |
| 108 | + public SomeEntity(Long id, String field, String property) { |
| 109 | + this.id = id; |
| 110 | + this.field = field; |
| 111 | + this.property = property; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * The following property accessor methods are purposely named incorrectly to |
| 116 | + * not match the "property" field. The HHH-16572 change ensures that |
| 117 | + * this entity is not (bytecode) enhanced. Eventually further changes will be made |
| 118 | + * such that this entity is enhanced in which case the FailureExpected can be removed |
| 119 | + * and the cleanup() uncommented. |
| 120 | + */ |
| 121 | + @Basic |
| 122 | + @Access(AccessType.PROPERTY) |
| 123 | + public String getPropertyMethod() { |
| 124 | + return "from getter: " + property; |
| 125 | + } |
| 126 | + |
| 127 | + public void setPropertyMethod(String property) { |
| 128 | + this.property = property; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments