Skip to content

Commit 9be40cb

Browse files
committed
tests for case of nested annotation values from JDK
1 parent 698a873 commit 9be40cb

File tree

6 files changed

+351
-2
lines changed

6 files changed

+351
-2
lines changed

hibernate-models-jandex/src/test/java/org/hibernate/models/SourceModelTestHelper.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.jboss.jandex.ClassInfo;
2424
import org.jboss.jandex.Index;
25+
import org.jboss.jandex.IndexView;
2526
import org.jboss.jandex.Indexer;
2627

2728
import static org.hibernate.models.internal.SimpleClassLoading.SIMPLE_CLASS_LOADING;
@@ -31,6 +32,10 @@
3132
*/
3233
public class SourceModelTestHelper {
3334

35+
public static SourceModelBuildingContext createBuildingContext(Class<?> modelClass) {
36+
return createBuildingContext( SIMPLE_CLASS_LOADING, modelClass );
37+
}
38+
3439
public static SourceModelBuildingContext createBuildingContext(Class<?>... modelClasses) {
3540
return createBuildingContext( SIMPLE_CLASS_LOADING, modelClasses );
3641
}
@@ -40,12 +45,16 @@ public static SourceModelBuildingContext createBuildingContext(ClassLoading clas
4045
return createBuildingContext( jandexIndex, modelClasses );
4146
}
4247

43-
public static SourceModelBuildingContext createBuildingContext(Index jandexIndex, Class<?>... modelClasses) {
48+
public static SourceModelBuildingContext createBuildingContext(IndexView jandexIndex, Class<?> modelClass) {
49+
return createBuildingContext( jandexIndex, SIMPLE_CLASS_LOADING, modelClass );
50+
}
51+
52+
public static SourceModelBuildingContext createBuildingContext(IndexView jandexIndex, Class<?>... modelClasses) {
4453
return createBuildingContext( jandexIndex, SIMPLE_CLASS_LOADING, modelClasses );
4554
}
4655

4756
public static SourceModelBuildingContext createBuildingContext(
48-
Index jandexIndex,
57+
IndexView jandexIndex,
4958
ClassLoading classLoadingAccess,
5059
Class<?>... modelClasses) {
5160
final SourceModelBuildingContext ctx;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.reflect.Proxy;
11+
12+
import org.hibernate.models.SourceModelTestHelper;
13+
import org.hibernate.models.spi.ClassDetails;
14+
import org.hibernate.models.spi.SourceModelBuildingContext;
15+
16+
import org.junit.jupiter.api.Test;
17+
18+
import org.jboss.jandex.Index;
19+
import org.jboss.jandex.IndexView;
20+
21+
import jakarta.persistence.Entity;
22+
import jakarta.persistence.ForeignKey;
23+
import jakarta.persistence.Id;
24+
import jakarta.persistence.PrimaryKeyJoinColumn;
25+
import jakarta.persistence.SecondaryTable;
26+
import jakarta.persistence.Table;
27+
28+
import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* @author Steve Ebersole
33+
*/
34+
public class NestedAnnotationTests {
35+
public static final String SECOND_TABLE = "second_table";
36+
public static final String JOIN_COLUMN_NAME = "person_fk";
37+
38+
@Test
39+
void testNestedAnnotationsNotProxyWithJandex() {
40+
final Index index = SourceModelTestHelper.buildJandexIndex(Person.class);
41+
testNestedAnnotationsNotProxy(index);
42+
}
43+
@Test
44+
void testNestedAnnotationsNotProxyWithoutJandex() {
45+
testNestedAnnotationsNotProxy(null);
46+
}
47+
48+
void testNestedAnnotationsNotProxy(IndexView jandexIndex) {
49+
final SourceModelBuildingContext buildingContext = SourceModelTestHelper.createBuildingContext(
50+
jandexIndex,
51+
Person.class
52+
);
53+
54+
final ClassDetails classDetails = buildingContext.getClassDetailsRegistry().getClassDetails( Person.class.getName() );
55+
final SecondaryTable secondaryTable = classDetails.getDirectAnnotationUsage( SecondaryTable.class );
56+
assertThat( secondaryTable.name() ).isEqualTo( SECOND_TABLE );
57+
58+
assertThat( Proxy.isProxyClass( secondaryTable.foreignKey().getClass() ) ).isFalse();
59+
assertThat( secondaryTable.foreignKey().value() ).isEqualTo( NO_CONSTRAINT );
60+
assertThat( secondaryTable.foreignKey().options() ).isEqualTo( "stuff" );
61+
62+
assertThat( secondaryTable.pkJoinColumns() ).hasSize( 1 );
63+
assertThat( Proxy.isProxyClass( secondaryTable.pkJoinColumns()[0].getClass() ) ).isFalse();
64+
assertThat( Proxy.isProxyClass( secondaryTable.pkJoinColumns()[0].foreignKey().getClass() ) ).isFalse();
65+
assertThat( secondaryTable.pkJoinColumns()[0].foreignKey().value() ).isEqualTo( NO_CONSTRAINT );
66+
assertThat( secondaryTable.pkJoinColumns()[0].foreignKey().options() ).isEqualTo( "things" );
67+
}
68+
69+
@Entity(name="Person")
70+
@Table(name="Person")
71+
@SecondaryTable( name = SECOND_TABLE,
72+
foreignKey = @ForeignKey(value = NO_CONSTRAINT, options = "stuff"),
73+
pkJoinColumns = {
74+
@PrimaryKeyJoinColumn( name = JOIN_COLUMN_NAME,
75+
referencedColumnName = "id",
76+
foreignKey = @ForeignKey(value = NO_CONSTRAINT, options = "things")
77+
)
78+
}
79+
)
80+
public static class Person {
81+
@Id
82+
private Integer id;
83+
private String name;
84+
}
85+
}

hibernate-models-testing/src/main/java/org/hibernate/models/orm/ColumnDetails.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,65 @@
1212
*/
1313
public interface ColumnDetails {
1414
String name();
15+
16+
void name(String value);
17+
18+
interface AlternateTableCapable {
19+
String table();
20+
21+
void table(String value);
22+
}
23+
24+
interface Nullable extends ColumnDetails {
25+
boolean nullable();
26+
27+
void nullable(boolean value);
28+
}
29+
30+
interface Mutable extends ColumnDetails {
31+
32+
boolean insertable();
33+
34+
void insertable(boolean value);
35+
36+
boolean updatable();
37+
38+
void updatable(boolean value);
39+
}
40+
41+
interface Sizable extends ColumnDetails {
42+
int length();
43+
44+
void length(int value);
45+
46+
int precision();
47+
48+
void precision(int value);
49+
50+
int scale();
51+
52+
void scale(int value);
53+
}
54+
55+
interface SecondSizable extends Sizable {
56+
int secondPrecision();
57+
58+
void secondPrecision(int value);
59+
}
60+
61+
interface Uniqueable extends ColumnDetails {
62+
boolean unique();
63+
64+
void unique(boolean value);
65+
}
66+
67+
interface Definable extends ColumnDetails {
68+
String columnDefinition();
69+
70+
void columnDefinition(String value);
71+
72+
String options();
73+
74+
void options(String value);
75+
}
1576
}

hibernate-models-testing/src/main/java/org/hibernate/models/orm/JpaAnnotations.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import jakarta.persistence.NamedNativeQuery;
2525
import jakarta.persistence.NamedQueries;
2626
import jakarta.persistence.NamedQuery;
27+
import jakarta.persistence.PrimaryKeyJoinColumn;
28+
import jakarta.persistence.PrimaryKeyJoinColumns;
2729
import jakarta.persistence.SecondaryTable;
2830
import jakarta.persistence.SecondaryTables;
2931
import jakarta.persistence.SequenceGenerator;
@@ -60,6 +62,9 @@ public interface JpaAnnotations {
6062
AnnotationDescriptor<SecondaryTables> SECONDARY_TABLES = new OrmAnnotationDescriptor<>( SecondaryTables.class, SecondaryTablesAnnotation.class );
6163
AnnotationDescriptor<SecondaryTable> SECONDARY_TABLE = new OrmAnnotationDescriptor<>( SecondaryTable.class, SecondaryTableAnnotation.class, SECONDARY_TABLES );
6264

65+
AnnotationDescriptor<PrimaryKeyJoinColumns> PRIMARY_KEY_JOIN_COLUMNS = new OrmAnnotationDescriptor<>( PrimaryKeyJoinColumns.class, PrimaryKeyJoinColumnsJpaAnnotation.class );
66+
AnnotationDescriptor<PrimaryKeyJoinColumn> PRIMARY_KEY_JOIN_COLUMN = new OrmAnnotationDescriptor<>( PrimaryKeyJoinColumn.class, PrimaryKeyJoinColumnJpaAnnotation.class, PRIMARY_KEY_JOIN_COLUMNS );
67+
6368
AnnotationDescriptor<CheckConstraint> CHECK_CONSTRAINT = new OrmAnnotationDescriptor<>( CheckConstraint.class, CheckConstraintAnnotation.class );
6469
AnnotationDescriptor<ForeignKey> FOREIGN_KEY = new OrmAnnotationDescriptor<>( ForeignKey.class, ForeignKeyAnnotation.class );
6570
AnnotationDescriptor<UniqueConstraint> UNIQUE_CONSTRAINT = new OrmAnnotationDescriptor<>( UniqueConstraint.class, UniqueConstraintAnnotation.class );
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
6+
*/
7+
package org.hibernate.models.orm;
8+
9+
import java.lang.annotation.Annotation;
10+
import java.util.Map;
11+
12+
import org.hibernate.models.spi.SourceModelBuildingContext;
13+
14+
import jakarta.persistence.PrimaryKeyJoinColumn;
15+
16+
@SuppressWarnings({ "ClassExplicitlyAnnotation", "unused" })
17+
public class PrimaryKeyJoinColumnJpaAnnotation implements PrimaryKeyJoinColumn, ColumnDetails, ColumnDetails.Definable {
18+
private String name;
19+
private String referencedColumnName;
20+
private String columnDefinition;
21+
private String options;
22+
private jakarta.persistence.ForeignKey foreignKey;
23+
24+
/**
25+
* Used in creating dynamic annotation instances (e.g. from XML)
26+
*/
27+
public PrimaryKeyJoinColumnJpaAnnotation(SourceModelBuildingContext modelContext) {
28+
this.name = "";
29+
this.referencedColumnName = "";
30+
this.columnDefinition = "";
31+
this.options = "";
32+
this.foreignKey = new ForeignKeyAnnotation( modelContext );
33+
}
34+
35+
/**
36+
* Used in creating annotation instances from JDK variant
37+
*/
38+
public PrimaryKeyJoinColumnJpaAnnotation(PrimaryKeyJoinColumn annotation, SourceModelBuildingContext modelContext) {
39+
this.name = annotation.name();
40+
this.referencedColumnName = annotation.referencedColumnName();
41+
this.columnDefinition = annotation.columnDefinition();
42+
this.options = annotation.options();
43+
this.foreignKey = new ForeignKeyAnnotation( annotation.foreignKey(), modelContext );
44+
}
45+
46+
/**
47+
* Used in creating annotation instances from Jandex variant
48+
*/
49+
public PrimaryKeyJoinColumnJpaAnnotation(
50+
Map<String, Object> attributeValues,
51+
SourceModelBuildingContext modelContext) {
52+
this.name = (String) attributeValues.get( "name" );
53+
this.referencedColumnName = (String) attributeValues.get( "referencedColumnName" );
54+
this.columnDefinition = (String) attributeValues.get( "columnDefinition" );
55+
this.options = (String) attributeValues.get( "options" );
56+
this.foreignKey = (jakarta.persistence.ForeignKey) attributeValues.get( "foreignKey" );
57+
}
58+
59+
@Override
60+
public Class<? extends Annotation> annotationType() {
61+
return PrimaryKeyJoinColumn.class;
62+
}
63+
64+
@Override
65+
public String name() {
66+
return name;
67+
}
68+
69+
public void name(String value) {
70+
this.name = value;
71+
}
72+
73+
74+
@Override
75+
public String referencedColumnName() {
76+
return referencedColumnName;
77+
}
78+
79+
public void referencedColumnName(String value) {
80+
this.referencedColumnName = value;
81+
}
82+
83+
84+
@Override
85+
public String columnDefinition() {
86+
return columnDefinition;
87+
}
88+
89+
public void columnDefinition(String value) {
90+
this.columnDefinition = value;
91+
}
92+
93+
94+
@Override
95+
public String options() {
96+
return options;
97+
}
98+
99+
public void options(String value) {
100+
this.options = value;
101+
}
102+
103+
104+
@Override
105+
public jakarta.persistence.ForeignKey foreignKey() {
106+
return foreignKey;
107+
}
108+
109+
public void foreignKey(jakarta.persistence.ForeignKey value) {
110+
this.foreignKey = value;
111+
}
112+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
6+
*/
7+
package org.hibernate.models.orm;
8+
9+
import java.lang.annotation.Annotation;
10+
import java.util.Map;
11+
12+
import org.hibernate.models.spi.SourceModelBuildingContext;
13+
14+
import jakarta.persistence.PrimaryKeyJoinColumn;
15+
import jakarta.persistence.PrimaryKeyJoinColumns;
16+
17+
@SuppressWarnings({ "ClassExplicitlyAnnotation", "unused" })
18+
public class PrimaryKeyJoinColumnsJpaAnnotation
19+
implements PrimaryKeyJoinColumns, RepeatableContainer<PrimaryKeyJoinColumn> {
20+
private PrimaryKeyJoinColumn[] value;
21+
private jakarta.persistence.ForeignKey foreignKey;
22+
23+
/**
24+
* Used in creating dynamic annotation instances (e.g. from XML)
25+
*/
26+
public PrimaryKeyJoinColumnsJpaAnnotation(SourceModelBuildingContext modelContext) {
27+
this.foreignKey = modelContext.getAnnotationDescriptorRegistry()
28+
.getDescriptor( jakarta.persistence.ForeignKey.class )
29+
.createUsage( modelContext );
30+
}
31+
32+
/**
33+
* Used in creating annotation instances from JDK variant
34+
*/
35+
public PrimaryKeyJoinColumnsJpaAnnotation(
36+
PrimaryKeyJoinColumns annotation,
37+
SourceModelBuildingContext modelContext) {
38+
this.value = annotation.value();
39+
this.foreignKey = new ForeignKeyAnnotation( annotation.foreignKey(), modelContext );
40+
}
41+
42+
/**
43+
* Used in creating annotation instances from Jandex variant
44+
*/
45+
public PrimaryKeyJoinColumnsJpaAnnotation(
46+
Map<String, Object> attributeValues,
47+
SourceModelBuildingContext modelContext) {
48+
this.value = (PrimaryKeyJoinColumn[]) attributeValues.get( "value" );
49+
this.foreignKey = (jakarta.persistence.ForeignKey) attributeValues.get( "foreignKey" );
50+
}
51+
52+
@Override
53+
public Class<? extends Annotation> annotationType() {
54+
return PrimaryKeyJoinColumns.class;
55+
}
56+
57+
@Override
58+
public PrimaryKeyJoinColumn[] value() {
59+
return value;
60+
}
61+
62+
public void value(PrimaryKeyJoinColumn[] value) {
63+
this.value = value;
64+
}
65+
66+
67+
@Override
68+
public jakarta.persistence.ForeignKey foreignKey() {
69+
return foreignKey;
70+
}
71+
72+
public void foreignKey(jakarta.persistence.ForeignKey value) {
73+
this.foreignKey = value;
74+
}
75+
76+
77+
}

0 commit comments

Comments
 (0)