Skip to content

Commit 2869042

Browse files
committed
Fixed SQiShER#13. Type detection os now much more reliable.
1 parent 9ca65e0 commit 2869042

File tree

9 files changed

+60
-21
lines changed

9 files changed

+60
-21
lines changed

src/main/java/de/danielbechler/diff/BeanDiffer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Node compare(final Object working, final Object base)
5353

5454
public Node compare(final Node parentNode, final Instances instances)
5555
{
56-
final Node node = new DefaultNode(parentNode, instances.getSourceAccessor());
56+
final Node node = new DefaultNode(parentNode, instances.getSourceAccessor(), instances.getType());
5757
if (getDelegate().isIgnored(node))
5858
{
5959
node.setState(Node.State.IGNORED);
@@ -71,7 +71,7 @@ else if (instances.getType() == null)
7171

7272
private Node compareBean(final Node parentNode, final Instances instances)
7373
{
74-
final Node node = new DefaultNode(parentNode, instances.getSourceAccessor());
74+
final Node node = new DefaultNode(parentNode, instances.getSourceAccessor(), instances.getType());
7575
if (instances.hasBeenAdded())
7676
{
7777
node.setState(Node.State.ADDED);
@@ -110,7 +110,7 @@ private void compareProperties(final Node parentNode, final Instances instances)
110110
{
111111
for (final Accessor accessor : introspect(instances.getType()))
112112
{
113-
final Node node = new DefaultNode(parentNode, accessor);
113+
final Node node = new DefaultNode(parentNode, accessor, instances.getType());
114114
if (getDelegate().isIgnored(node))
115115
{
116116
if (getDelegate().isReturnable(node))

src/main/java/de/danielbechler/diff/CollectionDiffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public CollectionNode compare(final Collection<?> working, final Collection<?> b
4646

4747
public CollectionNode compare(final Node parentNode, final Instances instances)
4848
{
49-
final CollectionNode node = new CollectionNode(parentNode, instances.getSourceAccessor());
49+
final CollectionNode node = new CollectionNode(parentNode, instances.getSourceAccessor(), instances.getType());
5050
if (getDelegate().isIgnored(node))
5151
{
5252
node.setState(Node.State.IGNORED);

src/main/java/de/danielbechler/diff/Configuration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public boolean isExcluded(final Node node)
140140
@Override
141141
public boolean isEqualsOnly(final Node node)
142142
{
143-
final Class<?> propertyType = node.getPropertyType();
143+
final Class<?> propertyType = node.getValueType();
144144
if (propertyType != null)
145145
{
146146
if (propertyType.getAnnotation(ObjectDiffEqualsOnlyType.class) != null)

src/main/java/de/danielbechler/diff/MapDiffer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public MapNode compare(final Map<?, ?> modified, final Map<?, ?> base)
4646

4747
public MapNode compare(final Node parentNode, final Instances instances)
4848
{
49-
final MapNode node = new MapNode(parentNode, instances.getSourceAccessor());
49+
final MapNode node = new MapNode(parentNode, instances.getSourceAccessor(), instances.getType());
5050

5151
if (getConfiguration().isIgnored(node))
5252
{
@@ -115,7 +115,7 @@ private Node compareEntry(final Object key, Instances instances, final MapNode p
115115
if (instances.areSame())
116116
{
117117
// if the instances are the same, there is no need to delegate
118-
return new DefaultNode(parent, accessor);
118+
return new DefaultNode(parent, accessor, instances.getType());
119119
}
120120
return getDelegate().delegate(parent, instances);
121121
}

src/main/java/de/danielbechler/diff/node/CollectionNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
/** @author Daniel Bechler */
2525
public final class CollectionNode extends DefaultNode
2626
{
27-
public CollectionNode(final Node parent, final Accessor accessor)
27+
public CollectionNode(final Node parent, final Accessor accessor, final Class<?> valueType)
2828
{
29-
super(parent, accessor);
29+
super(parent, accessor, valueType);
3030
}
3131

3232
public Accessor accessorForItem(final Object item)

src/main/java/de/danielbechler/diff/node/DefaultNode.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,20 @@ public class DefaultNode implements Node
3232

3333
private State state = State.UNTOUCHED;
3434
private Node parentNode;
35+
private Class<?> valueType;
3536

36-
public DefaultNode(final Node parentNode, final Accessor accessor)
37+
/**
38+
*
39+
* @param parentNode
40+
* @param accessor
41+
* @param valueType
42+
*/
43+
public DefaultNode(final Node parentNode, final Accessor accessor, final Class<?> valueType)
3744
{
3845
Assert.notNull(accessor, "accessor");
3946
this.parentNode = parentNode;
4047
this.accessor = accessor;
48+
this.valueType = valueType;
4149
}
4250

4351
public State getState()
@@ -108,13 +116,18 @@ public MapNode toMapDifference()
108116
}
109117

110118
@Override
111-
public Class<?> getPropertyType()
119+
public Class<?> getValueType()
112120
{
113121
if (accessor instanceof TypeAwareAccessor)
114122
{
115123
return ((TypeAwareAccessor) accessor).getPropertyType();
116124
}
117-
return null;
125+
return valueType;
126+
}
127+
128+
public void setValueType(final Class<?> valueType)
129+
{
130+
this.valueType = valueType;
118131
}
119132

120133
public boolean hasChildren()
@@ -328,9 +341,9 @@ public String toString()
328341
final StringBuilder sb = new StringBuilder();
329342
sb.append(getPropertyPath());
330343
sb.append(" = { ").append(getState().toString().toLowerCase());
331-
if (getPropertyType() != null)
344+
if (getValueType() != null)
332345
{
333-
sb.append(", type is ").append(getPropertyType().getCanonicalName());
346+
sb.append(", type is ").append(getValueType().getCanonicalName());
334347
}
335348
if (getChildren().size() == 1)
336349
{

src/main/java/de/danielbechler/diff/node/MapNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public class MapNode extends DefaultNode
2626
{
2727
private final List<Object> referenceKeys = new LinkedList<Object>();
2828

29-
public MapNode(final Node parentNode, final Accessor accessor)
29+
public MapNode(final Node parentNode, final Accessor accessor, final Class<?> valueType)
3030
{
31-
super(parentNode, accessor);
31+
super(parentNode, accessor, valueType);
3232
}
3333

3434
public final int indexKey(final Object key)

src/main/java/de/danielbechler/diff/node/Node.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import java.util.*;
2424

2525
/**
26-
* Represents a part of an object. It could be the object itself, one of its properties, an item in a collection or a map
27-
* entry. A node may one parent node and any number of children. It also provides methods to read and write the property
28-
* represented by this node on any object of the same type as the original object. Last but not least, a node knows how the
29-
* associated property has changed compared to the base object.
26+
* Represents a part of an object. It could be the object itself, one of its properties, an item in a
27+
* collection or a map entry. A node may one parent node and any number of children. It also provides methods
28+
* to read and write the property represented by this node on any object of the same type as the original
29+
* object. Last but not least, a node knows how the associated property has changed compared to the base
30+
* object.
3031
*
3132
* @author Daniel Bechler
3233
*/
@@ -93,7 +94,15 @@ public enum State
9394
MapNode toMapDifference();
9495

9596
/** @return Returns the type of the property represented by this node, or null if unavailable. */
96-
Class<?> getPropertyType();
97+
Class<?> getValueType();
98+
99+
/**
100+
* Allows for explicit type definition. However, if the accessor is TypeAware, {@link #getValueType()} will
101+
* always return the type returned by the accessor.
102+
*
103+
* @param propertyType The type of the value represented by this node.
104+
*/
105+
void setValueType(Class<?> propertyType);
97106

98107
/** @return The absolute property path from the object root up to this node. */
99108
PropertyPath getPropertyPath();

src/test/java/de/danielbechler/diff/MapDifferTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,21 @@ public void testWithSameEntries()
167167
assertThat(pingNode, IsNull.notNullValue());
168168
assertThat(pingNode.getState(), is(Node.State.ADDED));
169169
}
170+
171+
@Test
172+
public void testWithChangedEntry()
173+
{
174+
final Map<String, String> working = new LinkedHashMap<String, String>(1);
175+
working.put("foo", "bar");
176+
177+
final Map<String, String> base = new LinkedHashMap<String, String>(1);
178+
base.put("foo", "woot");
179+
180+
final MapNode node = differ.compare(working, base);
181+
assertThat("Node should have exactly one child", node.getChildren().size(), is(1));
182+
assertThat("Child node should have changed", node.getChildren()
183+
.iterator()
184+
.next()
185+
.getState(), is(Node.State.CHANGED));
186+
}
170187
}

0 commit comments

Comments
 (0)