#465 Adding import for Locale to Joda generated mappers if required

This commit is contained in:
Gunnar Morling 2015-02-21 18:55:29 +01:00
parent fe10248416
commit a7d56e5122
5 changed files with 140 additions and 5 deletions

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.conversion;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
@ -42,11 +43,19 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
@Override
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
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
protected String getFromExpression(ConversionContext conversionContext) {
@ -55,7 +64,18 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
@Override
protected Set<Type> 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) {

View File

@ -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 ) );
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;
}
}