From bad2718b1b85d806221fce52c76fa1d388993396 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Wed, 7 Dec 2016 18:53:37 +0700 Subject: singleton java 8 date formatters --- src/test/java/com/juick/json/MessageSerializer.java | 19 ++++--------------- src/test/java/com/juick/tests/MessageTests.java | 5 +++-- 2 files changed, 7 insertions(+), 17 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/com/juick/json/MessageSerializer.java b/src/test/java/com/juick/json/MessageSerializer.java index b49c991a..7ed060dd 100644 --- a/src/test/java/com/juick/json/MessageSerializer.java +++ b/src/test/java/com/juick/json/MessageSerializer.java @@ -20,6 +20,7 @@ package com.juick.json; import com.juick.Message; import com.juick.Photo; import com.juick.Tag; +import com.juick.util.DateFormattersHolder; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -27,26 +28,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.TimeZone; import java.util.stream.Collectors; /** - * * @author Ugnich Anton */ public class MessageSerializer extends JSONSerializer { - private final static Logger LOGGER = LoggerFactory.getLogger(MessageSerializer.class); - + UserSerializer userSerializer = new UserSerializer(); PlaceSerializer placeSerializer = new PlaceSerializer(); - private final SimpleDateFormat df; - public MessageSerializer() { - df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - df.setTimeZone(TimeZone.getTimeZone("UTC")); } @Override @@ -66,7 +59,7 @@ public class MessageSerializer extends JSONSerializer { jmsg.setText(json.getString("body").replace(""", "\"")); jmsg.setUser(userSerializer.deserialize(json.getJSONObject("user"))); - jmsg.setDate(df.parse(json.getString("timestamp"))); + jmsg.setDate(DateFormattersHolder.getMessageFormatterInstance().parse(json.getString("timestamp"))); if (json.has("tags")) { JSONArray tags = json.getJSONArray("tags"); @@ -115,7 +108,7 @@ public class MessageSerializer extends JSONSerializer { json.put("body", msg.getText()); } if (msg.getDate() != null) { - json.put("timestamp", df.format(msg.getDate())); + json.put("timestamp", DateFormattersHolder.getMessageFormatterInstance().format(msg.getDate())); } if (msg.getUser() != null) { json.put("user", userSerializer.serialize(msg.getUser())); @@ -145,8 +138,4 @@ public class MessageSerializer extends JSONSerializer { return json; } - - public SimpleDateFormat getDf() { - return df; - } } diff --git a/src/test/java/com/juick/tests/MessageTests.java b/src/test/java/com/juick/tests/MessageTests.java index 45575ded..a0cfd444 100644 --- a/src/test/java/com/juick/tests/MessageTests.java +++ b/src/test/java/com/juick/tests/MessageTests.java @@ -3,6 +3,7 @@ package com.juick.tests; import static org.junit.Assert.assertEquals; import com.juick.json.MessageSerializer; +import com.juick.util.DateFormattersHolder; import com.juick.xmpp.extensions.JuickMessage; import org.json.JSONObject; import org.junit.Test; @@ -33,7 +34,7 @@ public class MessageTests { msg.setDate(currentDate); MessageSerializer serializer = new MessageSerializer(); JSONObject jsonMessage = serializer.serialize(msg); - assertEquals("date should be in timestamp field", serializer.getDf().format(currentDate), + assertEquals("date should be in timestamp field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate), jsonMessage.getString("timestamp")); JuickMessage xmppMessage = new JuickMessage(msg); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); @@ -42,7 +43,7 @@ public class MessageTests { Document doc = db.parse(new ByteArrayInputStream(xmppMessage.toString().getBytes("UTF-8"))); Node juickNode = doc.getElementsByTagName("juick").item(0); NamedNodeMap attrs = juickNode.getAttributes(); - assertEquals("date should be in ts field", serializer.getDf().format(currentDate), + assertEquals("date should be in ts field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate), attrs.getNamedItem("ts").getNodeValue()); } } \ No newline at end of file -- cgit v1.2.3