From 760be8f4f82c1ee6ab76222f5ee98c6bddf05e13 Mon Sep 17 00:00:00 2001 From: Andreas Gudian Date: Wed, 12 Nov 2014 21:24:59 +0100 Subject: [PATCH] Add extract of the unit tests for generics and @TargetType support to an integration test --- .../org/mapstruct/itest/simple/BaseType.java | 27 +++++++ .../itest/simple/ReferencedCustomMapper.java | 57 +++++++++++++++ .../mapstruct/itest/simple/SomeOtherType.java | 70 +++++++++++++++++++ .../org/mapstruct/itest/simple/SomeType.java | 70 +++++++++++++++++++ .../org/mapstruct/itest/simple/Source.java | 9 +++ .../simple/SourceTargetAbstractMapper.java | 2 +- .../itest/simple/SourceTargetMapper.java | 2 +- .../org/mapstruct/itest/simple/Target.java | 11 +++ 8 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/BaseType.java create mode 100644 integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/ReferencedCustomMapper.java create mode 100644 integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SomeOtherType.java create mode 100644 integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SomeType.java diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/BaseType.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/BaseType.java new file mode 100644 index 000000000..48385d5a1 --- /dev/null +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/BaseType.java @@ -0,0 +1,27 @@ +/** + * 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.itest.simple; + +/** + * @author Andreas Gudian + * + */ +public class BaseType { + +} diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/ReferencedCustomMapper.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/ReferencedCustomMapper.java new file mode 100644 index 000000000..31e76fda2 --- /dev/null +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/ReferencedCustomMapper.java @@ -0,0 +1,57 @@ +/** + * 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.itest.simple; + +import java.util.Map; + +import org.mapstruct.TargetType; + +/** + * @author Andreas Gudian + * + */ +public class ReferencedCustomMapper { + public long incrementingIntToLong(int source) { + return source + 1; + } + + @SuppressWarnings( "unchecked" ) + public T convert(String string, @TargetType Class clazz) { + if ( clazz == SomeType.class ) { + return (T) new SomeType( string ); + } + else if ( clazz == SomeOtherType.class ) { + return (T) new SomeOtherType( string ); + } + + return null; + } + + public String toString(BaseType baseType) { + return String.valueOf( baseType ); + } + + /** + * This method should not be chosen for the mapping, as our types are never within the bounds of + * {@code T extends Map} + */ + public > T unused(String string, @TargetType Class clazz) { + throw new RuntimeException( "should never be called" ); + } +} diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SomeOtherType.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SomeOtherType.java new file mode 100644 index 000000000..85eb1315a --- /dev/null +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SomeOtherType.java @@ -0,0 +1,70 @@ +/** + * 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.itest.simple; + +/** + * @author Andreas Gudian + * + */ +public class SomeOtherType extends BaseType { + private String value; + + public SomeOtherType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ( ( value == null ) ? 0 : value.hashCode() ); + return result; + } + + @Override + public boolean equals(Object obj) { + if ( this == obj ) { + return true; + } + if ( obj == null ) { + return false; + } + if ( getClass() != obj.getClass() ) { + return false; + } + SomeOtherType other = (SomeOtherType) obj; + if ( value == null ) { + if ( other.value != null ) { + return false; + } + } + else if ( !value.equals( other.value ) ) { + return false; + } + return true; + } +} diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SomeType.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SomeType.java new file mode 100644 index 000000000..80d6c4d68 --- /dev/null +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SomeType.java @@ -0,0 +1,70 @@ +/** + * 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.itest.simple; + +/** + * @author Andreas Gudian + * + */ +public class SomeType extends BaseType { + private String value; + + public SomeType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ( ( value == null ) ? 0 : value.hashCode() ); + return result; + } + + @Override + public boolean equals(Object obj) { + if ( this == obj ) { + return true; + } + if ( obj == null ) { + return false; + } + if ( getClass() != obj.getClass() ) { + return false; + } + SomeType other = (SomeType) obj; + if ( value == null ) { + if ( other.value != null ) { + return false; + } + } + else if ( !value.equals( other.value ) ) { + return false; + } + return true; + } +} diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/Source.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/Source.java index 49680c89b..fd558bcfb 100644 --- a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/Source.java +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/Source.java @@ -25,6 +25,7 @@ public class Source { private int qax; private Long baz; private int zip; + private String someType; public int getFoo() { return foo; @@ -65,4 +66,12 @@ public class Source { public void setZip(int zip) { this.zip = zip; } + + public String getSomeType() { + return someType; + } + + public void setSomeType(String someType) { + this.someType = someType; + } } diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetAbstractMapper.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetAbstractMapper.java index a76f453af..c230b7c62 100644 --- a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetAbstractMapper.java +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetAbstractMapper.java @@ -23,7 +23,7 @@ import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; -@Mapper +@Mapper( uses = ReferencedCustomMapper.class ) public abstract class SourceTargetAbstractMapper { public static SourceTargetAbstractMapper INSTANCE = Mappers.getMapper( SourceTargetAbstractMapper.class ); diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetMapper.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetMapper.java index da433a54b..98fcc7408 100644 --- a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetMapper.java +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetMapper.java @@ -24,7 +24,7 @@ import org.mapstruct.Mappings; import org.mapstruct.InheritInverseConfiguration; import org.mapstruct.factory.Mappers; -@Mapper +@Mapper(uses = ReferencedCustomMapper.class) public interface SourceTargetMapper { SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/Target.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/Target.java index 5aaa6703c..6cec8469d 100644 --- a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/Target.java +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/Target.java @@ -18,6 +18,8 @@ */ package org.mapstruct.itest.simple; +import org.mapstruct.itest.simple.SomeType; + public class Target { private Long foo; @@ -25,6 +27,7 @@ public class Target { private Long baz; private int qax; private String zip; + private SomeType someType; public Long getFoo() { return foo; @@ -65,4 +68,12 @@ public class Target { public void setZip(String zip) { this.zip = zip; } + + public SomeType getSomeType() { + return someType; + } + + public void setSomeType(SomeType someType) { + this.someType = someType; + } }