From 307b25108455b0b6950c2f9ece0c9e3e23d9bc83 Mon Sep 17 00:00:00 2001 From: Marco Tedone Date: Sun, 21 Jun 2015 20:46:47 +0100 Subject: [PATCH] Serenified XML Data type tests --- .../podam/test/unit/XMLDatatypeUnitTest.java | 86 ------------------- .../xmlTypes/XMLDatatypeUnitTest.java | 61 +++++++++++++ .../xmlTypes/XmlTypesExternalFactory.java | 47 ++++++++++ .../unit/features/xmlTypes/package-info.java | 5 ++ .../test/unit/steps/PodamFactorySteps.java | 9 ++ .../podam/test/utils/PodamTestConstants.java | 10 --- .../podam/test/utils/PodamTestUtils.java | 64 -------------- 7 files changed, 122 insertions(+), 160 deletions(-) delete mode 100644 src/test/java/uk/co/jemos/podam/test/unit/XMLDatatypeUnitTest.java create mode 100644 src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/XMLDatatypeUnitTest.java create mode 100644 src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/XmlTypesExternalFactory.java create mode 100644 src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/package-info.java diff --git a/src/test/java/uk/co/jemos/podam/test/unit/XMLDatatypeUnitTest.java b/src/test/java/uk/co/jemos/podam/test/unit/XMLDatatypeUnitTest.java deleted file mode 100644 index 30fd568d1..000000000 --- a/src/test/java/uk/co/jemos/podam/test/unit/XMLDatatypeUnitTest.java +++ /dev/null @@ -1,86 +0,0 @@ -package uk.co.jemos.podam.test.unit; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.junit.Assert; -import org.junit.Test; -import uk.co.jemos.podam.api.AbstractExternalFactory; -import uk.co.jemos.podam.api.PodamFactory; -import uk.co.jemos.podam.api.PodamFactoryImpl; -import uk.co.jemos.podam.test.dto.XMLDatatypePojo; - -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.Duration; -import javax.xml.datatype.XMLGregorianCalendar; -import java.lang.reflect.Type; -import java.util.GregorianCalendar; - -/** - * Test @uk.co.jemos.podam.test.dto.JAXBElementPojo@ construction - * - * @author daivanov - * - */ -public class XMLDatatypeUnitTest { - - private static final Logger LOG = LogManager.getLogger(XMLDatatypeUnitTest.class); - - private final static PodamFactory externalFactory = - new AbstractExternalFactory() { - - @Override - public T manufacturePojo(Class pojoClass, - Type... genericTypeArgs) { - try { - if (pojoClass.isAssignableFrom(XMLGregorianCalendar.class)) { - DatatypeFactory factory = DatatypeFactory.newInstance(); - @SuppressWarnings("unchecked") - T calendar = (T) factory.newXMLGregorianCalendar(new GregorianCalendar()); - LOG.info("Externally created XMLGregorianCalendar"); - return calendar; - } else if (pojoClass.isAssignableFrom(Duration.class)) { - DatatypeFactory factory = DatatypeFactory.newInstance(); - @SuppressWarnings("unchecked") - T duration = (T) factory.newDuration(0L); - LOG.info("Externally created Duration"); - return duration; - } - } catch (Exception e) { - throw new IllegalStateException("Manufacturing failed", e); - } - return null; - } - - @Override - public T populatePojo(T pojo, Type... genericTypeArgs) { - return pojo; - } - - }; - - private final static PodamFactory podam = new PodamFactoryImpl(externalFactory); - - @Test - public void testXMLGregorianCalendarManufacturing() throws Exception { - - XMLGregorianCalendar pojo = podam.manufacturePojo(XMLGregorianCalendar.class); - Assert.assertNotNull("Construction failed", pojo); - } - - @Test - public void testDurationManufacturing() throws Exception { - - Duration pojo = podam.manufacturePojo(Duration.class); - Assert.assertNotNull("Construction failed", pojo); - } - - @Test - public void testXMLDatatypesFieldSetting() throws Exception { - - XMLDatatypePojo pojo = podam.manufacturePojo(XMLDatatypePojo.class); - Assert.assertNotNull("Construction failed", pojo); - Assert.assertNotNull("XMLGregorianCalendar attr should not be empty", pojo.getCalendar()); - Assert.assertNotNull("Duration attr should not be empty", pojo.getDuration()); - } - -} diff --git a/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/XMLDatatypeUnitTest.java b/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/XMLDatatypeUnitTest.java new file mode 100644 index 000000000..c34d12438 --- /dev/null +++ b/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/XMLDatatypeUnitTest.java @@ -0,0 +1,61 @@ +package uk.co.jemos.podam.test.unit.features.xmlTypes; + +import net.serenitybdd.junit.runners.SerenityRunner; +import net.thucydides.core.annotations.Title; +import org.junit.Test; +import org.junit.runner.RunWith; +import uk.co.jemos.podam.api.PodamFactory; +import uk.co.jemos.podam.test.dto.XMLDatatypePojo; +import uk.co.jemos.podam.test.unit.AbstractPodamSteps; + +import javax.xml.datatype.Duration; +import javax.xml.datatype.XMLGregorianCalendar; + +/** + * Test @uk.co.jemos.podam.test.dto.JAXBElementPojo@ construction + * + * @author daivanov + * + */ +@RunWith(SerenityRunner.class) +public class XMLDatatypeUnitTest extends AbstractPodamSteps{ + + private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger(XMLDatatypeUnitTest.class); + + + @Test + @Title("When given an external factory, Podam should be able to create instances of XMLGregorianCalendar") + public void testXMLGregorianCalendarManufacturing() throws Exception { + + PodamFactory podamFactory = podamFactorySteps.givenAPodamFactoryWithXmlTypesExternalFactory(); + + XMLGregorianCalendar pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass( + XMLGregorianCalendar.class, podamFactory); + + podamValidationSteps.theObjectShouldNotBeNull(pojo); + } + + @Test + @Title("When given an external factory, Podam should be able to create instances of the Duration class") + public void testDurationManufacturing() throws Exception { + + PodamFactory podamFactory = podamFactorySteps.givenAPodamFactoryWithXmlTypesExternalFactory(); + + Duration pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass(Duration.class, podamFactory); + podamValidationSteps.theObjectShouldNotBeNull(pojo); + } + + @Test + @Title("When given an external factory, Podam should be able to fill POJOs with instance attributes" + + " containing XMLGregorianCalendar and Duration") + public void testXMLDatatypesFieldSetting() throws Exception { + + PodamFactory podamFactory = podamFactorySteps.givenAPodamFactoryWithXmlTypesExternalFactory(); + + XMLDatatypePojo pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass(XMLDatatypePojo.class, podamFactory); + podamValidationSteps.theObjectShouldNotBeNull(pojo); + podamValidationSteps.theObjectShouldNotBeNull(pojo.getCalendar()); + podamValidationSteps.theObjectShouldNotBeNull(pojo.getDuration()); + } + +} diff --git a/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/XmlTypesExternalFactory.java b/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/XmlTypesExternalFactory.java new file mode 100644 index 000000000..d46d6f958 --- /dev/null +++ b/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/XmlTypesExternalFactory.java @@ -0,0 +1,47 @@ +package uk.co.jemos.podam.test.unit.features.xmlTypes; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import uk.co.jemos.podam.api.AbstractExternalFactory; + +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.Duration; +import javax.xml.datatype.XMLGregorianCalendar; +import java.lang.reflect.Type; +import java.util.GregorianCalendar; + +/** + * Created by tedonema on 21/06/2015. + */ +public class XmlTypesExternalFactory extends AbstractExternalFactory { + + + /** The application logger */ + private static final Logger LOG = LogManager.getLogger(XmlTypesExternalFactory.class); + + @Override + public T manufacturePojo(Class pojoClass, Type... genericTypeArgs) { + try { + if (pojoClass.isAssignableFrom(XMLGregorianCalendar.class)) { + DatatypeFactory factory = DatatypeFactory.newInstance(); + T calendar = (T) factory.newXMLGregorianCalendar(new GregorianCalendar()); + LOG.info("Externally created XMLGregorianCalendar"); + return calendar; + } else if (pojoClass.isAssignableFrom(Duration.class)) { + DatatypeFactory factory = DatatypeFactory.newInstance(); + @SuppressWarnings("unchecked") + T duration = (T) factory.newDuration(0L); + LOG.info("Externally created Duration"); + return duration; + } + } catch (Exception e) { + throw new IllegalStateException("Manufacturing failed", e); + } + return null; + } + + @Override + public T populatePojo(T pojo, Type... genericTypeArgs) { + return pojo; + } +} diff --git a/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/package-info.java b/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/package-info.java new file mode 100644 index 000000000..16a2a55be --- /dev/null +++ b/src/test/java/uk/co/jemos/podam/test/unit/features/xmlTypes/package-info.java @@ -0,0 +1,5 @@ +/** + * Contains tests for XML types. + * Created by tedonema on 21/06/2015. + */ +package uk.co.jemos.podam.test.unit.features.xmlTypes; \ No newline at end of file diff --git a/src/test/java/uk/co/jemos/podam/test/unit/steps/PodamFactorySteps.java b/src/test/java/uk/co/jemos/podam/test/unit/steps/PodamFactorySteps.java index 69ec42b1f..959c9acd3 100644 --- a/src/test/java/uk/co/jemos/podam/test/unit/steps/PodamFactorySteps.java +++ b/src/test/java/uk/co/jemos/podam/test/unit/steps/PodamFactorySteps.java @@ -11,6 +11,7 @@ import uk.co.jemos.podam.test.unit.features.externalFactory.TestExternalFactory; import uk.co.jemos.podam.test.unit.features.inheritance.CustomDataProviderStrategy; import uk.co.jemos.podam.test.unit.features.inheritance.TrackingExternalFactory; +import uk.co.jemos.podam.test.unit.features.xmlTypes.XmlTypesExternalFactory; import javax.validation.Validation; import javax.validation.Validator; @@ -148,4 +149,12 @@ public Validator givenAJavaxValidator() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); return factory.getValidator(); } + + @Step("Given a Podam factory with XML Types external factory") + public PodamFactory givenAPodamFactoryWithXmlTypesExternalFactory() { + + PodamFactory externalFactory = new XmlTypesExternalFactory(); + PodamFactory factory = new PodamFactoryImpl(externalFactory); + return factory; + } } diff --git a/src/test/java/uk/co/jemos/podam/test/utils/PodamTestConstants.java b/src/test/java/uk/co/jemos/podam/test/utils/PodamTestConstants.java index 4686d0db2..046c70b80 100644 --- a/src/test/java/uk/co/jemos/podam/test/utils/PodamTestConstants.java +++ b/src/test/java/uk/co/jemos/podam/test/utils/PodamTestConstants.java @@ -85,14 +85,4 @@ private PodamTestConstants() { throw new AssertionError(); } - // ------------------->> Public methods - - // ------------------->> Getters / Setters - - // ------------------->> Private methods - - // ------------------->> equals() / hashcode() / toString() - - // ------------------->> Inner classes - } diff --git a/src/test/java/uk/co/jemos/podam/test/utils/PodamTestUtils.java b/src/test/java/uk/co/jemos/podam/test/utils/PodamTestUtils.java index 23202a3ce..0b9cf64cc 100644 --- a/src/test/java/uk/co/jemos/podam/test/utils/PodamTestUtils.java +++ b/src/test/java/uk/co/jemos/podam/test/utils/PodamTestUtils.java @@ -4,12 +4,7 @@ package uk.co.jemos.podam.test.utils; import java.util.Calendar; -import java.util.Collection; -import java.util.Map; import java.util.TimeZone; -import java.util.Map.Entry; - -import org.junit.Assert; /** * Test utility class @@ -49,63 +44,4 @@ public static Calendar getMyBirthday() { return myBirthday; } - /** - * Asserts collection is non-empty and elements are of certain type - * - * @param collection - * Collection to examine - * @param elementType - * Element type to ensure - */ - public static void assertCollectionElementsType(Collection collection, Class elementType) { - Assert.assertNotNull("List should not be null", collection); - Assert.assertFalse("List should not be empty", collection.isEmpty()); - for (Object element : collection) { - Assert.assertEquals("Wrong element type", elementType, element.getClass()); - } - } - - /** - * Asserts map is non-empty and key and values are of certain type - * - * @param map - * Map to examine - * @param keyType - * Key type to ensure - * @param valueType - * Value type to ensure - */ - public static void assertMapElementsType(Map map, Class keyType, Class valueType) { - Assert.assertNotNull("Map should not be null", map); - Assert.assertFalse("Map should not be empty", map.isEmpty()); - for (Entry element : map.entrySet()) { - Assert.assertEquals("Wrong key type", keyType, element.getKey().getClass()); - Assert.assertEquals("Wrong value type", valueType, element.getValue().getClass()); - } - } - - /** - * Asserts array is non-empty and elements are of certain type - * - * @param array - * Array to examine - * @param elementType - * Element type to ensure - */ - public static void assertArrayElementsType(Object[] array, Class elementType) { - Assert.assertNotNull("Array should not be null", array); - Assert.assertTrue("Array should not be empty", array.length > 0); - for (Object element : array) { - Assert.assertEquals("Wrong element type", elementType, element.getClass()); - } - } - - // ------------------->> Getters / Setters - - // ------------------->> Private methods - - // ------------------->> equals() / hashcode() / toString() - - // ------------------->> Inner classes - }