Author: kwin
Date: Fri Dec  5 09:46:18 2014
New Revision: 1643214

URL: http://svn.apache.org/viewvc?rev=1643214&view=rev
Log:
SLING-4218 throw exception in case validation model is not valid
clarify in JavaDoc which exceptions are thrown and where to expect null

Modified:
    
sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationModel.java
    
sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java
    
sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
    
sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java

Modified: 
sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationModel.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationModel.java?rev=1643214&r1=1643213&r2=1643214&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationModel.java
 (original)
+++ 
sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationModel.java
 Fri Dec  5 09:46:18 2014
@@ -29,30 +29,30 @@ public interface ValidationModel {
     /**
      * Returns the properties validated by this model.
      *
-     * @return the properties set
+     * @return the properties set (never {@code null}, but might be empty set)
      */
     Set<ResourceProperty> getResourceProperties();
 
     /**
      * Returns the type of resource this model validates.
      *
-     * @return the validated resource type
+     * @return the validated resource type, never {@code null}
      */
     String getValidatedResourceType();
 
     /**
      * Returns the paths under which resources will be validated by this 
model. 
-     * Is never null nor an empty array. Might return a single element array 
containing only the empty string, 
+     * Might return a single element array containing only the empty string, 
      * in which case the validation model has no path restriction.
      *
-     * @return a path array
+     * @return a path array. Is never {@code null} nor an empty array
      */
     String[] getApplicablePaths();
 
     /**
      * Returns the expected children for a resource validated by this model.
      *
-     * @return the children list (can be empty if there are no children)
+     * @return the children list (can be empty if there are no children), 
never {@code null}
      */
     List<ChildResource> getChildren();
 

Modified: 
sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java?rev=1643214&r1=1643213&r2=1643214&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java
 (original)
+++ 
sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java
 Fri Dec  5 09:46:18 2014
@@ -34,16 +34,19 @@ public interface ValidationService {
      * @param validatedResourceType the type of {@code Resources} the model 
validates
      * @param applicablePath        the model's applicable path (the path of 
the validated resource)
      * @return a {@code ValidationModel} if one is found, {@code null} 
otherwise
+     * @throws IllegalStateException in case an invalid validation model was 
found
+     * @throws IllegalArgumentException in case validatedResourceType was 
blank or {@code null}
      */
-    ValidationModel getValidationModel(String validatedResourceType, String 
applicablePath);
+    ValidationModel getValidationModel(String validatedResourceType, String 
applicablePath) throws IllegalStateException, IllegalArgumentException;
 
     /**
      * Tries to obtain a {@link ValidationModel} that is able to validate the 
given {@code resource}.
      *
      * @param resource the resource for which to obtain a validation model
      * @return a {@code ValidationModel} if one is found, {@code null} 
otherwise
+     * @throws IllegalStateException in case an invalid validation model was 
found
      */
-    ValidationModel getValidationModel(Resource resource);
+    ValidationModel getValidationModel(Resource resource) throws 
IllegalStateException;
 
     /**
      * Validates a {@link Resource} using a specific {@link ValidationModel}. 
If the {@code model} describes a resource tree,

Modified: 
sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java?rev=1643214&r1=1643213&r2=1643214&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
 (original)
+++ 
sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
 Fri Dec  5 09:46:18 2014
@@ -334,7 +334,7 @@ public class ValidationServiceImpl imple
                         Set<ResourceProperty> resourceProperties = 
JCRBuilder.buildProperties(validators, r);
                         List<ChildResource> children = 
JCRBuilder.buildChildren(model, model, validators);
                         if (resourceProperties.isEmpty() && 
children.isEmpty()) {
-                            LOG.warn("Incomplete validation model resource {}. 
Neither children nor properties set.", model.getPath());
+                            throw new IllegalArgumentException("Neither 
children nor properties set.");
                         } else {
                             vm = new JCRValidationModel(jcrPath, 
resourceProperties, validatedResourceType, applicablePaths, children);
                             modelsForResourceType = 
validationModelsCache.get(validatedResourceType);
@@ -354,12 +354,12 @@ public class ValidationServiceImpl imple
                             }
                         }
                     } catch (IllegalArgumentException e) {
-                        LOG.error("Found invalid validation model in '{}': 
{}", jcrPath, e.getMessage());
+                        throw new IllegalStateException("Found invalid 
validation model in '" + jcrPath +"': " + e.getMessage(), e);
                     }
                 }
             }
         } catch (LoginException e) {
-            LOG.error("Unable to obtain a resource resolver.", e);
+            throw new IllegalStateException("Unable to obtain a resource 
resolver.", e);
         } finally {
             if (rr != null) {
                 rr.close();

Modified: 
sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java?rev=1643214&r1=1643213&r2=1643214&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
 (original)
+++ 
sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
 Fri Dec  5 09:46:18 2014
@@ -192,6 +192,56 @@ public class ValidationServiceImplTest {
         }
     }
 
+    @Test(expected=IllegalStateException.class)
+    public void testGetValidationModelWithInvalidValidator() throws Exception {
+        
validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
 new RegexValidator());
+
+        TestProperty field = new TestProperty("field1");
+        // invalid validator name
+        
field.addValidator("org.apache.sling.validation.impl.validators1.RegexValidator");
+        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
+        Resource model = null;
+        try {
+            if (rr != null) {
+                model = createValidationModelResource(rr, 
appsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
+                        new String[]{"/apps/validation/1",
+                                "/apps/validation/2"}, field);
+            }
+
+            ValidationModel vm = 
validationService.getValidationModel("sling/validation/test", 
"/apps/validation/1/resource");
+        } finally {
+            if (rr != null) {
+                if (model != null) {
+                    rr.delete(model);
+                }
+                rr.commit();
+                rr.close();
+            }
+        }
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testGetValidationModelWithMissingChildrenAndProperties() 
throws Exception {
+        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
+        Resource model = null;
+        try {
+            if (rr != null) {
+                model = createValidationModelResource(rr, 
appsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
+                        new String[]{"/apps/validation/1",
+                                "/apps/validation/2"});
+            }
+            ValidationModel vm = 
validationService.getValidationModel("sling/validation/test", 
"/apps/validation/1/resource");
+        } finally {
+            if (rr != null) {
+                if (model != null) {
+                    rr.delete(model);
+                }
+                rr.commit();
+                rr.close();
+            }
+        }
+    }
+
     @Test()
     public void testValueMapWithWrongDataType() throws Exception {
         
validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
 new RegexValidator());
@@ -656,6 +706,9 @@ public class ValidationServiceImplTest {
     
     private void createValidationModelProperties(Resource model, 
TestProperty... properties) throws PersistenceException {
         ResourceResolver rr = model.getResourceResolver();
+        if (properties.length == 0) {
+            return;
+        }
         Resource propertiesResource = ResourceUtil.getOrCreateResource(rr, 
model.getPath() + "/" + Constants
                 .PROPERTIES, JcrConstants.NT_UNSTRUCTURED, null, true);
         if (propertiesResource != null) {


Reply via email to