mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#142 Fixing time zone dependent tests
This commit is contained in:
parent
16112a6156
commit
cfa2dc0bd0
@ -18,6 +18,8 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.builtin;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -28,33 +30,54 @@ import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
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 {
|
||||
|
||||
private TimeZone originalTimeZone;
|
||||
|
||||
@BeforeClass
|
||||
public void setDefaultTimeZoneToCet() {
|
||||
originalTimeZone = TimeZone.getDefault();
|
||||
TimeZone.setDefault( TimeZone.getTimeZone( "Europe/Berlin" ) );
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public void restoreOriginalTimeZone() {
|
||||
TimeZone.setDefault( originalTimeZone );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses( { Source.class, Target.class, SourceTargetMapper.class } )
|
||||
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class })
|
||||
public void shouldApplyBuiltIn() throws ParseException, DatatypeConfigurationException {
|
||||
Source source = new Source();
|
||||
source.setProp1( createJaxb( "TEST" ) );
|
||||
source.setProp2( createJaxbList( "TEST2" ) );
|
||||
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.setProp5NoFormat( createLocaleDate( "31-08-1982 10:20:56" ) );
|
||||
source.setProp6( createXmlCal( 1999, 3, 2, 1 ) );
|
||||
source.setProp6NoFormat( createXmlCal( 1999, 3, 2, 1 ) );
|
||||
source.setProp6( createXmlCal( 1999, 3, 2, 60 ) );
|
||||
source.setProp6NoFormat( createXmlCal( 1999, 3, 2, 60 ) );
|
||||
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 );
|
||||
assertThat( target ).isNotNull();
|
||||
@ -66,66 +89,64 @@ public class BuiltInTest extends MapperTestBase {
|
||||
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.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.getProp8().getTimeInMillis() ).isEqualTo( 920329200000L );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses( { IterableSource.class, IterableTarget.class, IterableSourceTargetMapper.class } )
|
||||
@WithClasses({ IterableSource.class, IterableTarget.class, IterableSourceTargetMapper.class })
|
||||
public void shouldApplyBuiltInOnIterable() throws ParseException, DatatypeConfigurationException {
|
||||
|
||||
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 );
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getDates().size() ).isEqualTo( 1 );
|
||||
assertThat( target.getDates().get( 0 ) ).isEqualTo( "02.03.1999" );
|
||||
|
||||
assertThat( target.getDates() ).containsExactly( "02.03.1999" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses( { MapSource.class, MapTarget.class, MapSourceTargetMapper.class } )
|
||||
@WithClasses({ MapSource.class, MapTarget.class, MapSourceTargetMapper.class })
|
||||
public void shouldApplyBuiltInOnMap() throws ParseException, DatatypeConfigurationException {
|
||||
|
||||
MapSource source = new MapSource();
|
||||
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 );
|
||||
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) {
|
||||
return new JAXBElement<String>( new QName( "www.mapstruct.org", "test" ), String.class, test );
|
||||
}
|
||||
|
||||
private List<JAXBElement<String>> createJaxbList( String test ) {
|
||||
private List<JAXBElement<String>> createJaxbList(String test) {
|
||||
List<JAXBElement<String>> result = new ArrayList<JAXBElement<String>>();
|
||||
result.add( createJaxb( test ) );
|
||||
return result;
|
||||
}
|
||||
|
||||
private Date createDate( String date ) throws ParseException {
|
||||
private Date createDate(String date) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat( "dd-M-yyyy hh:mm:ss" );
|
||||
return sdf.parse( date );
|
||||
}
|
||||
|
||||
private Calendar createCalendar( String cal ) throws ParseException {
|
||||
private Calendar createCalendar(String cal) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" );
|
||||
GregorianCalendar gcal = new GregorianCalendar();
|
||||
gcal.setTime( sdf.parse( cal ) );
|
||||
return gcal;
|
||||
}
|
||||
|
||||
private XMLGregorianCalendar createXmlCal( int year, int month, int day, int tz )
|
||||
throws DatatypeConfigurationException {
|
||||
private XMLGregorianCalendar createXmlCal(int year, int month, int day, int tz)
|
||||
throws DatatypeConfigurationException {
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate( year, month, day, tz );
|
||||
}
|
||||
|
||||
private String createLocaleDate( String date ) throws ParseException {
|
||||
private String createLocaleDate(String date) throws ParseException {
|
||||
Date d = createDate( date );
|
||||
DateFormat df = SimpleDateFormat.getInstance();
|
||||
return df.format( d );
|
||||
|
@ -22,6 +22,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeConstants;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import javax.xml.namespace.QName;
|
||||
@ -70,14 +71,14 @@ public class NestedMappingMethodInvocationTest extends MapperTestBase {
|
||||
new JAXBElement<XMLGregorianCalendar>(
|
||||
QNAME,
|
||||
XMLGregorianCalendar.class,
|
||||
createXmlCal( 1999, 3, 2, 1 )
|
||||
createXmlCal( 1999, 3, 2 )
|
||||
)
|
||||
);
|
||||
dates.add(
|
||||
new JAXBElement<XMLGregorianCalendar>(
|
||||
QNAME,
|
||||
XMLGregorianCalendar.class,
|
||||
createXmlCal( 2004, 7, 29, 3 )
|
||||
createXmlCal( 2004, 7, 28 )
|
||||
)
|
||||
);
|
||||
|
||||
@ -101,9 +102,9 @@ public class NestedMappingMethodInvocationTest extends MapperTestBase {
|
||||
return orderType;
|
||||
}
|
||||
|
||||
private XMLGregorianCalendar createXmlCal(int year, int month, int day, int tz)
|
||||
private XMLGregorianCalendar createXmlCal(int year, int month, int day)
|
||||
throws DatatypeConfigurationException {
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate( year, month, day, tz );
|
||||
return DatatypeFactory.newInstance()
|
||||
.newXMLGregorianCalendarDate( year, month, day, DatatypeConstants.FIELD_UNDEFINED );
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user