40
40
import org .mockito .Mock ;
41
41
import org .mockito .Mockito ;
42
42
import org .mockito .junit .MockitoJUnitRunner ;
43
-
44
43
import org .springframework .aop .framework .ProxyFactory ;
45
44
import org .springframework .beans .ConversionNotSupportedException ;
46
45
import org .springframework .beans .factory .annotation .Value ;
@@ -1943,6 +1942,16 @@ public void mapsValueToExplicitTargetType() {
1943
1942
assertThat (target .get ("script" )).isEqualTo (new Code (source .script ));
1944
1943
}
1945
1944
1945
+ @ Test // DATAMONGO-2328
1946
+ public void readsScriptAsStringWhenAnnotatedWithFieldTargetType () {
1947
+
1948
+ String reference = "if (a > b) a else b" ;
1949
+ WithExplicitTargetTypes target = converter .read (WithExplicitTargetTypes .class ,
1950
+ new org .bson .Document ("script" , new Code (reference )));
1951
+
1952
+ assertThat (target .script ).isEqualTo (reference );
1953
+ }
1954
+
1946
1955
@ Test // DATAMONGO-1849
1947
1956
public void mapsCollectionValueToExplicitTargetType () {
1948
1957
@@ -1968,6 +1977,96 @@ public void mapsBigDecimalToDecimal128WhenAnnotatedWithFieldTargetType() {
1968
1977
assertThat (target .get ("bigDecimal" )).isEqualTo (new Decimal128 (source .bigDecimal ));
1969
1978
}
1970
1979
1980
+ @ Test // DATAMONGO-2328
1981
+ public void mapsDateToLongWhenAnnotatedWithFieldTargetType () {
1982
+
1983
+ WithExplicitTargetTypes source = new WithExplicitTargetTypes ();
1984
+ source .dateAsLong = new Date ();
1985
+
1986
+ org .bson .Document target = new org .bson .Document ();
1987
+ converter .write (source , target );
1988
+
1989
+ assertThat (target .get ("dateAsLong" )).isEqualTo (source .dateAsLong .getTime ());
1990
+ }
1991
+
1992
+ @ Test // DATAMONGO-2328
1993
+ public void readsLongAsDateWhenAnnotatedWithFieldTargetType () {
1994
+
1995
+ Date reference = new Date ();
1996
+ WithExplicitTargetTypes target = converter .read (WithExplicitTargetTypes .class ,
1997
+ new org .bson .Document ("dateAsLong" , reference .getTime ()));
1998
+
1999
+ assertThat (target .dateAsLong ).isEqualTo (reference );
2000
+ }
2001
+
2002
+ @ Test // DATAMONGO-2328
2003
+ public void mapsLongToDateWhenAnnotatedWithFieldTargetType () {
2004
+
2005
+ Date date = new Date ();
2006
+ WithExplicitTargetTypes source = new WithExplicitTargetTypes ();
2007
+ source .longAsDate = date .getTime ();
2008
+
2009
+ org .bson .Document target = new org .bson .Document ();
2010
+ converter .write (source , target );
2011
+
2012
+ assertThat (target .get ("longAsDate" )).isEqualTo (date );
2013
+ }
2014
+
2015
+ @ Test // DATAMONGO-2328
2016
+ public void readsDateAsLongWhenAnnotatedWithFieldTargetType () {
2017
+
2018
+ Date reference = new Date ();
2019
+ WithExplicitTargetTypes target = converter .read (WithExplicitTargetTypes .class ,
2020
+ new org .bson .Document ("longAsDate" , reference ));
2021
+
2022
+ assertThat (target .longAsDate ).isEqualTo (reference .getTime ());
2023
+ }
2024
+
2025
+ @ Test // DATAMONGO-2328
2026
+ public void mapsStringAsBooleanWhenAnnotatedWithFieldTargetType () {
2027
+
2028
+ WithExplicitTargetTypes source = new WithExplicitTargetTypes ();
2029
+ source .stringAsBoolean = "true" ;
2030
+
2031
+ org .bson .Document target = new org .bson .Document ();
2032
+ converter .write (source , target );
2033
+
2034
+ assertThat (target .get ("stringAsBoolean" )).isEqualTo (true );
2035
+ }
2036
+
2037
+ @ Test // DATAMONGO-2328
2038
+ public void readsBooleanAsStringWhenAnnotatedWithFieldTargetType () {
2039
+
2040
+ WithExplicitTargetTypes target = converter .read (WithExplicitTargetTypes .class ,
2041
+ new org .bson .Document ("stringAsBoolean" , true ));
2042
+
2043
+ assertThat (target .stringAsBoolean ).isEqualTo ("true" );
2044
+ }
2045
+
2046
+ @ Test // DATAMONGO-2328
2047
+ public void mapsDateAsObjectIdWhenAnnotatedWithFieldTargetType () {
2048
+
2049
+ WithExplicitTargetTypes source = new WithExplicitTargetTypes ();
2050
+ source .dateAsObjectId = new Date ();
2051
+
2052
+ org .bson .Document target = new org .bson .Document ();
2053
+ converter .write (source , target );
2054
+
2055
+ // need to compare the the timestamp as ObjectId has an internal counter
2056
+ assertThat (target .get ("dateAsObjectId" , ObjectId .class ).getTimestamp ())
2057
+ .isEqualTo (new ObjectId (source .dateAsObjectId ).getTimestamp ());
2058
+ }
2059
+
2060
+ @ Test // DATAMONGO-2328
2061
+ public void readsObjectIdAsDateWhenAnnotatedWithFieldTargetType () {
2062
+
2063
+ ObjectId reference = new ObjectId ();
2064
+ WithExplicitTargetTypes target = converter .read (WithExplicitTargetTypes .class ,
2065
+ new org .bson .Document ("dateAsObjectId" , reference ));
2066
+
2067
+ assertThat (target .dateAsObjectId ).isEqualTo (new Date (reference .getTimestamp ()));
2068
+ }
2069
+
1971
2070
static class GenericType <T > {
1972
2071
T content ;
1973
2072
}
@@ -1987,9 +2086,7 @@ void method() {}
1987
2086
},
1988
2087
SECOND {
1989
2088
@ Override
1990
- void method () {
1991
-
1992
- }
2089
+ void method () {}
1993
2090
};
1994
2091
1995
2092
abstract void method ();
@@ -2416,7 +2513,20 @@ static class WithExplicitTargetTypes {
2416
2513
@ Field (targetType = FieldType .SCRIPT ) //
2417
2514
List <String > scripts ;
2418
2515
2419
- @ Field (targetType = FieldType .DECIMAL128 ) BigDecimal bigDecimal ;
2516
+ @ Field (targetType = FieldType .DECIMAL128 ) //
2517
+ BigDecimal bigDecimal ;
2518
+
2519
+ @ Field (targetType = FieldType .INT64 ) //
2520
+ Date dateAsLong ;
2521
+
2522
+ @ Field (targetType = FieldType .DATE_TIME ) //
2523
+ Long longAsDate ;
2524
+
2525
+ @ Field (targetType = FieldType .BOOLEAN ) //
2526
+ String stringAsBoolean ;
2527
+
2528
+ @ Field (targetType = FieldType .OBJECT_ID ) //
2529
+ Date dateAsObjectId ;
2420
2530
}
2421
2531
2422
2532
}
0 commit comments