Skip to content

Commit 5447f84

Browse files
committed
HSEARCH-5358 Update exising tests to account for possible problems with enclosed instance constructor parameter
1 parent 393d3f5 commit 5447f84

18 files changed

+2110
-1967
lines changed

integrationtest/mapper/pojo-base/src/test/java-noparameters/org/hibernate/search/integrationtest/mapper/pojo/mapping/definition/noparameters/ProjectionConstructorClassNoParametersCompilerFlagIT.java

Lines changed: 100 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -48,157 +48,135 @@ void sourcesCompiledWithoutParametersFlag() {
4848

4949
@Test
5050
void implicitInnerMapping() {
51-
@ProjectionConstructor
52-
class MyProjection {
53-
private final String someText;
54-
private final Integer someInteger;
55-
private final InnerProjection someContained;
56-
57-
MyProjection(String someText, Integer someInteger, InnerProjection someContained) {
58-
this.someText = someText;
59-
this.someInteger = someInteger;
60-
this.someContained = someContained;
61-
}
62-
63-
@ProjectionConstructor
64-
static class InnerProjection {
65-
private final String someText2;
66-
private final Integer someInteger2;
67-
68-
InnerProjection(String someText2, Integer someInteger2) {
69-
this.someText2 = someText2;
70-
this.someInteger2 = someInteger2;
71-
}
72-
}
73-
}
7451
assertThatThrownBy( () -> setupHelper.start()
75-
.withAnnotatedTypes( MyProjection.class, MyProjection.InnerProjection.class )
52+
.withAnnotatedTypes( ImplicitInnerMappingMyProjection.class,
53+
ImplicitInnerMappingMyProjection.InnerProjection.class )
7654
.setup( IndexedEntity.class ) )
7755
.isInstanceOf( SearchException.class )
7856
.satisfies( FailureReportUtils.hasFailureReport()
79-
.typeContext( MyProjection.class.getName() )
80-
.constructorContext( ProjectionConstructorClassNoParametersCompilerFlagIT.class, String.class,
57+
.typeContext( ImplicitInnerMappingMyProjection.class.getName() )
58+
.constructorContext( String.class,
8159
Integer.class,
82-
MyProjection.InnerProjection.class )
83-
.methodParameterContext( 1 )
60+
ImplicitInnerMappingMyProjection.InnerProjection.class )
61+
.methodParameterContext( 0 )
8462
.failure( "Missing parameter names in Java metadata for projection constructor",
8563
"When inferring inner projections from constructor parameters,"
8664
+ " constructor parameter names must be known",
8765
"Either make sure this class was compiled with the '-parameters' compiler flag",
8866
"or set the path explicitly with '@FieldProjection(path = ...)'" )
89-
.methodParameterContext( 2 )
67+
.methodParameterContext( 1 )
9068
.failure( "Missing parameter names in Java metadata for projection constructor",
9169
"When inferring inner projections from constructor parameters,"
9270
+ " constructor parameter names must be known",
9371
"Either make sure this class was compiled with the '-parameters' compiler flag",
9472
"or set the path explicitly with '@FieldProjection(path = ...)' or '@ObjectProjection(path = ...)'" )
95-
.methodParameterContext( 3 )
73+
.methodParameterContext( 2 )
9674
.failure( "Missing parameter names in Java metadata for projection constructor",
9775
"When inferring inner projections from constructor parameters,"
9876
+ " constructor parameter names must be known",
9977
"Either make sure this class was compiled with the '-parameters' compiler flag",
10078
"or set the path explicitly with '@FieldProjection(path = ...)' or '@ObjectProjection(path = ...)'" ) );
10179
}
10280

103-
@Test
104-
void explicitInnerMapping_implicitPath() {
105-
@ProjectionConstructor
106-
class MyProjection {
107-
private final String someText;
108-
private final Integer someInteger;
109-
private final InnerProjection someContained;
110-
111-
MyProjection(@FieldProjection String someText,
112-
@FieldProjection Integer someInteger,
113-
@ObjectProjection InnerProjection someContained) {
114-
this.someText = someText;
115-
this.someInteger = someInteger;
116-
this.someContained = someContained;
117-
}
81+
@ProjectionConstructor
82+
static class ImplicitInnerMappingMyProjection {
83+
private final String someText;
84+
private final Integer someInteger;
85+
private final InnerProjection someContained;
11886

119-
@ProjectionConstructor
120-
static class InnerProjection {
121-
private final String someText2;
122-
private final Integer someInteger2;
87+
ImplicitInnerMappingMyProjection(String someText, Integer someInteger, InnerProjection someContained) {
88+
this.someText = someText;
89+
this.someInteger = someInteger;
90+
this.someContained = someContained;
91+
}
92+
93+
@ProjectionConstructor
94+
static class InnerProjection {
95+
private final String someText2;
96+
private final Integer someInteger2;
12397

124-
InnerProjection(@FieldProjection String someText2,
125-
@FieldProjection Integer someInteger2) {
126-
this.someText2 = someText2;
127-
this.someInteger2 = someInteger2;
128-
}
98+
InnerProjection(String someText2, Integer someInteger2) {
99+
this.someText2 = someText2;
100+
this.someInteger2 = someInteger2;
129101
}
130102
}
103+
}
104+
105+
@Test
106+
void explicitInnerMapping_implicitPath() {
131107
assertThatThrownBy( () -> setupHelper.start()
132-
.withAnnotatedTypes( MyProjection.class, MyProjection.InnerProjection.class )
108+
.withAnnotatedTypes( ExplicitInnerMapping_implicitPathMyProjection.class,
109+
ExplicitInnerMapping_implicitPathMyProjection.InnerProjection.class )
133110
.setup( IndexedEntity.class ) )
134111
.isInstanceOf( SearchException.class )
135112
.satisfies( FailureReportUtils.hasFailureReport()
136-
.typeContext( MyProjection.class.getName() )
137-
.constructorContext( ProjectionConstructorClassNoParametersCompilerFlagIT.class, String.class,
113+
.typeContext( ExplicitInnerMapping_implicitPathMyProjection.class.getName() )
114+
.constructorContext( String.class,
138115
Integer.class,
139-
MyProjection.InnerProjection.class )
140-
.methodParameterContext( 1 )
116+
ExplicitInnerMapping_implicitPathMyProjection.InnerProjection.class )
117+
.methodParameterContext( 0 )
141118
.failure( "Missing parameter names in Java metadata for projection constructor",
142119
"When mapping a projection constructor parameter to a field projection without providing a field path,"
143120
+ " constructor parameter names must be known",
144121
"Either make sure this class was compiled with the '-parameters' compiler flag",
145122
"or set the path explicitly with '@FieldProjection(path = ...)'" )
146-
.methodParameterContext( 2 )
123+
.methodParameterContext( 1 )
147124
.failure( "Missing parameter names in Java metadata for projection constructor",
148125
"When mapping a projection constructor parameter to a field projection without providing a field path,"
149126
+ " constructor parameter names must be known",
150127
"Either make sure this class was compiled with the '-parameters' compiler flag",
151128
"or set the path explicitly with '@FieldProjection(path = ...)'" )
152-
.methodParameterContext( 3 )
129+
.methodParameterContext( 2 )
153130
.failure( "Missing parameter names in Java metadata for projection constructor",
154131
"When mapping a projection constructor parameter to an object projection without providing a field path,"
155132
+ " constructor parameter names must be known",
156133
"Either make sure this class was compiled with the '-parameters' compiler flag",
157134
"or set the path explicitly with '@ObjectProjection(path = ...)'" ) );
158135
}
159136

160-
@Test
161-
void explicitInnerMapping_explicitPath() {
162-
@ProjectionConstructor
163-
class MyProjection {
164-
private final String someText;
165-
private final Integer someInteger;
166-
private final InnerProjection someContained;
167-
168-
MyProjection(@FieldProjection(path = "text") String someText,
169-
@FieldProjection(path = "integer") Integer someInteger,
170-
@ObjectProjection(path = "contained") InnerProjection someContained) {
171-
this.someText = someText;
172-
this.someInteger = someInteger;
173-
this.someContained = someContained;
174-
}
175-
176-
@ProjectionConstructor
177-
static class InnerProjection {
178-
private final String someText2;
179-
private final Integer someInteger2;
137+
@ProjectionConstructor
138+
static class ExplicitInnerMapping_implicitPathMyProjection {
139+
private final String someText;
140+
private final Integer someInteger;
141+
private final InnerProjection someContained;
142+
143+
ExplicitInnerMapping_implicitPathMyProjection(@FieldProjection String someText,
144+
@FieldProjection Integer someInteger,
145+
@ObjectProjection InnerProjection someContained) {
146+
this.someText = someText;
147+
this.someInteger = someInteger;
148+
this.someContained = someContained;
149+
}
180150

181-
InnerProjection(@FieldProjection(path = "text2") String someText2,
182-
@FieldProjection(path = "integer2") Integer someInteger2) {
183-
this.someText2 = someText2;
184-
this.someInteger2 = someInteger2;
185-
}
151+
@ProjectionConstructor
152+
static class InnerProjection {
153+
private final String someText2;
154+
private final Integer someInteger2;
155+
156+
InnerProjection(@FieldProjection String someText2,
157+
@FieldProjection Integer someInteger2) {
158+
this.someText2 = someText2;
159+
this.someInteger2 = someInteger2;
186160
}
187161
}
162+
}
163+
164+
@Test
165+
void explicitInnerMapping_explicitPath() {
188166
backendMock.expectAnySchema( INDEX_NAME );
189167
SearchMapping mapping = setupHelper.start()
190-
.withAnnotatedTypes( MyProjection.class, MyProjection.InnerProjection.class )
168+
.withAnnotatedTypes( ExplicitInnerMapping_explicitPathMyProjection.class,
169+
ExplicitInnerMapping_explicitPathMyProjection.InnerProjection.class )
191170
.setup( IndexedEntity.class, ContainedEntity.class );
192171
testSuccessfulRootProjection(
193-
mapping, IndexedEntity.class, MyProjection.class,
172+
mapping, IndexedEntity.class, ExplicitInnerMapping_explicitPathMyProjection.class,
194173
Arrays.asList(
195174
Arrays.asList( "result1", 1, Arrays.asList( "result1_1", 11 ) ),
196175
Arrays.asList( "result2", 2, Arrays.asList( "result2_1", 21 ) ),
197176
Arrays.asList( "result3", 3, Arrays.asList( "result3_1", 31 ) )
198177
),
199178
f -> f.composite()
200179
.from(
201-
dummyProjectionForEnclosingClassInstance( f ),
202180
f.field( "text", String.class ),
203181
f.field( "integer", Integer.class ),
204182
f.object( "contained" )
@@ -210,16 +188,43 @@ static class InnerProjection {
210188
)
211189
.asList(),
212190
Arrays.asList(
213-
new MyProjection( "result1", 1,
214-
new MyProjection.InnerProjection( "result1_1", 11 ) ),
215-
new MyProjection( "result2", 2,
216-
new MyProjection.InnerProjection( "result2_1", 21 ) ),
217-
new MyProjection( "result3", 3,
218-
new MyProjection.InnerProjection( "result3_1", 31 ) )
191+
new ExplicitInnerMapping_explicitPathMyProjection( "result1", 1,
192+
new ExplicitInnerMapping_explicitPathMyProjection.InnerProjection( "result1_1", 11 ) ),
193+
new ExplicitInnerMapping_explicitPathMyProjection( "result2", 2,
194+
new ExplicitInnerMapping_explicitPathMyProjection.InnerProjection( "result2_1", 21 ) ),
195+
new ExplicitInnerMapping_explicitPathMyProjection( "result3", 3,
196+
new ExplicitInnerMapping_explicitPathMyProjection.InnerProjection( "result3_1", 31 ) )
219197
)
220198
);
221199
}
222200

201+
@ProjectionConstructor
202+
static class ExplicitInnerMapping_explicitPathMyProjection {
203+
private final String someText;
204+
private final Integer someInteger;
205+
private final InnerProjection someContained;
206+
207+
ExplicitInnerMapping_explicitPathMyProjection(@FieldProjection(path = "text") String someText,
208+
@FieldProjection(path = "integer") Integer someInteger,
209+
@ObjectProjection(path = "contained") InnerProjection someContained) {
210+
this.someText = someText;
211+
this.someInteger = someInteger;
212+
this.someContained = someContained;
213+
}
214+
215+
@ProjectionConstructor
216+
static class InnerProjection {
217+
private final String someText2;
218+
private final Integer someInteger2;
219+
220+
InnerProjection(@FieldProjection(path = "text2") String someText2,
221+
@FieldProjection(path = "integer2") Integer someInteger2) {
222+
this.someText2 = someText2;
223+
this.someInteger2 = someInteger2;
224+
}
225+
}
226+
}
227+
223228
@Indexed(index = INDEX_NAME)
224229
static class IndexedEntity {
225230
@DocumentId

integrationtest/mapper/pojo-base/src/test/java/org/hibernate/search/integrationtest/mapper/pojo/mapping/annotation/processing/CustomConstructorMappingAnnotationBaseIT.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.lang.invoke.MethodHandles;
1616
import java.util.Collections;
1717

18-
import org.hibernate.search.engine.search.projection.dsl.ProjectionFinalStep;
1918
import org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactory;
2019
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.DocumentId;
2120
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField;
@@ -59,10 +58,6 @@ class CustomConstructorMappingAnnotationBaseIT {
5958
@RegisterExtension
6059
public StaticCounters counters = StaticCounters.create();
6160

62-
protected final ProjectionFinalStep<?> dummyProjectionForEnclosingClassInstance(SearchProjectionFactory<?, ?> f) {
63-
return f.constant( null );
64-
}
65-
6661
/**
6762
* Basic test checking that a simple constructor mapping will be applied as expected.
6863
*/
@@ -75,20 +70,12 @@ class IndexedEntity {
7570
@GenericField
7671
String text;
7772
}
78-
class MyProjection {
79-
public final String text;
80-
81-
@WorkingAnnotation
82-
public MyProjection(String text) {
83-
this.text = text;
84-
}
85-
}
8673

8774
backendMock.expectAnySchema( INDEX_NAME );
8875

8976
SearchMapping mapping = setupHelper.start()
9077
.expectCustomBeans()
91-
.withAnnotatedTypes( MyProjection.class )
78+
.withAnnotatedTypes( SimpleMyProjection.class )
9279
.setup( IndexedEntity.class );
9380
backendMock.verifyExpectationsMet();
9481

@@ -99,7 +86,6 @@ public MyProjection(String text) {
9986
SearchProjectionFactory<?, ?> f = mapping.scope( IndexedEntity.class ).projection();
10087
b.projection( f.composite()
10188
.from(
102-
dummyProjectionForEnclosingClassInstance( f ),
10389
f.field( "text", String.class )
10490
)
10591
.asList() );
@@ -112,18 +98,27 @@ public MyProjection(String text) {
11298
);
11399

114100
assertThat( session.search( IndexedEntity.class )
115-
.select( MyProjection.class )
101+
.select( SimpleMyProjection.class )
116102
.where( f -> f.matchAll() )
117103
.fetchAllHits() )
118104
.usingRecursiveFieldByFieldElementComparator()
119105
.containsExactly(
120-
new MyProjection( "hit1Text" ),
121-
new MyProjection( "hit2Text" )
106+
new SimpleMyProjection( "hit1Text" ),
107+
new SimpleMyProjection( "hit2Text" )
122108
);
123109
}
124110
backendMock.verifyExpectationsMet();
125111
}
126112

113+
static class SimpleMyProjection {
114+
public final String text;
115+
116+
@WorkingAnnotation
117+
public SimpleMyProjection(String text) {
118+
this.text = text;
119+
}
120+
}
121+
127122
@Retention(RetentionPolicy.RUNTIME)
128123
@Target(ElementType.CONSTRUCTOR)
129124
@ConstructorMapping(processor = @ConstructorMappingAnnotationProcessorRef(type = WorkingAnnotation.Processor.class))

0 commit comments

Comments
 (0)