From a7d56e51222a6b0fb0ee8a72e4f0445be9b1eaa6 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sat, 21 Feb 2015 18:55:29 +0100 Subject: [PATCH] #465 Adding import for Locale to Joda generated mappers if required --- .../AbstractJodaTypeToStringConversion.java | 30 +++++++++++++--- .../jodatime/JodaConversionTest.java | 19 +++++++++++ .../jodatime/SourceWithStringDate.java | 32 +++++++++++++++++ .../jodatime/StringToLocalDateMapper.java | 30 ++++++++++++++++ .../jodatime/TargetWithLocalDate.java | 34 +++++++++++++++++++ 5 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/SourceWithStringDate.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/StringToLocalDateMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/TargetWithLocalDate.java diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/AbstractJodaTypeToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/AbstractJodaTypeToStringConversion.java index 0372c99f5..4c0a31b1d 100755 --- a/processor/src/main/java/org/mapstruct/ap/conversion/AbstractJodaTypeToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/AbstractJodaTypeToStringConversion.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.conversion; +import java.util.Collections; import java.util.Locale; import java.util.Set; @@ -42,10 +43,18 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio @Override protected Set getToConversionImportTypes(ConversionContext conversionContext) { - return asSet( - conversionContext.getTypeFactory().getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ), - conversionContext.getTypeFactory().getType( Locale.class ) - ); + if ( conversionContext.getDateFormat() != null ) { + return Collections.singleton( + conversionContext.getTypeFactory() + .getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ) + ); + } + else { + return asSet( + conversionContext.getTypeFactory().getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ), + conversionContext.getTypeFactory().getType( Locale.class ) + ); + } } @Override @@ -55,7 +64,18 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio @Override protected Set getFromConversionImportTypes(ConversionContext conversionContext) { - return asSet( conversionContext.getTypeFactory().getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ) ); + if ( conversionContext.getDateFormat() != null ) { + return Collections.singleton( + conversionContext.getTypeFactory() + .getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ) + ); + } + else { + return asSet( + conversionContext.getTypeFactory().getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ), + conversionContext.getTypeFactory().getType( Locale.class ) + ); + } } private String conversionString(ConversionContext conversionContext, String method) { diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/JodaConversionTest.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/JodaConversionTest.java index d4d95e52c..272334227 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/JodaConversionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/JodaConversionTest.java @@ -19,6 +19,7 @@ package org.mapstruct.ap.test.conversion.jodatime; import java.util.Calendar; +import java.util.Locale; import java.util.TimeZone; import org.joda.time.DateTime; @@ -26,6 +27,7 @@ import org.joda.time.DateTimeZone; import org.joda.time.LocalDate; import org.joda.time.LocalDateTime; import org.joda.time.LocalTime; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.testutil.IssueKey; @@ -44,6 +46,11 @@ import static org.fest.assertions.Assertions.assertThat; @IssueKey("75") public class JodaConversionTest { + @Before + public void setDefaultLocale() { + Locale.setDefault( Locale.GERMAN ); + } + @Test public void testDateTimeToString() { Source src = new Source(); @@ -202,4 +209,16 @@ public class JodaConversionTest { assertThat( mappedSource ).isNotNull(); assertThat( mappedSource.getDateTimeForCalendarConversion() ).isEqualTo( dateTimeWithCalendar ); } + + @Test + @WithClasses({ StringToLocalDateMapper.class, SourceWithStringDate.class, TargetWithLocalDate.class }) + @IssueKey("456") + public void testStringToLocalDateUsingDefaultFormat() { + SourceWithStringDate source = new SourceWithStringDate(); + source.setDate( "19. November 2014" ); + + TargetWithLocalDate target = StringToLocalDateMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target.getDate() ).isEqualTo( new LocalDate( 2014, 11, 19 ) ); + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/SourceWithStringDate.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/SourceWithStringDate.java new file mode 100644 index 000000000..d1ce4ce7f --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/SourceWithStringDate.java @@ -0,0 +1,32 @@ +/** + * Copyright 2012-2015 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.conversion.jodatime; + +public class SourceWithStringDate { + + private String date; + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/StringToLocalDateMapper.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/StringToLocalDateMapper.java new file mode 100644 index 000000000..03395ea13 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/StringToLocalDateMapper.java @@ -0,0 +1,30 @@ +/** + * Copyright 2012-2015 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.conversion.jodatime; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface StringToLocalDateMapper { + + StringToLocalDateMapper INSTANCE = Mappers.getMapper( StringToLocalDateMapper.class ); + + TargetWithLocalDate sourceToTarget(SourceWithStringDate source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/TargetWithLocalDate.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/TargetWithLocalDate.java new file mode 100644 index 000000000..dcd69c241 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/jodatime/TargetWithLocalDate.java @@ -0,0 +1,34 @@ +/** + * Copyright 2012-2015 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.conversion.jodatime; + +import org.joda.time.LocalDate; + +public class TargetWithLocalDate { + + private LocalDate date; + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } +}