Skip to content

Commit

Permalink
Recognizing prefix changes in item paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Jul 16, 2014
1 parent 7d1a663 commit 146de80
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
Expand Up @@ -86,7 +86,7 @@ public int hashCode() {
*/
@Override
public boolean equals(Object obj) {
return equals(obj, true);
return equals(obj, false, false);
}

/**
Expand All @@ -98,31 +98,52 @@ public boolean equals(Object obj) {

@Override
public boolean equivalent(Object obj) {
return equals(obj, false);
return equals(obj, true, true);
}

public boolean equals(Object obj, boolean strictEquals) {
if (this == obj)
public boolean equals(Object obj, boolean allowUnqualified, boolean allowDifferentPrefixes) {
if (this == obj) {
return true;
if (!super.equals(obj))
}
if (!super.equals(obj)) {
return false;
if (getClass() != obj.getClass())
}
if (getClass() != obj.getClass()) {
return false;
NameItemPathSegment other = (NameItemPathSegment) obj;
if (isVariable != other.isVariable)
}
NameItemPathSegment other = (NameItemPathSegment) obj;
if (isVariable != other.isVariable) {
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!strictEquals && (StringUtils.isEmpty(name.getNamespaceURI()) || StringUtils.isEmpty(other.name.getNamespaceURI()))) {
if (!QNameUtil.match(name, other.name)){
return false;
}
} else if (!name.equals(other.name))
return false;
return true;
}
if (name == null) {
return other.name != null;
}

if (allowUnqualified) {
if (!allowDifferentPrefixes) {
throw new UnsupportedOperationException("It is not possible to disallow different prefixes while allowing unqualified names");
}
return QNameUtil.match(name, other.name);
} else {
if (!name.equals(other.name)) { // compares namespace and local part
return false;
}
// in order to differentiate between x:name and name (when x is undefined) we will compare the prefixes as well
if (!allowDifferentPrefixes && !normalizedPrefix(name).equals(normalizedPrefix(other.name))) {
return false;
}
return true;
}
}

private String normalizedPrefix(QName name) {
if (name.getPrefix() == null) {
return "";
} else {
return name.getPrefix();
}
}

public NameItemPathSegment clone() {
NameItemPathSegment clone = new NameItemPathSegment(this.name, this.isVariable);
clone.setWildcard(this.isWildcard());
Expand Down
Expand Up @@ -28,6 +28,8 @@
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.path.ItemPathSegment;
import com.evolveum.midpoint.prism.path.NameItemPathSegment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

Expand Down Expand Up @@ -831,7 +833,7 @@ public static void assertEqualsFilter(ObjectFilter objectFilter, QName expectedF
assertEquals("Wrong filter definition element name", expectedFilterDef, filter.getDefinition().getName());
assertEquals("Wrong filter definition type", expectedTypeName, filter.getDefinition().getTypeName());
}
assertEquals("Wrong filter path", path, filter.getFullPath());
assertPathEquivalent("Wrong filter path", path, filter.getFullPath());
}

public static <T> void assertEqualsFilterValue(EqualFilter filter, T value) {
Expand All @@ -849,7 +851,7 @@ public static void assertRefFilter(ObjectFilter objectFilter, QName expectedFilt
RefFilter filter = (RefFilter) objectFilter;
assertEquals("Wrong filter definition element name", expectedFilterDef, filter.getDefinition().getName());
assertEquals("Wrong filter definition type", expectedTypeName, filter.getDefinition().getTypeName());
assertEquals("Wrong filter path", path, filter.getFullPath());
assertPathEquivalent("Wrong filter path", path, filter.getFullPath());
}

// Local version of JUnit assers to avoid pulling JUnit dependecy to main
Expand Down Expand Up @@ -922,4 +924,26 @@ public static void assertAssignableFrom(Class<?> expected, Object actualObject)
assert expected.isAssignableFrom(actualObject.getClass()) : "Expected "+expected+" but got "+actualObject.getClass();
}

public static void assertPathEquivalent(String message, ItemPath expected, ItemPath actual) {
if (!expected.equivalent(actual)) {
assert false : message
+ ": expected " + MiscUtil.getValueWithClass(expected)
+ ", was " + MiscUtil.getValueWithClass(actual);
}
}

public static void assertPathEqualsExceptForPrefixes(String message, ItemPath expected, ItemPath actual) {
assertEquals(message + ": wrong path size", expected.size(), actual.size());
for (int i = 0; i < expected.size(); i++) {
ItemPathSegment expectedSegment = expected.getSegments().get(i);
ItemPathSegment actualSegment = actual.getSegments().get(i);
if (expectedSegment instanceof NameItemPathSegment) {
assertEquals(message + ": wrong path segment #" + (i+1), ((NameItemPathSegment) expectedSegment).getName(),
((NameItemPathSegment) actualSegment).getName());
} else {
assertEquals(message + ": wrong path segment #" + (i+1), expectedSegment, actualSegment);
}
}
}

}
Expand Up @@ -468,7 +468,7 @@ private void assertResourcePrism(PrismObject<ResourceType> resource, boolean isS
assertTrue("Wrong kind of filter: " + objectFilter, objectFilter instanceof EqualFilter);
EqualFilter equalFilter = (EqualFilter) objectFilter;
ItemPath path = equalFilter.getPath(); // should be extension/x:extConnType
assertPathEquals("Wrong filter path", new ItemPath(new QName("extension"), new QName("http://x/", "extConnType")), path);
PrismAsserts.assertPathEqualsExceptForPrefixes("Wrong filter path", new ItemPath(new QName("extension"), new QName("http://x/", "extConnType")), path);
PrismPropertyValue filterValue = (PrismPropertyValue) equalFilter.getValues().get(0);
assertEquals("Wrong filter value", "org.identityconnectors.ldap.LdapConnector", ((String) filterValue.getValue()).trim());
}
Expand Down Expand Up @@ -523,7 +523,7 @@ private void assertResourcePrism(PrismObject<ResourceType> resource, boolean isS
assertEquals("Wrong number of expression evaluators in correlation expression", 1, expressionType.getExpressionEvaluator().size());
ItemPathType itemPathType = (ItemPathType) expressionType.getExpressionEvaluator().get(0).getValue();
// $account/c:attributes/my:yyy
assertPathEquals("path in correlation expression",
PrismAsserts.assertPathEqualsExceptForPrefixes("path in correlation expression",
new ItemPath(
new NameItemPathSegment(new QName("account"), true),
new NameItemPathSegment(new QName(SchemaConstantsGenerated.NS_COMMON, "attributes")),
Expand All @@ -535,22 +535,6 @@ private void assertResourcePrism(PrismObject<ResourceType> resource, boolean isS

}


// quite strict version of path comparison
private void assertPathEquals(String message, ItemPath expected, ItemPath actual) {
assertEquals(message + ": wrong path size", expected.size(), actual.size());
for (int i = 0; i < expected.size(); i++) {
ItemPathSegment expectedSegment = expected.getSegments().get(i);
ItemPathSegment actualSegment = actual.getSegments().get(i);
assertEquals(message + ": wrong path segment", expectedSegment, actualSegment);
if (expectedSegment instanceof NameItemPathSegment) {
// default equals uses QNameUtil.match, not QName.equals
assertEquals(message + ": wrong path segment #" + (i+1) + " content", ((NameItemPathSegment) expectedSegment).getName(),
((NameItemPathSegment) actualSegment).getName());
}
}
}

private void assertResourceJaxb(ResourceType resourceType, boolean isSimple) throws SchemaException {
assertEquals("Wrong oid (JAXB)", RESOURCE_OID, resourceType.getOid());
assertEquals("Wrong name (JAXB)", PrismTestUtil.createPolyStringType("Embedded Test OpenDJ"), resourceType.getName());
Expand Down Expand Up @@ -608,7 +592,7 @@ private void assertResourceJaxb(ResourceType resourceType, boolean isSimple) thr
new NameItemPathSegment(new QName("user"), true),
new NameItemPathSegment(new QName("extension")),
new NameItemPathSegment(new QName("http://z/", "dept")));
assertPathEquals("source for departmentNubmer", expected, source.getPath().getItemPath());
PrismAsserts.assertPathEqualsExceptForPrefixes("source for departmentNubmer", expected, source.getPath().getItemPath());
}
}
assertTrue("ri:description attribute was not found", foundDescription);
Expand Down

0 comments on commit 146de80

Please sign in to comment.