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/11/07 10:21:25 UTC
[sling-org-apache-sling-testing-resourceresolver-mock] 20/26:
SLING-4850 MockValueMap should implement containsKey
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.testing.resourceresolver-mock-1.1.10
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-resourceresolver-mock.git
commit 45855870de3556ff99c0f8852dcc34181d6f6548
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Jul 23 22:25:53 2015 +0000
SLING-4850 MockValueMap should implement containsKey
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/resourceresolver-mock@1692454 13f79535-47bb-0310-9956-ffa450edef68
---
.../DeepReadModifiableValueMapDecorator.java | 107 +++++++++++++++++++++
.../testing/resourceresolver/MockResource.java | 2 +-
.../testing/resourceresolver/MockValueMap.java | 19 +---
.../SlingCrudResourceResolverTest.java | 5 +
4 files changed, 115 insertions(+), 18 deletions(-)
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/DeepReadModifiableValueMapDecorator.java b/src/main/java/org/apache/sling/testing/resourceresolver/DeepReadModifiableValueMapDecorator.java
new file mode 100644
index 0000000..2c90cc3
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/DeepReadModifiableValueMapDecorator.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.sling.testing.resourceresolver;
+
+import org.apache.sling.api.resource.ModifiableValueMap;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.wrappers.ValueMapDecorator;
+
+/**
+ * This is copied from org.apache.sling.api.wrappers.DeepReadValueMapDecorator and DeepReadModifiableValueMapDecorator.
+ */
+class DeepReadModifiableValueMapDecorator extends ValueMapDecorator implements ModifiableValueMap {
+
+ private final String pathPrefix;
+
+ private final ResourceResolver resolver;
+
+ private final ValueMap base;
+
+ public DeepReadModifiableValueMapDecorator(final Resource resource, final ValueMap base) {
+ super(base);
+ this.pathPrefix = resource.getPath() + "/";
+ this.resolver = resource.getResourceResolver();
+ this.base = base;
+ }
+
+ private ValueMap getValueMap(final String name) {
+ final int pos = name.lastIndexOf("/");
+ if ( pos == -1 ) {
+ return this.base;
+ }
+ final Resource rsrc = this.resolver.getResource(pathPrefix + name.substring(0, pos));
+ if ( rsrc != null ) {
+ final ValueMap vm = rsrc.adaptTo(ValueMap.class);
+ if ( vm != null ) {
+ return vm;
+ }
+ }
+ return ValueMap.EMPTY; // fall back
+ }
+
+ private String getPropertyName(final String name) {
+ final int pos = name.lastIndexOf("/");
+ if ( pos == -1 ) {
+ return name;
+ }
+ return name.substring(pos + 1);
+ }
+
+ /**
+ * @see org.apache.sling.api.resource.ValueMap#get(java.lang.String, java.lang.Class)
+ */
+ @Override
+ public <T> T get(final String name, final Class<T> type) {
+ return this.getValueMap(name).get(this.getPropertyName(name), type);
+ }
+
+ /**
+ * @see org.apache.sling.api.resource.ValueMap#get(java.lang.String, java.lang.Object)
+ */
+ @Override
+ public <T> T get(final String name, T defaultValue) {
+ return this.getValueMap(name).get(this.getPropertyName(name), defaultValue);
+ }
+
+ /**
+ * @see org.apache.sling.api.wrappers.ValueMapDecorator#containsKey(java.lang.Object)
+ */
+ @Override
+ public boolean containsKey(final Object key) {
+ if ( key == null ) {
+ return false;
+ }
+ final String name = key.toString();
+ return this.getValueMap(name).containsKey(this.getPropertyName(name));
+ }
+
+ /**
+ * @see org.apache.sling.api.wrappers.ValueMapDecorator#get(java.lang.Object)
+ */
+ @Override
+ public Object get(final Object key) {
+ if ( key == null ) {
+ return null;
+ }
+ final String name = key.toString();
+ return this.getValueMap(name).get(this.getPropertyName(name));
+ }
+}
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
index 785759a..b6d4518 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
@@ -48,6 +48,7 @@ public class MockResource extends AbstractResource {
public MockResource(final String path,
final Map<String, Object> props,
final ResourceResolver resolver) {
+ this.resolver = resolver;
this.path = path;
if (props instanceof MockValueMap) {
this.props = (MockValueMap)props;
@@ -58,7 +59,6 @@ public class MockResource extends AbstractResource {
else {
this.props = new MockValueMap(this, props);
}
- this.resolver = resolver;
}
@Override
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
index d53a29a..a5e2536 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
@@ -30,7 +30,6 @@ import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.util.ISO8601;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.wrappers.ValueMapDecorator;
/**
@@ -41,34 +40,20 @@ import org.apache.sling.api.wrappers.ValueMapDecorator;
* <li>Converts InputStream to byte array and vice versa.</li>
* </ul>
*/
-public class MockValueMap extends ValueMapDecorator implements ModifiableValueMap {
-
- private final Resource resource;
+public class MockValueMap extends DeepReadModifiableValueMapDecorator implements ModifiableValueMap {
public MockValueMap(Resource resource) {
this(resource, new HashMap<String, Object>());
}
public MockValueMap(Resource resource, Map<String,Object> map) {
- super(convertForWriteAll(map));
- this.resource = resource;
+ super(resource, new ValueMapDecorator(convertForWriteAll(map)));
}
@SuppressWarnings("unchecked")
@Override
public <T> T get(String name, Class<T> type) {
- // check for deep path access
- int slashPos = name.lastIndexOf('/');
- if (slashPos >= 0) {
- String resourcePath = "./" + name.substring(0, slashPos);
- String propertyName = name.substring(slashPos + 1);
- Resource childResource = resource.getChild(resourcePath);
- if (childResource!=null) {
- return ResourceUtil.getValueMap(childResource).get(propertyName, type);
- }
- }
-
if (type == Calendar.class) {
// Support conversion of String to Calendar if value conforms to ISO8601 date format
Object value = get(name);
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
index e3020fc..b0ca80f 100644
--- a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
@@ -20,8 +20,10 @@ package org.apache.sling.testing.resourceresolver;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -119,6 +121,9 @@ public class SlingCrudResourceResolverTest {
assertEquals(DOUBLE_VALUE, props.get("node1/doubleProp", Double.class), 0.0001);
assertEquals(BOOLEAN_VALUE, props.get("node1/booleanProp", Boolean.class));
assertEquals(STRING_VALUE, props.get("node1/node11/stringProp11", String.class));
+
+ assertTrue(STRING_VALUE, props.containsKey("node1/stringProp"));
+ assertFalse(STRING_VALUE, props.containsKey("node1/unknownProp"));
}
@Test
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.