1+ /*
2+ * Copyright 2016 Daniel Bechler
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package de.danielbechler.diff.issues.issue155
18+
19+ import de.danielbechler.diff.ObjectDifferBuilder
20+ import de.danielbechler.diff.identity.IdentityStrategy
21+ import de.danielbechler.diff.node.DiffNode
22+ import de.danielbechler.diff.node.Visit
23+ import de.danielbechler.diff.path.NodePath
24+ import de.danielbechler.diff.selector.CollectionItemElementSelector
25+ import de.danielbechler.diff.selector.MapKeyElementSelector
26+ import spock.lang.Specification
27+
28+ public class MapIssuesTest extends Specification {
29+
30+ static class LocationIdentityStrategy implements IdentityStrategy {
31+
32+ @Override
33+ boolean equals (workingLocation , baseLocation ) {
34+ if (workingLocation. city == baseLocation. city) {
35+ return true
36+ }
37+ return false
38+ }
39+ }
40+
41+ static class LeafNodeCountingVisitor implements DiffNode. Visitor {
42+ int leafNodeCount = 0
43+
44+ @Override
45+ void node (DiffNode node , Visit visit ) {
46+ if (node. childCount() == 0 ) {
47+ leafNodeCount++ ;
48+ }
49+ }
50+ }
51+
52+ def ' compare with collections' () {
53+ given :
54+ def sharedCity = ' city'
55+ def working = [name : ' alice' , locations : [[street : ' street1' , city : sharedCity]]]
56+ def base = [name : ' alice' , locations : [[street : ' street2' , city : sharedCity]]]
57+
58+ when :
59+ def locationPath = NodePath . startBuilding(). mapKey(' locations' ). build()
60+ def locationIdentityStrategy = new LocationIdentityStrategy ()
61+ def node = ObjectDifferBuilder . startBuilding()
62+ .identity()
63+ .ofCollectionItems(locationPath). via(locationIdentityStrategy)
64+ .and(). build()
65+ .compare(working, base);
66+
67+ then :
68+ def leafNodeCountingVisitor = new LeafNodeCountingVisitor ()
69+ node. visit(leafNodeCountingVisitor)
70+ leafNodeCountingVisitor. leafNodeCount == 1
71+
72+ and :
73+ def streetNode = node. getChild([
74+ new MapKeyElementSelector (' locations' ),
75+ new CollectionItemElementSelector ([city : sharedCity]),
76+ new MapKeyElementSelector (' street' )
77+ ])
78+ streetNode. state == DiffNode.State . CHANGED
79+ }
80+ }
0 commit comments