Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,23 @@ class IdentityStrategyAT extends Specification {
then:
node.untouched
}

def 'configure custom IdentityStrategyResolver'() {
given:
def strategy = new IdentityStrategy() {

boolean equals(Object working, Object base) {
return working.getAt('id') == base.getAt('id')
}
}
def objectDiffer = ObjectDifferBuilder.startBuilding()
.identity()
.setDefaultCollectionItemIdentityStrategy(strategy)
.and()
.build()
when:
def node = objectDiffer.compare([[id: '1', value: 'original']], [[id: '1', value: 'changed']])
then:
node.getChild(NodePath.startBuilding().collectionItem([id:'1']).build()).changed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
import de.danielbechler.diff.inclusion.ValueNode;
import de.danielbechler.diff.node.DiffNode;
import de.danielbechler.diff.path.NodePath;
import de.danielbechler.util.Assert;

class CollectionItemIdentityService implements IdentityStrategyResolver
{
private final ValueNode<IdentityStrategy> nodePathIdentityStrategies;
private final TypePropertyIdentityStrategyResolver typePropertyIdentityStrategyResolver;
private final IdentityConfigurer identityConfigurer;
private IdentityStrategy defaultIdentityStrategy = EqualsIdentityStrategy.getInstance();

public CollectionItemIdentityService(final IdentityConfigurer identityConfigurer)
CollectionItemIdentityService(final IdentityConfigurer identityConfigurer)
{
this.identityConfigurer = identityConfigurer;
this.nodePathIdentityStrategies = new ValueNode<IdentityStrategy>();
Expand All @@ -45,24 +47,31 @@ public IdentityStrategy resolveIdentityStrategy(final DiffNode node)
{
return identityStrategy;
}
return EqualsIdentityStrategy.getInstance();
return defaultIdentityStrategy;
}

public IdentityConfigurer.OfCollectionItems ofCollectionItems(final NodePath nodePath)
IdentityConfigurer.OfCollectionItems ofCollectionItems(final NodePath nodePath)
{
return new OfCollectionItemsByNodePath(nodePath);
}

public IdentityConfigurer.OfCollectionItems ofCollectionItems(final Class<?> type, final String propertyName)
IdentityConfigurer.OfCollectionItems ofCollectionItems(final Class<?> type, final String propertyName)
{
return new OfCollectionItemsByTypeProperty(type, propertyName);
}

IdentityConfigurer setDefaultIdentityStrategy(final IdentityStrategy identityStrategy)
{
Assert.notNull(identityStrategy, "identityStrategy");
this.defaultIdentityStrategy = identityStrategy;
return identityConfigurer;
}

private class OfCollectionItemsByNodePath implements IdentityConfigurer.OfCollectionItems
{
private final NodePath nodePath;

public OfCollectionItemsByNodePath(final NodePath nodePath)
OfCollectionItemsByNodePath(final NodePath nodePath)
{
this.nodePath = nodePath;
}
Expand All @@ -79,7 +88,7 @@ private class OfCollectionItemsByTypeProperty implements IdentityConfigurer.OfCo
private final Class<?> type;
private final String propertyName;

public OfCollectionItemsByTypeProperty(final Class<?> type, final String propertyName)
OfCollectionItemsByTypeProperty(final Class<?> type, final String propertyName)
{
this.type = type;
this.propertyName = propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public interface IdentityConfigurer
*/
OfCollectionItems ofCollectionItems(Class<?> type, String propertyName);

IdentityConfigurer setDefaultCollectionItemIdentityStrategy(IdentityStrategy identityStrategy);

ObjectDifferBuilder and();

interface OfCollectionItems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public OfCollectionItems ofCollectionItems(final Class<?> type, final String pro
return collectionItemIdentityService.ofCollectionItems(type, propertyName);
}

public IdentityConfigurer setDefaultCollectionItemIdentityStrategy(final IdentityStrategy identityStrategy)
{
return collectionItemIdentityService.setDefaultIdentityStrategy(identityStrategy);
}

public IdentityStrategy resolveIdentityStrategy(final DiffNode node)
{
return collectionItemIdentityService.resolveIdentityStrategy(node);
Expand Down