Skip to content

CustomValueComparator not being invoked #925

@vinnybod

Description

@vinnybod

Cross posting from: https://stackoverflow.com/questions/59185469/javers-customvaluecomparator-for-zoneddatetime

I am using JaVers 5.8.5, which is the latest as of this post.

I want to compare two objects that contain ZonedDateTime using JaVers. The issue is that it finds differences when the zones don't match up, but I'd really like it to treat the ZonedDateTimes as instants, since the timezones aren't always guaranteed in these situations.

It just doesn't seem like JaVers is using the custom comparator that I supplied. I provided a slimmed down test below. Note that I've tried the CustomValueComparator class, both as an anonymous class and not, and I've tried using the version of registerValue that takes the two lambdas.

import lombok.Builder;
import lombok.Value;
import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.diff.Diff;
import org.javers.core.diff.ListCompareAlgorithm;
import org.javers.core.diff.custom.CustomValueComparator;
import org.junit.Before;
import org.junit.Test;

import java.time.ZoneId;
import java.time.ZonedDateTime;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

public class JaversTest {

    private Javers javers;

    @Before
    public void setUp() throws Exception {
        javers = JaversBuilder
                .javers()
                // using CustomValueComparator
                .registerValue(ZonedDateTime.class, new CustomValueComparator<ZonedDateTime>() {
                    @Override
                    public boolean equals(ZonedDateTime a, ZonedDateTime b) {
                        return a.toInstant().equals(b.toInstant());
                    }

                    @Override
                    public String toString(ZonedDateTime value) {
                        return String.valueOf(value.toInstant().getEpochSecond());
                    }
                })
                // using lambdas
                .registerValue(ZonedDateTime.class,
                        (a, b) -> a.toInstant().getEpochSecond() == b.toInstant().getEpochSecond(),
                        (a) -> String.valueOf(a.toInstant().getEpochSecond()))
                .build();
    }

    @Test
    public void testZonedDateTime() {
        final ZonedDateTime now = ZonedDateTime.now();
        final ZonedDateTimeHolder a = ZonedDateTimeHolder.builder()
                .id(1L)
                .zonedDateTime(now.withZoneSameInstant(ZoneId.of("UTC"))).build();
        final ZonedDateTimeHolder b = ZonedDateTimeHolder.builder()
                .id(1L)
                .zonedDateTime(now.withZoneSameInstant(ZoneId.of("Z"))).build();
        final Diff compare = javers.compare(a, b);

        assertThat(compare.getChanges().size(), is(0));
    }

    @TypeName("ZonedDateTimeHolder")
    @Builder
    @Value
    public static class ZonedDateTimeHolder {

        @Id
        private long id;

        private ZonedDateTime zonedDateTime;
    }
}

Output of compare.toString()
Diff:
* changes on io.freestar.auditlog.JaversTest$ZonedDateTimeHolder/ :
  - 'zonedDateTime' value changed from '04 Dec 2019, 22:15:04+0000' to '04 Dec 2019, 22:15:04+0000'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions