From a9e1438c1ecdb14dd5321a4deab630e6b2b21fc9 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Wed, 26 Feb 2014 23:19:50 +0100 Subject: [PATCH] #151 improving UT for #120 and fixing some missing imports --- .../builtin/StringToXmlGregorianCalendar.java | 2 + .../builtin/XmlGregorianCalendarToString.java | 13 ++++++ .../{ConversionTest.java => BuiltInTest.java} | 35 ++++++++++++++-- .../ap/test/builtin/IterableSource.java | 39 ++++++++++++++++++ .../builtin/IterableSourceTargetMapper.java | 37 +++++++++++++++++ .../ap/test/builtin/IterableTarget.java | 39 ++++++++++++++++++ .../mapstruct/ap/test/builtin/MapSource.java | 40 +++++++++++++++++++ .../test/builtin/MapSourceTargetMapper.java | 36 +++++++++++++++++ .../mapstruct/ap/test/builtin/MapTarget.java | 39 ++++++++++++++++++ 9 files changed, 277 insertions(+), 3 deletions(-) rename processor/src/test/java/org/mapstruct/ap/test/builtin/{ConversionTest.java => BuiltInTest.java} (74%) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/IterableSource.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/IterableSourceTargetMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/IterableTarget.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/MapSource.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/MapSourceTargetMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/MapTarget.java diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java index 3a800793b..9185e2746 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java @@ -21,6 +21,7 @@ package org.mapstruct.ap.model.source.builtin; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Date; import java.util.GregorianCalendar; import java.util.Set; import javax.xml.datatype.DatatypeConfigurationException; @@ -48,6 +49,7 @@ public class StringToXmlGregorianCalendar extends BuiltInMethod { this.parameter = new Parameter( "date", typeFactory.getType( String.class ) ); this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); this.importTypes = asSet( + typeFactory.getType( Date.class ), typeFactory.getType( GregorianCalendar.class ), typeFactory.getType( SimpleDateFormat.class ), typeFactory.getType( DateFormat.class ), diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java index 136c94c56..a40d2ab4b 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java @@ -18,12 +18,16 @@ */ package org.mapstruct.ap.model.source.builtin; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Set; import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; +import static org.mapstruct.ap.util.Collections.asSet; /** * @author Sjaak Derksen @@ -32,10 +36,19 @@ public class XmlGregorianCalendarToString extends BuiltInMethod { private final Parameter parameter; private final Type returnType; + private final Set importTypes; public XmlGregorianCalendarToString(TypeFactory typeFactory) { this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) ); this.returnType = typeFactory.getType( String.class ); + this.importTypes = asSet( + typeFactory.getType( Date.class ), + typeFactory.getType( SimpleDateFormat.class ) ); + } + + @Override + public Set getImportTypes() { + return importTypes; } @Override diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/ConversionTest.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/BuiltInTest.java similarity index 74% rename from processor/src/test/java/org/mapstruct/ap/test/builtin/ConversionTest.java rename to processor/src/test/java/org/mapstruct/ap/test/builtin/BuiltInTest.java index a70bdecc0..bcea6612a 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/builtin/ConversionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/BuiltInTest.java @@ -22,9 +22,11 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.HashMap; import java.util.List; import javax.xml.bind.JAXBElement; import javax.xml.datatype.DatatypeConfigurationException; @@ -37,11 +39,11 @@ import org.testng.annotations.Test; import static org.fest.assertions.Assertions.assertThat; -@WithClasses( { Source.class, Target.class, SourceTargetMapper.class } ) -public class ConversionTest extends MapperTestBase { +public class BuiltInTest extends MapperTestBase { @Test - public void shouldApplyConversions() throws ParseException, DatatypeConfigurationException { + @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" ) ); @@ -69,6 +71,33 @@ public class ConversionTest extends MapperTestBase { assertThat( target.getProp8().getTimeInMillis() ).isEqualTo( 920329200000L ); } + @Test + @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 ) } ) ); + + IterableTarget target = IterableSourceTargetMapper.INSTANCE.sourceToTarget( source ); + assertThat( target ).isNotNull(); + assertThat( target.getDates().size() ).isEqualTo( 1 ); + assertThat( target.getDates().get( 0 ) ).isEqualTo( "02.03.1999" ); + + } + + @Test + @WithClasses( { MapSource.class, MapTarget.class, MapSourceTargetMapper.class } ) + public void shouldApplyBuiltInOnMap() throws ParseException, DatatypeConfigurationException { + + MapSource source = new MapSource(); + source.setExample( new HashMap, XMLGregorianCalendar>() ); + source.getExample().put( createJaxb( "TEST" ), createXmlCal( 1999, 3, 2, 1 ) ); + + MapTarget target = MapSourceTargetMapper.INSTANCE.sourceToTarget( source ); + assertThat( target ).isNotNull(); + assertThat( target.getExample().get( "TEST" ) ).isEqualTo( "1999-03-02+00:01" ); + } + private JAXBElement createJaxb( String test ) { return new JAXBElement( new QName( "www.mapstruct.org", "test" ), String.class, test ); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableSource.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableSource.java new file mode 100644 index 000000000..bf0bf3dab --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableSource.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2014 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; + +import java.util.List; +import javax.xml.datatype.XMLGregorianCalendar; + +/** + * + * @author Sjaak Derksen + */ +public class IterableSource { + + private List dates; + + public List getDates() { + return dates; + } + + public void setDates( List dates ) { + this.dates = dates; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableSourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableSourceTargetMapper.java new file mode 100644 index 000000000..b8c932148 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableSourceTargetMapper.java @@ -0,0 +1,37 @@ +/** + * Copyright 2012-2014 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; + +import java.util.List; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.mapstruct.IterableMapping; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface IterableSourceTargetMapper { + + IterableSourceTargetMapper INSTANCE = Mappers.getMapper( IterableSourceTargetMapper.class ); + + IterableTarget sourceToTarget(IterableSource source); + + @IterableMapping(dateFormat = "dd.MM.yyyy") + List stringListToDateList(List dates); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableTarget.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableTarget.java new file mode 100644 index 000000000..4833c7f9f --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/IterableTarget.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2014 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; + +import java.util.List; + +/** + * + * @author Sjaak Derksen + */ +public class IterableTarget { + + private List dates; + + public List getDates() { + return dates; + } + + public void setDates( List dates ) { + this.dates = dates; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/MapSource.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/MapSource.java new file mode 100644 index 000000000..b48fb3bf2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/MapSource.java @@ -0,0 +1,40 @@ +/** + * Copyright 2012-2014 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; + +import java.util.Map; +import javax.xml.bind.JAXBElement; +import javax.xml.datatype.XMLGregorianCalendar; + +/** + * + * @author Sjaak Derksen + */ +public class MapSource { + + private Map, XMLGregorianCalendar> example; + + public Map, XMLGregorianCalendar> getExample() { + return example; + } + + public void setExample( Map, XMLGregorianCalendar> example ) { + this.example = example; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/MapSourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/MapSourceTargetMapper.java new file mode 100644 index 000000000..cee36da67 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/MapSourceTargetMapper.java @@ -0,0 +1,36 @@ +/** + * Copyright 2012-2014 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; + +import java.util.Map; +import javax.xml.bind.JAXBElement; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface MapSourceTargetMapper { + + MapSourceTargetMapper INSTANCE = Mappers.getMapper( MapSourceTargetMapper.class ); + + MapTarget sourceToTarget(MapSource source); + Map longDateMapToStringStringMap(Map, XMLGregorianCalendar> source); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/MapTarget.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/MapTarget.java new file mode 100644 index 000000000..042cf5efb --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/MapTarget.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2014 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; + +import java.util.Map; + +/** + * + * @author Sjaak Derksen + */ +public class MapTarget { + + private Map example; + + public Map getExample() { + return example; + } + + public void setExample( Map example ) { + this.example = example; + } + +}