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/10/18 23:23:52 UTC

[sling-org-apache-sling-resourcecollection] 10/31: SLING-2857 : Fix the name of the members resource in ResourceCollection., Apply patch from Amit Gupta

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourcecollection.git

commit 17a0a8a677c4783262aa2e631a1d8e9a6a2790dd
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue May 7 06:09:02 2013 +0000

    SLING-2857 : Fix the name of the members resource in ResourceCollection., Apply patch from Amit Gupta
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1479769 13f79535-47bb-0310-9956-ffa450edef68
---
 .../collection/impl/ResourceCollectionImpl.java    | 40 +++++++++-------------
 .../impl/ResourceCollectionManagerImpl.java        |  7 ++--
 .../impl/ResourceCollectionImplTest.java           | 18 ++++++++++
 3 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java b/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
index ccf88a0..f1e937b 100755
--- a/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
+++ b/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
@@ -66,12 +66,6 @@ public class ResourceCollectionImpl implements
 
     private final Resource membersResource;
 
-    private static final String REF_PROPERTY = "sling:resource";
-
-    private static final String MEMBERS_NODE_NAME = "members";
-    
-    private static final String REFERENCES_PROP = "sling:references";
-
     /**
      * Creates a new collection from the given resource
      * 
@@ -80,7 +74,7 @@ public class ResourceCollectionImpl implements
     public ResourceCollectionImpl(Resource resource) {
         this.resource = resource;
         resolver = resource.getResourceResolver();
-        membersResource = resource.getChild(MEMBERS_NODE_NAME);
+        membersResource = resource.getChild(ResourceCollectionConstants.MEMBERS_NODE_NAME);
     }
     
     /**
@@ -103,15 +97,15 @@ public class ResourceCollectionImpl implements
     public boolean add(Resource res, Map<String, Object> properties) throws PersistenceException {
         if (res != null && !contains(res)) {
         	ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
-        	String[] order = vm.get(REFERENCES_PROP, new String[]{});
+        	String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[]{});
         	
         	order = (String[]) ArrayUtils.add(order, res.getPath());
-        	vm.put(REFERENCES_PROP, order);
+        	vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
         	
         	if (properties == null) {
         		properties = new HashMap<String, Object>();
         	}
-            properties.put(REF_PROPERTY, res.getPath());
+            properties.put(ResourceCollectionConstants.REF_PROPERTY, res.getPath());
             resolver.create(
                 membersResource,
                 ResourceCollectionUtil.createUniqueChildName(membersResource,
@@ -130,13 +124,13 @@ public class ResourceCollectionImpl implements
     public boolean add(Resource res) throws PersistenceException {
         if (res != null && !contains(res)) {
         	ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
-        	String[] order = vm.get(REFERENCES_PROP, new String[]{});
+        	String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[]{});
         	
         	order = (String[]) ArrayUtils.add(order, res.getPath());
-        	vm.put(REFERENCES_PROP, order);
+        	vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
         	
         	Map<String, Object> properties = new HashMap<String, Object>();
-        	properties.put(REF_PROPERTY, res.getPath());
+        	properties.put(ResourceCollectionConstants.REF_PROPERTY, res.getPath());
             resolver.create(
                 membersResource,
                 ResourceCollectionUtil.createUniqueChildName(membersResource,
@@ -157,7 +151,7 @@ public class ResourceCollectionImpl implements
     public Iterator<Resource> getResources() {
     	
     	ValueMap vm = membersResource.adaptTo(ValueMap.class);
-    	String[] references = vm.get(REFERENCES_PROP, new String[]{});
+    	String[] references = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[]{});
     	List<Resource> resources = new ArrayList<Resource>();
     	
         for (String path:references) {
@@ -186,7 +180,7 @@ public class ResourceCollectionImpl implements
     public boolean contains(Resource res) {
     	if (res != null) {
     		ValueMap vm = membersResource.adaptTo(ValueMap.class);
-        	String[] order = vm.get(REFERENCES_PROP, new String[]{});
+        	String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[]{});
         	
         	int index = ArrayUtils.indexOf(order, res.getPath(), 0);
         	
@@ -208,12 +202,12 @@ public class ResourceCollectionImpl implements
         resolver.delete(tobeRemovedRes);
         //remove from order array
         ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
-    	String[] order = vm.get(REFERENCES_PROP, new String[]{});
+    	String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[]{});
     	
     	int index = ArrayUtils.indexOf(order, res.getPath(), 0);
     	
     	order = (String[]) ArrayUtils.remove(order, index);
-    	vm.put(REFERENCES_PROP, order);
+    	vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
     	
     	return true;
     }
@@ -234,13 +228,13 @@ public class ResourceCollectionImpl implements
             String resName = res.getName();
             if (membersResource.getChild(resName) != null
                 && (res.getPath()).equals(ResourceUtil.getValueMap(
-                    membersResource.getChild(resName)).get(REF_PROPERTY, "")))
+                    membersResource.getChild(resName)).get(ResourceCollectionConstants.REF_PROPERTY, "")))
                 return membersResource.getChild(resName);
             // handle multiple res with same name but different paths
             Iterator<Resource> children = membersResource.listChildren();
             while (children.hasNext()) {
                 Resource r = children.next();
-                if (ResourceUtil.getValueMap(r).get(REF_PROPERTY, "").equals(
+                if (ResourceUtil.getValueMap(r).get(ResourceCollectionConstants.REF_PROPERTY, "").equals(
                     res.getPath())) return r;
             }
         }
@@ -252,7 +246,7 @@ public class ResourceCollectionImpl implements
 			throw new IllegalArgumentException("Source Resource can not be null");
 		}
 		ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
-    	String[] order = vm.get(REFERENCES_PROP, new String[]{});
+    	String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[]{});
     	String srcPath = srcResource.getPath();
 		int srcIndex = ArrayUtils.indexOf(order, srcPath);
     	if (srcIndex < 0) {
@@ -268,7 +262,7 @@ public class ResourceCollectionImpl implements
 			String destPath = destResource.getPath();
 			
 			if (destPath.equals(srcPath)) {
-				String message = MessageFormat.format("Collection ordering failed, as source {} and destination {} can not be same", 
+				String message = MessageFormat.format("Collection ordering failed, as source {0} and destination {1} can not be same", 
 	    				srcPath, destPath);
 				log.error(message);
 				throw new IllegalArgumentException(message);
@@ -289,7 +283,7 @@ public class ResourceCollectionImpl implements
 			order = (String[]) ArrayUtils.add(order, destIndex, srcPath);
 		}
 		
-		vm.put(REFERENCES_PROP, order);
+		vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
 	}
 
 	public ModifiableValueMap getProperties(Resource resource) {
@@ -297,7 +291,7 @@ public class ResourceCollectionImpl implements
         while (entries.hasNext()) {
         	Resource entry = entries.next();
         	String path = ResourceUtil.getValueMap(entry).get(
-                    REF_PROPERTY, "");
+        			ResourceCollectionConstants.REF_PROPERTY, "");
             
             if (resource.getPath().equals(path)) {
             	return entry.adaptTo(ModifiableValueMap.class);
diff --git a/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java b/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java
index 450bc7c..4518bf2 100755
--- a/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java
+++ b/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java
@@ -78,7 +78,7 @@ public class ResourceCollectionManagerImpl implements ResourceCollectionManager
             Map<String, Object> properties) throws PersistenceException {
         
         if (parentResource != null) {
-        	String fullPath = parentResource.getPath() + name;
+        	String fullPath = parentResource.getPath() + "/" + name;
 
             if (resolver.getResource(fullPath) != null) {
                 throw new IllegalArgumentException("invalid path, " + fullPath
@@ -100,11 +100,10 @@ public class ResourceCollectionManagerImpl implements ResourceCollectionManager
                     ResourceCollection.RESOURCE_TYPE);
             }
             Resource collectionRes = resolver.create(parentResource, name, properties);
-            resolver.create(collectionRes, "members", null);
+            resolver.create(collectionRes, ResourceCollectionConstants.MEMBERS_NODE_NAME, null);
             log.debug("collection  {} created", fullPath);
 
-            return new ResourceCollectionImpl(
-                resolver.getResource(collectionRes.getPath()));
+            return new ResourceCollectionImpl(collectionRes);
         } else {
             log.error("parent resource can not be null");
             throw new IllegalArgumentException("parent resource can not be null ");
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 d722c3a..dd6b03d 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
@@ -76,6 +76,17 @@ public class ResourceCollectionImplTest {
         Assert.assertNotNull(resResolver.getResource("/test1"));
         Assert.assertEquals(ResourceCollection.RESOURCE_TYPE, resResolver.getResource("/test1").getResourceType());
 	}
+	
+	@Test
+	public void testCheckPath() throws Exception {
+		final Resource rootResource = resResolver.create(resResolver.getResource("/"), "root",
+                Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"type"));
+		
+        final ResourceCollection collection = rcm.createCollection(rootResource, "test1");
+        
+
+        Assert.assertEquals(rootResource.getPath() + "/" + "test1", collection.getPath());
+ 	}
 
 	@Test
 	public void testGetCollection() throws Exception {
@@ -187,6 +198,13 @@ public class ResourceCollectionImplTest {
         	Assert.assertEquals(resPaths[numOfRes], entry.getPath());
         	numOfRes ++;
         }
+        
+        try {
+        	collection.orderBefore(resource, resource);
+        	Assert.fail("should have thrown IllegalArgument");
+        } catch (IllegalArgumentException e) {
+        	
+        }
 
         //change the order
         collection.orderBefore(resource2, resource);

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.