我正在使用java-object-diff https://github.com/SQiShER/java-object-diff并尝试比较实现相同接口的两个对象。作为一个例子,我用苹果和橘子设置了一个测试。我知道你不应该比较苹果和橘子,但我有一些用例,我需要这样做。
我尝试过施法。我也尝试用一个抽象的超级类Apple实现它,Apple和Apple类扩展它。没有骰子。我一直在寻找API文档和测试类中的一种方法,但到目前为止还没有运气。 任何熟悉这个API的人都可以指出我的方向很好。
以下是代码:
public interface Fruit {
public final static String SIZE_SMALL = "Small";
public final static String SIZE_LARGE = "Large";
public final static String CONDITION_RIPE = "Ripe";
public final static String CONDITION_NOT_RIPE = "Not Ripe";
public String getSize();
public String getCondition();
public Double getWeight();
public Date getSellBy();
}
public class Apple implements Fruit {
private String size;
private String condition;
private Double weight;
private Date sellBy;
public Apple (String size, String condition, Double weight, Date sellBy){
this.size= size;
this.condition = condition;
this.weight = weight;
this.sellBy = sellBy;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size= size;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
condition = condition;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public Date getSellBy() {
return sellBy;
}
public void setSellBy(Date sellBy) {
this.sellBy = sellBy;
}
}
public class Orange implements Fruit {
private String size;
private String condition;
private Double weight;
private Date sellBy;
public Orange (String size, String condition, Double weight, Date sellBy){
this.size = size;
this.condition = condition;
this.weight = weight;
this.sellBy = sellBy;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
condition = condition;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public Date getSellBy() {
return sellBy;
}
public void setSellBy(Date sellBy) {
this.sellBy = sellBy;
}
}
以下是两个测试用例:
测试案例1
@Test public void testCompareAppleAndOrangeIface() {
Apple apple = new Apple(Fruit.SIZE_LARGE,Fruit.CONDITION_RIPE, 0.3, new Date()); //(String size, String condition, Double weight, Date sellBy)
Orange orange = new Orange(Fruit.SIZE_SMALL,Fruit.CONDITION_RIPE, 0.2, new Date());
DiffNode diff = ObjectDifferBuilder.buildDefault().compare(apple, orange);
LOGGER.info("\n\nComparing apples and oranges IFace...");
LOGGER.info("diff.hasChanges() " + diff.hasChanges());
LOGGER.info("diff.childCount() " + diff.childCount());
diff.visit(new DiffNode.Visitor()
{
public void node(DiffNode node, Visit visit)
{
final Object baseValue = node.canonicalGet(apple);
final Object workingValue = node.canonicalGet(orange);
final String message = node.getPath() + " changed from " +
baseValue + " to " + workingValue;
LOGGER.info(message);
}
});
}
结果:
Comparing apples and oranges IFace...
[main] INFO xxx.compare.test.CompareTest - diff.hasChanges() true
[main] INFO xxx.compare.test.CompareTest - diff.childCount() 0
[main] INFO xxx.compare.test.CompareTest - / changed from xxx.compare.test.Apple@368102c8 to xxx.compare.test.Orange@6996db8
[main] INFO xxx.compare.test.CompareTest -
测试案例2
@Test public void testCompareFruitIface() {
LOGGER.info("\n\nComparing fruit Iface...");
Fruit fruitApple = new Apple(Fruit.SIZE_LARGE,Fruit.CONDITION_RIPE, 0.3, new Date()); //(String size, String condition, Double weight, Date sellBy)
Fruit fruitOrange = new Orange(Fruit.SIZE_SMALL,Fruit.CONDITION_RIPE, 0.2, new Date());
DiffNode diff = ObjectDifferBuilder.buildDefault().compare(fruitApple, fruitOrange);
LOGGER.info("diff.hasChanges() " + diff.hasChanges());
LOGGER.info("diff.childCount() " + diff.childCount());
diff.visit(new DiffNode.Visitor()
{
public void node(DiffNode node, Visit visit)
{
// Also tried this with node.get(fruitApple), same results
final Object baseValue = node.canonicalGet(fruitApple);
final Object workingValue = node.canonicalGet(fruitOrange);
final String message = node.getPath() + " changed from " +
baseValue + " to " + workingValue;
LOGGER.info(message);
}
});
}
结果:
Comparing fruit Iface...
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO xxx.compare.test.CompareTest - diff.hasChanges() true
[main] INFO xxx.compare.test.CompareTest - diff.childCount() 0
[main] INFO xxx.compare.test.CompareTest - / changed from xxx.compare.test.Apple@1963006a to xxx.compare.test.Orange@7fbe847c
[main] INFO xxx.compare.test.CompareTest -