incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1479769 - in /sling/trunk/contrib/extensions/collection/src: main/java/org/apache/sling/resource/collection/impl/ test/java/org/apache/sling/resource/collection/impl/
Date Tue, 07 May 2013 06:09:02 GMT
Author: cziegeler
Date: Tue May  7 06:09:02 2013
New Revision: 1479769

URL: http://svn.apache.org/r1479769
Log:
SLING-2857 : Fix the name of the members resource in ResourceCollection., Apply patch from
Amit Gupta

Modified:
    sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
    sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java
    sling/trunk/contrib/extensions/collection/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java

Modified: sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java?rev=1479769&r1=1479768&r2=1479769&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
(original)
+++ sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
Tue May  7 06:09:02 2013
@@ -66,12 +66,6 @@ public class ResourceCollectionImpl impl
 
     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 impl
     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 impl
     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 impl
     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 impl
     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 impl
     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 impl
         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 impl
             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 impl
 			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 impl
 			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 impl
 			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 impl
         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);

Modified: sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java?rev=1479769&r1=1479768&r2=1479769&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java
(original)
+++ sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionManagerImpl.java
Tue May  7 06:09:02 2013
@@ -78,7 +78,7 @@ public class ResourceCollectionManagerIm
             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 ResourceCollectionManagerIm
                     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 ");

Modified: sling/trunk/contrib/extensions/collection/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/collection/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java?rev=1479769&r1=1479768&r2=1479769&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/collection/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java
(original)
+++ sling/trunk/contrib/extensions/collection/src/test/java/org/apache/sling/resource/collection/impl/ResourceCollectionImplTest.java
Tue May  7 06:09:02 2013
@@ -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);



Mime
View raw message