Skip to content

Commit

Permalink
Merge pull request #200 from dmmiller612/199-Add-Array-To-LiveGraphFa…
Browse files Browse the repository at this point in the history
…ctory

Issue #199 added Object array to LiveGraphFactory to stop throwing ex…
  • Loading branch information
bartoszwalacik committed Sep 20, 2015
2 parents 784ec21 + c8e1b48 commit 03fb3a5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.javers.core.metamodel.object.Cdo;
import org.javers.core.metamodel.type.TypeMapper;

import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -44,6 +45,9 @@ private Object wrapTopLevelContainer(Object handle){
if (handle instanceof Set){
return new SetWrapper((Set)handle);
}
if (handle.getClass().isArray()){
return new ArrayWrapper(convertToObjectArray(handle));
}
return handle;
}

Expand All @@ -59,6 +63,10 @@ public static Class getListWrapperType(){
return ListWrapper.class;
}

public static Class getArrayWrapperType() {
return ArrayWrapper.class;
}

private class MapWrapper{
private final Map<Object,Object> map;

Expand All @@ -83,5 +91,26 @@ public ListWrapper(List list) {
}
}

private class ArrayWrapper {
private final Object[] array;

public ArrayWrapper(Object[] objects) {
this.array = objects;
}
}

//this is primarily used for copying array of primitives to array of objects
//as there seems be no legal way for casting
private static Object[] convertToObjectArray(Object obj) {
if (obj instanceof Object[]) {
return (Object[]) obj;
}
int arrayLength = Array.getLength(obj);
Object[] retArray = new Object[arrayLength];
for (int i = 0; i < arrayLength; ++i){
retArray[i] = Array.get(obj, i);
}
return retArray;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public static UnboundedValueObjectIdDTO unboundedListId(){
return new UnboundedValueObjectIdDTO(LiveGraphFactory.getListWrapperType());
}

public static UnboundedValueObjectIdDTO unboundedArrayId(){
return new UnboundedValueObjectIdDTO(LiveGraphFactory.getArrayWrapperType());
}

public static UnboundedValueObjectIdDTO unboundedValueObjectId(Class valueObjectClass) {
return new UnboundedValueObjectIdDTO(valueObjectClass);
}
Expand All @@ -42,4 +46,4 @@ public String value() {
public UnboundedValueObjectId create(TypeMapper typeMapper) {
return new UnboundedValueObjectId( typeMapper.getManagedClass(javaClass, ValueObject.class));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.javers.core.cases

import org.javers.core.JaversBuilder
import org.javers.core.diff.changetype.container.ArrayChange
import org.javers.core.diff.changetype.container.ContainerChange
import org.javers.core.diff.changetype.container.ListChange
import org.javers.core.diff.changetype.container.SetChange
import org.javers.core.diff.changetype.map.MapChange
Expand All @@ -15,7 +17,7 @@ import static org.javers.repository.jql.UnboundedValueObjectIdDTO.*
class TopLevelContainerTest extends Specification {

@Unroll
def "should compare top-level #colType(s)"() {
def "should compare top-level #containerType"() {
given:
def javers = JaversBuilder.javers().build();

Expand All @@ -24,14 +26,24 @@ class TopLevelContainerTest extends Specification {

then:
diff.changes.size() == 1
diff.changes[0].propertyName == colType
//println diff
with(diff.changes[0]) {
propertyName == pName
changes.size() == 1
}

where:
colType << ["map","list","set"]
expectedChangeType << [MapChange, ListChange, SetChange]
container1 << [ [a:1], [1], [1] as Set]
container2 << [ [a:1 , b:2], [1,2], [1,2] as Set]
pName << ["map","list","set", "array", "array"]
containerType << ["map","list","set", "primitive array", "object array"]
expectedChangeType << [MapChange, ListChange, SetChange, ArrayChange, ArrayChange]
container1 << [ [a:1], [1], [1] as Set, intArray([1,2]), ["a","b"].toArray()]
container2 << [ [a:1, b:2], [1,2], [1,2] as Set, intArray([1,2,3]), ["a","b","c"].toArray()]
}

int[] intArray(List values){
def ret = new int[values.size()]
values.eachWithIndex{ def entry, int i -> ret[i] = entry}
println ret
ret
}

@Unroll
Expand All @@ -43,17 +55,19 @@ class TopLevelContainerTest extends Specification {
javers.commit("author",container1)
javers.commit("author",container2)

def changes = javers.getChangeHistory(voId,2)

def changes = javers.getChangeHistory(voId,3)

then:
changes[0].propertyName == colType

where:
colType << ["map","list","set"]
expectedChangeType << [MapChange, ListChange, SetChange]
container1 << [ [a:1], [1], [1] as Set]
container2 << [ [a:1 , b:2], [1,2], [1,2] as Set]
voId << [unboundedMapId(), unboundedListId(), unboundedSetId()]
colType << ["map","list","set", "array"]
expectedChangeType << [MapChange, ListChange, SetChange, ArrayChange]
container1 << [ [a:1], [1], [1] as Set, [1, 2].toArray()]
container2 << [ [a:1 , b:2], [1,2], [1,2] as Set, [1].toArray()]
voId << [unboundedMapId(), unboundedListId(), unboundedSetId(), unboundedArrayId()]
}

}

0 comments on commit 03fb3a5

Please sign in to comment.