mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#689 Joda DateTime to XmlGregorianCalendar built in and vice versa
This commit is contained in:
parent
b653c6b464
commit
13395f5958
@ -24,6 +24,7 @@ import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.internal.util.Collections;
|
||||
import org.mapstruct.ap.internal.util.JavaTimeConstants;
|
||||
import org.mapstruct.ap.internal.util.JaxbConstants;
|
||||
import org.mapstruct.ap.internal.util.JodaTimeConstants;
|
||||
|
||||
/**
|
||||
* Registry for all built-in methods.
|
||||
@ -54,6 +55,11 @@ public class BuiltInMappingMethods {
|
||||
builtInMethods.add( new XmlGregorianCalendarToLocalDate( typeFactory ) );
|
||||
builtInMethods.add( new LocalDateToXmlGregorianCalendar( typeFactory ) );
|
||||
}
|
||||
|
||||
if ( isJodaTimeAvailable( typeFactory ) ) {
|
||||
builtInMethods.add( new JodaDateTimeToXmlGregorianCalendar( typeFactory ) );
|
||||
builtInMethods.add( new XmlGregorianCalendarToJodaDateTime( typeFactory ) );
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isJaxbAvailable(TypeFactory typeFactory) {
|
||||
@ -64,6 +70,10 @@ public class BuiltInMappingMethods {
|
||||
return typeFactory.isTypeAvailable( JavaTimeConstants.ZONED_DATE_TIME_FQN );
|
||||
}
|
||||
|
||||
private static boolean isJodaTimeAvailable(TypeFactory typeFactory) {
|
||||
return typeFactory.isTypeAvailable( JodaTimeConstants.DATE_TIME_FQN );
|
||||
}
|
||||
|
||||
public List<BuiltInMethod> getBuiltInMethods() {
|
||||
return builtInMethods;
|
||||
}
|
||||
|
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model.source.builtin;
|
||||
|
||||
import static org.mapstruct.ap.internal.util.Collections.asSet;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.internal.util.JodaTimeConstants;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class JodaDateTimeToXmlGregorianCalendar extends BuiltInMethod {
|
||||
|
||||
private final Parameter parameter;
|
||||
private final Type returnType;
|
||||
private final Set<Type> importTypes;
|
||||
|
||||
public JodaDateTimeToXmlGregorianCalendar(TypeFactory typeFactory) {
|
||||
this.parameter = new Parameter(
|
||||
"dt",
|
||||
typeFactory.getType( JodaTimeConstants.DATE_TIME_FQN )
|
||||
);
|
||||
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
|
||||
|
||||
this.importTypes = asSet(
|
||||
returnType,
|
||||
parameter.getType(),
|
||||
typeFactory.getType( DatatypeFactory.class ),
|
||||
typeFactory.getType( DatatypeConfigurationException.class )
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return importTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameter getParameter() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model.source.builtin;
|
||||
|
||||
import static org.mapstruct.ap.internal.util.Collections.asSet;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.xml.datatype.DatatypeConstants;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.internal.util.JodaTimeConstants;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class XmlGregorianCalendarToJodaDateTime extends BuiltInMethod {
|
||||
|
||||
private final Parameter parameter;
|
||||
private final Type returnType;
|
||||
private final Set<Type> importTypes;
|
||||
|
||||
public XmlGregorianCalendarToJodaDateTime(TypeFactory typeFactory) {
|
||||
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
|
||||
this.returnType = typeFactory.getType( JodaTimeConstants.DATE_TIME_FQN );
|
||||
this.importTypes = asSet(
|
||||
typeFactory.getType( DatatypeConstants.class ),
|
||||
typeFactory.getType( JodaTimeConstants.DATE_TIME_ZONE_FQN ),
|
||||
returnType,
|
||||
parameter.getType() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameter getParameter() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return importTypes;
|
||||
}
|
||||
}
|
@ -38,5 +38,7 @@ public final class JodaTimeConstants {
|
||||
|
||||
public static final String DATE_TIME_FORMAT_FQN = "org.joda.time.format.DateTimeFormat";
|
||||
|
||||
public static final String DATE_TIME_ZONE_FQN = "org.joda.time.DateTimeZone";
|
||||
|
||||
public static final String DATE_TIME_FORMAT = "LL";
|
||||
}
|
||||
|
@ -0,0 +1,40 @@
|
||||
<#--
|
||||
|
||||
Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
and/or other contributors as indicated by the @authors tag. See the
|
||||
copyright.txt file in the distribution for a full listing of all
|
||||
contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
-->
|
||||
private XMLGregorianCalendar ${name}( DateTime dt ) {
|
||||
if ( dt == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(
|
||||
dt.getYear(),
|
||||
dt.getMonthOfYear(),
|
||||
dt.getDayOfMonth(),
|
||||
dt.getHourOfDay(),
|
||||
dt.getMinuteOfHour(),
|
||||
dt.getSecondOfMinute(),
|
||||
dt.getMillisOfSecond(),
|
||||
dt.getZone().getOffset( null ) / 60000 );
|
||||
}
|
||||
catch ( DatatypeConfigurationException ex ) {
|
||||
throw new RuntimeException( ex );
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
<#--
|
||||
|
||||
Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
and/or other contributors as indicated by the @authors tag. See the
|
||||
copyright.txt file in the distribution for a full listing of all
|
||||
contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
-->
|
||||
private static DateTime ${name}( XMLGregorianCalendar xcal ) {
|
||||
if ( xcal == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( xcal.getYear() != DatatypeConstants.FIELD_UNDEFINED
|
||||
&& xcal.getMonth() != DatatypeConstants.FIELD_UNDEFINED
|
||||
&& xcal.getDay() != DatatypeConstants.FIELD_UNDEFINED
|
||||
&& xcal.getHour() != DatatypeConstants.FIELD_UNDEFINED
|
||||
&& xcal.getMinute() != DatatypeConstants.FIELD_UNDEFINED
|
||||
) {
|
||||
if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED
|
||||
&& xcal.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED
|
||||
&& xcal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED ) {
|
||||
return new DateTime( xcal.getYear(),
|
||||
xcal.getMonth(),
|
||||
xcal.getDay(),
|
||||
xcal.getHour(),
|
||||
xcal.getMinute(),
|
||||
xcal.getSecond(),
|
||||
xcal.getMillisecond(),
|
||||
DateTimeZone.forOffsetMillis( xcal.getTimezone() * 60000 )
|
||||
);
|
||||
}
|
||||
else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED
|
||||
&& xcal.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED ) {
|
||||
return new DateTime( xcal.getYear(),
|
||||
xcal.getMonth(),
|
||||
xcal.getDay(),
|
||||
xcal.getHour(),
|
||||
xcal.getMinute(),
|
||||
xcal.getSecond(),
|
||||
xcal.getMillisecond()
|
||||
);
|
||||
}
|
||||
else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED
|
||||
&& xcal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED ) {
|
||||
return new DateTime( xcal.getYear(),
|
||||
xcal.getMonth(),
|
||||
xcal.getDay(),
|
||||
xcal.getHour(),
|
||||
xcal.getMinute(),
|
||||
xcal.getSecond(),
|
||||
DateTimeZone.forOffsetMillis( xcal.getTimezone() * 60000 )
|
||||
);
|
||||
}
|
||||
else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED ) {
|
||||
return new DateTime( xcal.getYear(),
|
||||
xcal.getMonth(),
|
||||
xcal.getDay(),
|
||||
xcal.getHour(),
|
||||
xcal.getMinute(),
|
||||
xcal.getSecond()
|
||||
);
|
||||
}
|
||||
else if ( xcal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED ) {
|
||||
return new DateTime( xcal.getYear(),
|
||||
xcal.getMonth(),
|
||||
xcal.getDay(),
|
||||
xcal.getHour(),
|
||||
xcal.getMinute(),
|
||||
DateTimeZone.forOffsetMillis( xcal.getTimezone() * 60000 )
|
||||
);
|
||||
}
|
||||
else {
|
||||
return new DateTime( xcal.getYear(),
|
||||
xcal.getMonth(),
|
||||
xcal.getDay(),
|
||||
xcal.getHour(),
|
||||
xcal.getMinute()
|
||||
);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
@ -0,0 +1,263 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.builtin.bean.jodatime;
|
||||
|
||||
import java.util.TimeZone;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.DateTimeBean;
|
||||
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean;
|
||||
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.DateTimeToXmlGregorianCalendar;
|
||||
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToDateTime;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@WithClasses({
|
||||
DateTimeBean.class,
|
||||
XmlGregorianCalendarBean.class
|
||||
})
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
@IssueKey( "689" )
|
||||
public class JodaTimeTest {
|
||||
|
||||
@Test
|
||||
@WithClasses(DateTimeToXmlGregorianCalendar.class)
|
||||
public void shouldMapDateTimeToXmlGregorianCalendar() {
|
||||
|
||||
DateTimeBean in = new DateTimeBean();
|
||||
DateTime dt = new DateTime(2010, 1, 15, 1, 1, 1, 100, DateTimeZone.forOffsetHours( -1 ) );
|
||||
in.setDateTime( dt );
|
||||
XmlGregorianCalendarBean res = DateTimeToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean( in );
|
||||
|
||||
assertThat( res.getxMLGregorianCalendar().getYear() ).isEqualTo( 2010 );
|
||||
assertThat( res.getxMLGregorianCalendar().getMonth() ).isEqualTo( 1 );
|
||||
assertThat( res.getxMLGregorianCalendar().getDay() ).isEqualTo( 15 );
|
||||
assertThat( res.getxMLGregorianCalendar().getHour() ).isEqualTo( 1 );
|
||||
assertThat( res.getxMLGregorianCalendar().getMinute() ).isEqualTo( 1 );
|
||||
assertThat( res.getxMLGregorianCalendar().getSecond() ).isEqualTo( 1 );
|
||||
assertThat( res.getxMLGregorianCalendar().getMillisecond() ).isEqualTo( 100 );
|
||||
assertThat( res.getxMLGregorianCalendar().getTimezone() ).isEqualTo( -60 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses(DateTimeToXmlGregorianCalendar.class)
|
||||
public void shouldMapIncompleteDateTimeToXmlGregorianCalendar() {
|
||||
|
||||
DateTimeBean in = new DateTimeBean();
|
||||
DateTime dt = new DateTime(2010, 1, 15, 1, 1 );
|
||||
in.setDateTime( dt );
|
||||
XmlGregorianCalendarBean res = DateTimeToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean( in );
|
||||
|
||||
assertThat( res.getxMLGregorianCalendar().getYear() ).isEqualTo( 2010 );
|
||||
assertThat( res.getxMLGregorianCalendar().getMonth() ).isEqualTo( 1 );
|
||||
assertThat( res.getxMLGregorianCalendar().getDay() ).isEqualTo( 15 );
|
||||
assertThat( res.getxMLGregorianCalendar().getHour() ).isEqualTo( 1 );
|
||||
assertThat( res.getxMLGregorianCalendar().getMinute() ).isEqualTo( 1 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses(XmlGregorianCalendarToDateTime.class)
|
||||
public void shouldMapXmlGregorianCalendarToDateTime() throws Exception {
|
||||
|
||||
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
|
||||
XMLGregorianCalendar xcal =
|
||||
DatatypeFactory.newInstance().newXMLGregorianCalendar( 2010, 1, 15, 1, 1, 1, 100, 60 );
|
||||
in.setxMLGregorianCalendar( xcal );
|
||||
|
||||
DateTimeBean res = XmlGregorianCalendarToDateTime.INSTANCE.toDateTimeBean( in );
|
||||
assertThat( res.getDateTime().getYear() ).isEqualTo( 2010 );
|
||||
assertThat( res.getDateTime().getMonthOfYear() ).isEqualTo( 1 );
|
||||
assertThat( res.getDateTime().getDayOfMonth() ).isEqualTo( 15 );
|
||||
assertThat( res.getDateTime().getHourOfDay() ).isEqualTo( 1 );
|
||||
assertThat( res.getDateTime().getMinuteOfHour() ).isEqualTo( 1 );
|
||||
assertThat( res.getDateTime().getSecondOfMinute() ).isEqualTo( 1 );
|
||||
assertThat( res.getDateTime().getMillisOfSecond() ).isEqualTo( 100 );
|
||||
assertThat( res.getDateTime().getZone().getOffset( null ) ).isEqualTo( 3600000 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses(XmlGregorianCalendarToDateTime.class)
|
||||
public void shouldMapXmlGregorianCalendarWithoutTimeZoneToDateTimeWithDefaultTimeZone() throws Exception {
|
||||
|
||||
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
|
||||
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
|
||||
xcal.setYear( 1999 );
|
||||
xcal.setMonth( 5 );
|
||||
xcal.setDay( 25 );
|
||||
xcal.setHour( 23 );
|
||||
xcal.setMinute( 34 );
|
||||
xcal.setSecond( 45 );
|
||||
xcal.setMillisecond( 500 );
|
||||
in.setxMLGregorianCalendar( xcal );
|
||||
|
||||
DateTimeBean res = XmlGregorianCalendarToDateTime.INSTANCE.toDateTimeBean( in );
|
||||
assertThat( res.getDateTime().getYear() ).isEqualTo( 1999 );
|
||||
assertThat( res.getDateTime().getMonthOfYear() ).isEqualTo( 5 );
|
||||
assertThat( res.getDateTime().getDayOfMonth() ).isEqualTo( 25 );
|
||||
assertThat( res.getDateTime().getHourOfDay() ).isEqualTo( 23 );
|
||||
assertThat( res.getDateTime().getMinuteOfHour() ).isEqualTo( 34 );
|
||||
assertThat( res.getDateTime().getSecondOfMinute() ).isEqualTo( 45 );
|
||||
assertThat( res.getDateTime().getMillisOfSecond() ).isEqualTo( 500 );
|
||||
assertThat( res.getDateTime().getZone().getID() ).isEqualTo( TimeZone.getDefault().getID() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses(XmlGregorianCalendarToDateTime.class)
|
||||
public void shouldMapXmlGregorianCalendarWithoutMillisToDateTime() throws Exception {
|
||||
|
||||
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
|
||||
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
|
||||
xcal.setYear( 1999 );
|
||||
xcal.setMonth( 5 );
|
||||
xcal.setDay( 25 );
|
||||
xcal.setHour( 23 );
|
||||
xcal.setMinute( 34 );
|
||||
xcal.setSecond( 45 );
|
||||
xcal.setTimezone( 60 );
|
||||
in.setxMLGregorianCalendar( xcal );
|
||||
|
||||
DateTimeBean res = XmlGregorianCalendarToDateTime.INSTANCE.toDateTimeBean( in );
|
||||
assertThat( res.getDateTime().getYear() ).isEqualTo( 1999 );
|
||||
assertThat( res.getDateTime().getMonthOfYear() ).isEqualTo( 5 );
|
||||
assertThat( res.getDateTime().getDayOfMonth() ).isEqualTo( 25 );
|
||||
assertThat( res.getDateTime().getHourOfDay() ).isEqualTo( 23 );
|
||||
assertThat( res.getDateTime().getMinuteOfHour() ).isEqualTo( 34 );
|
||||
assertThat( res.getDateTime().getSecondOfMinute() ).isEqualTo( 45 );
|
||||
assertThat( res.getDateTime().getMillisOfSecond() ).isEqualTo( 0 );
|
||||
assertThat( res.getDateTime().getZone().getOffset( null ) ).isEqualTo( 3600000 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses(XmlGregorianCalendarToDateTime.class)
|
||||
public void shouldMapXmlGregorianCalendarWithoutMillisAndTimeZoneToDateTimeWithDefaultTimeZone() throws Exception {
|
||||
|
||||
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
|
||||
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
|
||||
xcal.setYear( 1999 );
|
||||
xcal.setMonth( 5 );
|
||||
xcal.setDay( 25 );
|
||||
xcal.setHour( 23 );
|
||||
xcal.setMinute( 34 );
|
||||
xcal.setSecond( 45 );
|
||||
in.setxMLGregorianCalendar( xcal );
|
||||
|
||||
DateTimeBean res = XmlGregorianCalendarToDateTime.INSTANCE.toDateTimeBean( in );
|
||||
assertThat( res.getDateTime().getYear() ).isEqualTo( 1999 );
|
||||
assertThat( res.getDateTime().getMonthOfYear() ).isEqualTo( 5 );
|
||||
assertThat( res.getDateTime().getDayOfMonth() ).isEqualTo( 25 );
|
||||
assertThat( res.getDateTime().getHourOfDay() ).isEqualTo( 23 );
|
||||
assertThat( res.getDateTime().getMinuteOfHour() ).isEqualTo( 34 );
|
||||
assertThat( res.getDateTime().getSecondOfMinute() ).isEqualTo( 45 );
|
||||
assertThat( res.getDateTime().getMillisOfSecond() ).isEqualTo( 0 );
|
||||
assertThat( res.getDateTime().getZone().getID() ).isEqualTo( TimeZone.getDefault().getID() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses(XmlGregorianCalendarToDateTime.class)
|
||||
public void shouldMapXmlGregorianCalendarWithoutSecondsToDateTime() throws Exception {
|
||||
|
||||
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
|
||||
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
|
||||
xcal.setYear( 1999 );
|
||||
xcal.setMonth( 5 );
|
||||
xcal.setDay( 25 );
|
||||
xcal.setHour( 23 );
|
||||
xcal.setMinute( 34 );
|
||||
xcal.setTimezone( 60 );
|
||||
in.setxMLGregorianCalendar( xcal );
|
||||
|
||||
DateTimeBean res = XmlGregorianCalendarToDateTime.INSTANCE.toDateTimeBean( in );
|
||||
assertThat( res.getDateTime().getYear() ).isEqualTo( 1999 );
|
||||
assertThat( res.getDateTime().getMonthOfYear() ).isEqualTo( 5 );
|
||||
assertThat( res.getDateTime().getDayOfMonth() ).isEqualTo( 25 );
|
||||
assertThat( res.getDateTime().getHourOfDay() ).isEqualTo( 23 );
|
||||
assertThat( res.getDateTime().getMinuteOfHour() ).isEqualTo( 34 );
|
||||
assertThat( res.getDateTime().getSecondOfMinute() ).isEqualTo( 0 );
|
||||
assertThat( res.getDateTime().getMillisOfSecond() ).isEqualTo( 0 );
|
||||
assertThat( res.getDateTime().getZone().getOffset( null ) ).isEqualTo( 3600000 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses(XmlGregorianCalendarToDateTime.class)
|
||||
public void shouldMapXmlGregorianCalendarWithoutSecondsAndTimeZoneToDateTimeWithDefaultTimeZone() throws Exception {
|
||||
|
||||
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
|
||||
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
|
||||
xcal.setYear( 1999 );
|
||||
xcal.setMonth( 5 );
|
||||
xcal.setDay( 25 );
|
||||
xcal.setHour( 23 );
|
||||
xcal.setMinute( 34 );
|
||||
in.setxMLGregorianCalendar( xcal );
|
||||
|
||||
DateTimeBean res = XmlGregorianCalendarToDateTime.INSTANCE.toDateTimeBean( in );
|
||||
assertThat( res.getDateTime().getYear() ).isEqualTo( 1999 );
|
||||
assertThat( res.getDateTime().getMonthOfYear() ).isEqualTo( 5 );
|
||||
assertThat( res.getDateTime().getDayOfMonth() ).isEqualTo( 25 );
|
||||
assertThat( res.getDateTime().getHourOfDay() ).isEqualTo( 23 );
|
||||
assertThat( res.getDateTime().getMinuteOfHour() ).isEqualTo( 34 );
|
||||
assertThat( res.getDateTime().getSecondOfMinute() ).isEqualTo( 0 );
|
||||
assertThat( res.getDateTime().getMillisOfSecond() ).isEqualTo( 0 );
|
||||
assertThat( res.getDateTime().getZone().getID() ).isEqualTo( TimeZone.getDefault().getID() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses(XmlGregorianCalendarToDateTime.class)
|
||||
public void shouldNotMapXmlGregorianCalendarWithoutMinutes() throws Exception {
|
||||
|
||||
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
|
||||
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
|
||||
xcal.setYear( 1999 );
|
||||
xcal.setMonth( 5 );
|
||||
xcal.setDay( 25 );
|
||||
xcal.setHour( 23 );
|
||||
in.setxMLGregorianCalendar( xcal );
|
||||
|
||||
DateTimeBean res = XmlGregorianCalendarToDateTime.INSTANCE.toDateTimeBean( in );
|
||||
assertThat( res.getDateTime() ).isNull();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@WithClasses({DateTimeToXmlGregorianCalendar.class, XmlGregorianCalendarToDateTime.class})
|
||||
public void shouldMapRoundTrip() {
|
||||
|
||||
DateTimeBean dtb1 = new DateTimeBean();
|
||||
DateTime dt = new DateTime(2010, 1, 15, 1, 1, 1, 100, DateTimeZone.forOffsetHours( -1 ) );
|
||||
dtb1.setDateTime( dt );
|
||||
XmlGregorianCalendarBean xcb1 = DateTimeToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean( dtb1 );
|
||||
DateTimeBean dtb2 = XmlGregorianCalendarToDateTime.INSTANCE.toDateTimeBean( xcb1 );
|
||||
XmlGregorianCalendarBean xcb2 = DateTimeToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean( dtb2 );
|
||||
|
||||
assertThat( dtb1.getDateTime() ).isEqualTo( dtb2.getDateTime() );
|
||||
assertThat( xcb1.getxMLGregorianCalendar() ).isEqualTo( xcb2.getxMLGregorianCalendar() );
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.builtin.bean.jodatime.bean;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class DateTimeBean {
|
||||
|
||||
private DateTime dateTime;
|
||||
|
||||
public DateTime getDateTime() {
|
||||
return dateTime;
|
||||
}
|
||||
|
||||
public void setDateTime(DateTime dateTime) {
|
||||
this.dateTime = dateTime;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.builtin.bean.jodatime.bean;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class XmlGregorianCalendarBean {
|
||||
|
||||
XMLGregorianCalendar xMLGregorianCalendar;
|
||||
|
||||
public XMLGregorianCalendar getxMLGregorianCalendar() {
|
||||
return xMLGregorianCalendar;
|
||||
}
|
||||
|
||||
public void setxMLGregorianCalendar(XMLGregorianCalendar xMLGregorianCalendar) {
|
||||
this.xMLGregorianCalendar = xMLGregorianCalendar;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.builtin.bean.jodatime.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.DateTimeBean;
|
||||
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Mapper
|
||||
public interface DateTimeToXmlGregorianCalendar {
|
||||
|
||||
DateTimeToXmlGregorianCalendar INSTANCE = Mappers.getMapper( DateTimeToXmlGregorianCalendar.class );
|
||||
|
||||
@Mapping( target = "xMLGregorianCalendar", source = "dateTime")
|
||||
XmlGregorianCalendarBean toXmlGregorianCalendarBean( DateTimeBean in );
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.builtin.bean.jodatime.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.DateTimeBean;
|
||||
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Mapper
|
||||
public interface XmlGregorianCalendarToDateTime {
|
||||
|
||||
XmlGregorianCalendarToDateTime INSTANCE = Mappers.getMapper( XmlGregorianCalendarToDateTime.class );
|
||||
|
||||
@Mapping( target = "dateTime", source = "xMLGregorianCalendar" )
|
||||
DateTimeBean toDateTimeBean( XmlGregorianCalendarBean in );
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user