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 09:58:26 UTC
[sling-org-apache-sling-resourcecollection] 09/20: SLING-2853 : Add
ResourceCollection to Sling - use resource resolver mock
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.resourcecollection-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourcecollection.git
commit 5a127e97093e5ace8bc8389bc92b9ac554283a08
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon May 6 18:35:43 2013 +0000
SLING-2853 : Add ResourceCollection to Sling - use resource resolver mock
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/collection@1479668 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 6 +
.../impl/ResourceCollectionImplTest.java | 116 +++++-----
.../resource/collection/test/MockResource.java | 105 ---------
.../collection/test/MockResourceResolver.java | 249 ---------------------
4 files changed, 71 insertions(+), 405 deletions(-)
diff --git a/pom.xml b/pom.xml
index 9184268..c873525 100755
--- a/pom.xml
+++ b/pom.xml
@@ -143,5 +143,11 @@
<artifactId>servlet-api</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
+ <version>0.1.0</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java b/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java
index 90435f0..d722c3a 100755
--- a/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java
+++ b/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java
@@ -18,6 +18,7 @@
*/
package org.apache.sling.resource.collection.impl;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -30,8 +31,7 @@ import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.JcrResourceConstants;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
-import org.apache.sling.resource.collection.test.MockResource;
-import org.apache.sling.resource.collection.test.MockResourceResolver;
+import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
import org.junit.Before;
import org.junit.Test;
@@ -41,151 +41,165 @@ public class ResourceCollectionImplTest {
@Before
public void setUp() throws Exception {
- resResolver = new MockResourceResolver();
+ resResolver = new MockResourceResolverFactory().getAdministrativeResourceResolver(null);
rcm = new ResourceCollectionManagerImpl(resResolver);
- // need a root resource
- new MockResource(resResolver, "/", "type");
}
-
+
@Test
public void testAddResource() throws Exception {
-
+
final ResourceCollection collection = rcm.createCollection(resResolver.getResource("/"), "test1");
- collection.add(new MockResource(resResolver, "/res1", "type"));
- final Resource resource = new MockResource(resResolver, "/res2", "type");
+ final Resource res1 = resResolver.create(resResolver.getResource("/"), "res1",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
+ collection.add(res1);
+ final Resource resource = resResolver.create(resResolver.getResource("/"), "res2",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
collection.add(resource);
-
+
Assert.assertEquals(true, collection.contains(resource));
Assert.assertEquals(true, collection.contains(resource));
Assert.assertNotNull(resResolver.getResource("/test1"));
Assert.assertEquals(ResourceCollection.RESOURCE_TYPE, resResolver.getResource("/test1").getResourceType());
}
-
+
@Test
public void testCreateCollection() throws Exception {
final ResourceCollection collection = rcm.createCollection(resResolver.getResource("/"), "test1");
- collection.add(new MockResource(resResolver, "/res1", "type"), null);
- final Resource resource = new MockResource(resResolver, "/res2", "type");
+ final Resource res1 = resResolver.create(resResolver.getResource("/"), "res1",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
+ collection.add(res1, null);
+ final Resource resource = resResolver.create(resResolver.getResource("/"), "res2",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
collection.add(resource, null);
-
+
Assert.assertEquals(true, collection.contains(resource));
Assert.assertNotNull(resResolver.getResource("/test1"));
Assert.assertEquals(ResourceCollection.RESOURCE_TYPE, resResolver.getResource("/test1").getResourceType());
}
-
+
@Test
public void testGetCollection() throws Exception {
ResourceCollection collection = rcm.createCollection(resResolver.getResource("/"), "test1");
- collection.add(new MockResource(resResolver, "/res1", "type"), null);
- final Resource resource = new MockResource(resResolver, "/res2", "type");
+ final Resource res1 = resResolver.create(resResolver.getResource("/"), "res1",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
+ collection.add(res1, null);
+ final Resource resource = resResolver.create(resResolver.getResource("/"), "res2",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
collection.add(resource, null);
-
+
collection = rcm.getCollection(resResolver.getResource(collection.getPath()));
-
+
Assert.assertEquals(true, collection.contains(resource));
Assert.assertNotNull(resResolver.getResource("/test1"));
Assert.assertEquals(ResourceCollection.RESOURCE_TYPE, resResolver.getResource("/test1").getResourceType());
}
-
+
@Test
public void testListCollection() throws Exception {
final ResourceCollection collection = rcm.createCollection(resResolver.getResource("/"), "collection1");
- collection.add(new MockResource(resResolver, "/res1", "type"), null);
- final Resource resource = new MockResource(resResolver, "/res2", "type");
-
+ final Resource res1 = resResolver.create(resResolver.getResource("/"), "res1",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
+ collection.add(res1, null);
+ final Resource resource = resResolver.create(resResolver.getResource("/"), "res2",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
+
collection.add(resource, null);
Assert.assertEquals(true, collection.contains(resource));
-
+
final Iterator<Resource> resources = collection.getResources();
int numOfRes = 0;
while (resources.hasNext()) {
resources.next();
numOfRes ++;
}
-
+
Assert.assertEquals(2, numOfRes);
}
-
+
@Test
public void testCreateCollectionWithProperties() throws Exception {
final Map<String, Object> props = new HashMap<String, Object>();
props.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "some/type");
props.put("creator", "slingdev");
-
+
final ResourceCollection collection = rcm.createCollection(resResolver.getResource("/"), "collection3", props);
- final Resource resource = new MockResource(resResolver, "/res1", "type");
+ final Resource resource = resResolver.create(resResolver.getResource("/"), "res1",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
collection.add(resource, null);
-
+
final Resource collectionRes = resResolver.getResource("/collection3");
Assert.assertNotNull(collectionRes);
-
+
Assert.assertEquals(true, collection.contains(resource));
Assert.assertEquals(ResourceCollection.RESOURCE_TYPE, collectionRes.getResourceSuperType());
-
+
ValueMap vm = collectionRes.adaptTo(ValueMap.class);
-
+
Assert.assertEquals("slingdev", vm.get("creator", ""));
}
-
+
@Test
public void testAddResourceWithProperties() throws Exception {
final Map<String, Object> props = new HashMap<String, Object>();
props.put("creator", "slingdev");
-
+
final ResourceCollection collection = rcm.createCollection(resResolver.getResource("/"), "collection3");
-
- final Resource resource = new MockResource(resResolver, "/res1", "type");
+
+ final Resource resource = resResolver.create(resResolver.getResource("/"), "res1",
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
collection.add(resource, props);
-
+
final Resource collectionRes = resResolver.getResource("/collection3");
Assert.assertNotNull(collectionRes);
-
+
Assert.assertEquals(true, collection.contains(resource));
-
+
ValueMap vm = collection.getProperties(resource);
-
+
if (vm != null) {
Assert.assertEquals("slingdev", vm.get("creator", ""));
} else {
Assert.fail("no resource entry in collection");
}
}
-
+
@Test
public void testOrdering() throws Exception {
final ResourceCollection collection = rcm.createCollection(resResolver.getResource("/"), "test1");
String[] resPaths = {"/res1", "/res2"};
- final Resource resource = new MockResource(resResolver, resPaths[0], "type");
-
+ final Resource resource = resResolver.create(resResolver.getResource("/"), resPaths[0].substring(1),
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
+
collection.add(resource, null);
- final Resource resource2 = new MockResource(resResolver, resPaths[1], "type");
+ final Resource resource2 = resResolver.create(resResolver.getResource("/"), resPaths[1].substring(1),
+ Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
collection.add(resource2, null);
-
+
Assert.assertEquals(true, collection.contains(resource2));
Assert.assertNotNull(resResolver.getResource("/test1"));
Assert.assertEquals(ResourceCollection.RESOURCE_TYPE, resResolver.getResource("/test1").getResourceType());
-
+
Iterator<Resource> resources = collection.getResources();
-
+
int numOfRes = 0;
while (resources.hasNext()) {
Resource entry = resources.next();
Assert.assertEquals(resPaths[numOfRes], entry.getPath());
numOfRes ++;
}
-
+
//change the order
collection.orderBefore(resource2, resource);
-
+
resources = collection.getResources();
-
+
numOfRes = 2;
while (resources.hasNext()) {
numOfRes --;
Resource entry = resources.next();
Assert.assertEquals(resPaths[numOfRes], entry.getPath());
}
-
+
Assert.assertEquals(0, numOfRes);
}
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resource/collection/test/MockResource.java b/src/test/java/org/apache/sling/resource/collection/test/MockResource.java
deleted file mode 100755
index 74595d8..0000000
--- a/src/test/java/org/apache/sling/resource/collection/test/MockResource.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * 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.resource.collection.test;
-
-import org.apache.sling.api.resource.ModifiableValueMap;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.SyntheticResource;
-import org.apache.sling.api.resource.ValueMap;
-import org.apache.sling.api.wrappers.ModifiableValueMapDecorator;
-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) {
- this(resourceResolver, path, resourceType, null);
- }
-
- public MockResource(ResourceResolver resourceResolver, String path,
- String resourceType, String resourceSuperType) {
- super(resourceResolver, path, resourceType);
-
- ((MockResourceResolver)resourceResolver).addResource(this);
- setResourceType(resourceType);
- 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;
- }
-
- public void setResourceType(String resourceType) {
- this.resourceType = resourceType;
- }
-
- @Override
- public String getResourceSuperType() {
- return resourceSuperType;
- }
-
- public void setResourceSuperType(String resourceSuperType) {
- 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);
- }
- for (String key : this.properties.keySet()) {
- map.put(key,this.properties.get(key));
- }
- return (AdapterType) map;
- }
-
- if (type == ModifiableValueMap.class) {
- ModifiableValueMap map = new ModifiableValueMapDecorator(this.properties);
- 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");
- }
-}
diff --git a/src/test/java/org/apache/sling/resource/collection/test/MockResourceResolver.java b/src/test/java/org/apache/sling/resource/collection/test/MockResourceResolver.java
deleted file mode 100755
index 741a657..0000000
--- a/src/test/java/org/apache/sling/resource/collection/test/MockResourceResolver.java
+++ /dev/null
@@ -1,249 +0,0 @@
-/*
- * 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.resource.collection.test;
-
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.NoSuchElementException;
-
-import javax.servlet.http.HttpServletRequest;
-
-import org.apache.sling.api.resource.LoginException;
-import org.apache.sling.api.resource.PersistenceException;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.jcr.resource.JcrResourceConstants;
-
-public class MockResourceResolver implements ResourceResolver {
-
- private String[] searchPath;
-
- private Map<String, Resource> resources = new LinkedHashMap<String, Resource>();
-
- private Map<String, Collection<Resource>> children = new LinkedHashMap<String, Collection<Resource>>();
-
- void addResource(Resource resource) {
- this.resources.put(resource.getPath(), resource);
- }
-
- public void addChildren(Resource parent, Collection<Resource> children) {
- this.children.put(parent.getPath(), children);
- }
-
- public Resource resolve(HttpServletRequest request) {
- throw new UnsupportedOperationException("Not implemented");
-
- }
-
- public Resource resolve(String absPath) {
- throw new UnsupportedOperationException("Not implemented");
-
- }
-
- public String map(String resourcePath) {
- return resourcePath; // a rather simplistic 1:1 map...
-
- }
-
- public Resource getResource(String path) {
- return resources.get(path);
- }
-
- public Resource getResource(Resource base, String path) {
- if (!path.startsWith("/")) {
- path = base.getPath() + "/" + path;
- }
- return getResource(path);
- }
-
- public String[] getSearchPath() {
- return searchPath.clone();
-
- }
-
- public Iterator<Resource> listChildren(final Resource parent) {
- Collection<Resource> childCollection = children.get(parent.getPath());
- if (childCollection != null) {
- return childCollection.iterator();
- }
-
- return new Iterator<Resource>() {
- final String parentPath = parent.getPath() + "/";
-
- final Iterator<Resource> elements = resources.values().iterator();
-
- Resource nextResource = seek();
-
- public boolean hasNext() {
- return nextResource != null;
- }
-
- public Resource next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
-
- Resource result = nextResource;
- nextResource = seek();
- return result;
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- private Resource seek() {
- while (elements.hasNext()) {
- Resource next = elements.next();
- String path = next.getPath();
- if (path.startsWith(parentPath)
- && path.indexOf('/', parentPath.length()) < 0) {
- return next;
- }
- }
- return null;
- }
- };
- }
-
- public Iterator<Resource> findResources(String query, String language) {
- throw new UnsupportedOperationException("Not implemented");
-
- }
-
- public Iterator<Map<String, Object>> queryResources(String query,
- String language) {
- throw new UnsupportedOperationException("Not implemented");
-
- }
-
- public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
- throw new UnsupportedOperationException("Not implemented");
-
- }
-
- public void setSearchPath(String... searchPath) {
- if (searchPath == null) {
- this.searchPath = new String[0];
- } else {
- this.searchPath = new String[searchPath.length];
- for (int i=0; i < searchPath.length; i++) {
- String entry = searchPath[i];
- if (!entry.endsWith("/")) {
- entry = entry.concat("/");
- }
- this.searchPath[i] = entry;
- }
- }
- }
-
- public String map(HttpServletRequest request, String resourcePath) {
- return request.getContextPath() + resourcePath;
- }
-
- public Resource resolve(HttpServletRequest request, String absPath) {
- throw new UnsupportedOperationException("Not implemented");
- }
-
- public void close() {
- // nothing to do
- }
-
- public String getUserID() {
- return null;
- }
-
- public boolean isLive() {
- return true;
- }
-
- public ResourceResolver clone(Map<String, Object> authenticationInfo)
- throws LoginException {
- // TODO Auto-generated method stub
- return null;
- }
-
- public Object getAttribute(String name) {
- // TODO Auto-generated method stub
- return null;
- }
-
- public Iterator<String> getAttributeNames() {
- // TODO Auto-generated method stub
- return null;
- }
-
- public void commit() throws PersistenceException {
- // TODO Auto-generated method stub
-
- }
-
- public Resource create(Resource arg0, String arg1, Map<String, Object> arg2)
- throws PersistenceException {
- String superType = "SuperType";
- String resType = "ResType";
-
- if (arg2 != null) {
- if (arg2.containsKey(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY)) {
- superType = (String) arg2.remove(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY);
- }
-
- if (arg2.containsKey(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY)) {
- resType = (String) arg2.remove(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY);
- }
- }
-
- String resourcePath = arg0.getPath();
- if (arg0.getPath().equals("/")) {
- resourcePath = resourcePath + arg1;
- } else {
- resourcePath = resourcePath + "/" + arg1;
- }
- MockResource resource = new MockResource(this, resourcePath, resType, superType);
- if (arg2 != null) {
- for (String key: arg2.keySet()) {
- resource.addProperty(key, arg2.get(key));
- }
- }
- this.resources.put(resource.getPath(), resource);
- return resource;
- }
-
- public void delete(Resource arg0) throws PersistenceException {
- // TODO Auto-generated method stub
- }
-
- public Iterable<Resource> getChildren(Resource arg0) {
- // TODO Auto-generated method stub
- return null;
- }
-
- public boolean hasChanges() {
- // TODO Auto-generated method stub
- return false;
- }
-
- public void revert() {
- // TODO Auto-generated method stub
-
- }
-}
-
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.