diff --git a/core/src/main/java/ma/glasnost/orika/impl/generator/specification/ArrayOrCollectionToCollection.java b/core/src/main/java/ma/glasnost/orika/impl/generator/specification/ArrayOrCollectionToCollection.java index 41550984..4939c99f 100644 --- a/core/src/main/java/ma/glasnost/orika/impl/generator/specification/ArrayOrCollectionToCollection.java +++ b/core/src/main/java/ma/glasnost/orika/impl/generator/specification/ArrayOrCollectionToCollection.java @@ -57,7 +57,7 @@ public String generateMappingCode(FieldMap fieldMap, VariableRef source, Variabl out.append(statement("%s.addAll(asList(%s));", newDest, s)); } else { out.append("\n"); - out.append(statement("%s.addAll(mapperFacade.mapAsList(asList(%s), %s.class, mappingContext));", newDest, s, d.typeName())); + out.append(statement("%s.addAll(mapperFacade.mapAsList(asList(%s), %s.class, mappingContext));", newDest, s, d.elementTypeName())); } } else { append(out, diff --git a/tests/src/main/java/ma/glasnost/orika/test/array/ArrayTestCase.java b/tests/src/main/java/ma/glasnost/orika/test/array/ArrayTestCase.java index b18ff72a..0369447a 100644 --- a/tests/src/main/java/ma/glasnost/orika/test/array/ArrayTestCase.java +++ b/tests/src/main/java/ma/glasnost/orika/test/array/ArrayTestCase.java @@ -52,6 +52,21 @@ public void testArrayToList() { } + @Test + public void testWrapperArrayToList() { + MapperFacade mapperFacade = MappingUtil.getMapperFactory().getMapperFacade(); + + ArrayTestCaseClasses.C source = new ArrayTestCaseClasses.C(); + Byte[] buffer = new Byte[]{1,2,3,4}; + source.setBuffer(buffer); + + + ArrayTestCaseClasses.D destination = mapperFacade.map(source, ArrayTestCaseClasses.D.class); + + Assert.assertEquals(Arrays.asList((byte)1,(byte)2,(byte)3,(byte)4), destination.getBuffer()); + + } + @Test public void testListToArray() { MapperFacade mapperFacade = MappingUtil.getMapperFactory().getMapperFacade(); diff --git a/tests/src/main/java/ma/glasnost/orika/test/community/IssueArrayToListTestCase.java b/tests/src/main/java/ma/glasnost/orika/test/community/IssueArrayToListTestCase.java new file mode 100755 index 00000000..c8e9dd21 --- /dev/null +++ b/tests/src/main/java/ma/glasnost/orika/test/community/IssueArrayToListTestCase.java @@ -0,0 +1,166 @@ +package ma.glasnost.orika.test.community; + + + +import static java.util.Arrays.asList; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.List; + +import ma.glasnost.orika.MapperFacade; +import ma.glasnost.orika.MapperFactory; +import ma.glasnost.orika.impl.DefaultMapperFactory; + +import org.junit.Test; + +public class IssueArrayToListTestCase { + + static { + //System.setProperty(OrikaSystemProperties.COMPILER_STRATEGY, EclipseJdtCompilerStrategy.class.getName()); + } + + public static class A { + String[] strings; + + int[] ints; + + Integer[] integers; + + public String[] getStrings() { + return strings; + } + + public void setStrings(String[] strings) { + this.strings = strings; + } + + public int[] getInts() { + return ints; + } + + public void setInts(int[] ints) { + this.ints = ints; + } + + public Integer[] getIntegers() { + return integers; + } + + public void setIntegers(Integer[] integers) { + this.integers = integers; + } + } + + public static class B { + List strings; + + List integers; + + public List getStrings() { + return strings; + } + + public void setStrings(List strings) { + this.strings = strings; + } + + public List getIntegers() { + return integers; + } + + public void setIntegers(List integers) { + this.integers = integers; + } + } + + @Test + public void testStringArrayToListOfString() { + MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); + + MapperFacade mapperFacade = mapperFactory.getMapperFacade(); + + A a = new A(); + a.setStrings(new String[] { "4" }); + + B b = mapperFacade.map(a, B.class); + assertEquals(asList("4"), b.getStrings()); + } + + @Test + public void testListOfStringToStringArray() { + MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); + + MapperFacade mapperFacade = mapperFactory.getMapperFacade(); + + B b = new B(); + b.setStrings(asList("5")); + + A a = mapperFacade.map(b, A.class); + assertArrayEquals(new String[] { "5" }, a.getStrings()); + } + + @Test + public void testIntArrayToListOfInteger() { + MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); + + mapperFactory.classMap(A.class, B.class).field("ints", "integers").byDefault().register(); + + MapperFacade mapperFacade = mapperFactory.getMapperFacade(); + + A a = new A(); + a.setInts(new int[] { 4 }); + + B b = mapperFacade.map(a, B.class); + assertNotNull(b.getIntegers()); + assertEquals(1, b.getIntegers().size()); + assertEquals(Integer.class, b.getIntegers().get(0).getClass()); + assertEquals(Integer.valueOf(4), b.getIntegers().get(0)); + } + + @Test + public void testListOfIntegerToIntArray() { + MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); + + mapperFactory.classMap(A.class, B.class).field("ints", "integers").byDefault().register(); + + MapperFacade mapperFacade = mapperFactory.getMapperFacade(); + + B b = new B(); + b.setIntegers(asList(Integer.valueOf(6))); + + A a = mapperFacade.map(b, A.class); + assertArrayEquals(new int[] { 6 }, a.getInts()); + } + + @Test + public void testIntegerArrayToListOfInteger() { + MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); + + MapperFacade mapperFacade = mapperFactory.getMapperFacade(); + + A a = new A(); + a.setIntegers(new Integer[] { 4 }); + + B b = mapperFacade.map(a, B.class); + assertNotNull(b.getIntegers()); + assertEquals(1, b.getIntegers().size()); + assertEquals(Integer.class, b.getIntegers().get(0).getClass()); + assertEquals(Integer.valueOf(4), b.getIntegers().get(0)); + } + + @Test + public void testListOfIntegerToIntegerArray() { + MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); + + MapperFacade mapperFacade = mapperFactory.getMapperFacade(); + + B b = new B(); + b.setIntegers(asList(Integer.valueOf(7))); + + A a = mapperFacade.map(b, A.class); + assertArrayEquals(new Integer[] { 7 }, a.getIntegers()); + } + +}