You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2012/10/01 16:50:39 UTC
svn commit: r1392367 - in /sling/trunk/bundles/commons/testing/src:
main/java/org/apache/sling/commons/testing/sling/MockResource.java
test/java/org/apache/sling/commons/testing/sling/
test/java/org/apache/sling/commons/testing/sling/MockResourceTest.java
Author: bdelacretaz
Date: Mon Oct 1 14:50:38 2012
New Revision: 1392367
URL: http://svn.apache.org/viewvc?rev=1392367&view=rev
Log:
SLING-2611 - add Properties support to MockResource - based on a contribution by Dan Kico, thanks!
Added:
sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/sling/
sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/sling/MockResourceTest.java
Modified:
sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/sling/MockResource.java
Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/sling/MockResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/sling/MockResource.java?rev=1392367&r1=1392366&r2=1392367&view=diff
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/sling/MockResource.java (original)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/sling/MockResource.java Mon Oct 1 14:50:38 2012
@@ -24,11 +24,13 @@ import org.apache.sling.api.resource.Val
import org.apache.sling.api.wrappers.ValueMapDecorator;
import java.util.HashMap;
+import java.util.Map;
public class MockResource extends SyntheticResource {
private String resourceType;
private String resourceSuperType;
+ private Map<String,Object> properties = new HashMap<String,Object>();
public MockResource(ResourceResolver resourceResolver, String path,
String resourceType) {
@@ -43,6 +45,14 @@ public class MockResource extends Synthe
setResourceSuperType(resourceSuperType);
}
+ public void addProperty(String key, Object value){
+ this.properties.put(key,value);
+ }
+
+ public Map<String,Object> getProperties(){
+ return this.properties;
+ }
+
@Override
public String getResourceType() {
return resourceType;
@@ -61,19 +71,21 @@ public class MockResource extends Synthe
this.resourceSuperType = resourceSuperType;
}
- @SuppressWarnings("unchecked")
- public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
- if (type == ValueMap.class) {
- ValueMap map = new ValueMapDecorator(new HashMap<String, Object>());
- if (resourceType != null) {
- map.put("resourceType", resourceType);
- }
- if (resourceSuperType != null) {
- map.put("resourceSuperType", resourceSuperType);
- }
- return (AdapterType) map;
- }
- throw new UnsupportedOperationException("AdaptTo " + type.getSimpleName() + " not implemented");
- }
-
+ @SuppressWarnings("unchecked")
+ public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+ if (type == ValueMap.class) {
+ ValueMap map = new ValueMapDecorator(new HashMap<String, Object>());
+ if (resourceType != null) {
+ map.put("resourceType", resourceType);
+ }
+ if (resourceSuperType != null) {
+ map.put("resourceSuperType", resourceSuperType);
+ }
+ for (String key : this.properties.keySet()) {
+ map.put(key,this.properties.get(key));
+ }
+ return (AdapterType) map;
+ }
+ throw new UnsupportedOperationException("AdaptTo " + type.getSimpleName() + " not implemented");
+ }
}
Added: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/sling/MockResourceTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/sling/MockResourceTest.java?rev=1392367&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/sling/MockResourceTest.java (added)
+++ sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/sling/MockResourceTest.java Mon Oct 1 14:50:38 2012
@@ -0,0 +1,63 @@
+/*
+ * 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.commons.testing.sling;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.junit.Test;
+
+public class MockResourceTest {
+ public static final String PATH = "/foo/bar";
+ public static final String RT = "someResourceType";
+
+ @Test
+ public void basicTest() throws Exception {
+ final MockResource r = new MockResource(null, PATH, RT);
+ r.addProperty("1", Integer.MAX_VALUE);
+ r.addProperty("2", "two");
+
+ assertTrue(r instanceof Resource);
+ assertEquals(PATH, r.getPath());
+ assertEquals(RT, r.getResourceType());
+ assertNull(r.getResourceSuperType());
+
+ final ValueMap m = r.adaptTo(ValueMap.class);
+ assertEquals(Integer.MAX_VALUE, m.get("1"));
+ assertEquals("two", m.get("2"));
+
+ // changes to r do not affect m
+ assertNull(m.get("third"));
+ r.getProperties().put("third", "trois");
+ assertNull(m.get("third"));
+
+ // but we get the new value after adapting again
+ assertEquals("trois", r.adaptTo(ValueMap.class).get("third"));
+ }
+
+ @Test
+ public void testNoProperties() throws Exception {
+ final MockResource r = new MockResource(null, PATH, RT);
+ assertNotNull(r.adaptTo(ValueMap.class));
+ }
+}
\ No newline at end of file