Skip to content

Commit 44e63c6

Browse files
tomekl007adamd1985
authored andcommitted
Bael 769 javers (eugenp#1543)
* BAEL-769 code for javers article * BAEL-769 add more examples
1 parent 0932951 commit 44e63c6

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed

libraries/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
<artifactId>jsonassert</artifactId>
6868
<version>${jsonassert.version}</version>
6969
</dependency>
70+
<dependency>
71+
<groupId>org.javers</groupId>
72+
<artifactId>javers-core</artifactId>
73+
<version>${javers.version}</version>
74+
</dependency>
7075
</dependencies>
7176

7277
<properties>
@@ -78,6 +83,7 @@
7883
<javaassist.version>3.21.0-GA</javaassist.version>
7984
<assertj.version>3.6.2</assertj.version>
8085
<jsonassert.version>1.5.0</jsonassert.version>
86+
<javers.version>3.1.0</javers.version>
8187
</properties>
8288

8389
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.javers;
2+
3+
4+
public class Address {
5+
private String country;
6+
7+
public Address(String country) {
8+
this.country = country;
9+
}
10+
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.javers;
2+
3+
public class Person {
4+
private Integer id;
5+
private String name;
6+
7+
public Person(Integer id, String name) {
8+
this.id = id;
9+
this.name = name;
10+
}
11+
12+
public Integer getId() {
13+
return id;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setId(Integer id) {
21+
this.id = id;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.javers;
2+
3+
4+
import java.util.List;
5+
6+
public class PersonWithAddress {
7+
private Integer id;
8+
private String name;
9+
private List<Address> address;
10+
11+
public PersonWithAddress(Integer id, String name, List<Address> address) {
12+
this.id = id;
13+
this.name = name;
14+
this.address = address;
15+
}
16+
17+
public Integer getId() {
18+
return id;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setId(Integer id) {
26+
this.id = id;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public List<Address> getAddress() {
34+
return address;
35+
}
36+
37+
public void setAddress(List<Address> address) {
38+
this.address = address;
39+
}
40+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.baeldung.javers;
2+
3+
4+
import org.javers.common.collections.Lists;
5+
import org.javers.core.Javers;
6+
import org.javers.core.JaversBuilder;
7+
import org.javers.core.diff.Diff;
8+
import org.javers.core.diff.changetype.NewObject;
9+
import org.javers.core.diff.changetype.ObjectRemoved;
10+
import org.javers.core.diff.changetype.ValueChange;
11+
import org.javers.core.diff.changetype.container.ListChange;
12+
import org.junit.Test;
13+
14+
import java.util.Arrays;
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
public class JaversTest {
21+
22+
@Test
23+
public void givenPersonObject_whenApplyModificationOnIt_thenShouldDetectChange() {
24+
//given
25+
Javers javers = JaversBuilder.javers().build();
26+
27+
Person person = new Person(1, "Michael Program");
28+
Person personAfterModification = new Person(1, "Michael Java");
29+
30+
//when
31+
Diff diff = javers.compare(person, personAfterModification);
32+
33+
//then
34+
ValueChange change = diff.getChangesByType(ValueChange.class).get(0);
35+
36+
assertThat(diff.getChanges()).hasSize(1);
37+
assertThat(change.getPropertyName()).isEqualTo("name");
38+
assertThat(change.getLeft()).isEqualTo("Michael Program");
39+
assertThat(change.getRight()).isEqualTo("Michael Java");
40+
}
41+
42+
43+
@Test
44+
public void givenListOfPersons_whenCompare_ThenShouldDetectChanges() {
45+
//given
46+
Javers javers = JaversBuilder.javers().build();
47+
Person personThatWillBeRemoved = new Person(2, "Thomas Link");
48+
List<Person> oldList = Lists.asList(new Person(1, "Michael Program"), personThatWillBeRemoved);
49+
List<Person> newList = Lists.asList(new Person(1, "Michael Not Program"));
50+
51+
//when
52+
Diff diff = javers.compareCollections(oldList, newList, Person.class);
53+
54+
//then
55+
assertThat(diff.getChanges()).hasSize(3);
56+
57+
58+
ValueChange valueChange = diff.getChangesByType(ValueChange.class).get(0);
59+
assertThat(valueChange.getPropertyName()).isEqualTo("name");
60+
assertThat(valueChange.getLeft()).isEqualTo("Michael Program");
61+
assertThat(valueChange.getRight()).isEqualTo("Michael Not Program");
62+
63+
ObjectRemoved objectRemoved = diff.getChangesByType(ObjectRemoved.class).get(0);
64+
assertThat(objectRemoved.getAffectedObject().get().equals(personThatWillBeRemoved)).isTrue();
65+
66+
ListChange listChange = diff.getChangesByType(ListChange.class).get(0);
67+
assertThat(listChange.getValueRemovedChanges().size()).isEqualTo(1);
68+
69+
}
70+
71+
@Test
72+
public void givenListOfPerson_whenPersonHasNewAddress_thenDetectThatChange() {
73+
//given
74+
Javers javers = JaversBuilder.javers().build();
75+
76+
PersonWithAddress person =
77+
new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England")));
78+
79+
PersonWithAddress personWithNewAddress =
80+
new PersonWithAddress(1, "Tom",
81+
Arrays.asList(new Address("England"), new Address("USA")));
82+
83+
84+
//when
85+
Diff diff = javers.compare(person, personWithNewAddress);
86+
List objectsByChangeType = diff.getObjectsByChangeType(NewObject.class);
87+
88+
//then
89+
assertThat(objectsByChangeType).hasSize(1);
90+
assertThat(objectsByChangeType.get(0).equals(new Address("USA")));
91+
}
92+
93+
@Test
94+
public void givenListOfPerson_whenPersonRemovedAddress_thenDetectThatChange() {
95+
//given
96+
Javers javers = JaversBuilder.javers().build();
97+
98+
PersonWithAddress person =
99+
new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England")));
100+
101+
PersonWithAddress personWithNewAddress =
102+
new PersonWithAddress(1, "Tom", Collections.emptyList());
103+
104+
105+
//when
106+
Diff diff = javers.compare(person, personWithNewAddress);
107+
List objectsByChangeType = diff.getObjectsByChangeType(ObjectRemoved.class);
108+
109+
//then
110+
assertThat(objectsByChangeType).hasSize(1);
111+
assertThat(objectsByChangeType.get(0).equals(new Address("England")));
112+
}
113+
}

0 commit comments

Comments
 (0)