@@ -138,8 +138,10 @@ public class MongoTemplateTests {
138
138
139
139
@ Autowired
140
140
public void setApplicationContext (ConfigurableApplicationContext context ) {
141
- context . addApplicationListener ( new PersonWithIdPropertyOfTypeUUIDListener ());
141
+
142
142
this .context = context ;
143
+
144
+ context .addApplicationListener (new PersonWithIdPropertyOfTypeUUIDListener ());
143
145
}
144
146
145
147
@ Autowired
@@ -149,12 +151,12 @@ public void setMongo(Mongo mongo) throws Exception {
149
151
Arrays .asList (DateToDateTimeConverter .INSTANCE , DateTimeToDateConverter .INSTANCE ));
150
152
151
153
MongoMappingContext mappingContext = new MongoMappingContext ();
152
- mappingContext .setInitialEntitySet (new HashSet <Class <?>>(Arrays . asList ( PersonWith_idPropertyOfTypeObjectId . class ,
153
- PersonWith_idPropertyOfTypeString . class , PersonWithIdPropertyOfTypeObjectId .class ,
154
- PersonWithIdPropertyOfTypeString .class , PersonWithIdPropertyOfTypeInteger .class ,
155
- PersonWithIdPropertyOfTypeBigInteger .class , PersonWithIdPropertyOfPrimitiveInt .class ,
156
- PersonWithIdPropertyOfTypeLong .class , PersonWithIdPropertyOfPrimitiveLong .class ,
157
- PersonWithIdPropertyOfTypeUUID .class )));
154
+ mappingContext .setInitialEntitySet (new HashSet <Class <?>>(
155
+ Arrays . asList ( PersonWith_idPropertyOfTypeObjectId . class , PersonWith_idPropertyOfTypeString .class ,
156
+ PersonWithIdPropertyOfTypeObjectId .class , PersonWithIdPropertyOfTypeString .class ,
157
+ PersonWithIdPropertyOfTypeInteger .class , PersonWithIdPropertyOfTypeBigInteger .class ,
158
+ PersonWithIdPropertyOfPrimitiveInt .class , PersonWithIdPropertyOfTypeLong .class ,
159
+ PersonWithIdPropertyOfPrimitiveLong . class , PersonWithIdPropertyOfTypeUUID .class )));
158
160
mappingContext .setSimpleTypeHolder (conversions .getSimpleTypeHolder ());
159
161
mappingContext .initialize ();
160
162
@@ -168,8 +170,11 @@ public void setMongo(Mongo mongo) throws Exception {
168
170
169
171
@ Before
170
172
public void setUp () {
173
+
171
174
cleanDb ();
172
175
queryMongoVersionIfNecessary ();
176
+
177
+ this .mappingTemplate .setApplicationContext (context );
173
178
}
174
179
175
180
@ After
@@ -659,7 +664,6 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
659
664
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID ();
660
665
p13 .setFirstName ("Sven_10" );
661
666
p13 .setAge (22 );
662
- p13 .setId (UUID .randomUUID ());
663
667
// insert
664
668
mongoTemplate .insert (p13 );
665
669
// also try save
@@ -3216,6 +3220,223 @@ public void updateShouldWorkForTypesContainingGeoJsonTypes() {
3216
3220
assertThat (template .findOne (query (where ("id" ).is (wgj .id )), WithGeoJson .class ).point , is (equalTo (wgj .point )));
3217
3221
}
3218
3222
3223
+ @ Test // DATAMONGO-1404
3224
+ public void updatesDateValueCorrectlyWhenUsingMinOperator () {
3225
+
3226
+ Calendar cal = Calendar .getInstance (Locale .US );
3227
+ cal .set (2013 , 10 , 13 , 0 , 0 , 0 );
3228
+
3229
+ TypeWithDate twd = new TypeWithDate ();
3230
+ twd .date = new Date ();
3231
+ template .save (twd );
3232
+ template .updateFirst (query (where ("id" ).is (twd .id )), new Update ().min ("date" , cal .getTime ()), TypeWithDate .class );
3233
+
3234
+ TypeWithDate loaded = template .find (query (where ("id" ).is (twd .id )), TypeWithDate .class ).get (0 );
3235
+ assertThat (loaded .date , equalTo (cal .getTime ()));
3236
+ }
3237
+
3238
+ @ Test // DATAMONGO-1404
3239
+ public void updatesNumericValueCorrectlyWhenUsingMinOperator () {
3240
+
3241
+ TypeWithNumbers twn = new TypeWithNumbers ();
3242
+ twn .byteVal = 100 ;
3243
+ twn .doubleVal = 200D ;
3244
+ twn .floatVal = 300F ;
3245
+ twn .intVal = 400 ;
3246
+ twn .longVal = 500L ;
3247
+
3248
+ // Note that $min operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
3249
+ twn .bigIntegerVal = new BigInteger ("600" );
3250
+ twn .bigDeciamVal = new BigDecimal ("700.0" );
3251
+
3252
+ template .save (twn );
3253
+
3254
+ byte byteVal = 90 ;
3255
+ Update update = new Update ()//
3256
+ .min ("byteVal" , byteVal ) //
3257
+ .min ("doubleVal" , 190D ) //
3258
+ .min ("floatVal" , 290F ) //
3259
+ .min ("intVal" , 390 ) //
3260
+ .min ("longVal" , 490 ) //
3261
+ .min ("bigIntegerVal" , new BigInteger ("590" )) //
3262
+ .min ("bigDeciamVal" , new BigDecimal ("690" )) //
3263
+ ;
3264
+
3265
+ template .updateFirst (query (where ("id" ).is (twn .id )), update , TypeWithNumbers .class );
3266
+
3267
+ TypeWithNumbers loaded = template .find (query (where ("id" ).is (twn .id )), TypeWithNumbers .class ).get (0 );
3268
+ assertThat (loaded .byteVal , equalTo (byteVal ));
3269
+ assertThat (loaded .doubleVal , equalTo (190D ));
3270
+ assertThat (loaded .floatVal , equalTo (290F ));
3271
+ assertThat (loaded .intVal , equalTo (390 ));
3272
+ assertThat (loaded .longVal , equalTo (490L ));
3273
+ assertThat (loaded .bigIntegerVal , equalTo (new BigInteger ("590" )));
3274
+ assertThat (loaded .bigDeciamVal , equalTo (new BigDecimal ("690" )));
3275
+ }
3276
+
3277
+ @ Test // DATAMONGO-1404
3278
+ public void updatesDateValueCorrectlyWhenUsingMaxOperator () {
3279
+
3280
+ Calendar cal = Calendar .getInstance (Locale .US );
3281
+ cal .set (2013 , 10 , 13 , 0 , 0 , 0 );
3282
+
3283
+ TypeWithDate twd = new TypeWithDate ();
3284
+ twd .date = cal .getTime ();
3285
+ template .save (twd );
3286
+
3287
+ cal .set (2019 , 10 , 13 , 0 , 0 , 0 );
3288
+ template .updateFirst (query (where ("id" ).is (twd .id )), new Update ().max ("date" , cal .getTime ()), TypeWithDate .class );
3289
+
3290
+ TypeWithDate loaded = template .find (query (where ("id" ).is (twd .id )), TypeWithDate .class ).get (0 );
3291
+ assertThat (loaded .date , equalTo (cal .getTime ()));
3292
+ }
3293
+
3294
+ @ Test // DATAMONGO-1404
3295
+ public void updatesNumericValueCorrectlyWhenUsingMaxOperator () {
3296
+
3297
+ TypeWithNumbers twn = new TypeWithNumbers ();
3298
+ twn .byteVal = 100 ;
3299
+ twn .doubleVal = 200D ;
3300
+ twn .floatVal = 300F ;
3301
+ twn .intVal = 400 ;
3302
+ twn .longVal = 500L ;
3303
+
3304
+ // Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
3305
+ twn .bigIntegerVal = new BigInteger ("600" );
3306
+ twn .bigDeciamVal = new BigDecimal ("700.0" );
3307
+
3308
+ template .save (twn );
3309
+
3310
+ byte byteVal = 101 ;
3311
+ Update update = new Update ()//
3312
+ .max ("byteVal" , byteVal ) //
3313
+ .max ("doubleVal" , 290D ) //
3314
+ .max ("floatVal" , 390F ) //
3315
+ .max ("intVal" , 490 ) //
3316
+ .max ("longVal" , 590 ) //
3317
+ .max ("bigIntegerVal" , new BigInteger ("690" )) //
3318
+ .max ("bigDeciamVal" , new BigDecimal ("790" )) //
3319
+ ;
3320
+
3321
+ template .updateFirst (query (where ("id" ).is (twn .id )), update , TypeWithNumbers .class );
3322
+
3323
+ TypeWithNumbers loaded = template .find (query (where ("id" ).is (twn .id )), TypeWithNumbers .class ).get (0 );
3324
+ assertThat (loaded .byteVal , equalTo (byteVal ));
3325
+ assertThat (loaded .doubleVal , equalTo (290D ));
3326
+ assertThat (loaded .floatVal , equalTo (390F ));
3327
+ assertThat (loaded .intVal , equalTo (490 ));
3328
+ assertThat (loaded .longVal , equalTo (590L ));
3329
+ assertThat (loaded .bigIntegerVal , equalTo (new BigInteger ("690" )));
3330
+ assertThat (loaded .bigDeciamVal , equalTo (new BigDecimal ("790" )));
3331
+ }
3332
+
3333
+ @ Test // DATAMONGO-1404
3334
+ public void updatesBigNumberValueUsingStringComparisonWhenUsingMaxOperator () {
3335
+
3336
+ TypeWithNumbers twn = new TypeWithNumbers ();
3337
+
3338
+ // Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
3339
+ // Therefore "80" is considered greater than "700"
3340
+ twn .bigIntegerVal = new BigInteger ("600" );
3341
+ twn .bigDeciamVal = new BigDecimal ("700.0" );
3342
+
3343
+ template .save (twn );
3344
+
3345
+ Update update = new Update ()//
3346
+ .max ("bigIntegerVal" , new BigInteger ("70" )) //
3347
+ .max ("bigDeciamVal" , new BigDecimal ("80" )) //
3348
+ ;
3349
+
3350
+ template .updateFirst (query (where ("id" ).is (twn .id )), update , TypeWithNumbers .class );
3351
+
3352
+ TypeWithNumbers loaded = template .find (query (where ("id" ).is (twn .id )), TypeWithNumbers .class ).get (0 );
3353
+ assertThat (loaded .bigIntegerVal , equalTo (new BigInteger ("70" )));
3354
+ assertThat (loaded .bigDeciamVal , equalTo (new BigDecimal ("80" )));
3355
+ }
3356
+
3357
+ @ Test // DATAMONGO-1404
3358
+ public void updatesBigNumberValueUsingStringComparisonWhenUsingMinOperator () {
3359
+
3360
+ TypeWithNumbers twn = new TypeWithNumbers ();
3361
+
3362
+ // Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
3363
+ // Therefore "80" is considered greater than "700"
3364
+ twn .bigIntegerVal = new BigInteger ("80" );
3365
+ twn .bigDeciamVal = new BigDecimal ("90.0" );
3366
+
3367
+ template .save (twn );
3368
+
3369
+ Update update = new Update ()//
3370
+ .min ("bigIntegerVal" , new BigInteger ("700" )) //
3371
+ .min ("bigDeciamVal" , new BigDecimal ("800" )) //
3372
+ ;
3373
+
3374
+ template .updateFirst (query (where ("id" ).is (twn .id )), update , TypeWithNumbers .class );
3375
+
3376
+ TypeWithNumbers loaded = template .find (query (where ("id" ).is (twn .id )), TypeWithNumbers .class ).get (0 );
3377
+ assertThat (loaded .bigIntegerVal , equalTo (new BigInteger ("700" )));
3378
+ assertThat (loaded .bigDeciamVal , equalTo (new BigDecimal ("800" )));
3379
+ }
3380
+
3381
+ @ Test // DATAMONGO-1431
3382
+ public void streamExecutionUsesExplicitCollectionName () {
3383
+
3384
+ template .remove (new Query (), "some_special_collection" );
3385
+ template .remove (new Query (), Document .class );
3386
+
3387
+ Document document = new Document ();
3388
+
3389
+ template .insert (document , "some_special_collection" );
3390
+
3391
+ CloseableIterator <Document > stream = template .stream (new Query (), Document .class );
3392
+
3393
+ assertThat (stream .hasNext (), is (false ));
3394
+
3395
+ stream = template .stream (new Query (), Document .class , "some_special_collection" );
3396
+
3397
+ assertThat (stream .hasNext (), is (true ));
3398
+ assertThat (stream .next ().id , is (document .id ));
3399
+ assertThat (stream .hasNext (), is (false ));
3400
+ }
3401
+
3402
+ @ Test // DATAMONGO-1194
3403
+ public void shouldFetchListOfReferencesCorrectly () {
3404
+
3405
+ Sample one = new Sample ("1" , "jon snow" );
3406
+ Sample two = new Sample ("2" , "tyrion lannister" );
3407
+
3408
+ template .save (one );
3409
+ template .save (two );
3410
+
3411
+ DocumentWithDBRefCollection source = new DocumentWithDBRefCollection ();
3412
+ source .dbRefAnnotatedList = Arrays .asList (two , one );
3413
+
3414
+ template .save (source );
3415
+
3416
+ assertThat (template .findOne (query (where ("id" ).is (source .id )), DocumentWithDBRefCollection .class ), is (source ));
3417
+ }
3418
+
3419
+ @ Test // DATAMONGO-1194
3420
+ public void shouldFetchListOfLazyReferencesCorrectly () {
3421
+
3422
+ Sample one = new Sample ("1" , "jon snow" );
3423
+ Sample two = new Sample ("2" , "tyrion lannister" );
3424
+
3425
+ template .save (one );
3426
+ template .save (two );
3427
+
3428
+ DocumentWithDBRefCollection source = new DocumentWithDBRefCollection ();
3429
+ source .lazyDbRefAnnotatedList = Arrays .asList (two , one );
3430
+
3431
+ template .save (source );
3432
+
3433
+ DocumentWithDBRefCollection target = template .findOne (query (where ("id" ).is (source .id )),
3434
+ DocumentWithDBRefCollection .class );
3435
+
3436
+ assertThat (target .lazyDbRefAnnotatedList , instanceOf (LazyLoadingProxy .class ));
3437
+ assertThat (target .getLazyDbRefAnnotatedList (), contains (two , one ));
3438
+ }
3439
+
3219
3440
/**
3220
3441
* @see DATAMONGO-1513
3221
3442
*/
@@ -3629,10 +3850,12 @@ static class WithObjectTypeProperty {
3629
3850
Object value ;
3630
3851
}
3631
3852
3632
- static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener <PersonWithIdPropertyOfTypeUUID > {
3853
+ static class PersonWithIdPropertyOfTypeUUIDListener
3854
+ extends AbstractMongoEventListener <PersonWithIdPropertyOfTypeUUID > {
3633
3855
3634
3856
@ Override
3635
3857
public void onBeforeConvert (BeforeConvertEvent <PersonWithIdPropertyOfTypeUUID > event ) {
3858
+
3636
3859
PersonWithIdPropertyOfTypeUUID person = event .getSource ();
3637
3860
3638
3861
if (person .getId () != null ) {
@@ -3641,6 +3864,5 @@ public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> e
3641
3864
3642
3865
person .setId (UUID .randomUUID ());
3643
3866
}
3644
-
3645
3867
}
3646
3868
}
0 commit comments