#142 Fixing time zone dependent tests

This commit is contained in:
Gunnar Morling 2014-03-02 19:07:14 +01:00
parent 16112a6156
commit cfa2dc0bd0
2 changed files with 50 additions and 28 deletions

View File

@ -18,6 +18,8 @@
*/ */
package org.mapstruct.ap.test.builtin; package org.mapstruct.ap.test.builtin;
import static org.fest.assertions.Assertions.assertThat;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -28,19 +30,40 @@ import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBElement;
import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar; import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import org.mapstruct.ap.testutil.MapperTestBase; import org.mapstruct.ap.testutil.MapperTestBase;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.fest.assertions.Assertions.assertThat; /**
* Test for the generation of built-in mapping methods.
*
* @author Sjaak Derksen
*/
public class BuiltInTest extends MapperTestBase { public class BuiltInTest extends MapperTestBase {
private TimeZone originalTimeZone;
@BeforeClass
public void setDefaultTimeZoneToCet() {
originalTimeZone = TimeZone.getDefault();
TimeZone.setDefault( TimeZone.getTimeZone( "Europe/Berlin" ) );
}
@AfterClass
public void restoreOriginalTimeZone() {
TimeZone.setDefault( originalTimeZone );
}
@Test @Test
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class }) @WithClasses({ Source.class, Target.class, SourceTargetMapper.class })
public void shouldApplyBuiltIn() throws ParseException, DatatypeConfigurationException { public void shouldApplyBuiltIn() throws ParseException, DatatypeConfigurationException {
@ -48,13 +71,13 @@ public class BuiltInTest extends MapperTestBase {
source.setProp1( createJaxb( "TEST" ) ); source.setProp1( createJaxb( "TEST" ) );
source.setProp2( createJaxbList( "TEST2" ) ); source.setProp2( createJaxbList( "TEST2" ) );
source.setProp3( createDate( "31-08-1982 10:20:56" ) ); source.setProp3( createDate( "31-08-1982 10:20:56" ) );
source.setProp4( createXmlCal( 1999, 3, 2, 1 ) ); source.setProp4( createXmlCal( 1999, 3, 2, 60 ) );
source.setProp5( "05.07.1999" ); source.setProp5( "05.07.1999" );
source.setProp5NoFormat( createLocaleDate( "31-08-1982 10:20:56" ) ); source.setProp5NoFormat( createLocaleDate( "31-08-1982 10:20:56" ) );
source.setProp6( createXmlCal( 1999, 3, 2, 1 ) ); source.setProp6( createXmlCal( 1999, 3, 2, 60 ) );
source.setProp6NoFormat( createXmlCal( 1999, 3, 2, 1 ) ); source.setProp6NoFormat( createXmlCal( 1999, 3, 2, 60 ) );
source.setProp7( createCalendar( "02.03.1999" ) ); source.setProp7( createCalendar( "02.03.1999" ) );
source.setProp8( createXmlCal( 1999, 3, 2, 1 ) ); source.setProp8( createXmlCal( 1999, 3, 2, 60 ) );
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull(); assertThat( target ).isNotNull();
@ -66,7 +89,7 @@ public class BuiltInTest extends MapperTestBase {
assertThat( target.getProp5().toString() ).isEqualTo( "1999-07-05T00:00:00.000+02:00" ); assertThat( target.getProp5().toString() ).isEqualTo( "1999-07-05T00:00:00.000+02:00" );
assertThat( target.getProp5NoFormat().toString() ).isEqualTo( "1982-08-31T10:20:00.000+02:00" ); assertThat( target.getProp5NoFormat().toString() ).isEqualTo( "1982-08-31T10:20:00.000+02:00" );
assertThat( target.getProp6().toString() ).isEqualTo( "02.03.1999" ); assertThat( target.getProp6().toString() ).isEqualTo( "02.03.1999" );
assertThat( target.getProp6NoFormat().toString() ).isEqualTo( "1999-03-02+00:01" ); assertThat( target.getProp6NoFormat().toString() ).isEqualTo( "1999-03-02+01:00" );
assertThat( target.getProp7().toString() ).isEqualTo( "1999-03-02T00:00:00.000+01:00" ); assertThat( target.getProp7().toString() ).isEqualTo( "1999-03-02T00:00:00.000+01:00" );
assertThat( target.getProp8().getTimeInMillis() ).isEqualTo( 920329200000L ); assertThat( target.getProp8().getTimeInMillis() ).isEqualTo( 920329200000L );
} }
@ -76,13 +99,11 @@ public class BuiltInTest extends MapperTestBase {
public void shouldApplyBuiltInOnIterable() throws ParseException, DatatypeConfigurationException { public void shouldApplyBuiltInOnIterable() throws ParseException, DatatypeConfigurationException {
IterableSource source = new IterableSource(); IterableSource source = new IterableSource();
source.setDates( Arrays.asList( new XMLGregorianCalendar[] { createXmlCal( 1999, 3, 2, 1 ) } ) ); source.setDates( Arrays.asList( new XMLGregorianCalendar[] { createXmlCal( 1999, 3, 2, 60 ) } ) );
IterableTarget target = IterableSourceTargetMapper.INSTANCE.sourceToTarget( source ); IterableTarget target = IterableSourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull(); assertThat( target ).isNotNull();
assertThat( target.getDates().size() ).isEqualTo( 1 ); assertThat( target.getDates() ).containsExactly( "02.03.1999" );
assertThat( target.getDates().get( 0 ) ).isEqualTo( "02.03.1999" );
} }
@Test @Test
@ -91,11 +112,11 @@ public class BuiltInTest extends MapperTestBase {
MapSource source = new MapSource(); MapSource source = new MapSource();
source.setExample( new HashMap<JAXBElement<String>, XMLGregorianCalendar>() ); source.setExample( new HashMap<JAXBElement<String>, XMLGregorianCalendar>() );
source.getExample().put( createJaxb( "TEST" ), createXmlCal( 1999, 3, 2, 1 ) ); source.getExample().put( createJaxb( "TEST" ), createXmlCal( 1999, 3, 2, 60 ) );
MapTarget target = MapSourceTargetMapper.INSTANCE.sourceToTarget( source ); MapTarget target = MapSourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull(); assertThat( target ).isNotNull();
assertThat( target.getExample().get( "TEST" ) ).isEqualTo( "1999-03-02+00:01" ); assertThat( target.getExample().get( "TEST" ) ).isEqualTo( "1999-03-02+01:00" );
} }
private JAXBElement<String> createJaxb(String test) { private JAXBElement<String> createJaxb(String test) {

View File

@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBElement;
import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar; import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
@ -70,14 +71,14 @@ public class NestedMappingMethodInvocationTest extends MapperTestBase {
new JAXBElement<XMLGregorianCalendar>( new JAXBElement<XMLGregorianCalendar>(
QNAME, QNAME,
XMLGregorianCalendar.class, XMLGregorianCalendar.class,
createXmlCal( 1999, 3, 2, 1 ) createXmlCal( 1999, 3, 2 )
) )
); );
dates.add( dates.add(
new JAXBElement<XMLGregorianCalendar>( new JAXBElement<XMLGregorianCalendar>(
QNAME, QNAME,
XMLGregorianCalendar.class, XMLGregorianCalendar.class,
createXmlCal( 2004, 7, 29, 3 ) createXmlCal( 2004, 7, 28 )
) )
); );
@ -101,9 +102,9 @@ public class NestedMappingMethodInvocationTest extends MapperTestBase {
return orderType; return orderType;
} }
private XMLGregorianCalendar createXmlCal(int year, int month, int day, int tz) private XMLGregorianCalendar createXmlCal(int year, int month, int day)
throws DatatypeConfigurationException { throws DatatypeConfigurationException {
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate( year, month, day, tz ); return DatatypeFactory.newInstance()
.newXMLGregorianCalendarDate( year, month, day, DatatypeConstants.FIELD_UNDEFINED );
} }
} }