From 13395f595865040ab17d634523ae852b964088be Mon Sep 17 00:00:00 2001 From: sjaakd Date: Sun, 4 Sep 2016 16:24:53 +0200 Subject: [PATCH] #689 Joda DateTime to XmlGregorianCalendar built in and vice versa --- .../source/builtin/BuiltInMappingMethods.java | 10 + .../JodaDateTimeToXmlGregorianCalendar.java | 72 +++++ .../XmlGregorianCalendarToJodaDateTime.java | 65 +++++ .../ap/internal/util/JodaTimeConstants.java | 2 + .../JodaDateTimeToXmlGregorianCalendar.ftl | 40 +++ .../XmlGregorianCalendarToJodaDateTime.ftl | 95 +++++++ .../builtin/bean/jodatime/JodaTimeTest.java | 263 ++++++++++++++++++ .../bean/jodatime/bean/DateTimeBean.java | 38 +++ .../bean/XmlGregorianCalendarBean.java | 39 +++ .../DateTimeToXmlGregorianCalendar.java | 38 +++ .../XmlGregorianCalendarToDateTime.java | 38 +++ 11 files changed, 700 insertions(+) create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.java create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.java create mode 100644 processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl create mode 100644 processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/JodaTimeTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/DateTimeBean.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/XmlGregorianCalendarBean.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/DateTimeToXmlGregorianCalendar.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/XmlGregorianCalendarToDateTime.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java index 4652a1532..cd7f2b760 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java @@ -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 getBuiltInMethods() { return builtInMethods; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.java new file mode 100644 index 000000000..bcc75cd3f --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.java @@ -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 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 getImportTypes() { + return importTypes; + } + + @Override + public Parameter getParameter() { + return parameter; + } + + @Override + public Type getReturnType() { + return returnType; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.java new file mode 100644 index 000000000..f50b7ed1b --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.java @@ -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 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 getImportTypes() { + return importTypes; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/JodaTimeConstants.java b/processor/src/main/java/org/mapstruct/ap/internal/util/JodaTimeConstants.java index ab94875c8..7c78d1f81 100755 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/JodaTimeConstants.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/JodaTimeConstants.java @@ -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"; } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl new file mode 100644 index 000000000..1ba64ea30 --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl @@ -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 ); + } +} \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl new file mode 100644 index 000000000..6ee91270e --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl @@ -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; +} \ No newline at end of file diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/JodaTimeTest.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/JodaTimeTest.java new file mode 100644 index 000000000..699e937f1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/JodaTimeTest.java @@ -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() ); + + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/DateTimeBean.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/DateTimeBean.java new file mode 100644 index 000000000..444b96774 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/DateTimeBean.java @@ -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; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/XmlGregorianCalendarBean.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/XmlGregorianCalendarBean.java new file mode 100644 index 000000000..3a8a7fd92 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/bean/XmlGregorianCalendarBean.java @@ -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; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/DateTimeToXmlGregorianCalendar.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/DateTimeToXmlGregorianCalendar.java new file mode 100644 index 000000000..f98640180 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/DateTimeToXmlGregorianCalendar.java @@ -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 ); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/XmlGregorianCalendarToDateTime.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/XmlGregorianCalendarToDateTime.java new file mode 100644 index 000000000..5b012ee6f --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/jodatime/mapper/XmlGregorianCalendarToDateTime.java @@ -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 ); +}