aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/tests
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-05-23 14:06:51 +0300
committerGravatar Vitaly Takmazov2017-05-23 15:50:31 +0300
commit4c6f3ba0d539f63a83c18e20ed4219bdef849b81 (patch)
tree184ad8afcaa21ec4eeb7b06bb4f751548e710028 /src/test/java/com/juick/tests
parent237739950a305b13c9f29e3ca25c2e835d563a3f (diff)
drop ugnich json
Diffstat (limited to 'src/test/java/com/juick/tests')
-rw-r--r--src/test/java/com/juick/tests/MessageTests.java39
-rw-r--r--src/test/java/com/juick/tests/SerializationTests.java97
2 files changed, 25 insertions, 111 deletions
diff --git a/src/test/java/com/juick/tests/MessageTests.java b/src/test/java/com/juick/tests/MessageTests.java
index 6bc291e2..89dd440e 100644
--- a/src/test/java/com/juick/tests/MessageTests.java
+++ b/src/test/java/com/juick/tests/MessageTests.java
@@ -1,31 +1,34 @@
package com.juick.tests;
-import static org.junit.Assert.assertEquals;
-
-import com.juick.json.MessageSerializer;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.juick.Message;
import com.juick.util.DateFormattersHolder;
-import com.juick.xmpp.extensions.JuickMessage;
import org.apache.commons.lang3.CharEncoding;
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.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import java.io.*;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
import java.text.ParseException;
import java.util.Date;
+import static org.junit.Assert.assertEquals;
+
public class MessageTests {
@Test
public void messageParserSerializer() throws ParseException, ParserConfigurationException,
- IOException, SAXException {
+ IOException, SAXException, JAXBException {
Message msg = new Message();
msg.parseTags("test test" + (char) 0xA0 + "2 test3");
assertEquals("First tag must be", "test", msg.getTags().get(0).getName());
@@ -33,15 +36,23 @@ public class MessageTests {
assertEquals("Count of tags must be", 3, msg.getTags().size());
Date currentDate = new Date();
msg.setDate(currentDate);
- MessageSerializer serializer = new MessageSerializer();
- JSONObject jsonMessage = serializer.serialize(msg);
+ ObjectMapper serializer = new ObjectMapper();
+ String jsonMessage = serializer.writeValueAsString(msg);
+ JSONObject jsonObject = new JSONObject(jsonMessage);
assertEquals("date should be in timestamp field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate),
- jsonMessage.getString("timestamp"));
- JuickMessage xmppMessage = new JuickMessage(msg);
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ jsonObject.getString("timestamp"));
+
+ JAXBContext context = JAXBContext
+ .newInstance(Message.class);
+ Marshaller m = context.createMarshaller();
+
+ StringWriter sw = new StringWriter();
+ m.marshal(msg, sw);
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse(new ByteArrayInputStream(xmppMessage.toString().getBytes(CharEncoding.UTF_8)));
+ Document doc = db.parse(new ByteArrayInputStream(sw.toString().getBytes(CharEncoding.UTF_8)));
Node juickNode = doc.getElementsByTagName("juick").item(0);
NamedNodeMap attrs = juickNode.getAttributes();
assertEquals("date should be in ts field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate),
diff --git a/src/test/java/com/juick/tests/SerializationTests.java b/src/test/java/com/juick/tests/SerializationTests.java
deleted file mode 100644
index 022a1bc7..00000000
--- a/src/test/java/com/juick/tests/SerializationTests.java
+++ /dev/null
@@ -1,97 +0,0 @@
-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.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.JAXBContext;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.bind.Unmarshaller;
-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");
- ObjectMapper mapper = new ObjectMapper();
- mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
- mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
- mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
- 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"));
- msg.setAttachmentType("png");
- 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);
- assertEquals("jackson message should have an attachment url",
- "http://i.juick.com/photos-1024/1.png", jacksonMessage.getAttachmentURL());
- assertEquals("jackson message should have image previews",
- "http://i.juick.com/photos-1024/1.png", jacksonMessage.getPhoto().getMedium());
- 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", 8, doc.getDocumentElement().getAttributes().getLength());
- assertEquals("xmpp message should have an attach attribute", "png", doc.getDocumentElement().getAttribute("attach"));
- }
-} \ No newline at end of file