16
16
package org .springframework .data .mapping .model ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
+ import static org .mockito .ArgumentMatchers .*;
19
20
import static org .mockito .Mockito .*;
20
21
21
22
import lombok .AllArgsConstructor ;
22
23
import lombok .Data ;
23
24
import lombok .Value ;
24
25
25
- import org . junit . jupiter . api . Test ;
26
+ import java . util . stream . Stream ;
26
27
28
+ import org .junit .jupiter .api .DynamicTest ;
29
+ import org .junit .jupiter .api .Named ;
30
+ import org .junit .jupiter .api .Test ;
31
+ import org .junit .jupiter .api .TestFactory ;
27
32
import org .springframework .core .convert .ConversionService ;
28
33
import org .springframework .core .convert .support .DefaultConversionService ;
34
+ import org .springframework .data .mapping .PersistentEntity ;
35
+ import org .springframework .data .mapping .PersistentProperty ;
29
36
import org .springframework .data .mapping .PersistentPropertyAccessor ;
30
37
import org .springframework .data .mapping .context .SampleMappingContext ;
31
38
import org .springframework .data .mapping .context .SamplePersistentProperty ;
@@ -122,17 +129,47 @@ public void shouldConvertToPropertyPathLeafType() {
122
129
var context = new SampleMappingContext ();
123
130
124
131
var accessor = context .getPersistentEntity (Order .class ).getPropertyAccessor (order );
125
- var convertingAccessor = new ConvertingPropertyAccessor <Order >(accessor ,
126
- new DefaultConversionService ());
132
+ var convertingAccessor = new ConvertingPropertyAccessor <Order >(accessor , new DefaultConversionService ());
127
133
128
- var path = context .getPersistentPropertyPath ("customer.firstname" ,
129
- Order .class );
134
+ var path = context .getPersistentPropertyPath ("customer.firstname" , Order .class );
130
135
131
136
convertingAccessor .setProperty (path , 2 );
132
137
133
138
assertThat (convertingAccessor .getBean ().getCustomer ().getFirstname ()).isEqualTo ("2" );
134
139
}
135
140
141
+ @ TestFactory // #2546
142
+ Stream <DynamicTest > doesNotInvokeConversionForMatchingPrimitives () {
143
+
144
+ IntegerWrapper wrapper = new IntegerWrapper ();
145
+ wrapper .primitive = 42 ;
146
+ wrapper .boxed = 42 ;
147
+
148
+ SampleMappingContext context = new SampleMappingContext ();
149
+ PersistentEntity <Object , SamplePersistentProperty > entity = context
150
+ .getRequiredPersistentEntity (IntegerWrapper .class );
151
+
152
+ SamplePersistentProperty primitiveProperty = entity .getRequiredPersistentProperty ("primitive" );
153
+ SamplePersistentProperty boxedProperty = entity .getRequiredPersistentProperty ("boxed" );
154
+
155
+ PersistentPropertyAccessor <IntegerWrapper > accessor = entity .getPropertyAccessor (wrapper );
156
+ ConversionService conversionService = mock (ConversionService .class );
157
+
158
+ ConvertingPropertyAccessor <IntegerWrapper > convertingAccessor = new ConvertingPropertyAccessor <>(accessor ,
159
+ conversionService );
160
+
161
+ Stream <PrimitiveFixture > fixtures = Stream .of (PrimitiveFixture .$ (boxedProperty , int .class ),
162
+ PrimitiveFixture .$ (boxedProperty , Integer .class ), PrimitiveFixture .$ (primitiveProperty , int .class ),
163
+ PrimitiveFixture .$ (primitiveProperty , Integer .class ));
164
+
165
+ return DynamicTest .stream (fixtures , it -> {
166
+
167
+ convertingAccessor .getProperty (it .property , it .type );
168
+
169
+ verify (conversionService , never ()).convert (any (), eq (it .type ));
170
+ });
171
+ }
172
+
136
173
private static ConvertingPropertyAccessor getAccessor (Object entity , ConversionService conversionService ) {
137
174
138
175
PersistentPropertyAccessor wrapper = new BeanWrapper <>(entity );
@@ -142,8 +179,7 @@ private static ConvertingPropertyAccessor getAccessor(Object entity, ConversionS
142
179
private static SamplePersistentProperty getIdProperty () {
143
180
144
181
var mappingContext = new SampleMappingContext ();
145
- var entity = mappingContext
146
- .getRequiredPersistentEntity (Entity .class );
182
+ var entity = mappingContext .getRequiredPersistentEntity (Entity .class );
147
183
return entity .getPersistentProperty ("id" );
148
184
}
149
185
@@ -161,4 +197,26 @@ static class Order {
161
197
static class Customer {
162
198
String firstname ;
163
199
}
200
+
201
+ static class IntegerWrapper {
202
+ int primitive ;
203
+ Integer boxed ;
204
+ }
205
+
206
+ @ Value (staticConstructor = "$" )
207
+ static class PrimitiveFixture implements Named <PrimitiveFixture > {
208
+
209
+ PersistentProperty <?> property ;
210
+ Class <?> type ;
211
+
212
+ @ Override
213
+ public String getName () {
214
+ return String .format ("Accessing %s as %s does not cause conversion." , property , type );
215
+ }
216
+
217
+ @ Override
218
+ public PrimitiveFixture getPayload () {
219
+ return this ;
220
+ }
221
+ }
164
222
}
0 commit comments