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 77a942b0cf2aee4a61a008ecfad6f1675f2cdb5d Author: Stefan Seifert <[email protected]> AuthorDate: Fri May 22 08:45:37 2015 +0000 SLING-4738 MockValueMap should also throw UOE for modification operations git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/resourceresolver-mock@1681045 13f79535-47bb-0310-9956-ffa450edef68 --- .../testing/resourceresolver/MockResource.java | 19 ++-- .../testing/resourceresolver/MockValueMap.java | 3 +- .../ReadonlyValueMapDecorator.java | 106 ++++++++++++++++++++ .../testing/resourceresolver/ValueMapTest.java | 107 +++++++++++++++++++++ 4 files changed, 228 insertions(+), 7 deletions(-) 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 d68ca4e..785759a 100644 --- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java +++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java @@ -28,7 +28,6 @@ import org.apache.sling.api.resource.ResourceMetadata; 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 AbstractResource { 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 AbstractResource { @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 AbstractResource { // part of Resource API 2.7.0 public ValueMap getValueMap() { - return this.props; + return this.adaptTo(ValueMap.class); } @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 a17fa7f..d53a29a 100644 --- a/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java +++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java @@ -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.ValueMapDecorator; * <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; diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java b/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java new file mode 100644 index 0000000..78d0842 --- /dev/null +++ b/src/main/java/org/apache/sling/testing/resourceresolver/ReadonlyValueMapDecorator.java @@ -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; + } + +} diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.java new file mode 100644 index 0000000..4368683 --- /dev/null +++ b/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapTest.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 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"); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
