sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From k...@apache.org
Subject svn commit: r1688209 - in /sling/trunk/bundles/extensions/validation: api/ api/src/main/java/org/apache/sling/validation/ core/ core/src/main/java/org/apache/sling/validation/impl/ core/src/test/java/org/apache/sling/validation/impl/
Date Mon, 29 Jun 2015 13:34:03 GMT
Author: kwin
Date: Mon Jun 29 13:34:03 2015
New Revision: 1688209

URL: http://svn.apache.org/r1688209
Log:
SLING-4822 allow filtering based on a predicate for recursive validations

Rename method for recursive validation and cleaned up its javadoc

Modified:
    sling/trunk/bundles/extensions/validation/api/pom.xml
    sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationService.java
    sling/trunk/bundles/extensions/validation/core/pom.xml
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
    sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java

Modified: sling/trunk/bundles/extensions/validation/api/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/api/pom.xml?rev=1688209&r1=1688208&r2=1688209&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/api/pom.xml (original)
+++ sling/trunk/bundles/extensions/validation/api/pom.xml Mon Jun 29 13:34:03 2015
@@ -40,17 +40,17 @@
         <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/validation/api</developerConnection>
         <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/api</url>
     </scm>
-    
+
     <build>
-    	<plugins>
-	    	<plugin>
-		        <groupId>org.apache.felix</groupId>
-		        <artifactId>maven-bundle-plugin</artifactId>
-		        <extensions>true</extensions>
-	         </plugin>
-    	</plugins>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+            </plugin>
+        </plugins>
     </build>
-    
+
     <dependencies>
         <dependency>
             <groupId>org.apache.sling</groupId>
@@ -58,12 +58,19 @@
             <version>2.4.2</version>
             <scope>provided</scope>
         </dependency>
-        
+
         <dependency>
             <groupId>com.google.code.findbugs</groupId>
             <artifactId>jsr305</artifactId>
             <version>3.0.0</version>
             <scope>provided</scope>
         </dependency>
+
+        <dependency>
+            <groupId>commons-collections</groupId>
+            <artifactId>commons-collections</artifactId>
+            <version>3.2.1</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 </project>

Modified: sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationService.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationService.java?rev=1688209&r1=1688208&r2=1688209&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationService.java
(original)
+++ sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationService.java
Mon Jun 29 13:34:03 2015
@@ -18,11 +18,10 @@
  */
 package org.apache.sling.validation;
 
-import java.util.Set;
-
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 
+import org.apache.commons.collections.Predicate;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ValueMap;
@@ -64,7 +63,7 @@ public interface ValidationService {
      * @param resource the resource to validate
      * @param model    the model with which to perform the validation
      * @return a {@link ValidationResult} that provides the necessary information
-     * @throws org.apache.sling.validation.exceptions.SlingValidationException if one validator
was called with invalid arguments
+     * @throws SlingValidationException if one validator was called with invalid arguments
      */
     @Nonnull ValidationResult validate(@Nonnull Resource resource, @Nonnull ValidationModel
model) throws SlingValidationException;
 
@@ -75,23 +74,22 @@ public interface ValidationService {
      *
      * @param valueMap the map to validate
      * @return a {@link ValidationResult} that provides the necessary information
-     * @throws org.apache.sling.validation.exceptions.SlingValidationException if one validator
was called with invalid arguments
+     * @throws SlingValidationException if one validator was called with invalid arguments
      */
     @Nonnull ValidationResult validate(@Nonnull ValueMap valueMap, @Nonnull ValidationModel
model) throws SlingValidationException;
 
     /**
-     * Validates a {@link Resource} and all child resources recursively by traversing starting
from the given resource.
-     * For all resources having a resourceType which is not contained in one of {@code ignoredResourceTypes}
the according {@link ValidationModel} is retrieved and validation called on those.
-     * @param resource the root resource which is validated (including all its children resources
having a valid resource type)
-     * @param enforceValidation if {@code true} will throw an IllegalArgumentException in
case a validation model could not be found for a (not-ignored) resource type
-     * set on one of the resource's children. 
-     * @param ignoredResourceTypes a set of resource types which should not be validated
(e.g. nt:unstructured, the default primary node type in case of underlying an JCR for nodes
not having a sling:resourceType property being set).
-     * May be {@code null} to not ignore any resource types.
+     * Validates a {@link Resource} and all child resources recursively by traversing through
the resource tree starting from the given resource.
+     * For all resources which are included in the given {@code filter} the according {@link
ValidationModel} is retrieved and validation is called on those resources.
+     * @param resource the root resource which is validated
+     * @param enforceValidation if {@code true} will throw an {@link IllegalArgumentException}
in case a validation model could not be found for a (not-ignored) resource 
+     * @param filter a {@link Predicate} on a resource which is evaluated to determine whether
a given resource should be validated. May be {@code null} in which case all resources are
validated. 
+     * Children of ignored resources are still validated (if this predicate applies to them).
      * @return the aggregated {@link ValidationResult} over all child resource validations
      * @throws IllegalStateException in case an invalid validation model was found
      * @throws IllegalArgumentException in case resourceType is absolute but outside of the
search paths or if no validation model could be found (and enforceValidation is {@code true}).
-     * @throws org.apache.sling.validation.exceptions.SlingValidationException if one validator
was called with invalid arguments
+     * @throws SlingValidationException if one validator was called with invalid arguments
      */
-    @Nonnull ValidationResult validateAllResourceTypesInResource(@Nonnull Resource resource,
boolean enforceValidation, Set<String> ignoredResourceTypes) throws IllegalStateException,
IllegalArgumentException, SlingValidationException;
+    @Nonnull ValidationResult validateResourceRecursively(@Nonnull Resource resource, boolean
enforceValidation, Predicate filter) throws IllegalStateException, IllegalArgumentException,
SlingValidationException;
 
 }

Modified: sling/trunk/bundles/extensions/validation/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/pom.xml?rev=1688209&r1=1688208&r2=1688209&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/pom.xml (original)
+++ sling/trunk/bundles/extensions/validation/core/pom.xml Mon Jun 29 13:34:03 2015
@@ -131,6 +131,12 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>commons-collections</groupId>
+            <artifactId>commons-collections</artifactId>
+            <version>3.2.1</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <scope>test</scope>

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java?rev=1688209&r1=1688208&r2=1688209&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java
Mon Jun 29 13:34:03 2015
@@ -20,12 +20,13 @@ package org.apache.sling.validation.impl
 
 import java.util.List;
 import java.util.Map.Entry;
-import java.util.Set;
 
 import javax.annotation.Nonnull;
 
+import org.apache.commons.collections.Predicate;
 import org.apache.sling.api.resource.AbstractResourceVisitor;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.validation.ValidationResult;
 import org.apache.sling.validation.model.ValidationModel;
 
@@ -34,15 +35,15 @@ public class ValidationResourceVisitor e
     private final ValidationServiceImpl validationService;
     private final boolean enforceValidation;
     private final @Nonnull ValidationResultImpl result;
-    private final Set<String> ignoredResourceTypes;
+    private final Predicate filter;
     private final String rootResourcePath;
 
-    public ValidationResourceVisitor(ValidationServiceImpl validationService, String rootResourcePath,
boolean enforceValidation, Set<String> ignoredResourceTypes) {
+    public ValidationResourceVisitor(ValidationServiceImpl validationService, String rootResourcePath,
boolean enforceValidation, Predicate filter) {
         super();
         this.validationService = validationService;
         this.rootResourcePath = rootResourcePath + "/";
         this.enforceValidation = enforceValidation;
-        this.ignoredResourceTypes = ignoredResourceTypes;
+        this.filter = filter;
         this.result = new ValidationResultImpl();
     }
 
@@ -80,11 +81,11 @@ public class ValidationResourceVisitor e
      * @return {@code true} in case the given resource should have its own Sling Validation
model
      */
     private boolean isValidSubResource(Resource resource) {
-        if (resource.getResourceType() == Resource.RESOURCE_TYPE_NON_EXISTING) {
+        if (ResourceUtil.isNonExistingResource(resource)) {
             return false;
         }
-        if (ignoredResourceTypes.contains(resource.getResourceType())) {
-            return false;
+        if (filter != null) {
+            return filter.evaluate(resource);
         }
         return true;
     }

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java?rev=1688209&r1=1688208&r2=1688209&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
Mon Jun 29 13:34:03 2015
@@ -20,15 +20,14 @@ package org.apache.sling.validation.impl
 
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
-import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 
+import org.apache.commons.collections.Predicate;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
@@ -198,12 +197,9 @@ public class ValidationServiceImpl imple
     }    
 
     @Override
-    public @Nonnull ValidationResult validateAllResourceTypesInResource(@Nonnull Resource
resource, boolean enforceValidation, Set<String> ignoredResourceTypes)
+    public @Nonnull ValidationResult validateResourceRecursively(@Nonnull Resource resource,
boolean enforceValidation, Predicate filter) 
             throws IllegalStateException, IllegalArgumentException, SlingValidationException
{
-        if (ignoredResourceTypes == null) {
-            ignoredResourceTypes = Collections.emptySet();
-        }
-        ValidationResourceVisitor visitor = new ValidationResourceVisitor(this, resource.getPath(),
enforceValidation, ignoredResourceTypes);
+        ValidationResourceVisitor visitor = new ValidationResourceVisitor(this, resource.getPath(),
enforceValidation, filter);
         visitor.accept(resource);
         return visitor.getResult();
     }

Modified: sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java?rev=1688209&r1=1688208&r2=1688209&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
Mon Jun 29 13:34:03 2015
@@ -29,6 +29,7 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.jcr.RepositoryException;
 
+import org.apache.commons.collections.Predicate;
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.ModifiableValueMap;
@@ -361,7 +362,7 @@ public class ValidationServiceImplTest {
     }
 
     @Test()
-    public void testValidateAllResourceTypesInResource() throws Exception {
+    public void testValidateResourceRecursively() throws Exception {
         modelBuilder.resourceProperty(propertyBuilder.build("field1"));
         final ValidationModel vm1 = modelBuilder.build("resourcetype1");
         modelBuilder = new ValidationModelBuilder();
@@ -404,8 +405,18 @@ public class ValidationServiceImplTest {
         properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype3");
         rr.create(testResource, "child3", properties);
 
-        ValidationResult vr = validationService.validateAllResourceTypesInResource(testResource,
true,
-                new HashSet<String>(Arrays.asList(JcrConstants.NT_UNSTRUCTURED, "resourcetype3")));
+        Predicate ignoreResourceType3Filter = new Predicate() {
+            @Override
+            public boolean evaluate(Object object) {
+                Resource resource = (Resource) object;
+                if ("resourcetype3".equals(resource.getResourceType())) {
+                    return false;
+                }
+                return true;
+            }
+        };
+        
+        ValidationResult vr = validationService.validateResourceRecursively(testResource,
true, ignoreResourceType3Filter);
         Assert.assertFalse("resource should have been considered invalid", vr.isValid());
         Assert.assertThat(vr.getFailureMessages(),
                 Matchers.hasEntry("field1", Arrays.asList("Missing required property.")));
@@ -415,7 +426,7 @@ public class ValidationServiceImplTest {
     }
 
     @Test(expected = IllegalArgumentException.class)
-    public void testValidateAllResourceTypesInResourceWithMissingValidationModel() throws
Exception {
+    public void testValidateResourceRecursivelyWithMissingValidationModel() throws Exception
{
         // set model retriever which never retrieves anything
         validationService.modelRetriever = new ValidationModelRetriever() {
             @Override
@@ -429,12 +440,11 @@ public class ValidationServiceImplTest {
         Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource",
"resourcetype1",
                 JcrConstants.NT_UNSTRUCTURED, true);
 
-        ValidationResult vr = validationService.validateAllResourceTypesInResource(testResource,
true,
-                Collections.singleton(JcrConstants.NT_UNSTRUCTURED));
+        ValidationResult vr = validationService.validateResourceRecursively(testResource,
true, null);
     }
 
     @Test()
-    public void testValidateAllResourceTypesInResourceWithMissingValidatorAndNoEnforcement()
throws Exception {
+    public void testValidateResourceRecursivelyWithMissingValidatorAndNoEnforcement() throws
Exception {
         // set model retriever which never retrieves anything
         validationService.modelRetriever = new ValidationModelRetriever() {
             @Override
@@ -448,8 +458,7 @@ public class ValidationServiceImplTest {
         Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource",
"resourcetype1",
                 JcrConstants.NT_UNSTRUCTURED, true);
 
-        ValidationResult vr = validationService.validateAllResourceTypesInResource(testResource,
false,
-                Collections.singleton(JcrConstants.NT_UNSTRUCTURED));
+        ValidationResult vr = validationService.validateResourceRecursively(testResource,
false, null);
         Assert.assertTrue(vr.isValid());
     }
 



Mime
View raw message