Skip to content

Native types are not being compared properly #38

@ishomersimpson

Description

@ishomersimpson

If I have a bean with native types, the comparison is not being performed properly in some cases.
For what I could understand, it does not work if the value in the "working" bean is the default value for the given data type (e.g 0 for int or false for boolean).

Odd as it may seem, it all works if the given bean class has private access (inner class), but does not work if is public.

Check the following test case where I show three examples.

By the way, this is a nice piece of software!

import de.danielbechler.diff.ObjectDiffer;
import de.danielbechler.diff.ObjectDifferFactory;
import de.danielbechler.diff.node.Node;
import de.danielbechler.diff.visitor.PrintingVisitor;

public class TestCase {

    // class has public access
    public static class TestIntBeanPublic {

        private int value;

        public int getValue() {
            return this.value;
        }

        public void setValue(final int value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "TestIntBeanPublic [value=" + this.value + "]";
        }
    }

    // class has private access
    private static class TestIntBeanPrivate {

        private int value;

        public int getValue() {
            return this.value;
        }

        public void setValue(final int value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "TestIntBeanPrivate [value=" + this.value + "]";
        }
    }

    public static void testIntegerFailsPublic() {

        TestIntBeanPublic working = new TestIntBeanPublic();
        working.setValue(0);

        TestIntBeanPublic base = new TestIntBeanPublic();
        base.setValue(1);

        ObjectDiffer differ = ObjectDifferFactory.getInstance();
        final Node root = differ.compare(working, base);

        root.visit(new PrintingVisitor(working, base));
    }

    public static void testIntegerWorksPrivate() {

        TestIntBeanPrivate working = new TestIntBeanPrivate();
        working.setValue(0);

        TestIntBeanPrivate base = new TestIntBeanPrivate();
        base.setValue(1);

        ObjectDiffer differ = ObjectDifferFactory.getInstance();
        final Node root = differ.compare(working, base);

        root.visit(new PrintingVisitor(working, base));
    }

    public static void testIntegerWorksPublic() {

        TestIntBeanPublic working = new TestIntBeanPublic();
        working.setValue(2);

        TestIntBeanPublic base = new TestIntBeanPublic();
        base.setValue(1);

        ObjectDiffer differ = ObjectDifferFactory.getInstance();
        final Node root = differ.compare(working, base);

        root.visit(new PrintingVisitor(working, base));
    }

    public static void main(final String[] args) {

        System.out.println("The following comparison works properly (detects change)");
        TestCase.testIntegerWorksPublic();
        System.out.println("The following comparison does not work properly (detects removal)");
        TestCase.testIntegerFailsPublic();
        System.out
                .println("The following comparison works properly bewcause the class has private access (detects change)");
        TestCase.testIntegerWorksPrivate();
    }
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions