Skip to content

Instantly share code, notes, and snippets.

@creade
Created November 21, 2012 02:58
Show Gist options
  • Save creade/4122745 to your computer and use it in GitHub Desktop.
Save creade/4122745 to your computer and use it in GitHub Desktop.
Object-diff withEqualsOnlyProperty and sets
import de.danielbechler.diff.Configuration;
import de.danielbechler.diff.ObjectDifferFactory;
import de.danielbechler.diff.node.Node;
import de.danielbechler.diff.path.PropertyPath;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.javafunk.funk.Literals.listWith;
import static org.javafunk.funk.Literals.setWith;
public class DiffTest {
@EqualsAndHashCode
private class Thing {
@Getter private String a;
@Getter private String b;
public Thing(String a, String b) {
this.a = a;
this.b = b;
}
}
@EqualsAndHashCode
private class ThingHolder {
@Getter private Set<Thing> things;
@Getter private String ignore;
@Getter private String include;
private ThingHolder(Set<Thing> things, String ignore, String include) {
this.things = things;
this.ignore = ignore;
this.include = include;
}
}
@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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment