Skip to content

Commit

Permalink
Fix issue related to mapping Arrays to Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
elaatifi committed Dec 27, 2012
1 parent ee99efa commit 8aed07f
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> strings;

List<Integer> integers;

public List<String> getStrings() {
return strings;
}

public void setStrings(List<String> strings) {
this.strings = strings;
}

public List<Integer> getIntegers() {
return integers;
}

public void setIntegers(List<Integer> 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());
}

}

0 comments on commit 8aed07f

Please sign in to comment.