@@ -32,8 +32,6 @@ public void testSerializationAsTimestampNanoseconds01() throws Exception
32
32
.with (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
33
33
.with (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
34
34
.writeValueAsString (duration );
35
-
36
- assertNotNull ("The value should not be null." , value );
37
35
assertEquals ("The value is not correct." , "60" +NO_NANOSECS_SUFFIX , value );
38
36
}
39
37
@@ -45,34 +43,47 @@ public void testSerializationAsTimestampNanoseconds02() throws Exception
45
43
.with (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
46
44
.with (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
47
45
.writeValueAsString (duration );
48
-
49
- assertNotNull ("The value should not be null." , value );
50
46
assertEquals ("The value is not correct." , "13498.000008374" , value );
51
47
}
52
48
49
+ // [modules-java8#165]
50
+ @ Test
51
+ public void testSerializationAsTimestampNanoseconds03 () throws Exception
52
+ {
53
+ ObjectWriter w = WRITER
54
+ .with (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
55
+ .with (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS );
56
+
57
+ // 20-Oct-2020, tatu: Very weird, but "use nanoseconds" actually results
58
+ // in unit being seconds, with fractions (with nanosec precision)
59
+ String value = w .writeValueAsString (Duration .ofMillis (1L ));
60
+ assertEquals ("The value is not correct." , "0.001000000" , value );
61
+
62
+ value = w .writeValueAsString (Duration .ofMillis (-1L ));
63
+ assertEquals ("The value is not correct." , "-0.001000000" , value );
64
+ }
65
+
53
66
@ Test
54
67
public void testSerializationAsTimestampMilliseconds01 () throws Exception
55
68
{
56
- Duration duration = Duration .ofSeconds (60L , 0 );
57
- String value = WRITER
69
+ final ObjectWriter w = WRITER
58
70
.with (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
59
- .without (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
60
- .writeValueAsString (duration );
71
+ .without (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS );
72
+ String value = w .writeValueAsString (Duration .ofSeconds (45L , 0 ));
73
+ assertEquals ("The value is not correct." , "45000" , value );
61
74
62
- assertNotNull ("The value should not be null." , value );
63
- assertEquals ("The value is not correct." , "60000" , value );
75
+ // and with negative value too
76
+ value = w .writeValueAsString (Duration .ofSeconds (-32L , 0 ));
77
+ assertEquals ("The value is not correct." , "-32000" , value );
64
78
}
65
79
66
80
@ Test
67
81
public void testSerializationAsTimestampMilliseconds02 () throws Exception
68
82
{
69
- Duration duration = Duration .ofSeconds (13498L , 8374 );
70
83
String value = WRITER
71
84
.with (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
72
85
.without (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
73
- .writeValueAsString (duration );
74
-
75
- assertNotNull ("The value should not be null." , value );
86
+ .writeValueAsString (Duration .ofSeconds (13498L , 8374 ));
76
87
assertEquals ("The value is not correct." , "13498000" , value );
77
88
}
78
89
@@ -84,8 +95,6 @@ public void testSerializationAsTimestampMilliseconds03() throws Exception
84
95
.with (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
85
96
.without (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
86
97
.writeValueAsString (duration );
87
-
88
- assertNotNull ("The value should not be null." , value );
89
98
assertEquals ("The value is not correct." , "13498837" , value );
90
99
}
91
100
@@ -96,8 +105,6 @@ public void testSerializationAsString01() throws Exception
96
105
String value = WRITER
97
106
.without (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
98
107
.writeValueAsString (duration );
99
-
100
- assertNotNull ("The value should not be null." , value );
101
108
assertEquals ("The value is not correct." , '"' + duration .toString () + '"' , value );
102
109
}
103
110
@@ -108,51 +115,49 @@ public void testSerializationAsString02() throws Exception
108
115
String value = WRITER
109
116
.without (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
110
117
.writeValueAsString (duration );
111
-
112
- assertNotNull ("The value should not be null." , value );
113
118
assertEquals ("The value is not correct." , '"' + duration .toString () + '"' , value );
114
119
}
115
120
116
121
@ Test
117
122
public void testSerializationWithTypeInfo01 () throws Exception
118
123
{
119
- ObjectMapper mapper = newMapper (); // need new to add mix-ins:
120
- mapper .configure (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS , true );
121
- mapper .configure (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS , true );
122
- mapper .addMixIn (TemporalAmount .class , MockObjectConfiguration .class );
124
+ ObjectMapper mapper = mapperBuilder ()
125
+ .addMixIn (TemporalAmount .class , MockObjectConfiguration .class )
126
+ .enable (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
127
+ .enable (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
128
+ .build ();
123
129
Duration duration = Duration .ofSeconds (13498L , 8374 );
124
130
String value = mapper .writeValueAsString (duration );
125
131
126
- assertNotNull ("The value should not be null." , value );
127
132
assertEquals ("The value is not correct." ,
128
133
"[\" " + Duration .class .getName () + "\" ,13498.000008374]" , value );
129
134
}
130
135
131
136
@ Test
132
137
public void testSerializationWithTypeInfo02 () throws Exception
133
138
{
134
- ObjectMapper mapper = newMapper (); // need new to add mix-ins:
135
- mapper .configure (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS , true );
136
- mapper .configure (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS , false );
137
- mapper .addMixIn (TemporalAmount .class , MockObjectConfiguration .class );
139
+ ObjectMapper mapper = mapperBuilder ()
140
+ .addMixIn (TemporalAmount .class , MockObjectConfiguration .class )
141
+ .enable (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
142
+ .disable (SerializationFeature .WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
143
+ .build ();
138
144
Duration duration = Duration .ofSeconds (13498L , 837481723 );
139
145
String value = mapper .writeValueAsString (duration );
140
146
141
- assertNotNull ("The value should not be null." , value );
142
147
assertEquals ("The value is not correct." ,
143
148
"[\" " + Duration .class .getName () + "\" ,13498837]" , value );
144
149
}
145
150
146
151
@ Test
147
152
public void testSerializationWithTypeInfo03 () throws Exception
148
153
{
149
- ObjectMapper mapper = newMapper (); // need new to add mix-ins:
150
- mapper .configure (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS , false );
151
- mapper .addMixIn (TemporalAmount .class , MockObjectConfiguration .class );
154
+ ObjectMapper mapper = mapperBuilder ()
155
+ .addMixIn (TemporalAmount .class , MockObjectConfiguration .class )
156
+ .disable (SerializationFeature .WRITE_DURATIONS_AS_TIMESTAMPS )
157
+ .build ();
152
158
Duration duration = Duration .ofSeconds (13498L , 8374 );
153
159
String value = mapper .writeValueAsString (duration );
154
160
155
- assertNotNull ("The value should not be null." , value );
156
161
assertEquals ("The value is not correct." ,
157
162
"[\" " + Duration .class .getName () + "\" ,\" " + duration .toString () + "\" ]" , value );
158
163
}
0 commit comments