From 1d0b4c4e83dfddd24a8ff129e0f0ef09164057d7 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 24 Jul 2013 17:36:15 +0100 Subject: [PATCH 1/4] adding ability to compare by the result of a given method on a property. shown in MethodEqualExample. Had trouble getting spock and testng in my dev environment. will look further into this tomorrow to ensure not broken stuff and to get some coverage for this. --- .../danielbechler/diff/CollectionDiffer.java | 11 +++ .../de/danielbechler/diff/Configuration.java | 69 ++++++++++++++++++- .../java/de/danielbechler/diff/Instances.java | 14 ++++ .../java/de/danielbechler/diff/MapDiffer.java | 12 ++++ .../de/danielbechler/diff/NodeInspector.java | 3 + .../diff/accessor/AbstractAccessor.java | 13 ++++ .../danielbechler/diff/accessor/Accessor.java | 1 + .../diff/accessor/PropertyDescriptor.java | 5 ++ .../ObjectDiffMethodEqualsType.java | 15 ++++ .../diff/annotation/ObjectDiffProperty.java | 2 + .../diff/example/MethodEqualExample.java | 61 ++++++++++++++++ .../diff/introspect/StandardIntrospector.java | 1 + .../danielbechler/diff/node/DefaultNode.java | 9 +++ .../java/de/danielbechler/diff/node/Node.java | 4 ++ .../diff/path/MethodEqualClassAndMethod.java | 16 +++++ .../MethodEqualPropertyPathAndMethod.java | 20 ++++++ 16 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/danielbechler/diff/annotation/ObjectDiffMethodEqualsType.java create mode 100644 src/main/java/de/danielbechler/diff/example/MethodEqualExample.java create mode 100644 src/main/java/de/danielbechler/diff/path/MethodEqualClassAndMethod.java create mode 100644 src/main/java/de/danielbechler/diff/path/MethodEqualPropertyPathAndMethod.java diff --git a/src/main/java/de/danielbechler/diff/CollectionDiffer.java b/src/main/java/de/danielbechler/diff/CollectionDiffer.java index 18a8f325..f3c8acc8 100644 --- a/src/main/java/de/danielbechler/diff/CollectionDiffer.java +++ b/src/main/java/de/danielbechler/diff/CollectionDiffer.java @@ -61,6 +61,17 @@ else if (nodeInspector.isEqualsOnly(collectionNode)) collectionNode.setState(Node.State.CHANGED); } } + else if (nodeInspector.isWithMethodEquals(collectionNode)){ + String method = nodeInspector.getWithMethodEqualsMethod(collectionNode); + if (collectionInstances.areMethodResultEqual(method)) + { + collectionNode.setState(Node.State.UNTOUCHED); + } + else + { + collectionNode.setState(Node.State.CHANGED); + } + } else if (collectionInstances.hasBeenAdded()) { compareItems(collectionNode, collectionInstances, collectionInstances.getWorking(Collection.class)); diff --git a/src/main/java/de/danielbechler/diff/Configuration.java b/src/main/java/de/danielbechler/diff/Configuration.java index 5396a2ea..1ca9cdcd 100644 --- a/src/main/java/de/danielbechler/diff/Configuration.java +++ b/src/main/java/de/danielbechler/diff/Configuration.java @@ -20,6 +20,7 @@ import de.danielbechler.diff.node.*; import de.danielbechler.diff.path.*; import de.danielbechler.util.*; +import groovy.lang.Tuple; import java.util.*; @@ -77,8 +78,10 @@ public enum PrimitiveDefaultValueMode private final Collection includedProperties = new HashSet(10); private final Collection excludedProperties = new HashSet(10); private final Collection equalsOnlyProperties = new LinkedHashSet(10); + private final Collection methodEqualProperties = new LinkedHashSet(10); private final Collection> compareToOnlyTypes = new LinkedHashSet>(10); private final Collection> equalsOnlyTypes = new LinkedHashSet>(10); + private final Collection methodEqualTypes = new LinkedHashSet(10); private boolean returnUnchangedNodes = false; private boolean returnIgnoredNodes = false; private boolean returnCircularNodes = true; @@ -124,7 +127,7 @@ public Configuration withoutProperty(final PropertyPath propertyPath) this.excludedProperties.add(propertyPath); return this; } - + public Configuration withCompareToOnlyType(final Class type) { this.compareToOnlyTypes.add(type); @@ -143,12 +146,22 @@ public Configuration withEqualsOnlyProperty(final PropertyPath propertyPath) return this; } + public Configuration withMethodEqualsProperty(final PropertyPath propertyPath, final String methodName) { + this.methodEqualProperties.add(new MethodEqualPropertyPathAndMethod(propertyPath, methodName)); + return this; + } + + public Configuration withMethodEqualsProperty(MethodEqualPropertyPathAndMethod propertyPathEqualsMethod) { + this.methodEqualProperties.add(propertyPathEqualsMethod); + return this; + } + public Configuration withIgnoredNodes() { this.returnIgnoredNodes = true; return this; } - + public Configuration withoutIgnoredNodes() { this.returnIgnoredNodes = false; @@ -309,6 +322,57 @@ public boolean isEqualsOnly(final Node node) } return false; } + + public boolean isWithMethodEquals(Node node){ + return getWithMethodEqualsMethod(node) != null; + } + + public String getWithMethodEqualsMethod(Node node){ + final Class propertyType = node.getType(); + if (propertyType != null) + { + ObjectDiffMethodEqualsType annotation = propertyType.getAnnotation(ObjectDiffMethodEqualsType.class); + if (annotation != null) + { + return annotation.method(); + } + + MethodEqualClassAndMethod applicable = findMethodEqualPropertyForClass(propertyType); + if (applicable != null) + { + return applicable.getMethod(); + } + } + if (node.isWithMethodEquals()) + { + return node.getWithMethodEqualsMethod(); + } + MethodEqualPropertyPathAndMethod applicable = findMethodEqualPropertyForPath(node.getPropertyPath()); + if (applicable != null) + { + return applicable.getMethod(); + } + return null; + } + + private MethodEqualClassAndMethod findMethodEqualPropertyForClass(Class clazz){ + for(MethodEqualClassAndMethod propertyPathEqualsMethod: methodEqualTypes){ + if(clazz.equals(propertyPathEqualsMethod.getClazz())){ + return propertyPathEqualsMethod; + } + } + return null; + + } + + private MethodEqualPropertyPathAndMethod findMethodEqualPropertyForPath(PropertyPath propertyPath){ + for(MethodEqualPropertyPathAndMethod propertyPathEqualsMethod: methodEqualProperties){ + if(propertyPath.equals(propertyPathEqualsMethod.getPropertyPath())){ + return propertyPathEqualsMethod; + } + } + return null; + } public boolean isReturnable(final Node node) { @@ -347,4 +411,5 @@ else if (node.isRemoved()) } return true; } + } diff --git a/src/main/java/de/danielbechler/diff/Instances.java b/src/main/java/de/danielbechler/diff/Instances.java index ee255b05..e6c30644 100644 --- a/src/main/java/de/danielbechler/diff/Instances.java +++ b/src/main/java/de/danielbechler/diff/Instances.java @@ -172,6 +172,19 @@ public boolean areEqual() { return isEqual(base, working); } + + public boolean areMethodResultEqual(String method) { + try { + Object baseMethodResult = base.getClass().getMethod(method).invoke(base); + Object workingMethodResult = working.getClass().getMethod(method).invoke(working); + if(baseMethodResult == null){ + return workingMethodResult == null; + } + return baseMethodResult.equals(workingMethodResult); + } catch (Exception e) { + throw new RuntimeException(e); + } + } public boolean areEqualByComparison() { @@ -281,4 +294,5 @@ public PropertyPath getPropertyPath(final Node parentNode) return PropertyPath.createBuilder().withRoot().build(); } } + } diff --git a/src/main/java/de/danielbechler/diff/MapDiffer.java b/src/main/java/de/danielbechler/diff/MapDiffer.java index 29398727..189dc141 100644 --- a/src/main/java/de/danielbechler/diff/MapDiffer.java +++ b/src/main/java/de/danielbechler/diff/MapDiffer.java @@ -21,6 +21,7 @@ import de.danielbechler.util.*; import de.danielbechler.util.Collections; +import java.lang.reflect.InvocationTargetException; import java.util.*; /** @@ -61,6 +62,17 @@ else if (nodeInspector.isEqualsOnly(mapNode)) mapNode.setState(Node.State.CHANGED); } } + else if (nodeInspector.isWithMethodEquals(mapNode)){ + String method = nodeInspector.getWithMethodEqualsMethod(mapNode); + if (instances.areMethodResultEqual(method)) + { + mapNode.setState(Node.State.UNTOUCHED); + } + else + { + mapNode.setState(Node.State.CHANGED); + } + } else if (instances.hasBeenAdded()) { compareEntries(mapNode, instances, instances.getWorking(Map.class).keySet()); diff --git a/src/main/java/de/danielbechler/diff/NodeInspector.java b/src/main/java/de/danielbechler/diff/NodeInspector.java index 9744eb4f..828da7f7 100644 --- a/src/main/java/de/danielbechler/diff/NodeInspector.java +++ b/src/main/java/de/danielbechler/diff/NodeInspector.java @@ -30,6 +30,9 @@ interface NodeInspector boolean isCompareToOnly(Node node); boolean isEqualsOnly(Node node); + + boolean isWithMethodEquals(Node node); + String getWithMethodEqualsMethod(Node node); boolean isReturnable(Node node); diff --git a/src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java b/src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java index 7262c2bb..7d08a6de 100644 --- a/src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java +++ b/src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java @@ -24,6 +24,7 @@ public abstract class AbstractAccessor implements Accessor private Set categories = new TreeSet(); private boolean equalsOnly; private boolean ignored; + private String withMethodEqualsMethod; public final Set getCategories() { @@ -54,5 +55,17 @@ public void setIgnored(final boolean ignored) { this.ignored = ignored; } + + public boolean isWithMethodEquals(){ + return this.withMethodEqualsMethod != null && !this.withMethodEqualsMethod.equals(""); + } + + public void setWithMethodEqualsMethod(String withMethodEqualsMethod) { + this.withMethodEqualsMethod = withMethodEqualsMethod; + } + + public String getWithMethodEqualsMethod(){ + return withMethodEqualsMethod; + } } diff --git a/src/main/java/de/danielbechler/diff/accessor/Accessor.java b/src/main/java/de/danielbechler/diff/accessor/Accessor.java index b5807692..7d02381d 100644 --- a/src/main/java/de/danielbechler/diff/accessor/Accessor.java +++ b/src/main/java/de/danielbechler/diff/accessor/Accessor.java @@ -24,4 +24,5 @@ public interface Accessor extends PropertyDescriptor void set(Object target, Object value); void unset(Object target); + } diff --git a/src/main/java/de/danielbechler/diff/accessor/PropertyDescriptor.java b/src/main/java/de/danielbechler/diff/accessor/PropertyDescriptor.java index 50cc094d..d4789e3b 100644 --- a/src/main/java/de/danielbechler/diff/accessor/PropertyDescriptor.java +++ b/src/main/java/de/danielbechler/diff/accessor/PropertyDescriptor.java @@ -30,4 +30,9 @@ public interface PropertyDescriptor boolean isIgnored(); boolean isEqualsOnly(); + + + boolean isWithMethodEquals(); + + String getWithMethodEqualsMethod(); } diff --git a/src/main/java/de/danielbechler/diff/annotation/ObjectDiffMethodEqualsType.java b/src/main/java/de/danielbechler/diff/annotation/ObjectDiffMethodEqualsType.java new file mode 100644 index 00000000..a0f62bb5 --- /dev/null +++ b/src/main/java/de/danielbechler/diff/annotation/ObjectDiffMethodEqualsType.java @@ -0,0 +1,15 @@ +package de.danielbechler.diff.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +@ObjectDiffAnnotation +public @interface ObjectDiffMethodEqualsType { + public String method(); +} diff --git a/src/main/java/de/danielbechler/diff/annotation/ObjectDiffProperty.java b/src/main/java/de/danielbechler/diff/annotation/ObjectDiffProperty.java index d592a19b..b2bbfc1d 100644 --- a/src/main/java/de/danielbechler/diff/annotation/ObjectDiffProperty.java +++ b/src/main/java/de/danielbechler/diff/annotation/ObjectDiffProperty.java @@ -51,4 +51,6 @@ * @return The categories for this property. */ public String[] categories() default {}; + + public String methodEqual() default ""; } diff --git a/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java b/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java new file mode 100644 index 00000000..a3083550 --- /dev/null +++ b/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java @@ -0,0 +1,61 @@ +package de.danielbechler.diff.example; + +import java.util.ArrayList; +import java.util.List; + +import de.danielbechler.diff.Configuration; +import de.danielbechler.diff.ObjectDifferFactory; +import de.danielbechler.diff.annotation.ObjectDiffProperty; +import de.danielbechler.diff.example.IgnoreExample.User; +import de.danielbechler.diff.node.Node; +import de.danielbechler.diff.path.PropertyPath; +import de.danielbechler.diff.visitor.PrintingVisitor; + +class MethodEqualExample { + private MethodEqualExample() + { + } + + public static void main(final String[] args) + { + List baseItems = new ArrayList(); + baseItems.add("baseitem"); + final EncompassingClass base = new EncompassingClass(baseItems); + List workingItems = new ArrayList(); + workingItems.add("workingitem"); + final EncompassingClass working = new EncompassingClass(workingItems); + + final Configuration configuration = new Configuration(); + + // (Option 1) Causes the ObjectDiffer to use the method "size" on the 'items' property of the root object + configuration.withMethodEqualsProperty(PropertyPath.buildWith("items"), "size"); + + final Node node = ObjectDifferFactory.getInstance(configuration).compare(working, base); + + node.visit(new PrintingVisitor(working, base)); + + // Output with ignore: + // Property at path '/' has not changed + // Output without ignore: + // Property at path '/items[workingitem]' has been added => [ workingitem ] + // Property at path '/items[baseitem]' with value [ baseitem ] has been removed + } + + public static class EncompassingClass + { + private final List items; + + public EncompassingClass(final List items) + { + this.items = items; + } + + /* (Option 2) This annotation causes the ObjectDiffer to always ignore this property */ + @ObjectDiffProperty(methodEqual = "size") + public List getItems() + { + return items; + } + } + +} diff --git a/src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java b/src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java index 2e958cc7..f6715e6c 100644 --- a/src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java +++ b/src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java @@ -99,6 +99,7 @@ private static void handleObjectDiffPropertyAnnotation(final Method readMethod, propertyAccessor.setEqualsOnly(annotation.equalsOnly()); propertyAccessor.setIgnored(annotation.ignore()); propertyAccessor.setCategories(Collections.setOf(annotation.categories())); + propertyAccessor.setWithMethodEqualsMethod(annotation.methodEqual()); } } diff --git a/src/main/java/de/danielbechler/diff/node/DefaultNode.java b/src/main/java/de/danielbechler/diff/node/DefaultNode.java index 024a3f41..5dd37c5a 100644 --- a/src/main/java/de/danielbechler/diff/node/DefaultNode.java +++ b/src/main/java/de/danielbechler/diff/node/DefaultNode.java @@ -317,6 +317,14 @@ public final boolean isIgnored() { return state == State.IGNORED || accessor.isIgnored(); } + + public boolean isWithMethodEquals() { + return accessor.isWithMethodEquals(); + } + + public String getWithMethodEqualsMethod() { + return accessor.getWithMethodEqualsMethod(); + } public final Set getCategories() { @@ -474,4 +482,5 @@ public void setCircleStartNode(final Node circleStartNode) { this.circleStartNode = circleStartNode; } + } diff --git a/src/main/java/de/danielbechler/diff/node/Node.java b/src/main/java/de/danielbechler/diff/node/Node.java index 90d0c6e1..a8822329 100644 --- a/src/main/java/de/danielbechler/diff/node/Node.java +++ b/src/main/java/de/danielbechler/diff/node/Node.java @@ -201,4 +201,8 @@ public enum State T getPropertyAnnotation(Class annotationClass); + boolean isWithMethodEquals(); + + String getWithMethodEqualsMethod(); + } diff --git a/src/main/java/de/danielbechler/diff/path/MethodEqualClassAndMethod.java b/src/main/java/de/danielbechler/diff/path/MethodEqualClassAndMethod.java new file mode 100644 index 00000000..2973bac3 --- /dev/null +++ b/src/main/java/de/danielbechler/diff/path/MethodEqualClassAndMethod.java @@ -0,0 +1,16 @@ +package de.danielbechler.diff.path; + +public class MethodEqualClassAndMethod { + private Class clazz; + private String method; + public MethodEqualClassAndMethod(Class clazz, String method){ + this.clazz = clazz; + this.method = method; + } + public Class getClazz() { + return clazz; + } + public String getMethod() { + return method; + } +} diff --git a/src/main/java/de/danielbechler/diff/path/MethodEqualPropertyPathAndMethod.java b/src/main/java/de/danielbechler/diff/path/MethodEqualPropertyPathAndMethod.java new file mode 100644 index 00000000..028b507b --- /dev/null +++ b/src/main/java/de/danielbechler/diff/path/MethodEqualPropertyPathAndMethod.java @@ -0,0 +1,20 @@ +package de.danielbechler.diff.path; + +public class MethodEqualPropertyPathAndMethod { + private PropertyPath propertyPath; + private String method; + + public MethodEqualPropertyPathAndMethod(){} + public MethodEqualPropertyPathAndMethod(PropertyPath propertyPath, String method){ + this.propertyPath = propertyPath; + this.method = method; + } + + public PropertyPath getPropertyPath() { + return propertyPath; + } + + public String getMethod() { + return method; + } +} From 2271bf37ce086bfb420692cee80dba1747ac2b59 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 25 Jul 2013 10:56:54 +0100 Subject: [PATCH 2/4] Signed-off-by: unknown --- .../de/danielbechler/diff/BeanDiffer.java | 11 ++ .../de/danielbechler/diff/Configuration.java | 20 ++-- .../diff/example/MethodEqualExample.java | 52 +++++---- ...lassAndMethod.java => ClassAndMethod.java} | 4 +- ...Method.java => PropertyPathAndMethod.java} | 6 +- .../danielbechler/diff/BeanDifferShould.java | 30 ++++++ .../diff/CollectionDifferShould.java | 22 ++++ .../de/danielbechler/diff/InstancesTest.java | 47 ++++++++ .../danielbechler/diff/MapDifferShould.java | 24 +++++ .../diff/ObjectDifferIntegrationTests.java | 100 +++++++++++++++++- .../ObjectWithMethodEqualsCollection.java | 19 ++++ .../diff/mock/ObjectWithMethodEqualsMap.java | 18 ++++ .../ObjectWithMethodEqualsNestedObject.java | 19 ++++ 13 files changed, 338 insertions(+), 34 deletions(-) rename src/main/java/de/danielbechler/diff/path/{MethodEqualClassAndMethod.java => ClassAndMethod.java} (68%) rename src/main/java/de/danielbechler/diff/path/{MethodEqualPropertyPathAndMethod.java => PropertyPathAndMethod.java} (61%) create mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsCollection.java create mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsMap.java create mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsNestedObject.java diff --git a/src/main/java/de/danielbechler/diff/BeanDiffer.java b/src/main/java/de/danielbechler/diff/BeanDiffer.java index 430e7289..8b8e0f7b 100644 --- a/src/main/java/de/danielbechler/diff/BeanDiffer.java +++ b/src/main/java/de/danielbechler/diff/BeanDiffer.java @@ -49,6 +49,17 @@ public final Node compare(final Node parentNode, final Instances instances) { beanNode.setState(Node.State.IGNORED); } + else if (nodeInspector.isWithMethodEquals(beanNode)){ + String method = nodeInspector.getWithMethodEqualsMethod(beanNode); + if (instances.areMethodResultEqual(method)) + { + beanNode.setState(Node.State.UNTOUCHED); + } + else + { + beanNode.setState(Node.State.CHANGED); + } + } else if (instances.areNull() || instances.areSame()) { beanNode.setState(Node.State.UNTOUCHED); diff --git a/src/main/java/de/danielbechler/diff/Configuration.java b/src/main/java/de/danielbechler/diff/Configuration.java index 1ca9cdcd..20588588 100644 --- a/src/main/java/de/danielbechler/diff/Configuration.java +++ b/src/main/java/de/danielbechler/diff/Configuration.java @@ -78,10 +78,10 @@ public enum PrimitiveDefaultValueMode private final Collection includedProperties = new HashSet(10); private final Collection excludedProperties = new HashSet(10); private final Collection equalsOnlyProperties = new LinkedHashSet(10); - private final Collection methodEqualProperties = new LinkedHashSet(10); + private final Collection methodEqualProperties = new LinkedHashSet(10); private final Collection> compareToOnlyTypes = new LinkedHashSet>(10); private final Collection> equalsOnlyTypes = new LinkedHashSet>(10); - private final Collection methodEqualTypes = new LinkedHashSet(10); + private final Collection methodEqualTypes = new LinkedHashSet(10); private boolean returnUnchangedNodes = false; private boolean returnIgnoredNodes = false; private boolean returnCircularNodes = true; @@ -147,11 +147,11 @@ public Configuration withEqualsOnlyProperty(final PropertyPath propertyPath) } public Configuration withMethodEqualsProperty(final PropertyPath propertyPath, final String methodName) { - this.methodEqualProperties.add(new MethodEqualPropertyPathAndMethod(propertyPath, methodName)); + this.methodEqualProperties.add(new PropertyPathAndMethod(propertyPath, methodName)); return this; } - public Configuration withMethodEqualsProperty(MethodEqualPropertyPathAndMethod propertyPathEqualsMethod) { + public Configuration withMethodEqualsProperty(PropertyPathAndMethod propertyPathEqualsMethod) { this.methodEqualProperties.add(propertyPathEqualsMethod); return this; } @@ -337,7 +337,7 @@ public String getWithMethodEqualsMethod(Node node){ return annotation.method(); } - MethodEqualClassAndMethod applicable = findMethodEqualPropertyForClass(propertyType); + ClassAndMethod applicable = findMethodEqualPropertyForClass(propertyType); if (applicable != null) { return applicable.getMethod(); @@ -347,7 +347,7 @@ public String getWithMethodEqualsMethod(Node node){ { return node.getWithMethodEqualsMethod(); } - MethodEqualPropertyPathAndMethod applicable = findMethodEqualPropertyForPath(node.getPropertyPath()); + PropertyPathAndMethod applicable = findMethodEqualPropertyForPath(node.getPropertyPath()); if (applicable != null) { return applicable.getMethod(); @@ -355,8 +355,8 @@ public String getWithMethodEqualsMethod(Node node){ return null; } - private MethodEqualClassAndMethod findMethodEqualPropertyForClass(Class clazz){ - for(MethodEqualClassAndMethod propertyPathEqualsMethod: methodEqualTypes){ + private ClassAndMethod findMethodEqualPropertyForClass(Class clazz){ + for(ClassAndMethod propertyPathEqualsMethod: methodEqualTypes){ if(clazz.equals(propertyPathEqualsMethod.getClazz())){ return propertyPathEqualsMethod; } @@ -365,8 +365,8 @@ private MethodEqualClassAndMethod findMethodEqualPropertyForClass(Class clazz } - private MethodEqualPropertyPathAndMethod findMethodEqualPropertyForPath(PropertyPath propertyPath){ - for(MethodEqualPropertyPathAndMethod propertyPathEqualsMethod: methodEqualProperties){ + private PropertyPathAndMethod findMethodEqualPropertyForPath(PropertyPath propertyPath){ + for(PropertyPathAndMethod propertyPathEqualsMethod: methodEqualProperties){ if(propertyPath.equals(propertyPathEqualsMethod.getPropertyPath())){ return propertyPathEqualsMethod; } diff --git a/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java b/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java index a3083550..0b15e64e 100644 --- a/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java +++ b/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java @@ -5,8 +5,8 @@ import de.danielbechler.diff.Configuration; import de.danielbechler.diff.ObjectDifferFactory; +import de.danielbechler.diff.annotation.ObjectDiffMethodEqualsType; import de.danielbechler.diff.annotation.ObjectDiffProperty; -import de.danielbechler.diff.example.IgnoreExample.User; import de.danielbechler.diff.node.Node; import de.danielbechler.diff.path.PropertyPath; import de.danielbechler.diff.visitor.PrintingVisitor; @@ -18,17 +18,15 @@ private MethodEqualExample() public static void main(final String[] args) { - List baseItems = new ArrayList(); - baseItems.add("baseitem"); - final EncompassingClass base = new EncompassingClass(baseItems); - List workingItems = new ArrayList(); - workingItems.add("workingitem"); - final EncompassingClass working = new EncompassingClass(workingItems); + PropertyClass prop = new PropertyClass("1", "2"); + final EncompassingClass base = new EncompassingClass(prop); + PropertyClass prop2 = new PropertyClass("1", "3"); + final EncompassingClass working = new EncompassingClass(prop2); final Configuration configuration = new Configuration(); - // (Option 1) Causes the ObjectDiffer to use the method "size" on the 'items' property of the root object - configuration.withMethodEqualsProperty(PropertyPath.buildWith("items"), "size"); + // (Option 1) Causes the ObjectDiffer to compare using the method "getProp1" on the 'prop' property of the root object + configuration.withMethodEqualsProperty(PropertyPath.buildWith("prop"), "getProp1"); final Node node = ObjectDifferFactory.getInstance(configuration).compare(working, base); @@ -37,24 +35,42 @@ public static void main(final String[] args) // Output with ignore: // Property at path '/' has not changed // Output without ignore: - // Property at path '/items[workingitem]' has been added => [ workingitem ] - // Property at path '/items[baseitem]' with value [ baseitem ] has been removed + // Property at path '/prop/prop2' has changed from [ 2 ] to [ 3 ] } public static class EncompassingClass { - private final List items; + private final PropertyClass prop; - public EncompassingClass(final List items) + public EncompassingClass(final PropertyClass prop) { - this.items = items; + this.prop = prop; } - /* (Option 2) This annotation causes the ObjectDiffer to always ignore this property */ - @ObjectDiffProperty(methodEqual = "size") - public List getItems() + /* (Option 2) This annotation causes the ObjectDiffer to use getProp1 method to compare */ + //@ObjectDiffProperty(methodEqual = "getProp1") + public PropertyClass getProp() { + return prop; + } + } + + /* (Option 3) This annotation causes the ObjectDiffer to use getProp1 method to compare */ + //@ObjectDiffMethodEqualsType(method="getProp1") + public static class PropertyClass + { + private String prop1; + private String prop2; + + public PropertyClass(String prop1, String prop2) { - return items; + this.prop1 = prop1; + this.prop2 = prop2; + } + public String getProp1() { + return prop1; + } + public String getProp2() { + return prop2; } } diff --git a/src/main/java/de/danielbechler/diff/path/MethodEqualClassAndMethod.java b/src/main/java/de/danielbechler/diff/path/ClassAndMethod.java similarity index 68% rename from src/main/java/de/danielbechler/diff/path/MethodEqualClassAndMethod.java rename to src/main/java/de/danielbechler/diff/path/ClassAndMethod.java index 2973bac3..7573f725 100644 --- a/src/main/java/de/danielbechler/diff/path/MethodEqualClassAndMethod.java +++ b/src/main/java/de/danielbechler/diff/path/ClassAndMethod.java @@ -1,9 +1,9 @@ package de.danielbechler.diff.path; -public class MethodEqualClassAndMethod { +public class ClassAndMethod { private Class clazz; private String method; - public MethodEqualClassAndMethod(Class clazz, String method){ + public ClassAndMethod(Class clazz, String method){ this.clazz = clazz; this.method = method; } diff --git a/src/main/java/de/danielbechler/diff/path/MethodEqualPropertyPathAndMethod.java b/src/main/java/de/danielbechler/diff/path/PropertyPathAndMethod.java similarity index 61% rename from src/main/java/de/danielbechler/diff/path/MethodEqualPropertyPathAndMethod.java rename to src/main/java/de/danielbechler/diff/path/PropertyPathAndMethod.java index 028b507b..894693b8 100644 --- a/src/main/java/de/danielbechler/diff/path/MethodEqualPropertyPathAndMethod.java +++ b/src/main/java/de/danielbechler/diff/path/PropertyPathAndMethod.java @@ -1,11 +1,11 @@ package de.danielbechler.diff.path; -public class MethodEqualPropertyPathAndMethod { +public class PropertyPathAndMethod { private PropertyPath propertyPath; private String method; - public MethodEqualPropertyPathAndMethod(){} - public MethodEqualPropertyPathAndMethod(PropertyPath propertyPath, String method){ + public PropertyPathAndMethod(){} + public PropertyPathAndMethod(PropertyPath propertyPath, String method){ this.propertyPath = propertyPath; this.method = method; } diff --git a/src/test/java/de/danielbechler/diff/BeanDifferShould.java b/src/test/java/de/danielbechler/diff/BeanDifferShould.java index 9aaeacdf..59a228ec 100644 --- a/src/test/java/de/danielbechler/diff/BeanDifferShould.java +++ b/src/test/java/de/danielbechler/diff/BeanDifferShould.java @@ -24,6 +24,8 @@ import org.mockito.Mock; import org.testng.annotations.*; +import static de.danielbechler.diff.node.Node.State.CHANGED; +import static de.danielbechler.diff.node.Node.State.UNTOUCHED; import static de.danielbechler.diff.node.NodeAssertions.assertThat; import static java.util.Arrays.*; import static org.hamcrest.MatcherAssert.assertThat; @@ -120,6 +122,34 @@ public void compare_bean_via_equals() assertThat(node).self().isUntouched(); } + + @Test + public void detect_no_change_when_comparing_using_with_method_equals_and_result_is_same() + { + final ObjectWithNestedObject working = new ObjectWithNestedObject("foo"); + working.setObject(new ObjectWithNestedObject("childid")); + final ObjectWithNestedObject base = new ObjectWithNestedObject("foo"); + base.setObject(new ObjectWithNestedObject("differentchildid")); + configuration.withMethodEqualsProperty(PropertyPath.buildRootPath(), "getId"); + + final Node node = differ.compare(Node.ROOT, Instances.of(working, base)); + + assertThat(node).self().isUntouched(); + } + + @Test + public void detect_change_when_comparing_using_with_method_equals_and_result_is_different() + { + final ObjectWithNestedObject working = new ObjectWithNestedObject("foo"); + working.setObject(new ObjectWithNestedObject("childid")); + final ObjectWithNestedObject base = new ObjectWithNestedObject("bar"); + base.setObject(new ObjectWithNestedObject("differentchildid")); + configuration.withMethodEqualsProperty(PropertyPath.buildRootPath(), "getId"); + + final Node node = differ.compare(Node.ROOT, Instances.of(working, base)); + + assertThat(node).self().hasChanges(); + } @Test public void compare_bean_via_introspection_and_delegate_comparison_of_properties() diff --git a/src/test/java/de/danielbechler/diff/CollectionDifferShould.java b/src/test/java/de/danielbechler/diff/CollectionDifferShould.java index 5c67395e..7283dea3 100644 --- a/src/test/java/de/danielbechler/diff/CollectionDifferShould.java +++ b/src/test/java/de/danielbechler/diff/CollectionDifferShould.java @@ -24,6 +24,8 @@ import java.util.*; +import static de.danielbechler.diff.node.Node.State.CHANGED; +import static de.danielbechler.diff.node.Node.State.UNTOUCHED; import static java.util.Arrays.*; import static java.util.Collections.*; import static org.mockito.Mockito.*; @@ -118,6 +120,26 @@ public void compare_only_via_equals_if_equals_only_is_enabled() compare(); verify(collectionNode).setState(Node.State.UNTOUCHED); } + + @Test + public void detect_no_change_when_comparing_using_with_method_equals_and_result_is_same() + { + when(nodeInspector.isWithMethodEquals(collectionNode)).thenReturn(true); + when(nodeInspector.getWithMethodEqualsMethod(collectionNode)).thenReturn("somemethod"); + when(instances.areMethodResultEqual("somemethod")).thenReturn(true); + compare(); + verify(collectionNode).setState(Node.State.UNTOUCHED); + } + + @Test + public void detect_change_when_comparing_using_with_method_equals_and_result_is_different() + { + when(nodeInspector.isWithMethodEquals(collectionNode)).thenReturn(true); + when(nodeInspector.getWithMethodEqualsMethod(collectionNode)).thenReturn("somemethod"); + when(instances.areMethodResultEqual("somemethod")).thenReturn(false); + compare(); + verify(collectionNode).setState(CHANGED); + } @Test public void detect_changes_if_equals_only_is_enabled() diff --git a/src/test/java/de/danielbechler/diff/InstancesTest.java b/src/test/java/de/danielbechler/diff/InstancesTest.java index ba685d8e..5073326d 100644 --- a/src/test/java/de/danielbechler/diff/InstancesTest.java +++ b/src/test/java/de/danielbechler/diff/InstancesTest.java @@ -17,6 +17,8 @@ package de.danielbechler.diff; import de.danielbechler.diff.accessor.*; +import de.danielbechler.diff.mock.ObjectWithString; + import org.testng.annotations.*; import java.lang.reflect.*; @@ -120,6 +122,51 @@ public void testIsPrimitiveTypeReturnsFalseForComplexType() assertThat(new Instances(RootAccessor.getInstance(), "1", "2", null).isPrimitiveType()).isFalse(); } + @Test + public void testMethodResultNotEqual() throws Exception + { + final Method readMethod = getClass().getDeclaredMethod("getTestValue"); + final PropertyAccessor accessor = new PropertyAccessor("testValue", readMethod, null); + + ObjectWithString working = new ObjectWithString("string1"); + ObjectWithString base = new ObjectWithString("string2"); + + final Instances instances = new Instances(accessor, working, base, null); + assertThat(instances.areMethodResultEqual("getValue")).isFalse(); + } + + @Test + public void testMethodResultEqual() throws Exception + { + final Method readMethod = getClass().getDeclaredMethod("getTestValue"); + final PropertyAccessor accessor = new PropertyAccessor("testValue", readMethod, null); + + ObjectWithString working = new ObjectWithString("string"); + ObjectWithString base = new ObjectWithString("string"); + + final Instances instances = new Instances(accessor, working, base, null); + assertThat(instances.areMethodResultEqual("getValue")).isTrue(); + } + + @Test + public void testMethodResultEqualInvalidMethod() throws Exception + { + final Method readMethod = getClass().getDeclaredMethod("getTestValue"); + final PropertyAccessor accessor = new PropertyAccessor("testValue", readMethod, null); + + ObjectWithString working = new ObjectWithString("string"); + ObjectWithString base = new ObjectWithString("string"); + + final Instances instances = new Instances(accessor, working, base, null); + try { + instances.areMethodResultEqual("invalid"); + fail("no exception thrown"); + } + catch(RuntimeException e){ + assertThat(e.getCause() instanceof NoSuchMethodException).isTrue(); + } + } + @SuppressWarnings({"MethodMayBeStatic", "UnusedDeclaration"}) public long getTestValue() { diff --git a/src/test/java/de/danielbechler/diff/MapDifferShould.java b/src/test/java/de/danielbechler/diff/MapDifferShould.java index 92848556..2aee41a6 100644 --- a/src/test/java/de/danielbechler/diff/MapDifferShould.java +++ b/src/test/java/de/danielbechler/diff/MapDifferShould.java @@ -90,6 +90,30 @@ public void detect_no_change_when_comparing_using_equals() verify(node).setState(UNTOUCHED); } + @Test + public void detect_no_change_when_comparing_using_with_method_equals_and_result_is_same() + { + when(nodeInspector.isWithMethodEquals(internalNode)).thenReturn(true); + when(nodeInspector.getWithMethodEqualsMethod(internalNode)).thenReturn("somemethod"); + when(instances.areMethodResultEqual("somemethod")).thenReturn(true); + + node = compare(working, base); + + verify(node).setState(UNTOUCHED); + } + + @Test + public void detect_change_when_comparing_using_with_method_equals_and_result_is_different() + { + when(nodeInspector.isWithMethodEquals(internalNode)).thenReturn(true); + when(nodeInspector.getWithMethodEqualsMethod(internalNode)).thenReturn("somemethod"); + when(instances.areMethodResultEqual("somemethod")).thenReturn(false); + + node = compare(working, base); + + verify(node).setState(CHANGED); + } + @Test public void detect_addition() { diff --git a/src/test/java/de/danielbechler/diff/ObjectDifferIntegrationTests.java b/src/test/java/de/danielbechler/diff/ObjectDifferIntegrationTests.java index 3e6b596b..e50b1d6d 100644 --- a/src/test/java/de/danielbechler/diff/ObjectDifferIntegrationTests.java +++ b/src/test/java/de/danielbechler/diff/ObjectDifferIntegrationTests.java @@ -63,7 +63,7 @@ public void testCompareCollectionWithIgnoredCollectionProperty() NodeAssertions.assertThat(node).self().hasState(Node.State.UNTOUCHED); NodeAssertions.assertThat(node).self().hasNoChildren(); } - + public void testCompareCollectionWithAddedItem() throws Exception { final Collection working = new LinkedList(asList("foo")); @@ -320,4 +320,102 @@ public void testCompareWithListContainingObjectOnceDetectsIfAnotherInstanceOfItG .withCollectionItem(new ObjectWithHashCodeAndEquals("foo")) .build()).hasState(Node.State.ADDED); } + + public void testCompareBeanWithEqualsMethodOnCollectionPropertyNoChangeInMethodResult() + { + List forWorking = new ArrayList(); + forWorking.add("one"); + List forBase = new ArrayList(); + forBase.add("uno"); + final ObjectWithMethodEqualsCollection working = new ObjectWithMethodEqualsCollection(forWorking); + final ObjectWithMethodEqualsCollection base = new ObjectWithMethodEqualsCollection(forBase); + + final Node node = objectDiffer.compare(working, base); + + NodeAssertions.assertThat(node).self().hasState(Node.State.UNTOUCHED); + NodeAssertions.assertThat(node).self().hasNoChildren(); + } + + public void testCompareBeanWithEqualsMethodOnCollectionPropertyWithChangeInMethodResult() + { + List forWorking = new ArrayList(); + forWorking.add("one"); + forWorking.add("two"); + List forBase = new ArrayList(); + forBase.add("uno"); + final ObjectWithMethodEqualsCollection working = new ObjectWithMethodEqualsCollection(forWorking); + final ObjectWithMethodEqualsCollection base = new ObjectWithMethodEqualsCollection(forBase); + + final Node node = objectDiffer.compare(working, base); + + NodeAssertions.assertThat(node).self().hasState(Node.State.CHANGED); + assertThat(node) + .child(PropertyPath.buildWith("collection")) + .hasState(Node.State.CHANGED); + } + + public void testCompareBeanWithEqualsMethodOnObjectPropertyNoChangeInMethodResult() + { + ObjectWithNestedObject forWorking = new ObjectWithNestedObject("childid"); + forWorking.setObject(new ObjectWithNestedObject("grandchildid")); + ObjectWithNestedObject forBase = new ObjectWithNestedObject("childid"); + forBase.setObject(new ObjectWithNestedObject("differentgrandchildid")); + final ObjectWithMethodEqualsNestedObject working = new ObjectWithMethodEqualsNestedObject("id", forWorking); + final ObjectWithMethodEqualsNestedObject base = new ObjectWithMethodEqualsNestedObject("id", forBase); + + final Node node = objectDiffer.compare(working, base); + + NodeAssertions.assertThat(node).self().hasState(Node.State.UNTOUCHED); + NodeAssertions.assertThat(node).self().hasNoChildren(); + } + + public void testCompareBeanWithEqualsMethodOnObjectPropertyWithChangeInMethodResult() + { + ObjectWithNestedObject forWorking = new ObjectWithNestedObject("childid"); + forWorking.setObject(new ObjectWithNestedObject("grandchildid")); + ObjectWithNestedObject forBase = new ObjectWithNestedObject("differentchildid"); + forBase.setObject(new ObjectWithNestedObject("differentgrandchildid")); + final ObjectWithMethodEqualsNestedObject working = new ObjectWithMethodEqualsNestedObject("id", forWorking); + final ObjectWithMethodEqualsNestedObject base = new ObjectWithMethodEqualsNestedObject("id", forBase); + + final Node node = objectDiffer.compare(working, base); + + NodeAssertions.assertThat(node).self().hasState(Node.State.CHANGED); + assertThat(node) + .child(PropertyPath.buildWith("object")) + .hasState(Node.State.CHANGED); + } + + public void testCompareBeanWithEqualsMethodOnMapPropertyNoChangeInMethodResult() + { + Map forWorking = new HashMap(); + forWorking.put("key1", "val1"); + Map forBase = new HashMap(); + forBase.put("keyone", "valone"); + final ObjectWithMethodEqualsMap working = new ObjectWithMethodEqualsMap(forWorking); + final ObjectWithMethodEqualsMap base = new ObjectWithMethodEqualsMap(forBase); + + final Node node = objectDiffer.compare(working, base); + + NodeAssertions.assertThat(node).self().hasState(Node.State.UNTOUCHED); + NodeAssertions.assertThat(node).self().hasNoChildren(); + } + + public void testCompareBeanWithEqualsMethodOnMapPropertyWithChangeInMethodResult() + { + Map forWorking = new HashMap(); + forWorking.put("key1", "val1"); + forWorking.put("key2", "val2"); + Map forBase = new HashMap(); + forBase.put("keyone", "valone"); + final ObjectWithMethodEqualsMap working = new ObjectWithMethodEqualsMap(forWorking); + final ObjectWithMethodEqualsMap base = new ObjectWithMethodEqualsMap(forBase); + + final Node node = objectDiffer.compare(working, base); + + NodeAssertions.assertThat(node).self().hasState(Node.State.CHANGED); + assertThat(node) + .child(PropertyPath.buildWith("map")) + .hasState(Node.State.CHANGED); + } } diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsCollection.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsCollection.java new file mode 100644 index 00000000..77514006 --- /dev/null +++ b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsCollection.java @@ -0,0 +1,19 @@ +package de.danielbechler.diff.mock; + +import java.util.Collection; + +import de.danielbechler.diff.annotation.ObjectDiffProperty; + +public class ObjectWithMethodEqualsCollection extends ObjectWithCollection { + + public ObjectWithMethodEqualsCollection(Collection collection){ + super(collection); + } + + @ObjectDiffProperty(methodEqual = "size") + @Override + public Collection getCollection() + { + return super.getCollection(); + } +} diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsMap.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsMap.java new file mode 100644 index 00000000..a9f87673 --- /dev/null +++ b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsMap.java @@ -0,0 +1,18 @@ +package de.danielbechler.diff.mock; + +import java.util.Map; + +import de.danielbechler.diff.annotation.ObjectDiffProperty; + +public class ObjectWithMethodEqualsMap extends ObjectWithMap { + public ObjectWithMethodEqualsMap(Map map){ + super(map); + } + + @ObjectDiffProperty(methodEqual = "size") + @Override + public Map getMap() + { + return super.getMap(); + } +} diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsNestedObject.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsNestedObject.java new file mode 100644 index 00000000..a7f6aae6 --- /dev/null +++ b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsNestedObject.java @@ -0,0 +1,19 @@ +package de.danielbechler.diff.mock; + +import de.danielbechler.diff.annotation.ObjectDiffProperty; + +public class ObjectWithMethodEqualsNestedObject extends ObjectWithNestedObject { + public ObjectWithMethodEqualsNestedObject(String id){ + super(id); + } + public ObjectWithMethodEqualsNestedObject(String id, ObjectWithNestedObject object){ + super(id); + setObject(object); + } + + @ObjectDiffProperty(methodEqual = "getId") + @Override + public ObjectWithNestedObject getObject() { + return super.getObject(); + } +} From 3eb2705ca70a05d210efd4275ac48e19718340f4 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 25 Jul 2013 11:17:44 +0100 Subject: [PATCH 3/4] removing unused imports. got test coverage. --- src/main/java/de/danielbechler/diff/Configuration.java | 1 - src/main/java/de/danielbechler/diff/MapDiffer.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/de/danielbechler/diff/Configuration.java b/src/main/java/de/danielbechler/diff/Configuration.java index 20588588..6a629df9 100644 --- a/src/main/java/de/danielbechler/diff/Configuration.java +++ b/src/main/java/de/danielbechler/diff/Configuration.java @@ -20,7 +20,6 @@ import de.danielbechler.diff.node.*; import de.danielbechler.diff.path.*; import de.danielbechler.util.*; -import groovy.lang.Tuple; import java.util.*; diff --git a/src/main/java/de/danielbechler/diff/MapDiffer.java b/src/main/java/de/danielbechler/diff/MapDiffer.java index 189dc141..354724f7 100644 --- a/src/main/java/de/danielbechler/diff/MapDiffer.java +++ b/src/main/java/de/danielbechler/diff/MapDiffer.java @@ -21,7 +21,6 @@ import de.danielbechler.util.*; import de.danielbechler.util.Collections; -import java.lang.reflect.InvocationTargetException; import java.util.*; /** From 151d38d9ddb9dfaee7417c783645f7d8415f4cc2 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 26 Jul 2013 10:36:49 +0100 Subject: [PATCH 4/4] renaming methods, annotations and properties following discussion on the pull request --- .../de/danielbechler/diff/BeanDiffer.java | 6 +-- .../danielbechler/diff/CollectionDiffer.java | 6 +-- .../de/danielbechler/diff/Configuration.java | 44 +++++++++---------- .../java/de/danielbechler/diff/Instances.java | 3 +- .../java/de/danielbechler/diff/MapDiffer.java | 6 +-- .../de/danielbechler/diff/NodeInspector.java | 5 ++- .../diff/accessor/AbstractAccessor.java | 14 +++--- .../danielbechler/diff/accessor/Accessor.java | 1 - .../diff/accessor/PropertyDescriptor.java | 5 +-- ...bjectDiffEqualsOnlyValueProvidedType.java} | 2 +- .../diff/annotation/ObjectDiffProperty.java | 2 +- ...EqualsOnlyValueProviderMethodExample.java} | 15 +++---- .../diff/introspect/StandardIntrospector.java | 2 +- .../danielbechler/diff/node/DefaultNode.java | 9 ++-- .../java/de/danielbechler/diff/node/Node.java | 4 +- .../danielbechler/diff/BeanDifferShould.java | 10 ++--- .../diff/CollectionDifferShould.java | 17 ++++--- .../de/danielbechler/diff/InstancesTest.java | 10 ++--- .../danielbechler/diff/MapDifferShould.java | 16 +++---- .../diff/ObjectDifferIntegrationTests.java | 36 +++++++-------- .../ObjectWithMethodEqualsCollection.java | 19 -------- .../diff/mock/ObjectWithMethodEqualsMap.java | 18 -------- .../ObjectWithMethodEqualsNestedObject.java | 19 -------- ...nlyValueProviderMethodOnGetCollection.java | 19 ++++++++ ...EqualsOnlyValueProviderMethodOnGetMap.java | 18 ++++++++ ...yValueProviderMethodOnGetNestedObject.java | 19 ++++++++ 26 files changed, 157 insertions(+), 168 deletions(-) rename src/main/java/de/danielbechler/diff/annotation/{ObjectDiffMethodEqualsType.java => ObjectDiffEqualsOnlyValueProvidedType.java} (86%) rename src/main/java/de/danielbechler/diff/example/{MethodEqualExample.java => EqualsOnlyValueProviderMethodExample.java} (80%) delete mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsCollection.java delete mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsMap.java delete mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsNestedObject.java create mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection.java create mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap.java create mode 100644 src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject.java diff --git a/src/main/java/de/danielbechler/diff/BeanDiffer.java b/src/main/java/de/danielbechler/diff/BeanDiffer.java index 8b8e0f7b..8977b390 100644 --- a/src/main/java/de/danielbechler/diff/BeanDiffer.java +++ b/src/main/java/de/danielbechler/diff/BeanDiffer.java @@ -49,9 +49,9 @@ public final Node compare(final Node parentNode, final Instances instances) { beanNode.setState(Node.State.IGNORED); } - else if (nodeInspector.isWithMethodEquals(beanNode)){ - String method = nodeInspector.getWithMethodEqualsMethod(beanNode); - if (instances.areMethodResultEqual(method)) + else if (nodeInspector.hasEqualsOnlyValueProviderMethod(beanNode)){ + String method = nodeInspector.getEqualsOnlyValueProviderMethod(beanNode); + if (instances.areMethodResultsEqual(method)) { beanNode.setState(Node.State.UNTOUCHED); } diff --git a/src/main/java/de/danielbechler/diff/CollectionDiffer.java b/src/main/java/de/danielbechler/diff/CollectionDiffer.java index f3c8acc8..c8033907 100644 --- a/src/main/java/de/danielbechler/diff/CollectionDiffer.java +++ b/src/main/java/de/danielbechler/diff/CollectionDiffer.java @@ -61,9 +61,9 @@ else if (nodeInspector.isEqualsOnly(collectionNode)) collectionNode.setState(Node.State.CHANGED); } } - else if (nodeInspector.isWithMethodEquals(collectionNode)){ - String method = nodeInspector.getWithMethodEqualsMethod(collectionNode); - if (collectionInstances.areMethodResultEqual(method)) + else if (nodeInspector.hasEqualsOnlyValueProviderMethod(collectionNode)){ + String method = nodeInspector.getEqualsOnlyValueProviderMethod(collectionNode); + if (collectionInstances.areMethodResultsEqual(method)) { collectionNode.setState(Node.State.UNTOUCHED); } diff --git a/src/main/java/de/danielbechler/diff/Configuration.java b/src/main/java/de/danielbechler/diff/Configuration.java index 6a629df9..1a2966a0 100644 --- a/src/main/java/de/danielbechler/diff/Configuration.java +++ b/src/main/java/de/danielbechler/diff/Configuration.java @@ -77,10 +77,10 @@ public enum PrimitiveDefaultValueMode private final Collection includedProperties = new HashSet(10); private final Collection excludedProperties = new HashSet(10); private final Collection equalsOnlyProperties = new LinkedHashSet(10); - private final Collection methodEqualProperties = new LinkedHashSet(10); + private final Collection equalsOnlyValueProviderMethods = new LinkedHashSet(10); private final Collection> compareToOnlyTypes = new LinkedHashSet>(10); private final Collection> equalsOnlyTypes = new LinkedHashSet>(10); - private final Collection methodEqualTypes = new LinkedHashSet(10); + private final Collection equalsOnlyValueProviderTypes = new LinkedHashSet(10); private boolean returnUnchangedNodes = false; private boolean returnIgnoredNodes = false; private boolean returnCircularNodes = true; @@ -145,13 +145,13 @@ public Configuration withEqualsOnlyProperty(final PropertyPath propertyPath) return this; } - public Configuration withMethodEqualsProperty(final PropertyPath propertyPath, final String methodName) { - this.methodEqualProperties.add(new PropertyPathAndMethod(propertyPath, methodName)); + public Configuration withEqualsOnlyValueProviderMethod(final PropertyPath propertyPath, final String methodName) { + this.equalsOnlyValueProviderMethods.add(new PropertyPathAndMethod(propertyPath, methodName)); return this; } - public Configuration withMethodEqualsProperty(PropertyPathAndMethod propertyPathEqualsMethod) { - this.methodEqualProperties.add(propertyPathEqualsMethod); + public Configuration withEqualsOnlyValueProviderMethod(PropertyPathAndMethod propertyPathEqualsMethod) { + this.equalsOnlyValueProviderMethods.add(propertyPathEqualsMethod); return this; } @@ -322,31 +322,31 @@ public boolean isEqualsOnly(final Node node) return false; } - public boolean isWithMethodEquals(Node node){ - return getWithMethodEqualsMethod(node) != null; + public boolean hasEqualsOnlyValueProviderMethod(Node node){ + return getEqualsOnlyValueProviderMethod(node) != null; } - public String getWithMethodEqualsMethod(Node node){ + public String getEqualsOnlyValueProviderMethod(Node node){ final Class propertyType = node.getType(); if (propertyType != null) { - ObjectDiffMethodEqualsType annotation = propertyType.getAnnotation(ObjectDiffMethodEqualsType.class); + ObjectDiffEqualsOnlyValueProvidedType annotation = propertyType.getAnnotation(ObjectDiffEqualsOnlyValueProvidedType.class); if (annotation != null) { return annotation.method(); } - ClassAndMethod applicable = findMethodEqualPropertyForClass(propertyType); + ClassAndMethod applicable = findEqualsOnlyValueProviderMethodForClass(propertyType); if (applicable != null) { return applicable.getMethod(); } } - if (node.isWithMethodEquals()) + if (node.hasEqualsOnlyValueProviderMethod()) { - return node.getWithMethodEqualsMethod(); + return node.getEqualsOnlyValueProviderMethod(); } - PropertyPathAndMethod applicable = findMethodEqualPropertyForPath(node.getPropertyPath()); + PropertyPathAndMethod applicable = findEqualsOnlyValueProviderMethodForPath(node.getPropertyPath()); if (applicable != null) { return applicable.getMethod(); @@ -354,20 +354,20 @@ public String getWithMethodEqualsMethod(Node node){ return null; } - private ClassAndMethod findMethodEqualPropertyForClass(Class clazz){ - for(ClassAndMethod propertyPathEqualsMethod: methodEqualTypes){ - if(clazz.equals(propertyPathEqualsMethod.getClazz())){ - return propertyPathEqualsMethod; + private ClassAndMethod findEqualsOnlyValueProviderMethodForClass(Class clazz){ + for(ClassAndMethod propertyPathEqualsOnValueProviderType: equalsOnlyValueProviderTypes){ + if(clazz.equals(propertyPathEqualsOnValueProviderType.getClazz())){ + return propertyPathEqualsOnValueProviderType; } } return null; } - private PropertyPathAndMethod findMethodEqualPropertyForPath(PropertyPath propertyPath){ - for(PropertyPathAndMethod propertyPathEqualsMethod: methodEqualProperties){ - if(propertyPath.equals(propertyPathEqualsMethod.getPropertyPath())){ - return propertyPathEqualsMethod; + private PropertyPathAndMethod findEqualsOnlyValueProviderMethodForPath(PropertyPath propertyPath){ + for(PropertyPathAndMethod propertyPathEqualsOnValueProviderMethod: equalsOnlyValueProviderMethods){ + if(propertyPath.equals(propertyPathEqualsOnValueProviderMethod.getPropertyPath())){ + return propertyPathEqualsOnValueProviderMethod; } } return null; diff --git a/src/main/java/de/danielbechler/diff/Instances.java b/src/main/java/de/danielbechler/diff/Instances.java index e6c30644..eb09f21d 100644 --- a/src/main/java/de/danielbechler/diff/Instances.java +++ b/src/main/java/de/danielbechler/diff/Instances.java @@ -173,7 +173,7 @@ public boolean areEqual() return isEqual(base, working); } - public boolean areMethodResultEqual(String method) { + public boolean areMethodResultsEqual(String method) { try { Object baseMethodResult = base.getClass().getMethod(method).invoke(base); Object workingMethodResult = working.getClass().getMethod(method).invoke(working); @@ -294,5 +294,4 @@ public PropertyPath getPropertyPath(final Node parentNode) return PropertyPath.createBuilder().withRoot().build(); } } - } diff --git a/src/main/java/de/danielbechler/diff/MapDiffer.java b/src/main/java/de/danielbechler/diff/MapDiffer.java index 354724f7..b33b4987 100644 --- a/src/main/java/de/danielbechler/diff/MapDiffer.java +++ b/src/main/java/de/danielbechler/diff/MapDiffer.java @@ -61,9 +61,9 @@ else if (nodeInspector.isEqualsOnly(mapNode)) mapNode.setState(Node.State.CHANGED); } } - else if (nodeInspector.isWithMethodEquals(mapNode)){ - String method = nodeInspector.getWithMethodEqualsMethod(mapNode); - if (instances.areMethodResultEqual(method)) + else if (nodeInspector.hasEqualsOnlyValueProviderMethod(mapNode)){ + String method = nodeInspector.getEqualsOnlyValueProviderMethod(mapNode); + if (instances.areMethodResultsEqual(method)) { mapNode.setState(Node.State.UNTOUCHED); } diff --git a/src/main/java/de/danielbechler/diff/NodeInspector.java b/src/main/java/de/danielbechler/diff/NodeInspector.java index 828da7f7..1ae5fa25 100644 --- a/src/main/java/de/danielbechler/diff/NodeInspector.java +++ b/src/main/java/de/danielbechler/diff/NodeInspector.java @@ -31,8 +31,9 @@ interface NodeInspector boolean isEqualsOnly(Node node); - boolean isWithMethodEquals(Node node); - String getWithMethodEqualsMethod(Node node); + boolean hasEqualsOnlyValueProviderMethod(Node node); + + String getEqualsOnlyValueProviderMethod(Node node); boolean isReturnable(Node node); diff --git a/src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java b/src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java index 7d08a6de..a278a0cc 100644 --- a/src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java +++ b/src/main/java/de/danielbechler/diff/accessor/AbstractAccessor.java @@ -24,7 +24,7 @@ public abstract class AbstractAccessor implements Accessor private Set categories = new TreeSet(); private boolean equalsOnly; private boolean ignored; - private String withMethodEqualsMethod; + private String equalsOnlyValueProviderMethod; public final Set getCategories() { @@ -56,16 +56,16 @@ public void setIgnored(final boolean ignored) this.ignored = ignored; } - public boolean isWithMethodEquals(){ - return this.withMethodEqualsMethod != null && !this.withMethodEqualsMethod.equals(""); + public boolean hasEqualsOnlyValueProviderMethod(){ + return this.equalsOnlyValueProviderMethod != null && !this.equalsOnlyValueProviderMethod.equals(""); } - public void setWithMethodEqualsMethod(String withMethodEqualsMethod) { - this.withMethodEqualsMethod = withMethodEqualsMethod; + public void setEqualsOnlyValueProviderMethod(String equalsOnlyValueProviderMethod) { + this.equalsOnlyValueProviderMethod = equalsOnlyValueProviderMethod; } - public String getWithMethodEqualsMethod(){ - return withMethodEqualsMethod; + public String getEqualsOnlyValueProviderMethod(){ + return equalsOnlyValueProviderMethod; } } diff --git a/src/main/java/de/danielbechler/diff/accessor/Accessor.java b/src/main/java/de/danielbechler/diff/accessor/Accessor.java index 7d02381d..b5807692 100644 --- a/src/main/java/de/danielbechler/diff/accessor/Accessor.java +++ b/src/main/java/de/danielbechler/diff/accessor/Accessor.java @@ -24,5 +24,4 @@ public interface Accessor extends PropertyDescriptor void set(Object target, Object value); void unset(Object target); - } diff --git a/src/main/java/de/danielbechler/diff/accessor/PropertyDescriptor.java b/src/main/java/de/danielbechler/diff/accessor/PropertyDescriptor.java index d4789e3b..3e7038df 100644 --- a/src/main/java/de/danielbechler/diff/accessor/PropertyDescriptor.java +++ b/src/main/java/de/danielbechler/diff/accessor/PropertyDescriptor.java @@ -30,9 +30,8 @@ public interface PropertyDescriptor boolean isIgnored(); boolean isEqualsOnly(); - - boolean isWithMethodEquals(); + boolean hasEqualsOnlyValueProviderMethod(); - String getWithMethodEqualsMethod(); + String getEqualsOnlyValueProviderMethod(); } diff --git a/src/main/java/de/danielbechler/diff/annotation/ObjectDiffMethodEqualsType.java b/src/main/java/de/danielbechler/diff/annotation/ObjectDiffEqualsOnlyValueProvidedType.java similarity index 86% rename from src/main/java/de/danielbechler/diff/annotation/ObjectDiffMethodEqualsType.java rename to src/main/java/de/danielbechler/diff/annotation/ObjectDiffEqualsOnlyValueProvidedType.java index a0f62bb5..75755a89 100644 --- a/src/main/java/de/danielbechler/diff/annotation/ObjectDiffMethodEqualsType.java +++ b/src/main/java/de/danielbechler/diff/annotation/ObjectDiffEqualsOnlyValueProvidedType.java @@ -10,6 +10,6 @@ @Target(ElementType.TYPE) @Inherited @ObjectDiffAnnotation -public @interface ObjectDiffMethodEqualsType { +public @interface ObjectDiffEqualsOnlyValueProvidedType { public String method(); } diff --git a/src/main/java/de/danielbechler/diff/annotation/ObjectDiffProperty.java b/src/main/java/de/danielbechler/diff/annotation/ObjectDiffProperty.java index b2bbfc1d..80c4b833 100644 --- a/src/main/java/de/danielbechler/diff/annotation/ObjectDiffProperty.java +++ b/src/main/java/de/danielbechler/diff/annotation/ObjectDiffProperty.java @@ -52,5 +52,5 @@ */ public String[] categories() default {}; - public String methodEqual() default ""; + public String equalsOnlyValueProviderMethod() default ""; } diff --git a/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java b/src/main/java/de/danielbechler/diff/example/EqualsOnlyValueProviderMethodExample.java similarity index 80% rename from src/main/java/de/danielbechler/diff/example/MethodEqualExample.java rename to src/main/java/de/danielbechler/diff/example/EqualsOnlyValueProviderMethodExample.java index 0b15e64e..723db0e6 100644 --- a/src/main/java/de/danielbechler/diff/example/MethodEqualExample.java +++ b/src/main/java/de/danielbechler/diff/example/EqualsOnlyValueProviderMethodExample.java @@ -1,18 +1,13 @@ package de.danielbechler.diff.example; -import java.util.ArrayList; -import java.util.List; - import de.danielbechler.diff.Configuration; import de.danielbechler.diff.ObjectDifferFactory; -import de.danielbechler.diff.annotation.ObjectDiffMethodEqualsType; -import de.danielbechler.diff.annotation.ObjectDiffProperty; import de.danielbechler.diff.node.Node; import de.danielbechler.diff.path.PropertyPath; import de.danielbechler.diff.visitor.PrintingVisitor; -class MethodEqualExample { - private MethodEqualExample() +class EqualsOnlyValueProviderMethodExample { + private EqualsOnlyValueProviderMethodExample() { } @@ -26,7 +21,7 @@ public static void main(final String[] args) final Configuration configuration = new Configuration(); // (Option 1) Causes the ObjectDiffer to compare using the method "getProp1" on the 'prop' property of the root object - configuration.withMethodEqualsProperty(PropertyPath.buildWith("prop"), "getProp1"); + configuration.withEqualsOnlyValueProviderMethod(PropertyPath.buildWith("prop"), "getProp1"); final Node node = ObjectDifferFactory.getInstance(configuration).compare(working, base); @@ -48,14 +43,14 @@ public EncompassingClass(final PropertyClass prop) } /* (Option 2) This annotation causes the ObjectDiffer to use getProp1 method to compare */ - //@ObjectDiffProperty(methodEqual = "getProp1") + //@ObjectDiffProperty(equalsOnlyValueProviderMethod = "getProp1") public PropertyClass getProp() { return prop; } } /* (Option 3) This annotation causes the ObjectDiffer to use getProp1 method to compare */ - //@ObjectDiffMethodEqualsType(method="getProp1") + //@ObjectDiffEqualsOnlyValueProvidedType(method="getProp1") public static class PropertyClass { private String prop1; diff --git a/src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java b/src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java index f6715e6c..25dec318 100644 --- a/src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java +++ b/src/main/java/de/danielbechler/diff/introspect/StandardIntrospector.java @@ -99,7 +99,7 @@ private static void handleObjectDiffPropertyAnnotation(final Method readMethod, propertyAccessor.setEqualsOnly(annotation.equalsOnly()); propertyAccessor.setIgnored(annotation.ignore()); propertyAccessor.setCategories(Collections.setOf(annotation.categories())); - propertyAccessor.setWithMethodEqualsMethod(annotation.methodEqual()); + propertyAccessor.setEqualsOnlyValueProviderMethod(annotation.equalsOnlyValueProviderMethod()); } } diff --git a/src/main/java/de/danielbechler/diff/node/DefaultNode.java b/src/main/java/de/danielbechler/diff/node/DefaultNode.java index 5dd37c5a..06d98615 100644 --- a/src/main/java/de/danielbechler/diff/node/DefaultNode.java +++ b/src/main/java/de/danielbechler/diff/node/DefaultNode.java @@ -318,12 +318,12 @@ public final boolean isIgnored() return state == State.IGNORED || accessor.isIgnored(); } - public boolean isWithMethodEquals() { - return accessor.isWithMethodEquals(); + public boolean hasEqualsOnlyValueProviderMethod() { + return accessor.hasEqualsOnlyValueProviderMethod(); } - public String getWithMethodEqualsMethod() { - return accessor.getWithMethodEqualsMethod(); + public String getEqualsOnlyValueProviderMethod() { + return accessor.getEqualsOnlyValueProviderMethod(); } public final Set getCategories() @@ -482,5 +482,4 @@ public void setCircleStartNode(final Node circleStartNode) { this.circleStartNode = circleStartNode; } - } diff --git a/src/main/java/de/danielbechler/diff/node/Node.java b/src/main/java/de/danielbechler/diff/node/Node.java index a8822329..7330a6eb 100644 --- a/src/main/java/de/danielbechler/diff/node/Node.java +++ b/src/main/java/de/danielbechler/diff/node/Node.java @@ -201,8 +201,8 @@ public enum State T getPropertyAnnotation(Class annotationClass); - boolean isWithMethodEquals(); + boolean hasEqualsOnlyValueProviderMethod(); - String getWithMethodEqualsMethod(); + String getEqualsOnlyValueProviderMethod(); } diff --git a/src/test/java/de/danielbechler/diff/BeanDifferShould.java b/src/test/java/de/danielbechler/diff/BeanDifferShould.java index 59a228ec..5719e987 100644 --- a/src/test/java/de/danielbechler/diff/BeanDifferShould.java +++ b/src/test/java/de/danielbechler/diff/BeanDifferShould.java @@ -24,8 +24,6 @@ import org.mockito.Mock; import org.testng.annotations.*; -import static de.danielbechler.diff.node.Node.State.CHANGED; -import static de.danielbechler.diff.node.Node.State.UNTOUCHED; import static de.danielbechler.diff.node.NodeAssertions.assertThat; import static java.util.Arrays.*; import static org.hamcrest.MatcherAssert.assertThat; @@ -124,13 +122,13 @@ public void compare_bean_via_equals() } @Test - public void detect_no_change_when_comparing_using_with_method_equals_and_result_is_same() + public void detect_no_change_when_comparing_using_with_equals_only_value_provider_method_and_result_is_same() { final ObjectWithNestedObject working = new ObjectWithNestedObject("foo"); working.setObject(new ObjectWithNestedObject("childid")); final ObjectWithNestedObject base = new ObjectWithNestedObject("foo"); base.setObject(new ObjectWithNestedObject("differentchildid")); - configuration.withMethodEqualsProperty(PropertyPath.buildRootPath(), "getId"); + configuration.withEqualsOnlyValueProviderMethod(PropertyPath.buildRootPath(), "getId"); final Node node = differ.compare(Node.ROOT, Instances.of(working, base)); @@ -138,13 +136,13 @@ public void detect_no_change_when_comparing_using_with_method_equals_and_result_ } @Test - public void detect_change_when_comparing_using_with_method_equals_and_result_is_different() + public void detect_change_when_comparing_using_equals_only_value_provider_method_and_result_is_different() { final ObjectWithNestedObject working = new ObjectWithNestedObject("foo"); working.setObject(new ObjectWithNestedObject("childid")); final ObjectWithNestedObject base = new ObjectWithNestedObject("bar"); base.setObject(new ObjectWithNestedObject("differentchildid")); - configuration.withMethodEqualsProperty(PropertyPath.buildRootPath(), "getId"); + configuration.withEqualsOnlyValueProviderMethod(PropertyPath.buildRootPath(), "getId"); final Node node = differ.compare(Node.ROOT, Instances.of(working, base)); diff --git a/src/test/java/de/danielbechler/diff/CollectionDifferShould.java b/src/test/java/de/danielbechler/diff/CollectionDifferShould.java index 7283dea3..b55bf50e 100644 --- a/src/test/java/de/danielbechler/diff/CollectionDifferShould.java +++ b/src/test/java/de/danielbechler/diff/CollectionDifferShould.java @@ -25,7 +25,6 @@ import java.util.*; import static de.danielbechler.diff.node.Node.State.CHANGED; -import static de.danielbechler.diff.node.Node.State.UNTOUCHED; import static java.util.Arrays.*; import static java.util.Collections.*; import static org.mockito.Mockito.*; @@ -122,21 +121,21 @@ public void compare_only_via_equals_if_equals_only_is_enabled() } @Test - public void detect_no_change_when_comparing_using_with_method_equals_and_result_is_same() + public void detect_no_change_when_comparing_using_with_equals_only_value_provider_method_and_result_is_same() { - when(nodeInspector.isWithMethodEquals(collectionNode)).thenReturn(true); - when(nodeInspector.getWithMethodEqualsMethod(collectionNode)).thenReturn("somemethod"); - when(instances.areMethodResultEqual("somemethod")).thenReturn(true); + when(nodeInspector.hasEqualsOnlyValueProviderMethod(collectionNode)).thenReturn(true); + when(nodeInspector.getEqualsOnlyValueProviderMethod(collectionNode)).thenReturn("somemethod"); + when(instances.areMethodResultsEqual("somemethod")).thenReturn(true); compare(); verify(collectionNode).setState(Node.State.UNTOUCHED); } @Test - public void detect_change_when_comparing_using_with_method_equals_and_result_is_different() + public void detect_change_when_comparing_using_with_equals_only_value_provider_method_and_result_is_different() { - when(nodeInspector.isWithMethodEquals(collectionNode)).thenReturn(true); - when(nodeInspector.getWithMethodEqualsMethod(collectionNode)).thenReturn("somemethod"); - when(instances.areMethodResultEqual("somemethod")).thenReturn(false); + when(nodeInspector.hasEqualsOnlyValueProviderMethod(collectionNode)).thenReturn(true); + when(nodeInspector.getEqualsOnlyValueProviderMethod(collectionNode)).thenReturn("somemethod"); + when(instances.areMethodResultsEqual("somemethod")).thenReturn(false); compare(); verify(collectionNode).setState(CHANGED); } diff --git a/src/test/java/de/danielbechler/diff/InstancesTest.java b/src/test/java/de/danielbechler/diff/InstancesTest.java index 5073326d..c1fcb749 100644 --- a/src/test/java/de/danielbechler/diff/InstancesTest.java +++ b/src/test/java/de/danielbechler/diff/InstancesTest.java @@ -123,7 +123,7 @@ public void testIsPrimitiveTypeReturnsFalseForComplexType() } @Test - public void testMethodResultNotEqual() throws Exception + public void testMethodResultEqualNotEqual() throws Exception { final Method readMethod = getClass().getDeclaredMethod("getTestValue"); final PropertyAccessor accessor = new PropertyAccessor("testValue", readMethod, null); @@ -132,11 +132,11 @@ public void testMethodResultNotEqual() throws Exception ObjectWithString base = new ObjectWithString("string2"); final Instances instances = new Instances(accessor, working, base, null); - assertThat(instances.areMethodResultEqual("getValue")).isFalse(); + assertThat(instances.areMethodResultsEqual("getValue")).isFalse(); } @Test - public void testMethodResultEqual() throws Exception + public void testMethodResultEqualIsEqual() throws Exception { final Method readMethod = getClass().getDeclaredMethod("getTestValue"); final PropertyAccessor accessor = new PropertyAccessor("testValue", readMethod, null); @@ -145,7 +145,7 @@ public void testMethodResultEqual() throws Exception ObjectWithString base = new ObjectWithString("string"); final Instances instances = new Instances(accessor, working, base, null); - assertThat(instances.areMethodResultEqual("getValue")).isTrue(); + assertThat(instances.areMethodResultsEqual("getValue")).isTrue(); } @Test @@ -159,7 +159,7 @@ public void testMethodResultEqualInvalidMethod() throws Exception final Instances instances = new Instances(accessor, working, base, null); try { - instances.areMethodResultEqual("invalid"); + instances.areMethodResultsEqual("invalid"); fail("no exception thrown"); } catch(RuntimeException e){ diff --git a/src/test/java/de/danielbechler/diff/MapDifferShould.java b/src/test/java/de/danielbechler/diff/MapDifferShould.java index 2aee41a6..3de11f08 100644 --- a/src/test/java/de/danielbechler/diff/MapDifferShould.java +++ b/src/test/java/de/danielbechler/diff/MapDifferShould.java @@ -91,11 +91,11 @@ public void detect_no_change_when_comparing_using_equals() } @Test - public void detect_no_change_when_comparing_using_with_method_equals_and_result_is_same() + public void detect_no_change_when_comparing_using_equals_only_provider_method_and_result_is_same() { - when(nodeInspector.isWithMethodEquals(internalNode)).thenReturn(true); - when(nodeInspector.getWithMethodEqualsMethod(internalNode)).thenReturn("somemethod"); - when(instances.areMethodResultEqual("somemethod")).thenReturn(true); + when(nodeInspector.hasEqualsOnlyValueProviderMethod(internalNode)).thenReturn(true); + when(nodeInspector.getEqualsOnlyValueProviderMethod(internalNode)).thenReturn("somemethod"); + when(instances.areMethodResultsEqual("somemethod")).thenReturn(true); node = compare(working, base); @@ -103,11 +103,11 @@ public void detect_no_change_when_comparing_using_with_method_equals_and_result_ } @Test - public void detect_change_when_comparing_using_with_method_equals_and_result_is_different() + public void detect_change_when_comparing_using_with_equals_only_provider_method_and_result_is_different() { - when(nodeInspector.isWithMethodEquals(internalNode)).thenReturn(true); - when(nodeInspector.getWithMethodEqualsMethod(internalNode)).thenReturn("somemethod"); - when(instances.areMethodResultEqual("somemethod")).thenReturn(false); + when(nodeInspector.hasEqualsOnlyValueProviderMethod(internalNode)).thenReturn(true); + when(nodeInspector.getEqualsOnlyValueProviderMethod(internalNode)).thenReturn("somemethod"); + when(instances.areMethodResultsEqual("somemethod")).thenReturn(false); node = compare(working, base); diff --git a/src/test/java/de/danielbechler/diff/ObjectDifferIntegrationTests.java b/src/test/java/de/danielbechler/diff/ObjectDifferIntegrationTests.java index e50b1d6d..f37d3272 100644 --- a/src/test/java/de/danielbechler/diff/ObjectDifferIntegrationTests.java +++ b/src/test/java/de/danielbechler/diff/ObjectDifferIntegrationTests.java @@ -321,14 +321,14 @@ public void testCompareWithListContainingObjectOnceDetectsIfAnotherInstanceOfItG .build()).hasState(Node.State.ADDED); } - public void testCompareBeanWithEqualsMethodOnCollectionPropertyNoChangeInMethodResult() + public void testCompareBeanWithEqualsOnlyValueProviderMethodOnGetCollectionPropertyNoChangeInMethodResult() { List forWorking = new ArrayList(); forWorking.add("one"); List forBase = new ArrayList(); forBase.add("uno"); - final ObjectWithMethodEqualsCollection working = new ObjectWithMethodEqualsCollection(forWorking); - final ObjectWithMethodEqualsCollection base = new ObjectWithMethodEqualsCollection(forBase); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection working = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection(forWorking); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection base = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection(forBase); final Node node = objectDiffer.compare(working, base); @@ -336,15 +336,15 @@ public void testCompareBeanWithEqualsMethodOnCollectionPropertyNoChangeInMethodR NodeAssertions.assertThat(node).self().hasNoChildren(); } - public void testCompareBeanWithEqualsMethodOnCollectionPropertyWithChangeInMethodResult() + public void testCompareBeanWithEqualOnlyValueProviderMethodOnGetCollectionPropertyWithChangeInMethodResult() { List forWorking = new ArrayList(); forWorking.add("one"); forWorking.add("two"); List forBase = new ArrayList(); forBase.add("uno"); - final ObjectWithMethodEqualsCollection working = new ObjectWithMethodEqualsCollection(forWorking); - final ObjectWithMethodEqualsCollection base = new ObjectWithMethodEqualsCollection(forBase); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection working = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection(forWorking); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection base = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection(forBase); final Node node = objectDiffer.compare(working, base); @@ -354,14 +354,14 @@ public void testCompareBeanWithEqualsMethodOnCollectionPropertyWithChangeInMetho .hasState(Node.State.CHANGED); } - public void testCompareBeanWithEqualsMethodOnObjectPropertyNoChangeInMethodResult() + public void testCompareBeanWithEqualsOnlyValueProviderMethodOnGetObjectPropertyNoChangeInMethodResult() { ObjectWithNestedObject forWorking = new ObjectWithNestedObject("childid"); forWorking.setObject(new ObjectWithNestedObject("grandchildid")); ObjectWithNestedObject forBase = new ObjectWithNestedObject("childid"); forBase.setObject(new ObjectWithNestedObject("differentgrandchildid")); - final ObjectWithMethodEqualsNestedObject working = new ObjectWithMethodEqualsNestedObject("id", forWorking); - final ObjectWithMethodEqualsNestedObject base = new ObjectWithMethodEqualsNestedObject("id", forBase); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject working = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject("id", forWorking); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject base = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject("id", forBase); final Node node = objectDiffer.compare(working, base); @@ -369,14 +369,14 @@ public void testCompareBeanWithEqualsMethodOnObjectPropertyNoChangeInMethodResul NodeAssertions.assertThat(node).self().hasNoChildren(); } - public void testCompareBeanWithEqualsMethodOnObjectPropertyWithChangeInMethodResult() + public void testCompareBeanWithEqualsOnlyValueProviderMethodOnGetObjectPropertyWithChangeInMethodResult() { ObjectWithNestedObject forWorking = new ObjectWithNestedObject("childid"); forWorking.setObject(new ObjectWithNestedObject("grandchildid")); ObjectWithNestedObject forBase = new ObjectWithNestedObject("differentchildid"); forBase.setObject(new ObjectWithNestedObject("differentgrandchildid")); - final ObjectWithMethodEqualsNestedObject working = new ObjectWithMethodEqualsNestedObject("id", forWorking); - final ObjectWithMethodEqualsNestedObject base = new ObjectWithMethodEqualsNestedObject("id", forBase); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject working = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject("id", forWorking); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject base = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject("id", forBase); final Node node = objectDiffer.compare(working, base); @@ -386,14 +386,14 @@ public void testCompareBeanWithEqualsMethodOnObjectPropertyWithChangeInMethodRes .hasState(Node.State.CHANGED); } - public void testCompareBeanWithEqualsMethodOnMapPropertyNoChangeInMethodResult() + public void testCompareBeanWithEqualsOnlyValueProviderMethodOnGetMapPropertyNoChangeInMethodResult() { Map forWorking = new HashMap(); forWorking.put("key1", "val1"); Map forBase = new HashMap(); forBase.put("keyone", "valone"); - final ObjectWithMethodEqualsMap working = new ObjectWithMethodEqualsMap(forWorking); - final ObjectWithMethodEqualsMap base = new ObjectWithMethodEqualsMap(forBase); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap working = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap(forWorking); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap base = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap(forBase); final Node node = objectDiffer.compare(working, base); @@ -401,15 +401,15 @@ public void testCompareBeanWithEqualsMethodOnMapPropertyNoChangeInMethodResult() NodeAssertions.assertThat(node).self().hasNoChildren(); } - public void testCompareBeanWithEqualsMethodOnMapPropertyWithChangeInMethodResult() + public void testCompareBeanWithEqualsOnlyValueProviderMethodOnGetMapPropertyWithChangeInMethodResult() { Map forWorking = new HashMap(); forWorking.put("key1", "val1"); forWorking.put("key2", "val2"); Map forBase = new HashMap(); forBase.put("keyone", "valone"); - final ObjectWithMethodEqualsMap working = new ObjectWithMethodEqualsMap(forWorking); - final ObjectWithMethodEqualsMap base = new ObjectWithMethodEqualsMap(forBase); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap working = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap(forWorking); + final ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap base = new ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap(forBase); final Node node = objectDiffer.compare(working, base); diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsCollection.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsCollection.java deleted file mode 100644 index 77514006..00000000 --- a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsCollection.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.danielbechler.diff.mock; - -import java.util.Collection; - -import de.danielbechler.diff.annotation.ObjectDiffProperty; - -public class ObjectWithMethodEqualsCollection extends ObjectWithCollection { - - public ObjectWithMethodEqualsCollection(Collection collection){ - super(collection); - } - - @ObjectDiffProperty(methodEqual = "size") - @Override - public Collection getCollection() - { - return super.getCollection(); - } -} diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsMap.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsMap.java deleted file mode 100644 index a9f87673..00000000 --- a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsMap.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.danielbechler.diff.mock; - -import java.util.Map; - -import de.danielbechler.diff.annotation.ObjectDiffProperty; - -public class ObjectWithMethodEqualsMap extends ObjectWithMap { - public ObjectWithMethodEqualsMap(Map map){ - super(map); - } - - @ObjectDiffProperty(methodEqual = "size") - @Override - public Map getMap() - { - return super.getMap(); - } -} diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsNestedObject.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsNestedObject.java deleted file mode 100644 index a7f6aae6..00000000 --- a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsNestedObject.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.danielbechler.diff.mock; - -import de.danielbechler.diff.annotation.ObjectDiffProperty; - -public class ObjectWithMethodEqualsNestedObject extends ObjectWithNestedObject { - public ObjectWithMethodEqualsNestedObject(String id){ - super(id); - } - public ObjectWithMethodEqualsNestedObject(String id, ObjectWithNestedObject object){ - super(id); - setObject(object); - } - - @ObjectDiffProperty(methodEqual = "getId") - @Override - public ObjectWithNestedObject getObject() { - return super.getObject(); - } -} diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection.java new file mode 100644 index 00000000..bb27faf7 --- /dev/null +++ b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection.java @@ -0,0 +1,19 @@ +package de.danielbechler.diff.mock; + +import java.util.Collection; + +import de.danielbechler.diff.annotation.ObjectDiffProperty; + +public class ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection extends ObjectWithCollection { + + public ObjectWithMethodEqualsOnlyValueProviderMethodOnGetCollection(Collection collection){ + super(collection); + } + + @ObjectDiffProperty(equalsOnlyValueProviderMethod = "size") + @Override + public Collection getCollection() + { + return super.getCollection(); + } +} diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap.java new file mode 100644 index 00000000..eb262690 --- /dev/null +++ b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap.java @@ -0,0 +1,18 @@ +package de.danielbechler.diff.mock; + +import java.util.Map; + +import de.danielbechler.diff.annotation.ObjectDiffProperty; + +public class ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap extends ObjectWithMap { + public ObjectWithMethodEqualsOnlyValueProviderMethodOnGetMap(Map map){ + super(map); + } + + @ObjectDiffProperty(equalsOnlyValueProviderMethod = "size") + @Override + public Map getMap() + { + return super.getMap(); + } +} diff --git a/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject.java b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject.java new file mode 100644 index 00000000..f0b03abf --- /dev/null +++ b/src/test/java/de/danielbechler/diff/mock/ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject.java @@ -0,0 +1,19 @@ +package de.danielbechler.diff.mock; + +import de.danielbechler.diff.annotation.ObjectDiffProperty; + +public class ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject extends ObjectWithNestedObject { + public ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject(String id){ + super(id); + } + public ObjectWithMethodEqualsOnlyValueProviderMethodOnGetNestedObject(String id, ObjectWithNestedObject object){ + super(id); + setObject(object); + } + + @ObjectDiffProperty(equalsOnlyValueProviderMethod = "getId") + @Override + public ObjectWithNestedObject getObject() { + return super.getObject(); + } +}