You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2015/05/22 10:45:37 UTC
svn commit: r1681045 - in
/sling/trunk/testing/mocks/resourceresolver-mock/src:
main/java/org/apache/sling/testing/resourceresolver/
test/java/org/apache/sling/testing/resourceresolver/
Author: sseifert
Date: Fri May 22 08:45:37 2015
New Revision: 1681045
URL: http://svn.apache.org/r1681045
Log:
SLING-4738 MockValueMap should also throw UOE for modification operations
Added:
sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java (with props)
sling/trunk/testing/mocks/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java (with props)
Modified:
sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
Modified: sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java?rev=1681045&r1=1681044&r2=1681045&view=diff
==============================================================================
--- sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java (original)
+++ sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java Fri May 22 08:45:37 2015
@@ -28,7 +28,6 @@ import org.apache.sling.api.resource.Res
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
-import org.apache.sling.api.wrappers.ModifiableValueMapDecorator;
public class MockResource extends AbstractResource {
@@ -50,7 +49,15 @@ public class MockResource extends Abstra
final Map<String, Object> props,
final ResourceResolver resolver) {
this.path = path;
- this.props = (props instanceof MockValueMap) ? (MockValueMap)props : new MockValueMap(this, props);
+ if (props instanceof MockValueMap) {
+ this.props = (MockValueMap)props;
+ }
+ else if (props instanceof ReadonlyValueMapDecorator && ((ReadonlyValueMapDecorator)props).getDelegate() instanceof MockValueMap) {
+ this.props = ((ReadonlyValueMapDecorator)props).getDelegate();
+ }
+ else {
+ this.props = new MockValueMap(this, props);
+ }
this.resolver = resolver;
}
@@ -87,12 +94,12 @@ public class MockResource extends Abstra
@SuppressWarnings("unchecked")
@Override
public <AdapterType> AdapterType adaptTo(final Class<AdapterType> type) {
- if ( type == ValueMap.class ) {
- return (AdapterType)this.props;
+ if ( type == ValueMap.class || type == Map.class ) {
+ return (AdapterType)new ReadonlyValueMapDecorator(this.props);
}
else if ( type == ModifiableValueMap.class ) {
((MockResourceResolver)this.resolver).addChanged(this.path, this.props);
- return (AdapterType)new ModifiableValueMapDecorator(this.props);
+ return (AdapterType)this.props;
}
else if ( type == InputStream.class ) {
InputStream is = getFileResourceInputStream();
@@ -123,7 +130,7 @@ public class MockResource extends Abstra
// part of Resource API 2.7.0
public ValueMap getValueMap() {
- return this.props;
+ return this.adaptTo(ValueMap.class);
}
@Override
Modified: sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java?rev=1681045&r1=1681044&r2=1681045&view=diff
==============================================================================
--- sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java (original)
+++ sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java Fri May 22 08:45:37 2015
@@ -28,6 +28,7 @@ import java.util.Map;
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;
@@ -40,7 +41,7 @@ import org.apache.sling.api.wrappers.Val
* <li>Converts InputStream to byte array and vice versa.</li>
* </ul>
*/
-public class MockValueMap extends ValueMapDecorator {
+public class MockValueMap extends ValueMapDecorator implements ModifiableValueMap {
private final Resource resource;
Added: sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java?rev=1681045&view=auto
==============================================================================
--- sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java (added)
+++ sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java Fri May 22 08:45:37 2015
@@ -0,0 +1,106 @@
+/*
+ * 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 java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.sling.api.resource.ValueMap;
+
+/**
+ * Decorator that disallows access to all methods that modify the value map.
+ */
+class ReadonlyValueMapDecorator implements ValueMap {
+
+ private final ValueMap delegate;
+
+ public ReadonlyValueMapDecorator(ValueMap base) {
+ this.delegate = base;
+ }
+
+ public Object put(String key, Object value) {
+ throw new UnsupportedOperationException("ValueMap is read-only.");
+ }
+
+ public Object remove(Object key) {
+ throw new UnsupportedOperationException("ValueMap is read-only.");
+ }
+
+ public void putAll(Map<? extends String, ?> t) {
+ throw new UnsupportedOperationException("ValueMap is read-only.");
+ }
+
+ public void clear() {
+ throw new UnsupportedOperationException("ValueMap is read-only.");
+ }
+
+ public <T> T get(String name, Class<T> type) {
+ return delegate.get(name, type);
+ }
+
+ public <T> T get(String name, T defaultValue) {
+ return delegate.get(name, defaultValue);
+ }
+
+ public int size() {
+ return delegate.size();
+ }
+
+ public boolean isEmpty() {
+ return delegate.isEmpty();
+ }
+
+ public boolean containsKey(Object key) {
+ return delegate.containsKey(key);
+ }
+
+ public boolean containsValue(Object value) {
+ return delegate.containsValue(value);
+ }
+
+ public Object get(Object key) {
+ return delegate.get(key);
+ }
+
+ public Set<String> keySet() {
+ return delegate.keySet();
+ }
+
+ public Collection<Object> values() {
+ return delegate.values();
+ }
+
+ public Set<java.util.Map.Entry<String, Object>> entrySet() {
+ return delegate.entrySet();
+ }
+
+ public boolean equals(Object o) {
+ return delegate.equals(o);
+ }
+
+ public int hashCode() {
+ return delegate.hashCode();
+ }
+
+ ValueMap getDelegate() {
+ return delegate;
+ }
+
+}
Propchange: sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri May 22 08:45:37 2015
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/testing/mocks/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java?rev=1681045&view=auto
==============================================================================
--- sling/trunk/testing/mocks/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java (added)
+++ sling/trunk/testing/mocks/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java Fri May 22 08:45:37 2015
@@ -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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.sling.api.resource.LoginException;
+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.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Test different ValueMap variants.
+ */
+public class ValueMapTest {
+
+ private ResourceResolver resourceResolver;
+ private Resource testRoot;
+
+ @Before
+ public final void setUp() throws IOException, LoginException {
+ resourceResolver = new MockResourceResolverFactory().getResourceResolver(null);
+
+ Resource root = resourceResolver.getResource("/");
+ testRoot = resourceResolver.create(root, "test", ValueMap.EMPTY);
+
+ resourceResolver.create(testRoot, "node1",
+ ImmutableMap.<String, Object>builder()
+ .put("prop1", "value1")
+ .build());
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testMap() throws IOException {
+ Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
+
+ Map<String, Object> map = resource1.adaptTo(Map.class);
+ assertTrue(map instanceof ValueMap && !(map instanceof ModifiableValueMap));
+
+ assertEquals("value1", map.get("prop1"));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test(expected = UnsupportedOperationException.class)
+ public void testMap_Readonly() throws IOException {
+ Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
+
+ Map<String, Object> map = resource1.adaptTo(Map.class);
+ map.put("prop1", "value2");
+ }
+
+ @Test
+ public void testValueMap() throws IOException {
+ Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
+
+ ValueMap map = resource1.adaptTo(ValueMap.class);
+ assertTrue(map instanceof ValueMap && !(map instanceof ModifiableValueMap));
+
+ assertEquals("value1", map.get("prop1"));
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testValueMapMap_Readonly() throws IOException {
+ Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
+
+ ValueMap map = resource1.adaptTo(ValueMap.class);
+ map.put("prop1", "value2");
+ }
+
+ @Test
+ public void testModifiableValueMap() throws IOException {
+ Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
+
+ ValueMap map = resource1.adaptTo(ModifiableValueMap.class);
+ assertTrue(map instanceof ValueMap && map instanceof ModifiableValueMap);
+
+ assertEquals("value1", map.get("prop1"));
+ map.put("prop1", "value2");
+ }
+
+}
Propchange: sling/trunk/testing/mocks/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri May 22 08:45:37 2015
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain