-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consider instance name in TreeReference comparison (#450)
- Loading branch information
1 parent
a457a7d
commit 196cd18
Showing
3 changed files
with
88 additions
and
7 deletions.
There are no files selected for viewing
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
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); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/org/javarosa/core/model/instance/TreeReferenceEqualsTest.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,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)); | ||
} | ||
} |