Last active
August 29, 2015 14:13
-
-
Save ctmay4/df042868def85a380ff8 to your computer and use it in GitHub Desktop.
Java object diff testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
import org.javers.core.Javers; | |
import org.javers.core.JaversBuilder; | |
import org.javers.core.MappingStyle; | |
import org.javers.core.changelog.SimpleTextChangeLog; | |
import org.javers.core.diff.Change; | |
import org.javers.core.metamodel.object.InstanceIdDTO; | |
import org.junit.Test; | |
import de.danielbechler.diff.ObjectDifferBuilder; | |
import de.danielbechler.diff.node.DiffNode; | |
import de.danielbechler.diff.node.DiffNode.Visitor; | |
import de.danielbechler.diff.node.Visit; | |
public class DiffTesting { | |
private class TestObject { | |
private Integer _id; | |
private String _name; | |
private List<String> _tags; | |
private List<List<String>> _table; | |
TestObject(Integer id) { | |
_id = id; | |
} | |
public Integer getId() { | |
return _id; | |
} | |
public void setId(Integer id) { | |
_id = id; | |
} | |
public String getName() { | |
return _name; | |
} | |
public void setName(String name) { | |
_name = name; | |
} | |
public List<List<String>> getTable() { | |
return _table; | |
} | |
public void setTable(List<List<String>> table) { | |
_table = table; | |
} | |
public List<String> getTags() { | |
return _tags; | |
} | |
public void setTags(List<String> tags) { | |
_tags = tags; | |
} | |
} | |
@Test | |
public void testJavers() { | |
Javers javers = JaversBuilder.javers().withMappingStyle(MappingStyle.FIELD).registerEntity(TestObject.class, "_id").build(); | |
TestObject base = new TestObject(1); | |
base.setName("A Name"); | |
base.setTable(Arrays.asList(Arrays.asList("A", "B", "C"), Arrays.asList("D", "E", "F"))); | |
javers.commit("[email protected]", base); | |
base.setName("A Different Name"); | |
base.setTags(Arrays.asList("tag1", "tag2")); | |
base.setTable(Arrays.asList(Arrays.asList("A", "B", "C"), Arrays.asList("X", "Y", "Z"))); | |
javers.commit("[email protected]", base); | |
// list out changes | |
List<Change> changes = javers.getChangeHistory(InstanceIdDTO.instanceId(1, TestObject.class), 5); | |
SimpleTextChangeLog log = new SimpleTextChangeLog(); | |
javers.processChangeList(changes, log); | |
System.out.println(log.result()); | |
} | |
@Test | |
public void testJavaObjectDiff() { | |
final TestObject base = new TestObject(1); | |
base.setName("A Name"); | |
base.setTable(Arrays.asList(Arrays.asList("A", "B", "C"), Arrays.asList("D", "E", "F"))); | |
final TestObject working = new TestObject(1); | |
working.setName("A Different Name"); | |
working.setTags(Arrays.asList("tag1", "tag2")); | |
working.setTable(Arrays.asList(Arrays.asList("A", "B", "C"), Arrays.asList("X", "Y", "Z"))); | |
DiffNode diff = ObjectDifferBuilder.buildDefault().compare(working, base); | |
diff.visit(new Visitor() { | |
@Override | |
public void node(DiffNode node, Visit visit) { | |
Object baseValue = node.canonicalGet(base); | |
Object workingValue = node.canonicalGet(working); | |
String message = node.getPath() + " changed from " + baseValue + " to " + workingValue; | |
System.out.println(message); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment