-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for case of nested annotation values from JDK
- Loading branch information
Showing
6 changed files
with
351 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...e-models-jandex/src/test/java/org/hibernate/models/annotations/NestedAnnotationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...els-testing/src/main/java/org/hibernate/models/orm/PrimaryKeyJoinColumnJpaAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...ls-testing/src/main/java/org/hibernate/models/orm/PrimaryKeyJoinColumnsJpaAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |