Skip to content

Diffing sets of complex objects while using equals only properties #43

@creade

Description

@creade

We noticed that while trying to diff two objects that have sets of complex objects after having configured equals only properties, differences in the objects in the set weren't being diffed:

public class DiffTest {


    private class Thing {
        private String a;
        private String b;

        public Thing(String a, String b) {
            this.a = a;
            this.b = b;
        }
        // Equals, hash code, getters
    }

    private class ThingHolder {
        private Set<Thing> things;
        private String ignore;
        private String include;


        private ThingHolder(Set<Thing> things, String ignore, String include) {
            this.things = things;
            this.ignore = ignore;
            this.include = include;
        }
        // Equals, hash code, getters
    }


    @Test
    public void shouldDiffThings() {
        List<String> propertyNames = listWith("things", "include");
        Configuration configuration = new Configuration();
        for (String name : propertyNames) {
            PropertyPath propertyPath = PropertyPath.buildWith(name);
            configuration = configuration.withEqualsOnlyProperty(propertyPath).withPropertyPath(propertyPath);
        }

        Thing thingOne = new Thing("a", "b");
        Thing thingTwo = new Thing("aa", "bb");

        ThingHolder first = new ThingHolder(setWith(thingOne), "ignore", "include");
        ThingHolder second = new ThingHolder(setWith(thingTwo), "ignore this change", "include");
        Node compareResults = ObjectDifferFactory.getInstance(configuration).compare(first, second);

        assertThat(compareResults.isChanged(), is(true));
    }
}

I think I've tracked it down to the nodes created for the items in the sets. Their property paths include a collection element meaning they won't pass the includedProperties.contains(node.getPropertyPath()) check called by isIgnored(node) in BeanDiffer line 59.

The whole test is at https://gist.github.com/4122745

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions