Javers在查找带有com.googlecode.objectify.annotation.Id注释的字段时返回不同的结果

时间:2018-10-05 10:53:22

标签: java javers

我正在使用Javers 3.11.6查找2个Java对象之间的差异。
当我在有无@@(com.googlecode.objectify.annotation.Id)批注的情况下运行测试时,Javers将返回不同的更改类型。

代码如下:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.diff.Change;
import org.javers.core.diff.Diff;
import org.javers.core.diff.changetype.NewObject;
import org.javers.core.diff.changetype.ObjectRemoved;
import org.javers.core.diff.changetype.PropertyChange;
import org.junit.Test;

import com.google.gson.Gson;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;

public class VersionDifferencesTest
{
    @Test
    public void viewDiffrences()
    {
        MyDomainBean version1 = getDomainBean(0);
        MyDomainBean version2 = getDomainBean(1);
        List<String> changeList = getVersionDifferences(version1, version2);

        System.out.println(version1);
        System.out.println(version2);
        System.out.println(changeList);
    }

    public List<String> getVersionDifferences(MyDomainBean version1, MyDomainBean version2)
    {
        List<String> changeList = new ArrayList<>();
        Gson gson = new Gson();

        Javers javers = JaversBuilder.javers().build();
        Diff diff = javers.compare(gson.fromJson(gson.toJson(version1), MyDomainBean.class), gson.fromJson(gson.toJson(version2), MyDomainBean.class));

        String className= MyDomainBean.class.getName();
        diff.getChanges()
            .forEach(change -> {
                                    String cleanDiffPropertyName = "";

                                    if(change instanceof NewObject)
                                    {
                                        NewObject nochange = (NewObject) change;
                                        cleanDiffPropertyName = nochange.getAffectedGlobalId().toString();
                                        System.out.println("NewObject [" + cleanDiffPropertyName + "]");
                                    }
                                    else if(change instanceof ObjectRemoved)
                                    {
                                        ObjectRemoved orchange = (ObjectRemoved) change;
                                        cleanDiffPropertyName = orchange.getAffectedGlobalId().toString();
                                        System.out.println("ObjectRemoved [" + cleanDiffPropertyName + "]");
                                    }
                                    else if(change instanceof PropertyChange)
                                    {
                                        PropertyChange pchange = (PropertyChange) change;
                                        cleanDiffPropertyName = pchange.getPropertyNameWithPath().toString();
                                        System.out.println("PropertyChange [" + cleanDiffPropertyName + "]");
                                    }

                                    changeList.add(cleanDiffPropertyName);
                                });

        return changeList;
    }

    public MyDomainBean getDomainBean(int version)
    {
        MyDomainBean domain = new MyDomainBean();

        domain.setDomainName("wwwcooljams");
        domain.setVersion("wwwcooljams_" + version);
        domain.setUrl("http://www.cool-jams.co.in" + version);
        domain.setDomainHost("http://www.cool-jams.co.in");

        return domain;
    }
}

@Entity
@Cache
class MyDomainBean implements Serializable 
{
    private static final long serialVersionUID = 3376280640820462872L;

    @Index private String domainName;
    @Id private String version;
    private String url;
    @Index private String domainHost;

    @Override
    public String toString()
    {
        return "MyDomainBean ["
                + " domainName=" + domainName 
                + ", version=" + version 
                + ", url=" + url 
                + ", domainHost=" + domainHost 
                + "]";
    }

    public String getDomainName()
    {
        return domainName;
    }
    public void setDomainName(String domainName)
    {
        this.domainName = domainName;
    }
    public String getVersion()
    {
        return version;
    }
    public void setVersion(String version)
    {
        this.version = version;
    }
    public String getUrl()
    {
        return url;
    }
    public void setUrl(String url)
    {
        this.url = url;
    }
    public String getDomainHost()
    {
        return domainHost;
    }
    public void setDomainHost(String domainHost)
    {
        this.domainHost = domainHost;
    }
}

version1:{domainName = wwwcooljams,version = wwwcooljams_0,url = http://www.cool-jams.co.in0,domainHost = http://www.cool-jams.co.in}
版本2:{domainName = wwwcooljams,version = wwwcooljams_1,url = http://www.cool-jams.co.in1,domainHost = http://www.cool-jams.co.in} [... MyDomainBean / wwwcooljams_1,... MyDomainBean / wwwcooljams_0]

带有@Id的输出;错误的一个:
NewObject [... MyDomainBean / wwwcooljams_1]
ObjectRemoved [... MyDomainBean / wwwcooljams_0]

不带@Id的输出:更正一个:
PropertyChange [版本]
PropertyChange [url]

谢谢, Dhananjay

0 个答案:

没有答案