#151 improving UT for #120 and fixing some missing imports

This commit is contained in:
sjaakd 2014-02-26 23:19:50 +01:00 committed by Gunnar Morling
parent f8aa758ecf
commit a9e1438c1e
9 changed files with 277 additions and 3 deletions

View File

@ -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 ),

View File

@ -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<Type> 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<Type> getImportTypes() {
return importTypes;
}
@Override

View File

@ -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<JAXBElement<String>, 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<String> createJaxb( String test ) {
return new JAXBElement<String>( new QName( "www.mapstruct.org", "test" ), String.class, test );
}

View File

@ -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<XMLGregorianCalendar> dates;
public List<XMLGregorianCalendar> getDates() {
return dates;
}
public void setDates( List<XMLGregorianCalendar> dates ) {
this.dates = dates;
}
}

View File

@ -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<String> stringListToDateList(List<XMLGregorianCalendar> dates);
}

View File

@ -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<String> dates;
public List<String> getDates() {
return dates;
}
public void setDates( List<String> dates ) {
this.dates = dates;
}
}

View File

@ -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<JAXBElement<String>, XMLGregorianCalendar> example;
public Map<JAXBElement<String>, XMLGregorianCalendar> getExample() {
return example;
}
public void setExample( Map<JAXBElement<String>, XMLGregorianCalendar> example ) {
this.example = example;
}
}

View File

@ -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<String, String> longDateMapToStringStringMap(Map<JAXBElement<String>, XMLGregorianCalendar> source);
}

View File

@ -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<String, String> example;
public Map<String, String> getExample() {
return example;
}
public void setExample( Map<String, String> example ) {
this.example = example;
}
}