diff options
author | Vitaly Takmazov | 2016-07-12 15:22:50 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-07-12 15:22:50 +0300 |
commit | 7ad313a4b0509d37fe5d8dfa215ab980c8c74ed0 (patch) | |
tree | cb477b0918a66ef59915ea85a38a4d162d36e51b | |
parent | 47ec7ca54927d586fe5c5dca3be377231cb59c11 (diff) |
fix date serializing with tests
3 files changed, 37 insertions, 3 deletions
diff --git a/juick-core/src/main/java/com/juick/json/MessageSerializer.java b/juick-core/src/main/java/com/juick/json/MessageSerializer.java index e8402d51..4238df60 100644 --- a/juick-core/src/main/java/com/juick/json/MessageSerializer.java +++ b/juick-core/src/main/java/com/juick/json/MessageSerializer.java @@ -39,7 +39,7 @@ public class MessageSerializer extends JSONSerializer<Message> { UserSerializer userSerializer = new UserSerializer(); PlaceSerializer placeSerializer = new PlaceSerializer(); - final SimpleDateFormat df; + private final SimpleDateFormat df; public MessageSerializer() { df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -135,4 +135,8 @@ public class MessageSerializer extends JSONSerializer<Message> { return json; } + + public SimpleDateFormat getDf() { + return df; + } } diff --git a/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java b/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java index 885b2375..510f7cc1 100644 --- a/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java +++ b/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java @@ -81,7 +81,7 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { if (sReadOnly != null) { jmsg.ReadOnly = true; } - String ts = parser.getAttributeValue(null, "timestamp"); + String ts = parser.getAttributeValue(null, "ts"); if (ts != null) { jmsg.setDate(df.parse(ts)); } diff --git a/src/test/java/com/juick/tests/MessageTests.java b/src/test/java/com/juick/tests/MessageTests.java index 0392c7b3..b35e71c6 100644 --- a/src/test/java/com/juick/tests/MessageTests.java +++ b/src/test/java/com/juick/tests/MessageTests.java @@ -2,17 +2,47 @@ package com.juick.tests; import static org.junit.Assert.assertEquals;
+import com.juick.json.MessageSerializer;
+import com.juick.xmpp.extensions.JuickMessage;
+import org.json.JSONObject;
import org.junit.Test;
import com.juick.Message;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.*;
+import java.text.ParseException;
+import java.util.Date;
public class MessageTests {
@Test
- public void messageTagsParser() {
+ public void messageParserSerializer() throws ParseException, ParserConfigurationException,
+ IOException, SAXException {
Message msg = new Message();
msg.parseTags("test test" + (char)0xA0 + "2 test3");
assertEquals("First tag must be", "test", msg.Tags.get(0));
assertEquals("Third tag must be", "test3", msg.Tags.get(2));
assertEquals("Count of tags must be", 3, msg.Tags.size());
+ Date currentDate = new Date();
+ msg.setDate(currentDate);
+ MessageSerializer serializer = new MessageSerializer();
+ JSONObject jsonMessage = serializer.serialize(msg);
+ assertEquals("date should be in timestamp field", serializer.getDf().format(currentDate),
+ jsonMessage.getString("timestamp"));
+ JuickMessage xmppMessage = new JuickMessage(msg);
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ 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),
+ attrs.getNamedItem("ts").getNodeValue());
}
}
\ No newline at end of file |