15
15
*/
16
16
package org .springframework .data .util ;
17
17
18
- import static org .assertj .core .api .Assertions .*;
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static org .assertj .core .api .Assertions .assertThatNoException ;
20
+
21
+ import java .net .URLClassLoader ;
22
+ import java .util .HashSet ;
23
+ import java .util .Set ;
19
24
20
25
import org .junit .jupiter .api .Test ;
21
26
import org .springframework .aot .generate .ClassNameGenerator ;
26
31
import org .springframework .data .aot .sample .ConfigWithQuerydslPredicateExecutor .Person ;
27
32
import org .springframework .data .aot .sample .QConfigWithQuerydslPredicateExecutor_Person ;
28
33
import org .springframework .data .classloadersupport .HidingClassLoader ;
29
- import org .springframework .data .querydsl .User ;
30
34
import org .springframework .javapoet .ClassName ;
31
35
32
36
import com .querydsl .core .types .EntityPath ;
33
37
34
38
/**
35
39
* Unit tests for {@link QTypeContributor}.
36
40
*
41
+ * @author Christoph Strobl
37
42
* @author ckdgus08
38
43
*/
39
44
class QTypeContributorUnitTests {
@@ -75,8 +80,8 @@ void doesNotAddQTypeHintIfQuerydslNotPresent() {
75
80
RuntimeHintsPredicates .reflection ().onType (QConfigWithQuerydslPredicateExecutor_Person .class ).negate ());
76
81
}
77
82
78
- @ Test // DATAMONGO-4958
79
- void doesNotAddQTypeHintForArrayType () {
83
+ @ Test // GH-3284
84
+ void addsQTypeHintForArrayType () {
80
85
81
86
GenerationContext generationContext = new DefaultGenerationContext (
82
87
new ClassNameGenerator (ClassName .get (this .getClass ())), new InMemoryGeneratedFiles ());
@@ -85,48 +90,47 @@ void doesNotAddQTypeHintForArrayType() {
85
90
86
91
assertThat (generationContext .getRuntimeHints ()).matches (
87
92
RuntimeHintsPredicates .reflection ().onType (QConfigWithQuerydslPredicateExecutor_Person .class ).negate ());
88
- assertThat (generationContext .getRuntimeHints ()). matches (
89
- RuntimeHintsPredicates .reflection ().onType (QConfigWithQuerydslPredicateExecutor_Person [].class ). negate ( ));
93
+ assertThat (generationContext .getRuntimeHints ())
94
+ . matches ( RuntimeHintsPredicates .reflection ().onType (QConfigWithQuerydslPredicateExecutor_Person [].class ));
90
95
}
91
96
92
- @ Test // DATAMONGO-4958
93
- void addsQTypeHintForQUserType () {
97
+ @ Test // GH-3284
98
+ void doesNotAddQTypeHintForPrimitiveType () {
94
99
95
100
GenerationContext generationContext = new DefaultGenerationContext (
96
101
new ClassNameGenerator (ClassName .get (this .getClass ())), new InMemoryGeneratedFiles ());
97
102
98
- QTypeContributor .contributeEntityPath (User .class , generationContext , getClass ().getClassLoader ());
103
+ QTypeContributor .contributeEntityPath (int .class , generationContext , getClass ().getClassLoader ());
99
104
100
- var qUserHintCount = generationContext .getRuntimeHints ().reflection ().typeHints ()
101
- .filter (hint -> hint .getType ().getName ().equals ("org.springframework.data.querydsl.QUser" ))
102
- .count ();
103
- assertThat (qUserHintCount ).isEqualTo (1 );
105
+ assertThat (generationContext .getRuntimeHints ().reflection ().typeHints ()).isEmpty ();
104
106
}
105
107
106
- @ Test // DATAMONGO-4958
107
- void doesNotAddQTypeHintForQUserArrayType () {
108
+ @ Test // GH-3284
109
+ void doesNotFailForTypeInDefaultPackage () throws Exception {
108
110
109
111
GenerationContext generationContext = new DefaultGenerationContext (
110
112
new ClassNameGenerator (ClassName .get (this .getClass ())), new InMemoryGeneratedFiles ());
111
- var classLoader = getClass ().getClassLoader ();
112
113
113
- QTypeContributor . contributeEntityPath ( User []. class , generationContext , classLoader );
114
+ class CapturingClassLoader extends ClassLoader {
114
115
115
- assertThat (generationContext .getRuntimeHints ().reflection ().typeHints ()).isEmpty ();
116
- var qUserHintCount = generationContext .getRuntimeHints ().reflection ().typeHints ()
117
- .filter (hint -> hint .getType ().getName ().equals ("org.springframework.data.querydsl.QUser" ))
118
- .count ();
119
- assertThat (qUserHintCount ).isEqualTo (0 );
120
- }
116
+ final Set <String > lookups = new HashSet <>(10 );
121
117
122
- @ Test // DATAMONGO-4958
123
- void doesNotAddQTypeHintForPrimitiveType () {
118
+ CapturingClassLoader () {
119
+ super (URLClassLoader .getSystemClassLoader ());
120
+ }
124
121
125
- GenerationContext generationContext = new DefaultGenerationContext (
126
- new ClassNameGenerator (ClassName .get (this .getClass ())), new InMemoryGeneratedFiles ());
122
+ @ Override
123
+ public Class <?> loadClass (String name ) throws ClassNotFoundException {
124
+ lookups .add (name );
125
+ return super .loadClass (name );
126
+ }
127
+ }
127
128
128
- QTypeContributor . contributeEntityPath ( int . class , generationContext , getClass (). getClassLoader () );
129
+ CapturingClassLoader classLoaderToUse = new CapturingClassLoader ( );
129
130
130
- assertThat (generationContext .getRuntimeHints ().reflection ().typeHints ()).isEmpty ();
131
+ var typeInDefaultPackage = Class .forName ("TypeInDefaultPackage" );
132
+ assertThatNoException ().isThrownBy (
133
+ () -> QTypeContributor .contributeEntityPath (typeInDefaultPackage , generationContext , classLoaderToUse ));
134
+ assertThat (classLoaderToUse .lookups ).contains ("QTypeInDefaultPackage" );
131
135
}
132
136
}
0 commit comments