diff --git a/pom.xml b/pom.xml index 0eaccc4..ea4a430 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.ljunggren + io.ljunggren xml-utils 0.0.1-SNAPSHOT jar diff --git a/src/main/java/com/ljunggren/xmlUtils/XmlUtils.java b/src/main/java/com/ljunggren/xmlUtils/XmlUtils.java deleted file mode 100644 index 3fc04b7..0000000 --- a/src/main/java/com/ljunggren/xmlUtils/XmlUtils.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.ljunggren.xmlUtils; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.dataformat.xml.XmlMapper; - -public class XmlUtils { - - public static String objectToXml(Object object) throws JsonProcessingException { - return objectToXml(object, new XmlMapper()); - } - - public static String prettyPrint(Object object) throws JsonProcessingException { - XmlMapper mapper = new XmlMapper(); - mapper.enable(SerializationFeature.INDENT_OUTPUT); - return objectToXml(object, mapper); - } - - private static String objectToXml(Object object, XmlMapper mapper) throws JsonProcessingException { - return mapper.writeValueAsString(object); - } - - public static T xmlToObject(String xml, Class clazz) throws JsonMappingException, JsonProcessingException { - return new XmlMapper().readValue(xml, clazz); - } - - public static boolean isValid(String xml) { - try { - new XmlMapper().readTree(xml); - return true; - } catch (Exception e) { - return false; - } - } - - public static boolean areEqual(String xml1, String xml2) throws JsonMappingException, JsonProcessingException { - XmlMapper mapper = new XmlMapper(); - return mapper.readTree(xml1).equals(mapper.readTree(xml2)); - } - -} diff --git a/src/main/java/io/ljunggren/xmlUtils/XmlUtils.java b/src/main/java/io/ljunggren/xmlUtils/XmlUtils.java new file mode 100644 index 0000000..8eec497 --- /dev/null +++ b/src/main/java/io/ljunggren/xmlUtils/XmlUtils.java @@ -0,0 +1,42 @@ +package io.ljunggren.xmlUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +public class XmlUtils { + + public static String objectToXml(Object object) throws JsonProcessingException { + return objectToXml(object, new XmlMapper()); + } + + public static String prettyPrint(Object object) throws JsonProcessingException { + XmlMapper mapper = new XmlMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + return objectToXml(object, mapper); + } + + private static String objectToXml(Object object, XmlMapper mapper) throws JsonProcessingException { + return mapper.writeValueAsString(object); + } + + public static T xmlToObject(String xml, Class clazz) throws JsonMappingException, JsonProcessingException { + return new XmlMapper().readValue(xml, clazz); + } + + public static boolean isValid(String xml) { + try { + new XmlMapper().readTree(xml); + return true; + } catch (Exception e) { + return false; + } + } + + public static boolean areEqual(String xml1, String xml2) throws JsonMappingException, JsonProcessingException { + XmlMapper mapper = new XmlMapper(); + return mapper.readTree(xml1).equals(mapper.readTree(xml2)); + } + +} diff --git a/src/test/java/com/ljunggren/xmlUtils/XmlUtilsTest.java b/src/test/java/com/ljunggren/xmlUtils/XmlUtilsTest.java deleted file mode 100644 index b67e013..0000000 --- a/src/test/java/com/ljunggren/xmlUtils/XmlUtilsTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.ljunggren.xmlUtils; - -import static org.junit.Assert.*; - -import org.junit.Test; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -public class XmlUtilsTest { - - @Data - @NoArgsConstructor - @AllArgsConstructor - private static class User { - private String name; - private int age; - @JacksonXmlProperty(isAttribute = true) - private boolean active; - } - - @Test - public void objectToXmlTest() throws JsonProcessingException { - User user = new User("Alex", 40, true); - String expected = "Alex40"; - String actual = XmlUtils.objectToXml(user); - assertEquals(expected, actual); - } - - @Test - public void prettyPrintTest() throws JsonProcessingException { - User user = new User("Alex", 40, true); - String expected = "\r\n" - + " Alex\r\n" - + " 40\r\n" - + "\r\n"; - String actual = XmlUtils.prettyPrint(user); - assertEquals(expected, actual); - } - - @Test - public void xmlToObjectTest() throws JsonMappingException, JsonProcessingException { - User user = new User("Alex", 40, true); - String xml = XmlUtils.objectToXml(user); - User generatedUser = XmlUtils.xmlToObject(xml, User.class); - assertEquals(user, generatedUser); - } - - @Test - public void isValidTest() throws JsonProcessingException { - User user = new User("Alex", 40, true); - String xml = XmlUtils.objectToXml(user); - assertTrue(XmlUtils.isValid(xml)); - } - - @Test - public void isValidNullTest() { - assertFalse(XmlUtils.isValid(null)); - } - - @Test - public void isValidFalseTest() { - assertFalse(XmlUtils.isValid("Alex40"; - String actual = XmlUtils.objectToXml(user); - assertTrue(XmlUtils.areEqual(expected, actual)); - } - - @Test - public void areEqualFalseTest() throws JsonMappingException, JsonProcessingException { - User user = new User("Alex", 40, true); - String expected = "Alexander40"; - String actual = XmlUtils.objectToXml(user); - assertFalse(XmlUtils.areEqual(expected, actual)); - } - -} diff --git a/src/test/java/io/ljunggren/xmlUtils/XmlUtilsTest.java b/src/test/java/io/ljunggren/xmlUtils/XmlUtilsTest.java new file mode 100644 index 0000000..fcc1a48 --- /dev/null +++ b/src/test/java/io/ljunggren/xmlUtils/XmlUtilsTest.java @@ -0,0 +1,89 @@ +package io.ljunggren.xmlUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +public class XmlUtilsTest { + + @Data + @NoArgsConstructor + @AllArgsConstructor + private static class User { + private String name; + private int age; + @JacksonXmlProperty(isAttribute = true) + private boolean active; + } + + @Test + public void objectToXmlTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "Alex40"; + String actual = XmlUtils.objectToXml(user); + assertEquals(expected, actual); + } + + @Test + public void prettyPrintTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "\r\n" + + " Alex\r\n" + + " 40\r\n" + + "\r\n"; + String actual = XmlUtils.prettyPrint(user); + assertEquals(expected, actual); + } + + @Test + public void xmlToObjectTest() throws JsonMappingException, JsonProcessingException { + User user = new User("Alex", 40, true); + String xml = XmlUtils.objectToXml(user); + User generatedUser = XmlUtils.xmlToObject(xml, User.class); + assertEquals(user, generatedUser); + } + + @Test + public void isValidTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String xml = XmlUtils.objectToXml(user); + assertTrue(XmlUtils.isValid(xml)); + } + + @Test + public void isValidNullTest() { + assertFalse(XmlUtils.isValid(null)); + } + + @Test + public void isValidFalseTest() { + assertFalse(XmlUtils.isValid("Alex40"; + String actual = XmlUtils.objectToXml(user); + assertTrue(XmlUtils.areEqual(expected, actual)); + } + + @Test + public void areEqualFalseTest() throws JsonMappingException, JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "Alexander40"; + String actual = XmlUtils.objectToXml(user); + assertFalse(XmlUtils.areEqual(expected, actual)); + } + +}