Skip to content

Commit

Permalink
Serenified XML Data type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtedone committed Jun 21, 2015
1 parent 3801ff2 commit 307b251
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 160 deletions.
86 changes: 0 additions & 86 deletions src/test/java/uk/co/jemos/podam/test/unit/XMLDatatypeUnitTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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> T manufacturePojo(Class<T> 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> T populatePojo(T pojo, Type... genericTypeArgs) {
return pojo;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
10 changes: 0 additions & 10 deletions src/test/java/uk/co/jemos/podam/test/utils/PodamTestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,4 @@ private PodamTestConstants() {
throw new AssertionError();
}

// ------------------->> Public methods

// ------------------->> Getters / Setters

// ------------------->> Private methods

// ------------------->> equals() / hashcode() / toString()

// ------------------->> Inner classes

}
64 changes: 0 additions & 64 deletions src/test/java/uk/co/jemos/podam/test/utils/PodamTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

}

0 comments on commit 307b251

Please sign in to comment.