|
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,58 +80,52 @@ 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 doesNotFailForArrayType() { |
80 | 85 |
|
81 | 86 | GenerationContext generationContext = new DefaultGenerationContext( |
82 | 87 | new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles()); |
83 | 88 |
|
84 | | - QTypeContributor.contributeEntityPath(Person[].class, generationContext, HidingClassLoader.hideTypes()); |
85 | | - |
86 | | - assertThat(generationContext.getRuntimeHints()).matches( |
87 | | - RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class).negate()); |
88 | | - assertThat(generationContext.getRuntimeHints()).matches( |
89 | | - RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person[].class).negate()); |
| 89 | + assertThatNoException().isThrownBy( |
| 90 | + () -> QTypeContributor.contributeEntityPath(Person[].class, generationContext, HidingClassLoader.hideTypes())); |
90 | 91 | } |
91 | 92 |
|
92 | | - @Test // DATAMONGO-4958 |
93 | | - void addsQTypeHintForQUserType() { |
| 93 | + @Test // GH-3284 |
| 94 | + void doesNotFailForPrimitiveType() { |
94 | 95 |
|
95 | 96 | GenerationContext generationContext = new DefaultGenerationContext( |
96 | 97 | new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles()); |
97 | 98 |
|
98 | | - QTypeContributor.contributeEntityPath(User.class, generationContext, getClass().getClassLoader()); |
99 | | - |
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); |
| 99 | + assertThatNoException().isThrownBy( |
| 100 | + () -> QTypeContributor.contributeEntityPath(int.class, generationContext, getClass().getClassLoader())); |
104 | 101 | } |
105 | 102 |
|
106 | | - @Test // DATAMONGO-4958 |
107 | | - void doesNotAddQTypeHintForQUserArrayType() { |
| 103 | + @Test // GH-3284 |
| 104 | + void doesNotFailForTypeInDefaultPackage() throws Exception { |
108 | 105 |
|
109 | 106 | GenerationContext generationContext = new DefaultGenerationContext( |
110 | 107 | new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles()); |
111 | | - var classLoader = getClass().getClassLoader(); |
112 | 108 |
|
113 | | - QTypeContributor.contributeEntityPath(User[].class, generationContext, classLoader); |
| 109 | + class CapturingClassLoader extends ClassLoader { |
114 | 110 |
|
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 | | - } |
| 111 | + final Set<String> lookups = new HashSet<>(10); |
121 | 112 |
|
122 | | - @Test // DATAMONGO-4958 |
123 | | - void doesNotAddQTypeHintForPrimitiveType() { |
| 113 | + CapturingClassLoader() { |
| 114 | + super(URLClassLoader.getSystemClassLoader()); |
| 115 | + } |
124 | 116 |
|
125 | | - GenerationContext generationContext = new DefaultGenerationContext( |
126 | | - new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles()); |
| 117 | + @Override |
| 118 | + public Class<?> loadClass(String name) throws ClassNotFoundException { |
| 119 | + lookups.add(name); |
| 120 | + return super.loadClass(name); |
| 121 | + } |
| 122 | + } |
127 | 123 |
|
128 | | - QTypeContributor.contributeEntityPath(int.class, generationContext, getClass().getClassLoader()); |
| 124 | + CapturingClassLoader classLoaderToUse = new CapturingClassLoader(); |
129 | 125 |
|
130 | | - assertThat(generationContext.getRuntimeHints().reflection().typeHints()).isEmpty(); |
| 126 | + var typeInDefaultPackage = Class.forName("TypeInDefaultPackage"); |
| 127 | + assertThatNoException().isThrownBy( |
| 128 | + () -> QTypeContributor.contributeEntityPath(typeInDefaultPackage, generationContext, classLoaderToUse)); |
| 129 | + assertThat(classLoaderToUse.lookups).contains("QTypeInDefaultPackage"); |
131 | 130 | } |
132 | 131 | } |
0 commit comments