Skip to content

Comparing objects? #180

Open
Open
@cmadsen

Description

@cmadsen

Why is does (using java-object-diff:0.94)

DiffNode diff = ObjectDifferBuilder.buildDefault().compare(
			new Person(1, "Bob", "London"),
			new Person(2, "Peter", "Paris"));

result in diff.hasChanges()==false?

import static java.lang.System.*;
import static org.assertj.core.api.Assertions.*;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.junit.Test;

import de.danielbechler.diff.ObjectDifferBuilder;
import de.danielbechler.diff.node.DiffNode;

public class Compare {
	static public class Person {
		int id;
		String name;
		String city;

		public Person() {
		}

		Person(int id, String name, String city) {
			this.id = id;
			this.name = name;
			this.city = city;
		}

		@Override
		public boolean equals(Object obj) {
			if (!(obj instanceof Person)) {
				return false;
			}
			if (this == obj) {
				return true;
			}
			final Person otherObject = (Person) obj;
			return new EqualsBuilder().append(this.id, otherObject.id)
					.isEquals();
		}

		@Override
		public int hashCode() {
			return new HashCodeBuilder().append(id).toHashCode();
		}
	}

	@Test
	public void compareMaps() {
		DiffNode diff = ObjectDifferBuilder.buildDefault().compare(
				new Person(1, "Bob", "London"),
				new Person(2, "Peter", "Paris"));
		assertThat(diff.hasChanges()).isTrue();
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions