0

我正在使用 javers ( https://javers.org/ ) 并尝试比较实现相同接口的两个对象。作为一个例子,我用苹果和橘子设置了一个测试。Apples to Apples 工作正常,但不是 Apples to Oranges。它似乎运行了比较,但没有获取新值。

如果我有一个 Fruit 超类和扩展它的苹果和橘子,但接口没有运气,这很好用。任何熟悉这个 API 的人都可以为我指明正确的方向。

这是模型代码:

@TypeName("FruitIface")
@Entity
public interface FruitIface {

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();

}



@TypeName("FruitIface")
@Entity
public class Apple implements FruitIface {


private String size;
private String condition;
private Double weight;
private Date sellBy;
@Id
private String compareId;

public Apple (String id,String size, String condition, Double weight, Date sellBy){
    this.compareId = id;
    this.size = size;
    this.condition = condition;
    this.weight = weight;
    this.sellBy = sellBy;
}
    ....

}


@TypeName("FruitIface")
@Entity
public class Orange implements FruitIface {

@PropertyName("size")
private String size;
private String condition;
private Double weight;
private Date sellBy;
@Id
private String compareId;


public Orange (String id, String size, String condition, Double weight, Date sellBy){
    this.compareId = id;
    this.size = size;
    this.condition = condition;
    this.weight = weight;
    this.sellBy = sellBy;
}
....
}

测试用例:

@Test public void testCompareAppleToAppleIface() {
    Apple appleOne = new Apple("01",FruitIface.SIZE_LARGE,FruitIface.CONDITION_RIPE, 0.3, new Date());
    Apple appleTwo = new Apple("01",FruitIface.SIZE_SMALL,FruitIface.CONDITION_RIPE, 0.2, new Date());

    LOGGER.info("\n\nComparing apples to apples...");
    Diff diff = javers.compare(appleOne, appleTwo);
    LOGGER.info("diff: " + diff);

}


@Test public void testCompareAppleAndOrangeIface() {
    Apple apple = new Apple("01",FruitIface.SIZE_LARGE,FruitIface.CONDITION_RIPE, 0.3, new Date()); //(String size, String condition, Double weight, Date sellBy)
    Orange orange = new Orange("01",FruitIface.SIZE_SMALL,FruitIface.CONDITION_RIPE, 0.2, new Date());


    LOGGER.info("\n\nComparing apples and oranges IFace...");
    Diff diff = javers.compare(apple, orange);
    LOGGER.info("diff: " + diff);
}

结果:

Comparing apples to apples...
[main] INFO xxx.compare.test.JaversCompareTestIface - diff: Diff:
1. ValueChange{globalId:'FruitIface/01', property:'size', oldVal:'Large', newVal:'Small'}
2. ValueChange{globalId:'FruitIface/01', property:'weight', oldVal:'0.3', newVal:'0.2'}


Comparing apples and oranges IFace...
[main] INFO xxx.compare.test.JaversCompareTestIface - diff: Diff:
1. ValueChange{globalId:'FruitIface/01', property:'size', oldVal:'Large', newVal:''}
2. ValueChange{globalId:'FruitIface/01', property:'condition', oldVal:'Ripe', newVal:''}
3. ValueChange{globalId:'FruitIface/01', property:'weight', oldVal:'0.3', newVal:''}
4. ValueChange{globalId:'FruitIface/01', property:'sellBy', oldVal:'Thu Aug 10 12:14:45 PDT 2017', newVal:''}
5. ValueChange{globalId:'FruitIface/01', property:'compareId', oldVal:'01', newVal:''}
4

1 回答 1

0

对多个类使用相同的 @TypeName 是一种误用,原因与对多个 Java 类使用相同的类名相同。

在这种情况下,JaVers 应该抛出异常,因为 JaVers 类型系统会混淆,所以您会得到某种随机结果。

在当前版本的 JaVers 中,即使 Apples 和 Oranges 都实现了 Fruit 接口,也无法将它们进行比较。JaVers 匹配相同的对象GlobalId并进行比较。当您拥有 ID 为 1 的 Apple 和 ID 为 1 的 Orange 时,它​​们的 GlobalId 是Apple#1Orange#1因此它们不会作为同一对象的两个版本匹配。

于 2017-08-12T19:20:48.663 回答