forked from javers/javers
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
javers#605 Add List compare Algorithm SET.
- Loading branch information
Kobyshev Sergey
committed
Nov 21, 2017
1 parent
793d89c
commit 88bf454
Showing
6 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
javers-core/src/main/java/org/javers/core/diff/ListCompareAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
javers-core/src/main/java/org/javers/core/diff/appenders/SetListChangeAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.javers.core.diff.appenders; | ||
|
||
import org.javers.core.diff.NodePair; | ||
import org.javers.core.diff.changetype.container.ContainerElementChange; | ||
import org.javers.core.diff.changetype.container.ListChange; | ||
import org.javers.core.metamodel.object.OwnerContext; | ||
import org.javers.core.metamodel.object.PropertyOwnerContext; | ||
import org.javers.core.metamodel.type.*; | ||
|
||
import java.util.List; | ||
|
||
import static org.javers.common.collections.Sets.asSet; | ||
|
||
/** | ||
* @author Sergey Kobyshev | ||
*/ | ||
public class SetListChangeAppender extends CorePropertyChangeAppender<ListChange> { | ||
|
||
private final SetChangeAppender setChangeAppender; | ||
private final TypeMapper typeMapper; | ||
|
||
SetListChangeAppender(SetChangeAppender setChangeAppender, TypeMapper typeMapper) { | ||
this.setChangeAppender = setChangeAppender; | ||
this.typeMapper = typeMapper; | ||
} | ||
|
||
@Override | ||
public boolean supports(JaversType propertyType) { | ||
return propertyType instanceof ListType; | ||
} | ||
|
||
@Override | ||
public ListChange calculateChanges(final NodePair pair, final JaversProperty property) { | ||
List leftList = (List) pair.getLeftPropertyValue(property); | ||
List rightList = (List) pair.getRightPropertyValue(property); | ||
|
||
ListType listType = ((JaversProperty) property).getType(); | ||
OwnerContext owner = new PropertyOwnerContext(pair.getGlobalId(), property.getName()); | ||
SetType setType = typeMapper.getSetType(listType); | ||
|
||
List<ContainerElementChange> entryChanges = | ||
setChangeAppender.calculateEntryChanges(setType, leftList == null ? null : asSet(leftList), rightList == null ? null : asSet(rightList), owner); | ||
|
||
if (!entryChanges.isEmpty()) { | ||
renderNotParametrizedWarningIfNeeded(setType.getItemType(), "item", "Set", property); | ||
return new ListChange(pair.getGlobalId(), property.getName(), entryChanges); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
javers-core/src/test/groovy/org/javers/core/diff/appenders/SetListChangeAppenderTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.javers.core.diff.appenders | ||
|
||
import org.javers.core.model.DummyUser | ||
import org.javers.core.model.SnapshotEntity | ||
import spock.lang.Unroll | ||
|
||
import java.time.LocalDate | ||
|
||
import static org.javers.core.diff.appenders.ContainerChangeAssert.getAssertThat | ||
import static org.javers.core.model.DummyUser.dummyUser | ||
|
||
/** | ||
* @author Sergey Kobyshev | ||
*/ | ||
class SetListChangeAppenderTest extends AbstractDiffAppendersTest { | ||
|
||
@Unroll | ||
def "should append #changesCount changes when left list is #leftList and right list is #rightList"() { | ||
|
||
when: | ||
def leftNode = dummyUser().withIntegerList(leftList) | ||
def rightNode = dummyUser().withIntegerList(rightList) | ||
|
||
def change = setListChangeAppender().calculateChanges( | ||
realNodePair(leftNode, rightNode), getProperty(DummyUser, "integerList")) | ||
|
||
then: | ||
change.changes.size() == changesCount | ||
change.changes.each { | ||
assert it.index == null | ||
} | ||
|
||
where: | ||
leftList | rightList || changesCount | ||
null | [1, 2] || 2 | ||
[] | [1, 2] || 2 | ||
[1, 2] | [1, 2, 3, 4] || 2 | ||
[1, 2] | [2, 1, 3] || 1 | ||
[1, 2] | [] || 2 | ||
[1, 2, 3, 4] | [1] || 3 | ||
[2, 1, 3] | [1, 2] || 1 | ||
} | ||
|
||
@Unroll | ||
def "should not append changes when left list #leftList and right list #rightList are equal"() { | ||
|
||
when: | ||
def leftNode = dummyUser().withIntegerList(leftList) | ||
def rightNode = dummyUser().withIntegerList(rightList) | ||
|
||
def change = setListChangeAppender().calculateChanges( | ||
realNodePair(leftNode, rightNode), getProperty(DummyUser, "integerList")) | ||
|
||
then: | ||
change == null | ||
|
||
where: | ||
leftList | rightList | ||
[] | [] | ||
[1, 2] | [1, 2] | ||
[1, 2] | [2, 1] | ||
} | ||
|
||
def "should append ValueAdded in List of Values"() { | ||
given: | ||
def leftCdo = new SnapshotEntity(listOfDates: [new LocalDate(2001,1,1)]) | ||
def rightCdo = new SnapshotEntity(listOfDates: [new LocalDate(2001,5,5), new LocalDate(2001,1,1)]) | ||
|
||
when: | ||
def change = setListChangeAppender() | ||
.calculateChanges(realNodePair(leftCdo, rightCdo), getProperty(SnapshotEntity, "listOfDates")) | ||
|
||
then: | ||
assertThat(change) | ||
.hasSize(1) | ||
.hasValueAdded(new LocalDate(2001,5,5)) | ||
|
||
} | ||
|
||
def "should append ValueRemoved in List of Values"() { | ||
given: | ||
def leftCdo = new SnapshotEntity(listOfDates: [new LocalDate(2001,5,5), new LocalDate(2001,1,1)]) | ||
def rightCdo = new SnapshotEntity(listOfDates: [new LocalDate(2001,1,1)]) | ||
|
||
when: | ||
def change = setListChangeAppender() | ||
.calculateChanges(realNodePair(leftCdo, rightCdo), getProperty(SnapshotEntity, "listOfDates")) | ||
|
||
then: | ||
assertThat(change) | ||
.hasSize(1) | ||
.hasValueRemoved(new LocalDate(2001,5,5)) | ||
|
||
} | ||
|
||
} |