From 9a50ad8dea5e9ebc5eb1c5efb19aa7a784283e14 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 16 Jan 2016 18:25:11 +0300 Subject: date and tests --- .../java/com/juick/json/MessageSerializer.java | 16 ++++++++---- src/test/java/com/juick/JsonTests.java | 30 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/juick/JsonTests.java (limited to 'src') diff --git a/src/main/java/com/juick/json/MessageSerializer.java b/src/main/java/com/juick/json/MessageSerializer.java index dccf1b3a..da87d4be 100644 --- a/src/main/java/com/juick/json/MessageSerializer.java +++ b/src/main/java/com/juick/json/MessageSerializer.java @@ -22,6 +22,9 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -31,10 +34,14 @@ import org.json.JSONObject; * @author Ugnich Anton */ public class MessageSerializer extends JSONSerializer { + + private final static Logger LOGGER = Logger.getLogger(MessageSerializer.class.getName()); UserSerializer userSerializer = new UserSerializer(); PlaceSerializer placeSerializer = new PlaceSerializer(); + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + @Override public Message deserialize(JSONObject json) throws JSONException { com.juick.Message jmsg = new com.juick.Message(); @@ -55,7 +62,7 @@ public class MessageSerializer extends JSONSerializer { try { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); df.setTimeZone(TimeZone.getTimeZone("UTC")); - jmsg.Timestamp = df.parse(json.getString("timestamp")); + jmsg.setDate(df.parse(json.getString("timestamp"))); } catch (ParseException e) { } @@ -100,10 +107,8 @@ public class MessageSerializer extends JSONSerializer { if (msg.getText() != null) { json.put("body", msg.getText()); } - if (msg.Timestamp != null) { - json.put("timestamp", msg.Timestamp); - } else if (msg.TimestampString != null) { - json.put("timestamp", msg.TimestampString); + if (msg.getDate() != null) { + json.put("timestamp", df.format(msg.getDate())); } if (msg.getUser() != null) { json.put("user", userSerializer.serialize(msg.getUser())); @@ -126,6 +131,7 @@ public class MessageSerializer extends JSONSerializer { json.put("photo", photo); } } catch (JSONException e) { + LOGGER.log(Level.SEVERE, "JSON Exception", e); } return json; diff --git a/src/test/java/com/juick/JsonTests.java b/src/test/java/com/juick/JsonTests.java new file mode 100644 index 00000000..5d78a2d6 --- /dev/null +++ b/src/test/java/com/juick/JsonTests.java @@ -0,0 +1,30 @@ +package com.juick; + +import static org.junit.Assert.assertEquals; + +import com.juick.json.MessageSerializer; +import org.junit.Assert; +import org.junit.Test; + +import com.juick.Message; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; + +public class JsonTests { + @Test + public void DateTest() { + Message msg = new Message(); + SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + try { + msg.setDate(df.parse("2015-01-01 00:00:00")); + MessageSerializer serializer = new MessageSerializer(); + String json = serializer.serialize(msg).toString(); + assertEquals("{\"timestamp\":\"2015-01-01 00:00:00\"}", json); + } catch (ParseException e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file -- cgit v1.2.3