@ -15,7 +15,12 @@
@@ -15,7 +15,12 @@
* /
package org.springframework.data.util ;
import static org.assertj.core.api.Assertions.* ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatNoException ;
import java.net.URLClassLoader ;
import java.util.HashSet ;
import java.util.Set ;
import org.junit.jupiter.api.Test ;
import org.springframework.aot.generate.ClassNameGenerator ;
@ -26,7 +31,6 @@ import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
@@ -26,7 +31,6 @@ import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.data.aot.sample.ConfigWithQuerydslPredicateExecutor.Person ;
import org.springframework.data.aot.sample.QConfigWithQuerydslPredicateExecutor_Person ;
import org.springframework.data.classloadersupport.HidingClassLoader ;
import org.springframework.data.querydsl.User ;
import org.springframework.javapoet.ClassName ;
import com.querydsl.core.types.EntityPath ;
@ -34,6 +38,7 @@ import com.querydsl.core.types.EntityPath;
@@ -34,6 +38,7 @@ import com.querydsl.core.types.EntityPath;
/ * *
* Unit tests for { @link QTypeContributor } .
*
* @author Christoph Strobl
* @author ckdgus08
* /
class QTypeContributorUnitTests {
@ -75,8 +80,8 @@ class QTypeContributorUnitTests {
@@ -75,8 +80,8 @@ class QTypeContributorUnitTests {
RuntimeHintsPredicates . reflection ( ) . onType ( QConfigWithQuerydslPredicateExecutor_Person . class ) . negate ( ) ) ;
}
@Test // DATAMONGO-4958
void doesNotAdd QTypeHintForArrayType( ) {
@Test // GH-3284
void adds QTypeHintForArrayType( ) {
GenerationContext generationContext = new DefaultGenerationContext (
new ClassNameGenerator ( ClassName . get ( this . getClass ( ) ) ) , new InMemoryGeneratedFiles ( ) ) ;
@ -85,48 +90,47 @@ class QTypeContributorUnitTests {
@@ -85,48 +90,47 @@ class QTypeContributorUnitTests {
assertThat ( generationContext . getRuntimeHints ( ) ) . matches (
RuntimeHintsPredicates . reflection ( ) . onType ( QConfigWithQuerydslPredicateExecutor_Person . class ) . negate ( ) ) ;
assertThat ( generationContext . getRuntimeHints ( ) ) . matches (
RuntimeHintsPredicates . reflection ( ) . onType ( QConfigWithQuerydslPredicateExecutor_Person [ ] . class ) . negate ( ) ) ;
assertThat ( generationContext . getRuntimeHints ( ) )
. matches ( RuntimeHintsPredicates . reflection ( ) . onType ( QConfigWithQuerydslPredicateExecutor_Person [ ] . class ) ) ;
}
@Test // DATAMONGO-4958
void addsQTypeHintForQUser Type( ) {
@Test // GH-3284
void doesNotAddQTypeHintForPrimitive Type( ) {
GenerationContext generationContext = new DefaultGenerationContext (
new ClassNameGenerator ( ClassName . get ( this . getClass ( ) ) ) , new InMemoryGeneratedFiles ( ) ) ;
QTypeContributor . contributeEntityPath ( User . class , generationContext , getClass ( ) . getClassLoader ( ) ) ;
QTypeContributor . contributeEntityPath ( int . class , generationContext , getClass ( ) . getClassLoader ( ) ) ;
var qUserHintCount = generationContext . getRuntimeHints ( ) . reflection ( ) . typeHints ( )
. filter ( hint - > hint . getType ( ) . getName ( ) . equals ( "org.springframework.data.querydsl.QUser" ) )
. count ( ) ;
assertThat ( qUserHintCount ) . isEqualTo ( 1 ) ;
assertThat ( generationContext . getRuntimeHints ( ) . reflection ( ) . typeHints ( ) ) . isEmpty ( ) ;
}
@Test // DATAMONGO-4958
void doesNotAddQTypeHintForQUserArrayType ( ) {
@Test // GH-3284
void doesNotFailForTypeInDefaultPackage ( ) throws Exception {
GenerationContext generationContext = new DefaultGenerationContext (
new ClassNameGenerator ( ClassName . get ( this . getClass ( ) ) ) , new InMemoryGeneratedFiles ( ) ) ;
var classLoader = getClass ( ) . getClassLoader ( ) ;
QTypeContributor . contributeEntityPath ( User [ ] . class , generationContext , classLoader ) ;
class CapturingClassLoader extends ClassLoader {
assertThat ( generationContext . getRuntimeHints ( ) . reflection ( ) . typeHints ( ) ) . isEmpty ( ) ;
var qUserHintCount = generationContext . getRuntimeHints ( ) . reflection ( ) . typeHints ( )
. filter ( hint - > hint . getType ( ) . getName ( ) . equals ( "org.springframework.data.querydsl.QUser" ) )
. count ( ) ;
assertThat ( qUserHintCount ) . isEqualTo ( 0 ) ;
}
final Set < String > lookups = new HashSet < > ( 10 ) ;
@Test // DATAMONGO-4958
void doesNotAddQTypeHintForPrimitiveType ( ) {
CapturingClassLoader ( ) {
super ( URLClassLoader . getSystemClassLoader ( ) ) ;
}
GenerationContext generationContext = new DefaultGenerationContext (
new ClassNameGenerator ( ClassName . get ( this . getClass ( ) ) ) , new InMemoryGeneratedFiles ( ) ) ;
@Override
public Class < ? > loadClass ( String name ) throws ClassNotFoundException {
lookups . add ( name ) ;
return super . loadClass ( name ) ;
}
}
QTypeContributor . contributeEntityPath ( int . class , generationContext , getClass ( ) . getClassLoader ( ) ) ;
CapturingClassLoader classLoaderToUse = new CapturingClassLoader ( ) ;
assertThat ( generationContext . getRuntimeHints ( ) . reflection ( ) . typeHints ( ) ) . isEmpty ( ) ;
var typeInDefaultPackage = Class . forName ( "TypeInDefaultPackage" ) ;
assertThatNoException ( ) . isThrownBy (
( ) - > QTypeContributor . contributeEntityPath ( typeInDefaultPackage , generationContext , classLoaderToUse ) ) ;
assertThat ( classLoaderToUse . lookups ) . contains ( "QTypeInDefaultPackage" ) ;
}
}