You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/10/18 23:25:38 UTC
[sling-org-apache-sling-query] 10/31: SLING-4325 - SlingQuery
doesn't compare multivalue properties
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-query.git
commit 5fa3b5f0d5bc9a143f1b42dac42c95ec3f93e052
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Thu Jan 22 10:18:01 2015 +0000
SLING-4325 - SlingQuery doesn't compare multivalue properties
Submitted by: Tomek Rękawek
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1653800 13f79535-47bb-0310-9956-ffa450edef68
---
.../query/resource/ResourcePropertyPredicate.java | 16 +++++-
.../apache/sling/query/AttributeOperatorTest.java | 30 ++++++++++++
...ResourceMock.java => PropertyResourceMock.java} | 16 +++++-
.../org/apache/sling/query/mock/ResourceMock.java | 57 ++++++++++++++--------
.../sling/query/mock/json/JsonToResource.java | 44 +++++++++++------
src/test/resources/sample_tree.json | 4 +-
6 files changed, 126 insertions(+), 41 deletions(-)
diff --git a/src/main/java/org/apache/sling/query/resource/ResourcePropertyPredicate.java b/src/main/java/org/apache/sling/query/resource/ResourcePropertyPredicate.java
index 25ba0ea..356c332 100644
--- a/src/main/java/org/apache/sling/query/resource/ResourcePropertyPredicate.java
+++ b/src/main/java/org/apache/sling/query/resource/ResourcePropertyPredicate.java
@@ -45,7 +45,21 @@ public class ResourcePropertyPredicate implements Predicate<Resource> {
} else if (value == null) {
return true;
} else {
+ return isEqualToValue(property);
+ }
+ }
+
+ private boolean isEqualToValue(Resource property) {
+ final String[] multiProperty = property.adaptTo(String[].class);
+ if (multiProperty != null) {
+ for (String p : multiProperty) {
+ if (operator.accepts(p, value)) {
+ return true;
+ }
+ }
+ return false;
+ } else {
return operator.accepts(property.adaptTo(String.class), value);
}
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/org/apache/sling/query/AttributeOperatorTest.java b/src/test/java/org/apache/sling/query/AttributeOperatorTest.java
index e1f0424..3140254 100644
--- a/src/test/java/org/apache/sling/query/AttributeOperatorTest.java
+++ b/src/test/java/org/apache/sling/query/AttributeOperatorTest.java
@@ -37,6 +37,12 @@ public class AttributeOperatorTest {
}
@Test
+ public void testEqualsWithMultivalue() {
+ SlingQuery query = $(tree).children("cq:PageContent[cq:allowedTemplates=other demo template]");
+ assertResourceSetEquals(query.iterator(), "jcr:content");
+ }
+
+ @Test
public void testNotEquals() {
SlingQuery query = $(tree).children("cq:PageContent[jcr:title=123]");
assertEmptyIterator(query.iterator());
@@ -49,6 +55,12 @@ public class AttributeOperatorTest {
}
@Test
+ public void testContainsWithMultivalue() {
+ SlingQuery query = $(tree).children("cq:PageContent[cq:allowedTemplates*=her demo templa]");
+ assertResourceSetEquals(query.iterator(), "jcr:content");
+ }
+
+ @Test
public void testNotContains() {
SlingQuery query = $(tree).children("cq:PageContent[jcr:title*=123]");
assertEmptyIterator(query.iterator());
@@ -61,6 +73,12 @@ public class AttributeOperatorTest {
}
@Test
+ public void testContainsWordWithMultivalue() {
+ SlingQuery query = $(tree).children("cq:PageContent[cq:allowedTemplates~=template]");
+ assertResourceSetEquals(query.iterator(), "jcr:content");
+ }
+
+ @Test
public void testNotContainsWord() {
SlingQuery query = $(tree).children("cq:PageContent[jcr:title~=mmons de]");
assertEmptyIterator(query.iterator());
@@ -73,6 +91,12 @@ public class AttributeOperatorTest {
}
@Test
+ public void testEndsWithWithMultivalue() {
+ SlingQuery query = $(tree).children("cq:PageContent[cq:allowedTemplates$=demo template]");
+ assertResourceSetEquals(query.iterator(), "jcr:content");
+ }
+
+ @Test
public void testNotEndsWith() {
SlingQuery query = $(tree).children("cq:PageContent[jcr:title$=CQ]");
assertEmptyIterator(query.iterator());
@@ -97,6 +121,12 @@ public class AttributeOperatorTest {
}
@Test
+ public void testStartsWithWithMultivalue() {
+ SlingQuery query = $(tree).children("cq:PageContent[cq:allowedTemplates^=other demo]");
+ assertResourceSetEquals(query.iterator(), "jcr:content");
+ }
+
+ @Test
public void testNotStartsWith() {
SlingQuery query = $(tree).children("cq:PageContent[jcr:title^=Commons]");
assertEmptyIterator(query.iterator());
diff --git a/src/test/java/org/apache/sling/query/mock/StringResourceMock.java b/src/test/java/org/apache/sling/query/mock/PropertyResourceMock.java
similarity index 83%
rename from src/test/java/org/apache/sling/query/mock/StringResourceMock.java
rename to src/test/java/org/apache/sling/query/mock/PropertyResourceMock.java
index 8407e78..55380c7 100644
--- a/src/test/java/org/apache/sling/query/mock/StringResourceMock.java
+++ b/src/test/java/org/apache/sling/query/mock/PropertyResourceMock.java
@@ -26,7 +26,7 @@ import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
-public class StringResourceMock implements Resource {
+public class PropertyResourceMock implements Resource {
private final String name;
@@ -34,10 +34,20 @@ public class StringResourceMock implements Resource {
private final String value;
- public StringResourceMock(Resource parent, String name, String value) {
+ private final String[] values;
+
+ public PropertyResourceMock(Resource parent, String name, String value) {
this.parent = parent;
this.name = name;
this.value = value;
+ this.values = null;
+ }
+
+ public PropertyResourceMock(Resource parent, String name, String[] values) {
+ this.parent = parent;
+ this.name = name;
+ this.value = null;
+ this.values = values;
}
@SuppressWarnings("unchecked")
@@ -45,6 +55,8 @@ public class StringResourceMock implements Resource {
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type.isAssignableFrom(String.class)) {
return (AdapterType) value;
+ } else if (type.isAssignableFrom(String[].class)) {
+ return (AdapterType) (value == null ? values : new String[] { value });
} else {
return null;
}
diff --git a/src/test/java/org/apache/sling/query/mock/ResourceMock.java b/src/test/java/org/apache/sling/query/mock/ResourceMock.java
index 6d8c524..39d7804 100644
--- a/src/test/java/org/apache/sling/query/mock/ResourceMock.java
+++ b/src/test/java/org/apache/sling/query/mock/ResourceMock.java
@@ -19,9 +19,12 @@
package org.apache.sling.query.mock;
+import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
@@ -34,19 +37,12 @@ public class ResourceMock implements Resource {
private final Map<String, Resource> children;
- private final Map<String, String> properties;
-
private final Resource parent;
public ResourceMock(Resource parent, String name) {
this.name = name;
this.parent = parent;
this.children = new LinkedHashMap<String, Resource>();
- this.properties = new LinkedHashMap<String, String>();
- }
-
- public void setProperty(String name, String value) {
- properties.put(name, value);
}
public void addChild(Resource resource) {
@@ -65,7 +61,13 @@ public class ResourceMock implements Resource {
@Override
public Iterator<Resource> listChildren() {
- return children.values().iterator();
+ List<Resource> nonProperties = new ArrayList<Resource>();
+ for (Resource r : children.values()) {
+ if (!(r instanceof PropertyResourceMock)) {
+ nonProperties.add(r);
+ }
+ }
+ return nonProperties.iterator();
}
@Override
@@ -76,13 +78,10 @@ public class ResourceMock implements Resource {
if (children.containsKey(firstPart)) {
return children.get(firstPart).getChild(rest);
}
- } else {
- if (children.containsKey(relPath)) {
- return children.get(relPath);
- } else if (properties.containsKey(relPath)) {
- return new StringResourceMock(this, relPath, properties.get(relPath));
- }
+ } else if (children.containsKey(relPath)) {
+ return children.get(relPath);
}
+
return null;
}
@@ -90,7 +89,18 @@ public class ResourceMock implements Resource {
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type.isAssignableFrom(Map.class)) {
- return (AdapterType) properties;
+ Map<String, Object> map = new LinkedHashMap<String, Object>();
+ for (Entry<String, Resource> e : children.entrySet()) {
+ Resource o = e.getValue();
+ String[] stringArray = o.adaptTo(String[].class);
+ String stringValue = o.adaptTo(String.class);
+ if (stringValue != null) {
+ map.put(e.getKey(), stringValue);
+ } else if (stringArray != null) {
+ map.put(e.getKey(), stringArray);
+ }
+ }
+ return (AdapterType) map;
} else {
return null;
}
@@ -99,16 +109,16 @@ public class ResourceMock implements Resource {
@Override
public boolean isResourceType(String resourceType) {
return StringUtils.isNotBlank(resourceType)
- && (resourceType.equals(properties.get("sling:resourceType")) || resourceType
- .equals(properties.get("jcr:primaryType")));
+ && (resourceType.equals(getPropertyAsString("sling:resourceType")) || resourceType
+ .equals(getPropertyAsString("jcr:primaryType")));
}
@Override
public String getResourceType() {
- if (properties.containsKey("sling:resourceType")) {
- return properties.get("sling:resourceType");
+ if (children.containsKey("sling:resourceType")) {
+ return getPropertyAsString("sling:resourceType");
} else {
- return properties.get("jcr:primaryType");
+ return getPropertyAsString("jcr:primaryType");
}
}
@@ -141,4 +151,11 @@ public class ResourceMock implements Resource {
return String.format("ResourceMock[%s]", name);
}
+ private String getPropertyAsString(String name) {
+ if (children.containsKey(name)) {
+ return children.get(name).adaptTo(String.class);
+ }
+ return null;
+ }
+
}
diff --git a/src/test/java/org/apache/sling/query/mock/json/JsonToResource.java b/src/test/java/org/apache/sling/query/mock/json/JsonToResource.java
index b7a30fc..3635ce1 100644
--- a/src/test/java/org/apache/sling/query/mock/json/JsonToResource.java
+++ b/src/test/java/org/apache/sling/query/mock/json/JsonToResource.java
@@ -21,16 +21,17 @@ package org.apache.sling.query.mock.json;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.util.LinkedHashMap;
-import java.util.Map;
import java.util.Map.Entry;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.query.mock.ResourceMock;
+import org.apache.sling.query.mock.PropertyResourceMock;
+import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
+import com.google.gson.JsonPrimitive;
public final class JsonToResource {
private JsonToResource() {
@@ -41,23 +42,34 @@ public final class JsonToResource {
return parseResource(element.getAsJsonObject(), "/", null);
}
- private static Resource parseResource(JsonObject object, String name, Resource parent) {
- Map<String, String> properties = new LinkedHashMap<String, String>();
- Map<String, JsonObject> children = new LinkedHashMap<String, JsonObject>();
- for (Entry<String, JsonElement> entry : object.entrySet()) {
- JsonElement value = entry.getValue();
- if (value.isJsonPrimitive()) {
- properties.put(entry.getKey(), value.getAsString());
- } else if (value.isJsonObject()) {
- children.put(entry.getKey(), value.getAsJsonObject());
- }
+ private static Resource parseResource(JsonElement object, String name, Resource parent) {
+ if (object.isJsonArray()) {
+ return parseResource(object.getAsJsonArray(), name, parent);
+ } else if (object.isJsonPrimitive()) {
+ return parseResource(object.getAsJsonPrimitive(), name, parent);
+ } else if (object.isJsonObject()) {
+ return parseResource(object.getAsJsonObject(), name, parent);
+ } else {
+ return null;
}
+ }
- ResourceMock resource = new ResourceMock(parent, name);
- for (Entry<String, String> entry : properties.entrySet()) {
- resource.setProperty(entry.getKey(), entry.getValue());
+ private static Resource parseResource(JsonArray array, String name, Resource parent) {
+ final String[] values = new String[array.size()];
+ int i = 0;
+ for (JsonElement e : array) {
+ values[i++] = e.getAsString();
}
- for (Entry<String, JsonObject> entry : children.entrySet()) {
+ return new PropertyResourceMock(parent, name, values);
+ }
+
+ private static Resource parseResource(JsonPrimitive primitive, String name, Resource parent) {
+ return new PropertyResourceMock(parent, name, primitive.getAsString());
+ }
+
+ private static Resource parseResource(JsonObject object, String name, Resource parent) {
+ ResourceMock resource = new ResourceMock(parent, name);
+ for (Entry<String, JsonElement> entry : object.entrySet()) {
resource.addChild(parseResource(entry.getValue(), entry.getKey(), resource));
}
return resource;
diff --git a/src/test/resources/sample_tree.json b/src/test/resources/sample_tree.json
index 03cab87..5009db9 100644
--- a/src/test/resources/sample_tree.json
+++ b/src/test/resources/sample_tree.json
@@ -6,8 +6,8 @@
"redirectTarget": "/content/cqc-demo/home",
"sling:resourceType": "demo/core/renderers/redirectRenderer",
"cq:allowedTemplates": [
- "/apps/demo/.*/templates(.*)?",
- "/apps/cq-commons(.*)?"
+ "some demo template",
+ "other demo template"
],
"jcr:title": "CQ Commons demo",
"cq:designPath": "/etc/designs/demo",
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.