Skip to content

Commit

Permalink
Consider instance name in TreeReference comparison (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel authored and ggalmazor committed Jul 8, 2019
1 parent a457a7d commit 196cd18
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/main/java/org/javarosa/core/model/instance/TreeReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@

package org.javarosa.core.model.instance;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.javarosa.core.util.DataUtil;
import org.javarosa.core.util.Objects;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
import org.javarosa.core.util.externalizable.ExtWrapNullable;
Expand All @@ -32,6 +26,13 @@
import org.javarosa.xpath.XPathException;
import org.javarosa.xpath.expr.XPathExpression;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class TreeReference implements Externalizable, Serializable {
/** @deprecated replaced by {@link #DEFAULT_MULTIPLICITY} */
@Deprecated public static final int DEFAULT_MUTLIPLICITY = 0;
Expand Down Expand Up @@ -403,6 +404,10 @@ public boolean equals (Object o) {
} else if (o instanceof TreeReference) {
TreeReference ref = (TreeReference)o;

if (!Objects.equals(getInstanceName(), ref.getInstanceName())) {
return false;
}

if (this.refLevel == ref.refLevel && this.size() == ref.size()) {
for (int i = 0; i < this.size(); i++) {
String nameA = this.getName(i);
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/javarosa/core/util/Objects.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.javarosa.core.util;

public class Objects {
/**
* Null-safe equivalent of {@code a.equals(b)}.
*/
public static boolean equals(Object a, Object b) {
return (a == null) ? (b == null) : a.equals(b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.javarosa.core.model.instance;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.javarosa.core.model.instance.TestHelpers.buildRef;

@RunWith(Parameterized.class)
public class TreeReferenceEqualsTest {
@Parameterized.Parameter(value = 0)
public String testCase;

@Parameterized.Parameter(value = 1)
public String pathA;

@Parameterized.Parameter(value = 2)
public String pathB;

@Parameterized.Parameter(value = 3)
public boolean expectToBeEqual;

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> testCases() {
return Arrays.asList(new Object[][]{
// region Equality across same or different instances

// Prior to JR 1.15, the instance was not taken into account when comparing two TreeReferences. That meant
// two references representing the same path in two different instances would be considered the same reference.
{"same path in primary instance are equal", "/foo/bar", "/foo/bar", true},
{"same path in secondary instance are equal", "instance('foo')/bar/baz", "instance('foo')/bar/baz", true},
{"same path in primary and secondary instance are not equal", "/foo/bar", "instance('foo')/foo/bar", false},
{"same path in secondary and primary instance are not equal", "instance('foo')/foo/bar", "/foo/bar", false},
{"same path in different secondary instances are not equal", "instance('foo')/foo/bar", "instance('bar')/foo/bar", false},
{"different paths in primary instance are not equal", "/foo/bar", "/foo/bar/quux", false},
{"different paths in secondary instance are not equal", "instance('foo')/foo/bar", "instance('foo')/foo/bar/quux", false}
//endregion
});
}

@Test
public void treeReferenceEqualsTest() {
assertThat(buildRef(pathA).equals(buildRef(pathB)), is(expectToBeEqual));
}
}

0 comments on commit 196cd18

Please sign in to comment.