Skip to content

Commit

Permalink
tests for case of nested annotation values from JDK
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Aug 28, 2024
1 parent 698a873 commit 9be40cb
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;

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

public static SourceModelBuildingContext createBuildingContext(Class<?> modelClass) {
return createBuildingContext( SIMPLE_CLASS_LOADING, modelClass );
}

public static SourceModelBuildingContext createBuildingContext(Class<?>... modelClasses) {
return createBuildingContext( SIMPLE_CLASS_LOADING, modelClasses );
}
Expand All @@ -40,12 +45,16 @@ public static SourceModelBuildingContext createBuildingContext(ClassLoading clas
return createBuildingContext( jandexIndex, modelClasses );
}

public static SourceModelBuildingContext createBuildingContext(Index jandexIndex, Class<?>... modelClasses) {
public static SourceModelBuildingContext createBuildingContext(IndexView jandexIndex, Class<?> modelClass) {
return createBuildingContext( jandexIndex, SIMPLE_CLASS_LOADING, modelClass );
}

public static SourceModelBuildingContext createBuildingContext(IndexView jandexIndex, Class<?>... modelClasses) {
return createBuildingContext( jandexIndex, SIMPLE_CLASS_LOADING, modelClasses );
}

public static SourceModelBuildingContext createBuildingContext(
Index jandexIndex,
IndexView jandexIndex,
ClassLoading classLoadingAccess,
Class<?>... modelClasses) {
final SourceModelBuildingContext ctx;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/

package org.hibernate.models.annotations;

import java.lang.reflect.Proxy;

import org.hibernate.models.SourceModelTestHelper;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.SourceModelBuildingContext;

import org.junit.jupiter.api.Test;

import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;

import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.Id;
import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.SecondaryTable;
import jakarta.persistence.Table;

import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Steve Ebersole
*/
public class NestedAnnotationTests {
public static final String SECOND_TABLE = "second_table";
public static final String JOIN_COLUMN_NAME = "person_fk";

@Test
void testNestedAnnotationsNotProxyWithJandex() {
final Index index = SourceModelTestHelper.buildJandexIndex(Person.class);
testNestedAnnotationsNotProxy(index);
}
@Test
void testNestedAnnotationsNotProxyWithoutJandex() {
testNestedAnnotationsNotProxy(null);
}

void testNestedAnnotationsNotProxy(IndexView jandexIndex) {
final SourceModelBuildingContext buildingContext = SourceModelTestHelper.createBuildingContext(
jandexIndex,
Person.class
);

final ClassDetails classDetails = buildingContext.getClassDetailsRegistry().getClassDetails( Person.class.getName() );
final SecondaryTable secondaryTable = classDetails.getDirectAnnotationUsage( SecondaryTable.class );
assertThat( secondaryTable.name() ).isEqualTo( SECOND_TABLE );

assertThat( Proxy.isProxyClass( secondaryTable.foreignKey().getClass() ) ).isFalse();
assertThat( secondaryTable.foreignKey().value() ).isEqualTo( NO_CONSTRAINT );
assertThat( secondaryTable.foreignKey().options() ).isEqualTo( "stuff" );

assertThat( secondaryTable.pkJoinColumns() ).hasSize( 1 );
assertThat( Proxy.isProxyClass( secondaryTable.pkJoinColumns()[0].getClass() ) ).isFalse();
assertThat( Proxy.isProxyClass( secondaryTable.pkJoinColumns()[0].foreignKey().getClass() ) ).isFalse();
assertThat( secondaryTable.pkJoinColumns()[0].foreignKey().value() ).isEqualTo( NO_CONSTRAINT );
assertThat( secondaryTable.pkJoinColumns()[0].foreignKey().options() ).isEqualTo( "things" );
}

@Entity(name="Person")
@Table(name="Person")
@SecondaryTable( name = SECOND_TABLE,
foreignKey = @ForeignKey(value = NO_CONSTRAINT, options = "stuff"),
pkJoinColumns = {
@PrimaryKeyJoinColumn( name = JOIN_COLUMN_NAME,
referencedColumnName = "id",
foreignKey = @ForeignKey(value = NO_CONSTRAINT, options = "things")
)
}
)
public static class Person {
@Id
private Integer id;
private String name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,65 @@
*/
public interface ColumnDetails {
String name();

void name(String value);

interface AlternateTableCapable {
String table();

void table(String value);
}

interface Nullable extends ColumnDetails {
boolean nullable();

void nullable(boolean value);
}

interface Mutable extends ColumnDetails {

boolean insertable();

void insertable(boolean value);

boolean updatable();

void updatable(boolean value);
}

interface Sizable extends ColumnDetails {
int length();

void length(int value);

int precision();

void precision(int value);

int scale();

void scale(int value);
}

interface SecondSizable extends Sizable {
int secondPrecision();

void secondPrecision(int value);
}

interface Uniqueable extends ColumnDetails {
boolean unique();

void unique(boolean value);
}

interface Definable extends ColumnDetails {
String columnDefinition();

void columnDefinition(String value);

String options();

void options(String value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import jakarta.persistence.NamedNativeQuery;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.PrimaryKeyJoinColumns;
import jakarta.persistence.SecondaryTable;
import jakarta.persistence.SecondaryTables;
import jakarta.persistence.SequenceGenerator;
Expand Down Expand Up @@ -60,6 +62,9 @@ public interface JpaAnnotations {
AnnotationDescriptor<SecondaryTables> SECONDARY_TABLES = new OrmAnnotationDescriptor<>( SecondaryTables.class, SecondaryTablesAnnotation.class );
AnnotationDescriptor<SecondaryTable> SECONDARY_TABLE = new OrmAnnotationDescriptor<>( SecondaryTable.class, SecondaryTableAnnotation.class, SECONDARY_TABLES );

AnnotationDescriptor<PrimaryKeyJoinColumns> PRIMARY_KEY_JOIN_COLUMNS = new OrmAnnotationDescriptor<>( PrimaryKeyJoinColumns.class, PrimaryKeyJoinColumnsJpaAnnotation.class );
AnnotationDescriptor<PrimaryKeyJoinColumn> PRIMARY_KEY_JOIN_COLUMN = new OrmAnnotationDescriptor<>( PrimaryKeyJoinColumn.class, PrimaryKeyJoinColumnJpaAnnotation.class, PRIMARY_KEY_JOIN_COLUMNS );

AnnotationDescriptor<CheckConstraint> CHECK_CONSTRAINT = new OrmAnnotationDescriptor<>( CheckConstraint.class, CheckConstraintAnnotation.class );
AnnotationDescriptor<ForeignKey> FOREIGN_KEY = new OrmAnnotationDescriptor<>( ForeignKey.class, ForeignKeyAnnotation.class );
AnnotationDescriptor<UniqueConstraint> UNIQUE_CONSTRAINT = new OrmAnnotationDescriptor<>( UniqueConstraint.class, UniqueConstraintAnnotation.class );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.models.orm;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.hibernate.models.spi.SourceModelBuildingContext;

import jakarta.persistence.PrimaryKeyJoinColumn;

@SuppressWarnings({ "ClassExplicitlyAnnotation", "unused" })
public class PrimaryKeyJoinColumnJpaAnnotation implements PrimaryKeyJoinColumn, ColumnDetails, ColumnDetails.Definable {
private String name;
private String referencedColumnName;
private String columnDefinition;
private String options;
private jakarta.persistence.ForeignKey foreignKey;

/**
* Used in creating dynamic annotation instances (e.g. from XML)
*/
public PrimaryKeyJoinColumnJpaAnnotation(SourceModelBuildingContext modelContext) {
this.name = "";
this.referencedColumnName = "";
this.columnDefinition = "";
this.options = "";
this.foreignKey = new ForeignKeyAnnotation( modelContext );
}

/**
* Used in creating annotation instances from JDK variant
*/
public PrimaryKeyJoinColumnJpaAnnotation(PrimaryKeyJoinColumn annotation, SourceModelBuildingContext modelContext) {
this.name = annotation.name();
this.referencedColumnName = annotation.referencedColumnName();
this.columnDefinition = annotation.columnDefinition();
this.options = annotation.options();
this.foreignKey = new ForeignKeyAnnotation( annotation.foreignKey(), modelContext );
}

/**
* Used in creating annotation instances from Jandex variant
*/
public PrimaryKeyJoinColumnJpaAnnotation(
Map<String, Object> attributeValues,
SourceModelBuildingContext modelContext) {
this.name = (String) attributeValues.get( "name" );
this.referencedColumnName = (String) attributeValues.get( "referencedColumnName" );
this.columnDefinition = (String) attributeValues.get( "columnDefinition" );
this.options = (String) attributeValues.get( "options" );
this.foreignKey = (jakarta.persistence.ForeignKey) attributeValues.get( "foreignKey" );
}

@Override
public Class<? extends Annotation> annotationType() {
return PrimaryKeyJoinColumn.class;
}

@Override
public String name() {
return name;
}

public void name(String value) {
this.name = value;
}


@Override
public String referencedColumnName() {
return referencedColumnName;
}

public void referencedColumnName(String value) {
this.referencedColumnName = value;
}


@Override
public String columnDefinition() {
return columnDefinition;
}

public void columnDefinition(String value) {
this.columnDefinition = value;
}


@Override
public String options() {
return options;
}

public void options(String value) {
this.options = value;
}


@Override
public jakarta.persistence.ForeignKey foreignKey() {
return foreignKey;
}

public void foreignKey(jakarta.persistence.ForeignKey value) {
this.foreignKey = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.models.orm;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.hibernate.models.spi.SourceModelBuildingContext;

import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.PrimaryKeyJoinColumns;

@SuppressWarnings({ "ClassExplicitlyAnnotation", "unused" })
public class PrimaryKeyJoinColumnsJpaAnnotation
implements PrimaryKeyJoinColumns, RepeatableContainer<PrimaryKeyJoinColumn> {
private PrimaryKeyJoinColumn[] value;
private jakarta.persistence.ForeignKey foreignKey;

/**
* Used in creating dynamic annotation instances (e.g. from XML)
*/
public PrimaryKeyJoinColumnsJpaAnnotation(SourceModelBuildingContext modelContext) {
this.foreignKey = modelContext.getAnnotationDescriptorRegistry()
.getDescriptor( jakarta.persistence.ForeignKey.class )
.createUsage( modelContext );
}

/**
* Used in creating annotation instances from JDK variant
*/
public PrimaryKeyJoinColumnsJpaAnnotation(
PrimaryKeyJoinColumns annotation,
SourceModelBuildingContext modelContext) {
this.value = annotation.value();
this.foreignKey = new ForeignKeyAnnotation( annotation.foreignKey(), modelContext );
}

/**
* Used in creating annotation instances from Jandex variant
*/
public PrimaryKeyJoinColumnsJpaAnnotation(
Map<String, Object> attributeValues,
SourceModelBuildingContext modelContext) {
this.value = (PrimaryKeyJoinColumn[]) attributeValues.get( "value" );
this.foreignKey = (jakarta.persistence.ForeignKey) attributeValues.get( "foreignKey" );
}

@Override
public Class<? extends Annotation> annotationType() {
return PrimaryKeyJoinColumns.class;
}

@Override
public PrimaryKeyJoinColumn[] value() {
return value;
}

public void value(PrimaryKeyJoinColumn[] value) {
this.value = value;
}


@Override
public jakarta.persistence.ForeignKey foreignKey() {
return foreignKey;
}

public void foreignKey(jakarta.persistence.ForeignKey value) {
this.foreignKey = value;
}


}

0 comments on commit 9be40cb

Please sign in to comment.