2424import static org .hamcrest .Matchers .notNullValue ;
2525import static org .junit .Assert .assertThat ;
2626
27+ import java .text .DateFormat ;
28+ import java .text .SimpleDateFormat ;
2729import java .time .Instant ;
2830import java .time .LocalDate ;
2931import java .time .LocalDateTime ;
4042 */
4143public class VPackSerializeDeserializeTest {
4244
45+ private static final DateFormat DATE_FORMAT = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );// ISO 8601
46+
4347 protected static class TestEntityDate {
4448 private Instant instant ;
4549 private LocalDate localDate ;
@@ -90,12 +94,12 @@ public void serializeDate() {
9094 final VPackSlice vpack = builder .build ().serialize (new TestEntityDate (1474988621 ));
9195 assertThat (vpack , is (notNullValue ()));
9296 assertThat (vpack .isObject (), is (true ));
93- assertThat (vpack .get ("instant" ).isDate (), is (true ));
94- assertThat (vpack .get ("instant" ).getAsDate (), is (new Date (1474988621 )));
95- assertThat (vpack .get ("localDate" ).isDate (), is (true ));
96- assertThat (vpack .get ("localDate" ).getAsDate (), is (new Date (70 , 0 , 18 )));
97- assertThat (vpack .get ("localDateTime" ).isDate (), is (true ));
98- assertThat (vpack .get ("localDateTime" ).getAsDate (), is (new Date (1474988621 )));
97+ assertThat (vpack .get ("instant" ).isString (), is (true ));
98+ assertThat (vpack .get ("instant" ).getAsString (), is (DATE_FORMAT . format ( new Date (1474988621 ) )));
99+ assertThat (vpack .get ("localDate" ).isString (), is (true ));
100+ assertThat (vpack .get ("localDate" ).getAsString (), is (DATE_FORMAT . format ( new Date (70 , 0 , 18 ) )));
101+ assertThat (vpack .get ("localDateTime" ).isString (), is (true ));
102+ assertThat (vpack .get ("localDateTime" ).getAsString (), is (DATE_FORMAT . format ( new Date (1474988621 ) )));
99103 }
100104
101105 @ SuppressWarnings ("deprecation" )
@@ -118,6 +122,26 @@ public void deserializeDate() {
118122 is (LocalDateTime .ofInstant (Instant .ofEpochMilli (1475062216 ), ZoneId .systemDefault ())));
119123 }
120124
125+ @ SuppressWarnings ("deprecation" )
126+ @ Test
127+ public void deserializeDateFromString () {
128+ final VPackBuilder builder = new VPackBuilder ();
129+ builder .add (ValueType .OBJECT );
130+ builder .add ("instant" , DATE_FORMAT .format (new Date (1475062216 )));
131+ builder .add ("localDate" , DATE_FORMAT .format (new Date (70 , 0 , 18 )));
132+ builder .add ("localDateTime" , DATE_FORMAT .format (new Date (1475062216 )));
133+ builder .close ();
134+
135+ final VPack .Builder vpackBuilder = new VPack .Builder ();
136+ VPackConfigureAsync .configure (vpackBuilder );
137+ final TestEntityDate entity = vpackBuilder .build ().deserialize (builder .slice (), TestEntityDate .class );
138+ assertThat (entity , is (notNullValue ()));
139+ assertThat (entity .instant , is (Instant .ofEpochMilli (1475062216 )));
140+ assertThat (entity .localDate , is (Instant .ofEpochMilli (1475062216 ).atZone (ZoneId .systemDefault ()).toLocalDate ()));
141+ assertThat (entity .localDateTime ,
142+ is (LocalDateTime .ofInstant (Instant .ofEpochMilli (1475062216 ), ZoneId .systemDefault ())));
143+ }
144+
121145 @ Test
122146 public void date () {
123147 final VPack .Builder builder = new VPack .Builder ();
0 commit comments