You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2016/05/25 12:42:46 UTC

svn commit: r1745478 - in /sling/trunk/bundles/extensions/validation/core/src: main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java

Author: kwin
Date: Wed May 25 12:42:46 2016
New Revision: 1745478

URL: http://svn.apache.org/viewvc?rev=1745478&view=rev
Log:
SLING-5749 do not fail with IAE in case of acting on a SyntheticResource

Modified:
    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/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=1745478&r1=1745477&r2=1745478&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 Wed May 25 12:42:46 2016
@@ -148,12 +148,10 @@ public class ValidationServiceImpl imple
             throw new IllegalArgumentException("ValidationService.validate - cannot accept null parameters");
         }
         CompositeValidationResult result = new CompositeValidationResult();
-        final ValueMap valueMap;
-        if (ResourceUtil.isNonExistingResource(resource)) {
-            // NonExistingResource can not adapt to a ValueMap, therefore just use the empty map here
+        ValueMap valueMap = resource.adaptTo(ValueMap.class);
+        if (valueMap == null) {
+            // SyntheticResources can not adapt to a ValueMap, therefore just use the empty map here
             valueMap = new ValueMapDecorator(Collections.emptyMap());
-        } else {
-            valueMap = resource.adaptTo(ValueMap.class);
         }
 
         // validate direct properties of the resource

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=1745478&r1=1745477&r2=1745478&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 Wed May 25 12:42:46 2016
@@ -37,6 +37,7 @@ import org.apache.sling.api.resource.Per
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.resource.SyntheticResource;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.wrappers.ValueMapDecorator;
 import org.apache.sling.jcr.resource.JcrResourceConstants;
@@ -210,6 +211,30 @@ public class ValidationServiceImplTest {
         ValidationResult vr = validationService.validate(nonExistingResource, vm);
         Assert.assertFalse("resource should have been considered invalid", vr.isValid());
         Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>containsInAnyOrder(
+                new DefaultValidationFailure("", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"),
+                new DefaultValidationFailure("", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_WITH_NAME, "child")
+                ));
+    }
+
+    // see https://issues.apache.org/jira/browse/SLING-5749
+    @Test
+    public void testSyntheticResource() throws Exception {
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, "\\d"); // accept any digits
+        ResourceProperty property = propertyBuilder.build("field1");
+        modelBuilder.resourceProperty(property);
+        
+        ChildResource modelChild = new ChildResourceImpl("child", null, true, Collections.singletonList(property), Collections.emptyList());
+        modelBuilder.childResource(modelChild);
+        
+        modelChild = new ChildResourceImpl("optionalChild", null, false, Collections.singletonList(property), Collections.emptyList());
+        modelBuilder.childResource(modelChild);
+        
+        ValidationModel vm = modelBuilder.build("sometype");
+        ResourceResolver rr = context.resourceResolver();
+        Resource nonExistingResource = new SyntheticResource(rr, "someresource", "resourceType");
+        ValidationResult vr = validationService.validate(nonExistingResource, vm);
+        Assert.assertFalse("resource should have been considered invalid", vr.isValid());
+        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>containsInAnyOrder(
                 new DefaultValidationFailure("", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"),
                 new DefaultValidationFailure("", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_WITH_NAME, "child")
                 ));