aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/tests/SerializationTests.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com/juick/tests/SerializationTests.java')
-rw-r--r--src/test/java/com/juick/tests/SerializationTests.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/test/java/com/juick/tests/SerializationTests.java b/src/test/java/com/juick/tests/SerializationTests.java
new file mode 100644
index 00000000..dd2812c7
--- /dev/null
+++ b/src/test/java/com/juick/tests/SerializationTests.java
@@ -0,0 +1,96 @@
+package com.juick.tests;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.juick.Message;
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.json.MessageSerializer;
+import com.juick.json.UserSerializer;
+import com.juick.xmpp.extensions.JuickMessage;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import javax.xml.bind.*;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import static org.junit.Assert.assertEquals;
+
+public class SerializationTests {
+ @Test
+ public void DateTest() {
+ Message msg = new Message();
+ SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ df.setTimeZone(TimeZone.getTimeZone("UTC"));
+ try {
+ msg.setDate(df.parse("2015-01-01 05:02:10"));
+ MessageSerializer serializer = new MessageSerializer();
+ String json = serializer.serialize(msg).toString();
+ assertEquals("{\"timestamp\":\"2015-01-01 05:02:10\"}", json);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ }
+ @Test
+ public void serializersTest() throws IOException, JAXBException, ParserConfigurationException, SAXException {
+ User user = new User();
+ user.setName("ugnich");
+ user.setUid(1);
+ user.setFullName("Anton Ugnich");
+ UserSerializer userSerializer = new UserSerializer();
+ JSONObject handmadeJson = userSerializer.serialize(user);
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
+ User jacksonUser = mapper.readValue(handmadeJson.toString(), User.class);
+ assertEquals("jackson should deserialize ugnich json", user, jacksonUser);
+ String jacksonString = mapper.writeValueAsString(user);
+ JSONObject jacksonJson = new JSONObject(jacksonString);
+ assertEquals("jackson should serialize as ugnich", handmadeJson.length(), jacksonJson.length());
+ Message msg = new Message();
+ msg.setMid(1);
+ msg.setUser(user);
+ msg.setText("yo");
+ msg.setDate(new Date());
+ msg.getTags().add(new Tag("test"));
+ msg.getTags().add(new Tag("json"));
+ MessageSerializer messageSerializer = new MessageSerializer();
+ JSONObject handmadeJsonMessage = messageSerializer.serialize(msg);
+ Message jacksonMessage = mapper.readValue(handmadeJsonMessage.toString(), Message.class);
+ assertEquals("jackson should deserialize ugnich json", msg, jacksonMessage);
+ String jacksonStringMessage = mapper.writeValueAsString(jacksonMessage);
+ JSONObject jacksonJsonMessage = new JSONObject(jacksonStringMessage);
+ assertEquals("jackson should serialize as ugnich", handmadeJsonMessage.length(), jacksonJsonMessage.length());
+
+ JuickMessage jmsg = new JuickMessage(msg);
+ String handmadeXml = jmsg.toString();
+
+ JAXBContext messageContext = JAXBContext.newInstance(Message.class);
+ Unmarshaller unmarshaller = messageContext.createUnmarshaller();
+ Message jaxbMessage = (Message) unmarshaller.unmarshal(new StringReader(handmadeXml));
+ assertEquals("jaxb should unmarshal ugnich xml", msg, jaxbMessage);
+ StringWriter sw = new StringWriter();
+ Marshaller messageMarshaller = messageContext.createMarshaller();
+ messageMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
+ messageMarshaller.marshal(msg, sw);
+ String xmlString = sw.toString();
+ DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
+ Document doc = dBuilder.parse(new InputSource(new StringReader(xmlString)));
+ assertEquals("jaxb should marshal as ugnich", 4, doc.getDocumentElement().getChildNodes().getLength());
+ assertEquals("jaxb should marshal as ugnich", 7, doc.getDocumentElement().getAttributes().getLength());
+ }
+} \ No newline at end of file