23
23
import java .util .HashMap ;
24
24
import java .util .List ;
25
25
import java .util .Map ;
26
+ import java .util .Optional ;
26
27
27
28
import org .junit .jupiter .api .Test ;
28
29
import org .springframework .aop .Advisor ;
34
35
* Unit tests for {@link ProxyProjectionFactory}.
35
36
*
36
37
* @author Oliver Gierke
38
+ * @author Wim Deblauwe
39
+ * @author Mark Paluch
37
40
*/
38
41
class ProxyProjectionFactoryUnitTests {
39
42
@@ -218,6 +221,61 @@ void doesNotCreateWrappingProxyIfTargetImplementsProjectionInterface() {
218
221
assertThat (factory .createProjection (Contact .class , customer )).isSameAs (customer );
219
222
}
220
223
224
+ @ Test // DATACMNS-1762
225
+ void supportsOptionalAsReturnTypeIfEmpty () {
226
+
227
+ Customer customer = new Customer ();
228
+ customer .picture = null ;
229
+
230
+ CustomerWithOptional excerpt = factory .createProjection (CustomerWithOptional .class , customer );
231
+
232
+ assertThat (excerpt .getPicture ()).isEmpty ();
233
+ }
234
+
235
+ @ Test // DATACMNS-1762
236
+ void supportsOptionalAsReturnTypeIfPresent () {
237
+
238
+ Customer customer = new Customer ();
239
+ customer .picture = new byte [] { 1 , 2 , 3 };
240
+
241
+ CustomerWithOptional excerpt = factory .createProjection (CustomerWithOptional .class , customer );
242
+
243
+ assertThat (excerpt .getPicture ()).hasValueSatisfying (bytes -> {
244
+ assertThat (bytes ).isEqualTo (new byte [] { 1 , 2 , 3 });
245
+ });
246
+ }
247
+
248
+ @ Test // DATACMNS-1762
249
+ void supportsOptionalBackedByOptional () {
250
+
251
+ Customer customer = new Customer ();
252
+ customer .optional = Optional .of ("foo" );
253
+
254
+ CustomerWithOptional excerpt = factory .createProjection (CustomerWithOptional .class , customer );
255
+
256
+ assertThat (excerpt .getOptional ()).hasValue ("foo" );
257
+ }
258
+
259
+ @ Test // DATACMNS-1762
260
+ void supportsOptionalWithProjectionAsReturnTypeIfPresent () {
261
+
262
+ Customer customer = new Customer ();
263
+ customer .firstname = "Dave" ;
264
+ customer .lastname = "Matthews" ;
265
+
266
+ customer .address = new Address ();
267
+ customer .address .city = "New York" ;
268
+ customer .address .zipCode = "ZIP" ;
269
+
270
+ CustomerWithOptionalHavingProjection excerpt = factory .createProjection (CustomerWithOptionalHavingProjection .class ,
271
+ customer );
272
+
273
+ assertThat (excerpt .getFirstname ()).isEqualTo ("Dave" );
274
+ assertThat (excerpt .getAddress ()).hasValueSatisfying (addressExcerpt -> {
275
+ assertThat (addressExcerpt .getZipCode ()).isEqualTo ("ZIP" );
276
+ });
277
+ }
278
+
221
279
interface Contact {}
222
280
223
281
static class Customer implements Contact {
@@ -228,6 +286,7 @@ static class Customer implements Contact {
228
286
byte [] picture ;
229
287
Address [] shippingAddresses ;
230
288
Map <String , Object > data ;
289
+ Optional <String > optional ;
231
290
}
232
291
233
292
static class Address {
@@ -261,4 +320,20 @@ interface CustomerProxy {
261
320
262
321
void setFirstname (String firstname );
263
322
}
323
+
324
+ interface CustomerWithOptional {
325
+
326
+ String getFirstname ();
327
+
328
+ Optional <byte []> getPicture ();
329
+
330
+ Optional <String > getOptional ();
331
+ }
332
+
333
+ interface CustomerWithOptionalHavingProjection {
334
+
335
+ String getFirstname ();
336
+
337
+ Optional <AddressExcerpt > getAddress ();
338
+ }
264
339
}
0 commit comments