16
16
17
17
package org .springframework .data .couchbase .core .mapping ;
18
18
19
- import static org .assertj .core .api .Assertions .*;
19
+ import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
20
21
import static org .assertj .core .data .Offset .offset ;
21
- import static org .junit .jupiter .api .Assertions .* ;
22
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
22
23
23
24
import java .math .BigDecimal ;
25
+ import java .math .BigInteger ;
26
+ import java .text .ChoiceFormat ;
24
27
import java .time .LocalDateTime ;
25
28
import java .time .temporal .ChronoUnit ;
26
- import java .util .*;
29
+ import java .util .ArrayList ;
30
+ import java .util .Arrays ;
31
+ import java .util .Calendar ;
32
+ import java .util .Collection ;
33
+ import java .util .Date ;
34
+ import java .util .HashMap ;
35
+ import java .util .HashSet ;
36
+ import java .util .LinkedHashMap ;
37
+ import java .util .LinkedList ;
38
+ import java .util .List ;
39
+ import java .util .Map ;
40
+ import java .util .Set ;
41
+ import java .util .TreeMap ;
42
+ import java .util .TreeSet ;
43
+ import java .util .UUID ;
44
+ import java .util .stream .Collectors ;
27
45
28
46
import org .junit .jupiter .api .Test ;
29
47
import org .springframework .core .convert .converter .Converter ;
35
53
import org .springframework .data .couchbase .core .convert .CouchbaseCustomConversions ;
36
54
import org .springframework .data .couchbase .core .convert .CouchbaseJsr310Converters .LocalDateTimeToLongConverter ;
37
55
import org .springframework .data .couchbase .core .convert .MappingCouchbaseConverter ;
38
- import org .springframework .data .couchbase .core .mapping .id .*;
56
+ import org .springframework .data .couchbase .core .mapping .id .GeneratedValue ;
57
+ import org .springframework .data .couchbase .core .mapping .id .GenerationStrategy ;
58
+ import org .springframework .data .couchbase .core .mapping .id .IdAttribute ;
59
+ import org .springframework .data .couchbase .core .mapping .id .IdPrefix ;
60
+ import org .springframework .data .couchbase .core .mapping .id .IdSuffix ;
39
61
import org .springframework .data .couchbase .domain .Address ;
40
62
import org .springframework .data .couchbase .domain .Config ;
41
63
import org .springframework .data .couchbase .domain .Person ;
@@ -70,8 +92,9 @@ void shouldNotThrowNPE() {
70
92
void doesNotAllowSimpleType1 () {
71
93
try {
72
94
converter .write ("hello" , new CouchbaseDocument ());
73
- } catch (Exception e ){
74
- if (!(e instanceof MappingException ) && !e .getClass ().getName ().equals ("java.lang.reflect.InaccessibleObjectException" )){
95
+ } catch (Exception e ) {
96
+ if (!(e instanceof MappingException )
97
+ && !e .getClass ().getName ().equals ("java.lang.reflect.InaccessibleObjectException" )) {
75
98
throw new RuntimeException ("Should have thrown MappingException or InaccessibleObjectException" , e );
76
99
}
77
100
}
@@ -81,8 +104,9 @@ void doesNotAllowSimpleType1() {
81
104
void doesNotAllowSimpleType2 () {
82
105
try {
83
106
converter .write (true , new CouchbaseDocument ());
84
- } catch (Exception e ){
85
- if (!(e instanceof MappingException ) && !e .getClass ().getName ().equals ("java.lang.reflect.InaccessibleObjectException" )){
107
+ } catch (Exception e ) {
108
+ if (!(e instanceof MappingException )
109
+ && !e .getClass ().getName ().equals ("java.lang.reflect.InaccessibleObjectException" )) {
86
110
throw new RuntimeException ("Should have thrown MappingException or InaccessibleObjectException" , e );
87
111
}
88
112
}
@@ -92,8 +116,9 @@ void doesNotAllowSimpleType2() {
92
116
void doesNotAllowSimpleType3 () {
93
117
try {
94
118
converter .write (42 , new CouchbaseDocument ());
95
- } catch (Exception e ){
96
- if (!(e instanceof MappingException ) && !e .getClass ().getName ().equals ("java.lang.reflect.InaccessibleObjectException" )){
119
+ } catch (Exception e ) {
120
+ if (!(e instanceof MappingException )
121
+ && !e .getClass ().getName ().equals ("java.lang.reflect.InaccessibleObjectException" )) {
97
122
throw new RuntimeException ("Should have thrown MappingException or InaccessibleObjectException" , e );
98
123
}
99
124
}
@@ -112,7 +137,7 @@ void writesString() {
112
137
converter .write (entity , converted );
113
138
Map <String , Object > result = converted .export ();
114
139
assertThat (result .get ("_class" )).isEqualTo (entity .getClass ().getName ());
115
- assertThat (result .get ("attr0" )).isEqualTo ("foobar" );
140
+ assertThat (result .get ("attr0" )).isEqualTo (entity . attr0 );
116
141
assertThat (converted .getId ()).isEqualTo (BaseEntity .ID );
117
142
}
118
143
@@ -123,7 +148,51 @@ void readsString() {
123
148
source .put ("attr0" , "foobar" );
124
149
125
150
StringEntity converted = converter .read (StringEntity .class , source );
126
- assertThat (converted .attr0 ).isEqualTo ("foobar" );
151
+ assertThat (converted .attr0 ).isEqualTo (source .get ("attr0" ));
152
+ }
153
+
154
+ @ Test
155
+ void writesBigInteger () {
156
+ CouchbaseDocument converted = new CouchbaseDocument ();
157
+ BigIntegerEntity entity = new BigIntegerEntity (new BigInteger ("12345" ));
158
+
159
+ converter .write (entity , converted );
160
+ Map <String , Object > result = converted .export ();
161
+ assertThat (result .get ("_class" )).isEqualTo (entity .getClass ().getName ());
162
+ assertThat (result .get ("attr0" )).isEqualTo (entity .attr0 .toString ());
163
+ assertThat (converted .getId ()).isEqualTo (BaseEntity .ID );
164
+ }
165
+
166
+ @ Test
167
+ void readsBigInteger () {
168
+ CouchbaseDocument source = new CouchbaseDocument ();
169
+ source .put ("_class" , BigIntegerEntity .class .getName ());
170
+ source .put ("attr0" , "12345" );
171
+
172
+ BigIntegerEntity converted = converter .read (BigIntegerEntity .class , source );
173
+ assertThat (converted .attr0 ).isEqualTo (new BigInteger ((String ) source .get ("attr0" )));
174
+ }
175
+
176
+ @ Test
177
+ void writesBigDecimal () {
178
+ CouchbaseDocument converted = new CouchbaseDocument ();
179
+ BigDecimalEntity entity = new BigDecimalEntity (new BigDecimal ("123.45" ));
180
+
181
+ converter .write (entity , converted );
182
+ Map <String , Object > result = converted .export ();
183
+ assertThat (result .get ("_class" )).isEqualTo (entity .getClass ().getName ());
184
+ assertThat (result .get ("attr0" )).isEqualTo (entity .attr0 .toString ());
185
+ assertThat (converted .getId ()).isEqualTo (BaseEntity .ID );
186
+ }
187
+
188
+ @ Test
189
+ void readsBigDecimal () {
190
+ CouchbaseDocument source = new CouchbaseDocument ();
191
+ source .put ("_class" , BigDecimalEntity .class .getName ());
192
+ source .put ("attr0" , "123.45" );
193
+
194
+ BigDecimalEntity converted = converter .read (BigDecimalEntity .class , source );
195
+ assertThat (converted .attr0 ).isEqualTo (new BigDecimal ((String ) source .get ("attr0" )));
127
196
}
128
197
129
198
@ Test
@@ -438,8 +507,8 @@ void writesAndReadsValueClassCustomType() {
438
507
@ Test
439
508
void writesAndReadsCustomConvertedClass () {
440
509
List <Object > converters = new ArrayList <>();
441
- converters .add (BigDecimalToStringConverter .INSTANCE );
442
- converters .add (StringToBigDecimalConverter .INSTANCE );
510
+ converters .add (ChoiceFormatToStringConverter .INSTANCE );
511
+ converters .add (StringToChoiceFormatConverter .INSTANCE );
443
512
CustomConversions customConversions = new CouchbaseCustomConversions (converters );
444
513
converter .setCustomConversions (customConversions );
445
514
converter .afterPropertiesSet ();
@@ -448,14 +517,14 @@ void writesAndReadsCustomConvertedClass() {
448
517
449
518
CouchbaseDocument converted = new CouchbaseDocument ();
450
519
451
- final String valueStr = "12.345 " ;
452
- final BigDecimal value = new BigDecimal (valueStr );
453
- final String value2Str = "0.6789 " ;
454
- final BigDecimal value2 = new BigDecimal (value2Str );
455
- List <BigDecimal > listOfValues = new ArrayList <>();
520
+ final String valueStr = "1.0,2.0,3.0,4.0,5.0,6.0,7.0|Sun,Mon,Tue,Wed,Thur,Fri,Sat " ;
521
+ final ChoiceFormat value = fromString (valueStr );
522
+ final String value2Str = "1.0,2.0,3.0|Jan,Feb,Mar " ;
523
+ final ChoiceFormat value2 = fromString (value2Str );
524
+ List <ChoiceFormat > listOfValues = new ArrayList <>();
456
525
listOfValues .add (value );
457
526
listOfValues .add (value2 );
458
- Map <String , BigDecimal > mapOfValues = new TreeMap <>();
527
+ Map <String , ChoiceFormat > mapOfValues = new TreeMap <>();
459
528
mapOfValues .put ("val1" , value );
460
529
mapOfValues .put ("val2" , value2 );
461
530
@@ -486,11 +555,25 @@ void writesAndReadsCustomConvertedClass() {
486
555
assertThat (readConverted .mapOfValues .get ("val2" )).isEqualTo (mapOfValues .get ("val2" ));
487
556
}
488
557
558
+ static private String toString (ChoiceFormat choiceFormat ) {
559
+ String limits = Arrays .stream (choiceFormat .getLimits ()).mapToObj (String ::valueOf ).collect (Collectors .joining ("," ));
560
+ String formats = Arrays .stream (choiceFormat .getFormats ()).map ((o ) -> String .valueOf (o ))
561
+ .collect (Collectors .joining ("," ));
562
+ return limits + "|" + formats ;
563
+ }
564
+
565
+ static private ChoiceFormat fromString (String source ) {
566
+ String [] split = source .split ("\\ |" );
567
+ double [] limits = Arrays .stream (split [0 ].split ("," )).mapToDouble (Double ::parseDouble ).toArray ();
568
+ String [] formats = split [1 ].split ("," );
569
+ return new ChoiceFormat (limits , formats );
570
+ }
571
+
489
572
@ Test
490
573
void writesAndReadsCustomFieldsConvertedClass () {
491
574
List <Object > converters = new ArrayList <>();
492
- converters .add (BigDecimalToStringConverter .INSTANCE );
493
- converters .add (StringToBigDecimalConverter .INSTANCE );
575
+ converters .add (ChoiceFormatToStringConverter .INSTANCE );
576
+ converters .add (StringToChoiceFormatConverter .INSTANCE );
494
577
CustomConversions customConversions = new CouchbaseCustomConversions (converters );
495
578
converter .setCustomConversions (customConversions );
496
579
converter .afterPropertiesSet ();
@@ -499,14 +582,14 @@ void writesAndReadsCustomFieldsConvertedClass() {
499
582
500
583
CouchbaseDocument converted = new CouchbaseDocument ();
501
584
502
- final String valueStr = "12.345 " ;
503
- final BigDecimal value = new BigDecimal (valueStr );
504
- final String value2Str = "0.6789 " ;
505
- final BigDecimal value2 = new BigDecimal (value2Str );
506
- List <BigDecimal > listOfValues = new ArrayList <>();
585
+ final String valueStr = "1.0,2.0,3.0,4.0,5.0,6.0,7.0|Sun,Mon,Tue,Wed,Thur,Fri,Sat " ;
586
+ final ChoiceFormat value = fromString (valueStr );
587
+ final String value2Str = "1.0,2.0,3.0|Jan,Feb,Mar " ;
588
+ final ChoiceFormat value2 = fromString (value2Str );
589
+ List <ChoiceFormat > listOfValues = new ArrayList <>();
507
590
listOfValues .add (value );
508
591
listOfValues .add (value2 );
509
- Map <String , BigDecimal > mapOfValues = new HashMap <>();
592
+ Map <String , ChoiceFormat > mapOfValues = new TreeMap <>();
510
593
mapOfValues .put ("val1" , value );
511
594
mapOfValues .put ("val2" , value2 );
512
595
@@ -515,18 +598,18 @@ void writesAndReadsCustomFieldsConvertedClass() {
515
598
516
599
CouchbaseDocument source = new CouchbaseDocument ();
517
600
source .put ("_class" , CustomFieldsEntity .class .getName ());
518
- source .put ("decimalValue " , valueStr );
601
+ source .put ("choiceFormatValue " , valueStr );
519
602
CouchbaseList listOfValuesDoc = new CouchbaseList ();
520
603
listOfValuesDoc .put (valueStr );
521
604
listOfValuesDoc .put (value2Str );
522
- source .put ("listOfDecimalValues " , listOfValuesDoc );
605
+ source .put ("listOfChoiceFormatValues " , listOfValuesDoc );
523
606
CouchbaseDocument mapOfValuesDoc = new CouchbaseDocument ();
524
607
mapOfValuesDoc .put ("val1" , valueStr );
525
608
mapOfValuesDoc .put ("val2" , value2Str );
526
- source .put ("mapOfDecimalValues " , mapOfValuesDoc );
609
+ source .put ("mapOfChoiceFormatValues " , mapOfValuesDoc );
527
610
528
- assertThat (valueStr ).isEqualTo (((CouchbaseList ) converted .getContent ().get ("listOfDecimalValues " )).get (0 ));
529
- assertThat (value2Str ).isEqualTo (((CouchbaseList ) converted .getContent ().get ("listOfDecimalValues " )).get (1 ));
611
+ assertThat (valueStr ).isEqualTo (((CouchbaseList ) converted .getContent ().get ("listOfChoiceFormatValues " )).get (0 ));
612
+ assertThat (value2Str ).isEqualTo (((CouchbaseList ) converted .getContent ().get ("listOfChoiceFormatValues " )).get (1 ));
530
613
assertThat (converted .export ().toString ()).isEqualTo (source .export ().toString ());
531
614
532
615
CustomFieldsEntity readConverted = converter .read (CustomFieldsEntity .class , source );
@@ -540,8 +623,8 @@ void writesAndReadsCustomFieldsConvertedClass() {
540
623
@ Test
541
624
void writesAndReadsClassContainingCustomConvertedObjects () {
542
625
List <Object > converters = new ArrayList <>();
543
- converters .add (BigDecimalToStringConverter .INSTANCE );
544
- converters .add (StringToBigDecimalConverter .INSTANCE );
626
+ converters .add (ChoiceFormatToStringConverter .INSTANCE );
627
+ converters .add (StringToChoiceFormatConverter .INSTANCE );
545
628
CustomConversions customConversions = new CouchbaseCustomConversions (converters );
546
629
converter .setCustomConversions (customConversions );
547
630
converter .afterPropertiesSet ();
@@ -635,22 +718,22 @@ void writesAndReadsNestedClass() {
635
718
}
636
719
637
720
@ WritingConverter
638
- public enum BigDecimalToStringConverter implements Converter <BigDecimal , String > {
721
+ public enum ChoiceFormatToStringConverter implements Converter <ChoiceFormat , String > {
639
722
INSTANCE ;
640
723
641
724
@ Override
642
- public String convert (BigDecimal source ) {
643
- return source . toPlainString ( );
725
+ public String convert (ChoiceFormat source ) {
726
+ return MappingCouchbaseConverterTests . toString ( source );
644
727
}
645
728
}
646
729
647
730
@ ReadingConverter
648
- public enum StringToBigDecimalConverter implements Converter <String , BigDecimal > {
731
+ public enum StringToChoiceFormatConverter implements Converter <String , ChoiceFormat > {
649
732
INSTANCE ;
650
733
651
734
@ Override
652
- public BigDecimal convert (String source ) {
653
- return new BigDecimal (source );
735
+ public ChoiceFormat convert (String source ) {
736
+ return MappingCouchbaseConverterTests . fromString (source );
654
737
}
655
738
}
656
739
@@ -676,6 +759,22 @@ public StringEntity(String attr0) {
676
759
}
677
760
}
678
761
762
+ static class BigIntegerEntity extends BaseEntity {
763
+ private BigInteger attr0 ;
764
+
765
+ public BigIntegerEntity (BigInteger attr0 ) {
766
+ this .attr0 = attr0 ;
767
+ }
768
+ }
769
+
770
+ static class BigDecimalEntity extends BaseEntity {
771
+ private BigDecimal attr0 ;
772
+
773
+ public BigDecimalEntity (BigDecimal attr0 ) {
774
+ this .attr0 = attr0 ;
775
+ }
776
+ }
777
+
679
778
static class NumberEntity extends BaseEntity {
680
779
private long attr0 ;
681
780
@@ -770,23 +869,24 @@ public Email(String emailAddr) {
770
869
}
771
870
772
871
static class CustomEntity extends BaseEntity {
773
- private BigDecimal value ;
774
- private List <BigDecimal > listOfValues ;
775
- private Map <String , BigDecimal > mapOfValues ;
872
+ private ChoiceFormat value ;
873
+ private List <ChoiceFormat > listOfValues ;
874
+ private Map <String , ChoiceFormat > mapOfValues ;
776
875
777
- public CustomEntity (BigDecimal value , List <BigDecimal > listOfValues , Map <String , BigDecimal > mapOfValues ) {
876
+ public CustomEntity (ChoiceFormat value , List <ChoiceFormat > listOfValues , Map <String , ChoiceFormat > mapOfValues ) {
778
877
this .value = value ;
779
878
this .listOfValues = listOfValues ;
780
879
this .mapOfValues = mapOfValues ;
781
880
}
782
881
}
783
882
784
883
static class CustomFieldsEntity extends BaseEntity {
785
- @ Field ("decimalValue " ) private BigDecimal value ;
786
- @ Field ("listOfDecimalValues " ) private List <BigDecimal > listOfValues ;
787
- @ Field ("mapOfDecimalValues " ) private Map <String , BigDecimal > mapOfValues ;
884
+ @ Field ("choiceFormatValue " ) private ChoiceFormat value ;
885
+ @ Field ("listOfChoiceFormatValues " ) private List <ChoiceFormat > listOfValues ;
886
+ @ Field ("mapOfChoiceFormatValues " ) private Map <String , ChoiceFormat > mapOfValues ;
788
887
789
- public CustomFieldsEntity (BigDecimal value , List <BigDecimal > listOfValues , Map <String , BigDecimal > mapOfValues ) {
888
+ public CustomFieldsEntity (ChoiceFormat value , List <ChoiceFormat > listOfValues ,
889
+ Map <String , ChoiceFormat > mapOfValues ) {
790
890
this .value = value ;
791
891
this .listOfValues = listOfValues ;
792
892
this .mapOfValues = mapOfValues ;
0 commit comments