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 2015/02/03 16:13:12 UTC

svn commit: r1656755 - in /sling/trunk/contrib/extensions/validation: api/src/main/java/org/apache/sling/validation/api/ core/src/main/java/org/apache/sling/validation/impl/ core/src/test/java/org/apache/sling/validation/impl/ core/src/test/java/org/ap...

Author: kwin
Date: Tue Feb  3 15:13:12 2015
New Revision: 1656755

URL: http://svn.apache.org/r1656755
Log:
SLING-4387 support recursive validation of resources

cleanup of tests and mocked resource

Added:
    sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java
Modified:
    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
    sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResource.java
    sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java

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=1656755&r1=1656754&r2=1656755&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 Tue Feb  3 15:13:12 2015
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.validation.api;
 
+import java.util.Set;
+
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ValueMap;
@@ -46,8 +48,9 @@ public interface ValidationService {
      * @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
+     * @throws IllegalArgumentException in case resourceType being set on the given resource is blank, not set or absolute but outside of the search paths.
      */
-    ValidationModel getValidationModel(Resource resource) throws IllegalStateException;
+    ValidationModel getValidationModel(Resource resource) throws IllegalStateException, IllegalArgumentException;
 
     /**
      * Validates a {@link Resource} using a specific {@link ValidationModel}. If the {@code model} describes a resource tree,
@@ -71,4 +74,19 @@ public interface ValidationService {
      * @throws org.apache.sling.validation.api.exceptions.SlingValidationException if one validator was called with invalid arguments
      */
     ValidationResult validate(ValueMap valueMap, 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)
+     * @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.api.exceptions.SlingValidationException if one validator was called with invalid arguments
+     */
+    ValidationResult validateAllResourceTypesInResource(Resource resource, boolean enforceValidation, Set<String> ignoredResourceTypes) throws IllegalStateException, IllegalArgumentException, SlingValidationException;
+
 }

Added: sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java?rev=1656755&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java (added)
+++ sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationResourceVisitor.java Tue Feb  3 15:13:12 2015
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.validation.impl;
+
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.sling.api.resource.AbstractResourceVisitor;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.validation.api.ValidationModel;
+import org.apache.sling.validation.api.ValidationResult;
+
+public class ValidationResourceVisitor extends AbstractResourceVisitor {
+
+    private final ValidationServiceImpl validationService;
+    private final boolean enforceValidation;
+    private final ValidationResultImpl result;
+    private final Set<String> ignoredResourceTypes;
+    private final String rootResourcePath;
+
+    public ValidationResourceVisitor(ValidationServiceImpl validationService, String rootResourcePath, boolean enforceValidation, Set<String> ignoredResourceTypes) {
+        super();
+        this.validationService = validationService;
+        this.rootResourcePath = rootResourcePath + "/";
+        this.enforceValidation = enforceValidation;
+        this.ignoredResourceTypes = ignoredResourceTypes;
+        this.result = new ValidationResultImpl();
+    }
+
+    @Override
+    protected void visit(Resource resource) {
+        if (isValidSubResource(resource)) {
+            // JCR will return then primary type instead!!
+            ValidationModel model = validationService.getValidationModel(resource);
+            if (model == null) {
+                if (enforceValidation) {
+                    throw new IllegalArgumentException("No model for resource type " + resource.getResourceType() + " found.");
+                }
+            }
+            // the relative path must end with a slash and not start with a slash
+            final String relativePath;
+            if (resource.getPath().startsWith(rootResourcePath)) {
+                relativePath = resource.getPath().substring(rootResourcePath.length()) + "/";
+            } else {
+                relativePath = "";
+            }
+            ValidationResult localResult = validationService.validate(resource, model, relativePath);
+            for (Entry<String, List<String>> entry : localResult.getFailureMessages().entrySet()) {
+                for (String message : entry.getValue()) {
+                    // calculate the property name correctly from the root
+                    result.addFailureMessage(entry.getKey(), message);
+                }
+            }
+        }
+    }
+    
+    /**
+     * 
+     * @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) {
+            return false;
+        }
+        if (ignoredResourceTypes.contains(resource.getResourceType())) {
+            return false;
+        }
+        return true;
+    }
+
+    public ValidationResultImpl getResult() {
+        return result;
+    }
+
+}

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=1656755&r1=1656754&r2=1656755&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 Tue Feb  3 15:13:12 2015
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.validation.impl;
 
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -106,7 +107,11 @@ public class ValidationServiceImpl imple
             modelsForResourceType = searchAndStoreValidationModel(validatedResourceType);
             if (modelsForResourceType != null) {
                 model = modelsForResourceType.getElementForLongestMatchingKey(resourcePath).getValue();
+                if (model == null) {
+                    LOG.warn("Although model for resource type {} is available, it is not allowed for path {}", validatedResourceType, resourcePath);
+                }
             }
+            
         }
         return model;
     }
@@ -118,16 +123,20 @@ public class ValidationServiceImpl imple
 
     @Override
     public ValidationResult validate(Resource resource, ValidationModel model) {
+        return validate(resource, model, "");
+    }
+    
+    protected ValidationResult validate(Resource resource, ValidationModel model, String relativePath) {
         if (resource == null || model == null) {
             throw new IllegalArgumentException("ValidationService.validate - cannot accept null parameters");
         }
         ValidationResultImpl result = new ValidationResultImpl();
 
         // validate direct properties of the resource
-        validateValueMap(resource.adaptTo(ValueMap.class), "", model.getResourceProperties(), result );
+        validateValueMap(resource.adaptTo(ValueMap.class), relativePath, model.getResourceProperties(), result );
 
         // validate children resources, if any
-        validateChildren(resource, "", model.getChildren(), result);
+        validateChildren(resource, relativePath, model.getChildren(), result);
         return result;
     }
 
@@ -211,6 +220,17 @@ public class ValidationServiceImpl imple
         ValidationResultImpl result = new ValidationResultImpl();
         validateValueMap(valueMap,  "", model.getResourceProperties(), result);
         return result;
+    }    
+
+    @Override
+    public ValidationResult validateAllResourceTypesInResource(Resource resource, boolean enforceValidation, Set<String> ignoredResourceTypes)
+            throws IllegalStateException, IllegalArgumentException, SlingValidationException {
+        if (ignoredResourceTypes == null) {
+            ignoredResourceTypes = Collections.emptySet();
+        }
+        ValidationResourceVisitor visitor = new ValidationResourceVisitor(this, resource.getPath(), true, ignoredResourceTypes);
+        visitor.accept(resource);
+        return visitor.getResult();
     }
 
     // EventHandler ########################################################################################################################

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=1656755&r1=1656754&r2=1656755&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 Tue Feb  3 15:13:12 2015
@@ -24,13 +24,14 @@ import static org.junit.Assert.assertTru
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.jackrabbit.JcrConstants;
+import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
@@ -47,6 +48,7 @@ import org.apache.sling.validation.impl.
 import org.apache.sling.validation.impl.util.examplevalidators.DateValidator;
 import org.apache.sling.validation.impl.validators.RegexValidator;
 import org.hamcrest.Matchers;
+import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.Before;
@@ -68,6 +70,7 @@ public class ValidationServiceImplTest {
     private static Resource appsValidatorsRoot;
     private static Resource libsValidatorsRoot;
     private ValidationServiceImpl validationService;
+    private ResourceResolver rr;
 
     @BeforeClass
     public static void init() throws Exception {
@@ -79,10 +82,10 @@ public class ValidationServiceImplTest {
         });
         ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
         if (rr != null) {
-            appsValidatorsRoot = ResourceUtil.getOrCreateResource(rr, APPS + "/" + VALIDATION_MODELS_RELATIVE_PATH, (Map<String, Object>) null,
-                    "sling:Folder", true);
-            libsValidatorsRoot = ResourceUtil.getOrCreateResource(rr, LIBS + "/" + VALIDATION_MODELS_RELATIVE_PATH, (Map<String, Object>) null,
-                    "sling:Folder", true);
+            appsValidatorsRoot = ResourceUtil.getOrCreateResource(rr, APPS + "/" + VALIDATION_MODELS_RELATIVE_PATH,
+                    (Map<String, Object>) null, "sling:Folder", true);
+            libsValidatorsRoot = ResourceUtil.getOrCreateResource(rr, LIBS + "/" + VALIDATION_MODELS_RELATIVE_PATH,
+                    (Map<String, Object>) null, "sling:Folder", true);
             rr.close();
         }
     }
@@ -103,78 +106,78 @@ public class ValidationServiceImplTest {
     }
 
     @Before
-    public void setUp() {
+    public void setUp() throws LoginException {
         validationService = new ValidationServiceImpl();
         validationService.validators = new HashMap<String, Validator<?>>();
         Whitebox.setInternalState(validationService, "rrf", rrf);
-       
+        rr = rrf.getAdministrativeResourceResolver(null);
     }
-    
+
+    @After
+    public void tearDown() throws PersistenceException {
+        rr.commit();
+        rr.close();
+    }
+
     @Test
     public void testGetValidationModel() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
-        
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
+
         TestProperty property = new TestProperty("field1");
         property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
         Resource model1 = null, model2 = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
-                model2 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel2", "sling/validation/test",
-                        new String[]{"/apps/validation/1",
-                        "/apps/validation/2"}, property);
-            }
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, property);
+            model2 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel2",
+                    "sling/validation/test", new String[] { "/apps/validation/1", "/apps/validation/2" }, property);
 
             // BEST MATCHING PATH = /apps/validation/1; assume the applicable paths contain /apps/validation/2
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
-            assertTrue(arrayContainsString(vm.getApplicablePaths(), "/apps/validation/2"));
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
+            assertThat(vm.getApplicablePaths(), Matchers.hasItemInArray("/apps/validation/2"));
 
-            // BEST MATCHING PATH = /apps/validation; assume the applicable paths contain /apps/validation but not /apps/validation/1
+            // BEST MATCHING PATH = /apps/validation; assume the applicable paths contain /apps/validation but not
+            // /apps/validation/1
             vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/resource");
-            assertTrue(arrayContainsString(vm.getApplicablePaths(), "/apps/validation"));
-            assertTrue(!arrayContainsString(vm.getApplicablePaths(), "/apps/validation/1"));
+            assertThat(vm.getApplicablePaths(), Matchers.hasItemInArray("/apps/validation"));
+            assertThat(vm.getApplicablePaths(), Matchers.not(Matchers.hasItemInArray("/apps/validation/1")));
+        } finally {
             if (model1 != null) {
                 rr.delete(model1);
             }
             if (model2 != null) {
                 rr.delete(model2);
             }
-        } finally {
-            if (rr != null) {
-                rr.commit();
-                rr.close();
-            }
         }
     }
 
     @Test
     public void testGetValidationModelWithOverlay() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty field = new TestProperty("field1");
         field.addValidator("org.apache.sling.validation.impl.validators.RegexValidator");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
         Resource model1 = null, model2 = null, model3 = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation/1"}, field);
-                model2 = createValidationModelResource(rr, appsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation/1",
-                                "/apps/validation/2"}, field);
-                model3 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel2", "sling/validation/test",
-                        new String[]{"/apps/validation/3"}, field);
-            }
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation/1" }, field);
+            model2 = createValidationModelResource(rr, appsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation/1", "/apps/validation/2" }, field);
+            model3 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel2",
+                    "sling/validation/test", new String[] { "/apps/validation/3" }, field);
 
             // BEST MATCHING PATH = /apps/validation/1; assume the applicable paths contain /apps/validation/2
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
-            assertTrue(arrayContainsString(vm.getApplicablePaths(), "/apps/validation/2"));
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
+            assertThat(vm.getApplicablePaths(), Matchers.hasItemInArray("/apps/validation/2"));
 
             vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/3/resource");
-            assertTrue(arrayContainsString(vm.getApplicablePaths(), "/apps/validation/3"));
+            assertThat(vm.getApplicablePaths(), Matchers.hasItemInArray("/apps/validation/3"));
 
+        } finally {
             if (model1 != null) {
                 rr.delete(model1);
             }
@@ -184,102 +187,85 @@ public class ValidationServiceImplTest {
             if (model3 != null) {
                 rr.delete(model3);
             }
-        } finally {
-            if (rr != null) {
-                rr.commit();
-                rr.close();
-            }
         }
     }
 
-    @Test(expected=IllegalStateException.class)
+    @Test(expected = IllegalStateException.class)
     public void testGetValidationModelWithInvalidValidator() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        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");
+            model = createValidationModelResource(rr, appsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation/1", "/apps/validation/2" }, field);
+            validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
         } finally {
-            if (rr != null) {
-                if (model != null) {
-                    rr.delete(model);
-                }
-                rr.commit();
-                rr.close();
+            if (model != null) {
+                rr.delete(model);
             }
         }
     }
 
-    @Test(expected=IllegalStateException.class)
+    @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");
+            model = createValidationModelResource(rr, appsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation/1", "/apps/validation/2" });
+            validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
         } finally {
-            if (rr != null) {
-                if (model != null) {
-                    rr.delete(model);
-                }
-                rr.commit();
-                rr.close();
+            if (model != null) {
+                rr.delete(model);
             }
         }
     }
 
     @Test()
     public void testValueMapWithWrongDataType() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
-        validationService.validators.put("org.apache.sling.validation.impl.util.examplevalidators.DateValidator",new DateValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.util.examplevalidators.DateValidator",
+                new DateValidator());
 
         TestProperty property = new TestProperty("field1");
         property.addValidator("org.apache.sling.validation.impl.util.examplevalidators.DateValidator");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
         Resource model1 = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
-            }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
-            HashMap<String, Object> hashMap = new HashMap<String, Object>() {{
-                put("field1", "1");
-            }};
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, property);
+
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
+            HashMap<String, Object> hashMap = new HashMap<String, Object>() {
+                {
+                    put("field1", "1");
+                }
+            };
             ValueMap map = new ValueMapDecorator(hashMap);
             ValidationResult vr = validationService.validate(map, vm);
-            
+
             Map<String, List<String>> expectedFailureMessages = new HashMap<String, List<String>>();
-            expectedFailureMessages.put("field1", Arrays.asList("Property was expected to be of type 'class java.util.Date' but cannot be converted to that type."));
+            expectedFailureMessages
+                    .put("field1",
+                            Arrays.asList("Property was expected to be of type 'class java.util.Date' but cannot be converted to that type."));
             Assert.assertThat(vr.getFailureMessages().entrySet(), Matchers.equalTo(expectedFailureMessages.entrySet()));
         } finally {
             if (model1 != null) {
                 rr.delete(model1);
             }
-            if (rr != null) {
-                rr.commit();
-                rr.close();
-            }
         }
     }
-    
+
     @Test()
     public void testValueMapWithMissingField() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
         property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=.*");
@@ -289,20 +275,21 @@ public class ValidationServiceImplTest {
         property3.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=.*");
         TestProperty property4 = new TestProperty("field4");
         property3.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=.*");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
         Resource model1 = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property, property2, property3, property4);
-            }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, property, property2, property3,
+                    property4);
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
             // this should not be detected as missing property
-            HashMap<String, Object> hashMap = new HashMap<String, Object>() {{
-                put("field1", new String[]{});
-                put("field2", new String[]{"null"});
-                put("field3", "");
-            }};
+            HashMap<String, Object> hashMap = new HashMap<String, Object>() {
+                {
+                    put("field1", new String[] {});
+                    put("field2", new String[] { "null" });
+                    put("field3", "");
+                }
+            };
             ValueMap map = new ValueMapDecorator(hashMap);
             ValidationResult vr = validationService.validate(map, vm);
             Map<String, List<String>> expectedFailureMessages = new HashMap<String, List<String>>();
@@ -312,32 +299,30 @@ public class ValidationServiceImplTest {
             if (model1 != null) {
                 rr.delete(model1);
             }
-            if (rr != null) {
-                rr.commit();
-                rr.close();
-            }
         }
     }
-    
+
     @Test()
     public void testValueMapWithMissingOptionalValue() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
         property.optional = true;
         property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "");
-        
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
         Resource model1 = null;
         try {
             if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
+                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                        "sling/validation/test", new String[] { "/apps/validation" }, property);
             }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
-            HashMap<String, Object> hashMap = new HashMap<String, Object>() {{
-                put("field2", "1");
-            }};
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
+            HashMap<String, Object> hashMap = new HashMap<String, Object>() {
+                {
+                    put("field2", "1");
+                }
+            };
             ValueMap map = new ValueMapDecorator(hashMap);
             ValidationResult vr = validationService.validate(map, vm);
             Assert.assertTrue(vr.isValid());
@@ -345,32 +330,29 @@ public class ValidationServiceImplTest {
             if (model1 != null) {
                 rr.delete(model1);
             }
-            if (rr != null) {
-                rr.commit();
-                rr.close();
-            }
         }
     }
-    
+
     @Test()
     public void testValueMapWithEmptyOptionalValue() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
         property.optional = true;
         property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=abc");
-        
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
         Resource model1 = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
-            }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
-            HashMap<String, Object> hashMap = new HashMap<String, Object>() {{
-                put("field1", "");
-            }};
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, property);
+
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
+            HashMap<String, Object> hashMap = new HashMap<String, Object>() {
+                {
+                    put("field1", "");
+                }
+            };
             ValueMap map = new ValueMapDecorator(hashMap);
             ValidationResult vr = validationService.validate(map, vm);
             Assert.assertFalse(vr.isValid());
@@ -382,34 +364,31 @@ public class ValidationServiceImplTest {
             if (model1 != null) {
                 rr.delete(model1);
             }
-            if (rr != null) {
-                rr.commit();
-                rr.close();
-            }
         }
     }
 
     @Test
     public void testValueMapWithCorrectDataType() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty field1 = new TestProperty("field1");
         field1.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=^\\p{L}+$");
         TestProperty field2 = new TestProperty("field2");
         final String TEST_REGEX = "^test$";
-        field2.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex="+TEST_REGEX);
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
+        field2.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=" + TEST_REGEX);
         Resource model1 = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, field1, field2);
-            }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
-            HashMap<String, Object> hashMap = new HashMap<String, Object>() {{
-                put("field1", "HelloWorld");
-                put("field2", "HelloWorld");
-            }};
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, field1, field2);
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
+            HashMap<String, Object> hashMap = new HashMap<String, Object>() {
+                {
+                    put("field1", "HelloWorld");
+                    put("field2", "HelloWorld");
+                }
+            };
             ValueMap map = new ValueMapDecorator(hashMap);
             ValidationResult vr = validationService.validate(map, vm);
             assertFalse(vr.isValid());
@@ -417,217 +396,231 @@ public class ValidationServiceImplTest {
             Map<String, List<String>> expectedFailureMessages = new HashMap<String, List<String>>();
             expectedFailureMessages.put("field2", Arrays.asList("Property does not match the pattern " + TEST_REGEX));
             Assert.assertThat(vr.getFailureMessages().entrySet(), Matchers.equalTo(expectedFailureMessages.entrySet()));
+        } finally {
             if (model1 != null) {
                 rr.delete(model1);
             }
-        } finally {
-            if (rr != null) {
-                rr.commit();
-                rr.close();
-            }
         }
     }
-    
 
     @Test
     public void testResourceWithMissingChildProperty() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
-        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", RegexValidator.REGEX_PARAM + "=" + "\\d");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
+        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", RegexValidator.REGEX_PARAM
+                + "=" + "\\d");
         Resource model1 = null;
         Resource testResource = null;
         try {
             if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
-                
-                Resource child = createValidationModelChildResource(model1, "child1", null, false,  new TestProperty("hello"));
+                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                        "sling/validation/test", new String[] { "/apps/validation" }, property);
+
+                Resource child = createValidationModelChildResource(model1, "child1", null, false, new TestProperty(
+                        "hello"));
                 createValidationModelChildResource(child, "grandChild1", null, false, new TestProperty("hello"));
 
-                testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource", JcrConstants.NT_UNSTRUCTURED,
-                        JcrConstants.NT_UNSTRUCTURED, true);
+                testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource",
+                        JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
                 ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
                 mvm.put("field1", "1");
-                
-                Resource childResource = rr.create(testResource, "child1", new HashMap<String, Object>(){{
-                    put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
-                
-                Resource resourceChild = rr.create(testResource, "child1", new HashMap<String, Object>(){{
-                    put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
+
+                Resource childResource = rr.create(testResource, "child1", new HashMap<String, Object>() {
+                    {
+                        put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
+                    }
+                });
+
+                Resource resourceChild = rr.create(testResource, "child1", new HashMap<String, Object>() {
+                    {
+                        put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
+                    }
+                });
                 mvm = resourceChild.adaptTo(ModifiableValueMap.class);
                 mvm.put("hello", "1");
 
                 // /apps/validation/1/resource/child1/grandChild1 will miss its mandatory "hello" property
-                Resource resourceGrandChild = rr.create(resourceChild, "grandChild1", new HashMap<String, Object>(){{
-                    put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
+                Resource resourceGrandChild = rr.create(resourceChild, "grandChild1", new HashMap<String, Object>() {
+                    {
+                        put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
+                    }
+                });
                 rr.commit();
-                
+
                 mvm = resourceGrandChild.adaptTo(ModifiableValueMap.class);
                 mvm.put("field1", "1");
                 rr.commit();
             }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
             ValidationResult vr = validationService.validate(testResource, vm);
             assertFalse(vr.isValid());
             assertThat(vr.getFailureMessages(), Matchers.hasKey("child1/grandChild1/hello"));
             assertThat(vr.getFailureMessages().keySet(), Matchers.hasSize(1));
         } finally {
-            if (rr != null) {
+            if (model1 != null) {
                 rr.delete(model1);
             }
             if (testResource != null) {
-                 rr.delete(testResource);
+                rr.delete(testResource);
             }
-            rr.commit();
-            rr.close();
         }
     }
-    
+
     @Test
     public void testResourceWithMissingOptionalChildProperty() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
-        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", RegexValidator.REGEX_PARAM + "=" + "\\d");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
+        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", RegexValidator.REGEX_PARAM
+                + "=" + "\\d");
         Resource model1 = null;
         Resource testResource = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
-                
-                createValidationModelChildResource(model1, "child1", null, true, new TestProperty("hello"));
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, property);
 
-                testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource", JcrConstants.NT_UNSTRUCTURED,
-                        JcrConstants.NT_UNSTRUCTURED, true);
-                ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
-                mvm.put("field1", "1");
-                
-                rr.create(testResource, "child2", new HashMap<String, Object>(){{
+            createValidationModelChildResource(model1, "child1", null, true, new TestProperty("hello"));
+
+            testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource",
+                    JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
+            ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
+            mvm.put("field1", "1");
+
+            rr.create(testResource, "child2", new HashMap<String, Object>() {
+                {
                     put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
-                rr.commit();
-            }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
+                }
+            });
+            rr.commit();
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
             ValidationResult vr = validationService.validate(testResource, vm);
             assertTrue(vr.isValid());
         } finally {
-            if (rr != null) {
+            if (model1 != null) {
                 rr.delete(model1);
             }
             if (testResource != null) {
-                 rr.delete(testResource);
+                rr.delete(testResource);
             }
-            rr.commit();
-            rr.close();
         }
     }
-    
+
     @Test
     public void testResourceWithNestedChildren() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
-        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", RegexValidator.REGEX_PARAM + "=" + "\\d");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
+        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", RegexValidator.REGEX_PARAM
+                + "=" + "\\d");
         Resource model1 = null;
         Resource testResource = null;
         try {
             if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
-                
-                Resource child = createValidationModelChildResource(model1, "child1", null, false, new TestProperty("hello"));
+                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                        "sling/validation/test", new String[] { "/apps/validation" }, property);
+
+                Resource child = createValidationModelChildResource(model1, "child1", null, false, new TestProperty(
+                        "hello"));
                 createValidationModelChildResource(child, "grandChild1", null, false, new TestProperty("hello"));
 
-                testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource", JcrConstants.NT_UNSTRUCTURED,
-                        JcrConstants.NT_UNSTRUCTURED, true);
-                Resource childResource = rr.create(testResource, "child1", new HashMap<String, Object>(){{
-                    put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
+                testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource",
+                        JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
+                Resource childResource = rr.create(testResource, "child1", new HashMap<String, Object>() {
+                    {
+                        put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
+                    }
+                });
 
                 ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
                 mvm.put("field1", "1");
 
-                Resource resourceChild = rr.create(testResource, "child1", new HashMap<String, Object>(){{
-                    put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
+                Resource resourceChild = rr.create(testResource, "child1", new HashMap<String, Object>() {
+                    {
+                        put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
+                    }
+                });
                 mvm = resourceChild.adaptTo(ModifiableValueMap.class);
                 mvm.put("hello", "test");
 
-                Resource resourceGrandChild = rr.create(resourceChild, "grandChild1", new HashMap<String, Object>(){{
-                    put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
+                Resource resourceGrandChild = rr.create(resourceChild, "grandChild1", new HashMap<String, Object>() {
+                    {
+                        put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
+                    }
+                });
                 mvm = resourceGrandChild.adaptTo(ModifiableValueMap.class);
                 mvm.put("hello", "test");
                 rr.commit();
             }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
             ValidationResult vr = validationService.validate(testResource, vm);
             assertTrue(vr.isValid());
         } finally {
-            if (rr != null) {
-                if (model1 != null) {
-                    rr.delete(model1);
-                }
-                if (testResource != null) {
-                    rr.delete(testResource);
-                }
-                rr.commit();
-                rr.close();
+            if (model1 != null) {
+                rr.delete(model1);
+            }
+            if (testResource != null) {
+                rr.delete(testResource);
             }
         }
     }
-    
+
     @Test
     public void testResourceWithNestedChildrenAndPatternMatching() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
-        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", RegexValidator.REGEX_PARAM + "=" + "\\d");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
+        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", RegexValidator.REGEX_PARAM
+                + "=" + "\\d");
         Resource model1 = null;
         Resource testResource = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
-                Resource child = createValidationModelChildResource(model1, "child1", "child.*", false, new TestProperty("hello"));
-                createValidationModelChildResource(child, "grandChild", "grandChild.*", false, new TestProperty("hello"));
-                rr.commit();
-                
-                testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource", JcrConstants.NT_UNSTRUCTURED,
-                        JcrConstants.NT_UNSTRUCTURED, true);
-                ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
-                mvm.put("field1", "1");
-                
-                Resource childResource = rr.create(testResource, "child1", new HashMap<String, Object>(){{
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, property);
+            Resource child = createValidationModelChildResource(model1, "child1", "child.*", false, new TestProperty(
+                    "hello"));
+            createValidationModelChildResource(child, "grandChild", "grandChild.*", false, new TestProperty("hello"));
+            rr.commit();
+
+            testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource",
+                    JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
+            ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
+            mvm.put("field1", "1");
+
+            Resource childResource = rr.create(testResource, "child1", new HashMap<String, Object>() {
+                {
                     put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
+                }
+            });
 
-                mvm = childResource.adaptTo(ModifiableValueMap.class);
-                mvm.put("hello", "test");
+            mvm = childResource.adaptTo(ModifiableValueMap.class);
+            mvm.put("hello", "test");
 
-                Resource resourceChild = rr.create(testResource, "child2", new HashMap<String, Object>(){{
+            Resource resourceChild = rr.create(testResource, "child2", new HashMap<String, Object>() {
+                {
                     put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
-                mvm = resourceChild.adaptTo(ModifiableValueMap.class);
-                mvm.put("hello2", "test");
+                }
+            });
+            mvm = resourceChild.adaptTo(ModifiableValueMap.class);
+            mvm.put("hello2", "test");
 
-                Resource resourceGrandChild = rr.create(resourceChild, "grandChild1", new HashMap<String, Object>(){{
+            Resource resourceGrandChild = rr.create(resourceChild, "grandChild1", new HashMap<String, Object>() {
+                {
                     put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                }});
-                mvm = resourceGrandChild.adaptTo(ModifiableValueMap.class);
-                mvm.put("hello", "test");
-                rr.commit();
-            }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
+                }
+            });
+            mvm = resourceGrandChild.adaptTo(ModifiableValueMap.class);
+            mvm.put("hello", "test");
+            rr.commit();
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
             ValidationResult vr = validationService.validate(testResource, vm);
             assertFalse(vr.isValid());
             // check for correct error message
@@ -636,143 +629,217 @@ public class ValidationServiceImplTest {
             expectedFailureMessages.put("child1/grandChild.*", Arrays.asList("Missing required child resource."));
             Assert.assertThat(vr.getFailureMessages().entrySet(), Matchers.equalTo(expectedFailureMessages.entrySet()));
         } finally {
-            if (rr != null) {
-                if (model1 != null) {
-                    rr.delete(model1);
-                }
-                if (testResource != null) {
-                    rr.delete(testResource);
-                }
-                rr.commit();
-                rr.close();
+            if (model1 != null) {
+                rr.delete(model1);
+            }
+            if (testResource != null) {
+                rr.delete(testResource);
             }
         }
     }
-    
+
     @Test
     public void testResourceWithPropertyPatternMatching() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
         final String TEST_REGEX = "^testvalue.*$";
-        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex="+TEST_REGEX);
+        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=" + TEST_REGEX);
         property.setNameRegex("property[1-4]");
-        
+
         TestProperty otherProperty = new TestProperty("field2");
-        otherProperty.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex="+TEST_REGEX);
+        otherProperty.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=" + TEST_REGEX);
         otherProperty.setNameRegex("otherproperty[1-4]");
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
         Resource model1 = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property, otherProperty);
-            }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
-            HashMap<String, Object> hashMap = new HashMap<String, Object>() {{
-                put("property1", "testvalue1"); 
-                put("property2", "test1value1"); // does not match validator pattern
-                put("property3", "testvalue1");
-                put("property4", "1testvalue1"); // does not match validator pattern
-                put("property5", "invalid");     // does not match property name pattern
-            }};
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, property, otherProperty);
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
+            HashMap<String, Object> hashMap = new HashMap<String, Object>() {
+                {
+                    put("property1", "testvalue1");
+                    put("property2", "test1value1"); // does not match validator pattern
+                    put("property3", "testvalue1");
+                    put("property4", "1testvalue1"); // does not match validator pattern
+                    put("property5", "invalid"); // does not match property name pattern
+                }
+            };
             ValueMap map = new ValueMapDecorator(hashMap);
             ValidationResult vr = validationService.validate(map, vm);
             assertFalse(vr.isValid());
             // check for correct error message
             Map<String, List<String>> expectedFailureMessages = new HashMap<String, List<String>>();
-            expectedFailureMessages.put("property2", Arrays.asList("Property does not match the pattern " + TEST_REGEX));
-            expectedFailureMessages.put("property4", Arrays.asList("Property does not match the pattern " + TEST_REGEX));
+            expectedFailureMessages
+                    .put("property2", Arrays.asList("Property does not match the pattern " + TEST_REGEX));
+            expectedFailureMessages
+                    .put("property4", Arrays.asList("Property does not match the pattern " + TEST_REGEX));
             expectedFailureMessages.put("otherproperty[1-4]", Arrays.asList("Missing required property."));
             Assert.assertThat(vr.getFailureMessages().entrySet(), Matchers.equalTo(expectedFailureMessages.entrySet()));
         } finally {
             if (model1 != null) {
                 rr.delete(model1);
             }
-            if (rr != null) {
-                rr.commit();
-                rr.close();
-            }
         }
     }
-    
+
     @Test
     public void testResourceWithMultivalueProperties() throws Exception {
-        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator", new RegexValidator());
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
 
         TestProperty property = new TestProperty("field1");
         final String TEST_REGEX = "^testvalue.*$";
-        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex="+TEST_REGEX);
-        ResourceResolver rr = rrf.getAdministrativeResourceResolver(null);
+        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator", "regex=" + TEST_REGEX);
         Resource model1 = null;
         try {
-            if (rr != null) {
-                model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", "sling/validation/test",
-                        new String[]{"/apps/validation"}, property);
-            }
-            ValidationModel vm = validationService.getValidationModel("sling/validation/test", "/apps/validation/1/resource");
-            HashMap<String, Object> hashMap = new HashMap<String, Object>() {{
-                put("field1", new String[] {"testvalue1", "test2value", "testvalue3"});
-            }};
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/apps/validation" }, property);
+            ValidationModel vm = validationService.getValidationModel("sling/validation/test",
+                    "/apps/validation/1/resource");
+            HashMap<String, Object> hashMap = new HashMap<String, Object>() {
+                {
+                    put("field1", new String[] { "testvalue1", "test2value", "testvalue3" });
+                }
+            };
             ValueMap map = new ValueMapDecorator(hashMap);
             ValidationResult vr = validationService.validate(map, vm);
             assertFalse(vr.isValid());
             // check for correct error message
             Map<String, List<String>> expectedFailureMessages = new HashMap<String, List<String>>();
-            expectedFailureMessages.put("field1[1]", Arrays.asList("Property does not match the pattern " + TEST_REGEX));
+            expectedFailureMessages
+                    .put("field1[1]", Arrays.asList("Property does not match the pattern " + TEST_REGEX));
             Assert.assertThat(vr.getFailureMessages().entrySet(), Matchers.equalTo(expectedFailureMessages.entrySet()));
         } finally {
             if (model1 != null) {
                 rr.delete(model1);
             }
-            if (rr != null) {
-                rr.commit();
-                rr.close();
+        }
+    }
+
+    @Test()
+    public void testValidateAllResourceTypesInResource() throws Exception {
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
+
+        TestProperty property = new TestProperty("field1");
+        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator");
+        Resource model1 = null;
+        Resource model2 = null;
+        Resource resource = null;
+        try {
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/content" }, property);
+            model2 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel2",
+                    "sling/validation/test2", new String[] { "/content" }, property);
+            resource = ResourceUtil.getOrCreateResource(rr, "/content/testpage", "sling/validation/test",
+                    JcrConstants.NT_UNSTRUCTURED, true);
+            ModifiableValueMap values = resource.adaptTo(ModifiableValueMap.class);
+            values.put("field2", "somvalue");
+            Resource grandChildResource = ResourceUtil.getOrCreateResource(rr, "/content/testpage/par/testpar",
+                    "sling/validation/test2", JcrConstants.NT_UNSTRUCTURED, true);
+            values = grandChildResource.adaptTo(ModifiableValueMap.class);
+            values.put("field2", "somvalue");
+            ValidationResult vr = validationService.validateAllResourceTypesInResource(resource, true,
+                    Collections.singleton(JcrConstants.NT_UNSTRUCTURED));
+
+            Map<String, List<String>> expectedFailureMessages = new HashMap<String, List<String>>();
+            expectedFailureMessages.put("field1", Arrays.asList("Missing required property."));
+            expectedFailureMessages.put("par/testpar/field1", Arrays.asList("Missing required property."));
+            Assert.assertThat(vr.getFailureMessages().entrySet(), Matchers.equalTo(expectedFailureMessages.entrySet()));
+        } finally {
+            if (resource != null) {
+                rr.delete(resource);
+            }
+            if (model1 != null) {
+                rr.delete(model1);
+            }
+            if (model2 != null) {
+                rr.delete(model2);
+            }
+        }
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testValidateAllResourceTypesInResourceWithMissingValidator() throws Exception {
+        validationService.validators.put("org.apache.sling.validation.impl.validators.RegexValidator",
+                new RegexValidator());
+
+        TestProperty property = new TestProperty("field1");
+        property.addValidator("org.apache.sling.validation.impl.validators.RegexValidator");
+        Resource model1 = null;
+        Resource resource = null;
+        try {
+            model1 = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1",
+                    "sling/validation/test", new String[] { "/content" }, property);
+            resource = ResourceUtil.getOrCreateResource(rr, "/content/testpage", "sling/validation/test",
+                    JcrConstants.NT_UNSTRUCTURED, true);
+            ModifiableValueMap values = resource.adaptTo(ModifiableValueMap.class);
+            values.put("field2", "somvalue");
+            Resource grandChildResource = ResourceUtil.getOrCreateResource(rr, "/content/testpage/par/testpar",
+                    "sling/validation/test2", JcrConstants.NT_UNSTRUCTURED, true);
+            values = grandChildResource.adaptTo(ModifiableValueMap.class);
+            values.put("field2", "somvalue");
+            ValidationResult vr = validationService.validateAllResourceTypesInResource(resource, true,
+                    Collections.singleton(JcrConstants.NT_UNSTRUCTURED));
+        } finally {
+            if (resource != null) {
+                rr.delete(resource);
+            }
+            if (model1 != null) {
+                rr.delete(model1);
             }
         }
     }
-    
+
     @Test
     public void testGetRelativeResourcePath() {
         // return relative paths unmodified
         Assert.assertThat(validationService.getRelativeResourceType("relative/path"), Matchers.equalTo("relative/path"));
-        Assert.assertThat(validationService.getRelativeResourceType("/apps/relative/path"), Matchers.equalTo("relative/path"));
-        Assert.assertThat(validationService.getRelativeResourceType("/libs/relative/path"), Matchers.equalTo("relative/path"));
+        Assert.assertThat(validationService.getRelativeResourceType("/apps/relative/path"),
+                Matchers.equalTo("relative/path"));
+        Assert.assertThat(validationService.getRelativeResourceType("/libs/relative/path"),
+                Matchers.equalTo("relative/path"));
     }
-    
-    @Test(expected=IllegalArgumentException.class)
+
+    @Test(expected = IllegalArgumentException.class)
     public void testGetRelativeResourcePathWithAbsolutePathOutsideOfTheSearchPaths() {
         // return relative paths unmodified
         validationService.getRelativeResourceType("/apps2/relative/path");
     }
 
-    private Resource createValidationModelResource(ResourceResolver rr, String root, String name, String validatedResourceType,
-                                               String[] applicableResourcePaths, TestProperty... properties) throws Exception {
+    private Resource createValidationModelResource(ResourceResolver rr, String root, String name,
+            String validatedResourceType, String[] applicableResourcePaths, TestProperty... properties)
+            throws Exception {
         Map<String, Object> modelProperties = new HashMap<String, Object>();
         modelProperties.put(Constants.VALIDATED_RESOURCE_TYPE, validatedResourceType);
         modelProperties.put(Constants.APPLICABLE_PATHS, applicableResourcePaths);
-        modelProperties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, Constants.VALIDATION_MODEL_RESOURCE_TYPE);
+        modelProperties
+                .put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, Constants.VALIDATION_MODEL_RESOURCE_TYPE);
         modelProperties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-        Resource model = ResourceUtil.getOrCreateResource(rr, root + "/" + name, modelProperties, JcrResourceConstants.NT_SLING_FOLDER, true);
+        Resource model = ResourceUtil.getOrCreateResource(rr, root + "/" + name, modelProperties,
+                JcrResourceConstants.NT_SLING_FOLDER, true);
         if (model != null) {
             createValidationModelProperties(model, properties);
         }
         return model;
     }
-    
-    private void createValidationModelProperties(Resource model, TestProperty... properties) throws PersistenceException {
+
+    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);
+        Resource propertiesResource = ResourceUtil.getOrCreateResource(rr,
+                model.getPath() + "/" + Constants.PROPERTIES, JcrConstants.NT_UNSTRUCTURED, null, true);
         if (propertiesResource != null) {
             for (TestProperty property : properties) {
                 Map<String, Object> modelPropertyJCRProperties = new HashMap<String, Object>();
                 modelPropertyJCRProperties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-                Resource propertyResource = ResourceUtil.getOrCreateResource(rr, propertiesResource.getPath() + "/" + property.name,
-                        modelPropertyJCRProperties, null, true);
+                Resource propertyResource = ResourceUtil.getOrCreateResource(rr, propertiesResource.getPath() + "/"
+                        + property.name, modelPropertyJCRProperties, null, true);
                 if (propertyResource != null) {
                     ModifiableValueMap values = propertyResource.adaptTo(ModifiableValueMap.class);
                     if (property.nameRegex != null) {
@@ -780,9 +847,8 @@ public class ValidationServiceImplTest {
                     }
                     values.put(Constants.PROPERTY_MULTIPLE, property.multiple);
                     values.put(Constants.OPTIONAL, property.optional);
-                    Resource validators = ResourceUtil.getOrCreateResource(rr,
-                            propertyResource.getPath() + "/" + Constants.VALIDATORS,
-                            JcrConstants.NT_UNSTRUCTURED, null, true);
+                    Resource validators = ResourceUtil.getOrCreateResource(rr, propertyResource.getPath() + "/"
+                            + Constants.VALIDATORS, JcrConstants.NT_UNSTRUCTURED, null, true);
                     if (validators != null) {
                         for (Map.Entry<String, String[]> v : property.validators.entrySet()) {
                             Map<String, Object> validatorProperties = new HashMap<String, Object>();
@@ -790,23 +856,28 @@ public class ValidationServiceImplTest {
                             if (v.getValue() != null) {
                                 validatorProperties.put(Constants.VALIDATOR_ARGUMENTS, v.getValue());
                             }
-                             ResourceUtil.getOrCreateResource(rr, validators.getPath() + "/" + v.getKey(), validatorProperties, null,
-                                    true);
+                            ResourceUtil.getOrCreateResource(rr, validators.getPath() + "/" + v.getKey(),
+                                    validatorProperties, null, true);
                         }
                     }
                 }
             }
         }
     }
-    
-    private Resource createValidationModelChildResource(Resource parentResource, String name, String nameRegex, boolean isOptional, TestProperty... properties) throws PersistenceException {
+
+    private Resource createValidationModelChildResource(Resource parentResource, String name, String nameRegex,
+            boolean isOptional, TestProperty... properties) throws PersistenceException {
         ResourceResolver rr = parentResource.getResourceResolver();
-        Resource modelChildren = rr.create(parentResource, Constants.CHILDREN, new HashMap<String, Object>(){{
-            put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-        }});
-        Resource child = rr.create(modelChildren, name, new HashMap<String, Object>(){{
-            put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
-        }});
+        Resource modelChildren = rr.create(parentResource, Constants.CHILDREN, new HashMap<String, Object>() {
+            {
+                put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
+            }
+        });
+        Resource child = rr.create(modelChildren, name, new HashMap<String, Object>() {
+            {
+                put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
+            }
+        });
         ModifiableValueMap mvm = child.adaptTo(ModifiableValueMap.class);
         if (nameRegex != null) {
             mvm.put(Constants.NAME_REGEX, nameRegex);
@@ -816,19 +887,6 @@ public class ValidationServiceImplTest {
         return child;
     }
 
-    private boolean arrayContainsString(String[] array, String string) {
-        boolean result = false;
-        if (array != null && string != null) {
-            for (String s : array) {
-                if (string.equals(s)) {
-                    result = true;
-                    break;
-                }
-            }
-        }
-        return result;
-    }
-
     private class TestProperty {
         public boolean optional;
         public boolean multiple;
@@ -843,12 +901,12 @@ public class ValidationServiceImplTest {
             this.optional = false;
             this.multiple = false;
         }
-        
+
         TestProperty setNameRegex(String nameRegex) {
             this.nameRegex = nameRegex;
             return this;
         }
-        
+
         TestProperty addValidator(String name, String... parameters) {
             validators.put(name, parameters);
             return this;

Modified: sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResource.java?rev=1656755&r1=1656754&r2=1656755&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResource.java (original)
+++ sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResource.java Tue Feb  3 15:13:12 2015
@@ -47,20 +47,13 @@ public class MockedResource extends Synt
     private Session session;
 
     public MockedResource(MockedResourceResolver resourceResolver, Node node) throws RepositoryException {
-        super(resourceResolver, node.getPath(), node.getProperty("./sling:resourceType").getString() != null ? node.getProperty("./" +
+        super(resourceResolver, node.getPath(), node.hasProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY) && node.getProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY).getString() != null ? node.getProperty(
                 JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY).getString() : node.getProperty(JcrConstants.JCR_PRIMARYTYPE).getString
                 ());
         mockedResourceResolver = resourceResolver;
 
     }
 
-    public MockedResource(MockedResourceResolver resourceResolver, String path,
-                          String resourceType) {
-        super(resourceResolver, path, resourceType);
-        mockedResourceResolver = resourceResolver;
-        resourceResolver.register(this);
-    }
-
     private Session getSession() {
         synchronized (this) {
             if (session == null) {
@@ -74,23 +67,6 @@ public class MockedResource extends Synt
         }
     }
 
-    @Override
-    protected void finalize() throws Throwable {
-        close();
-        super.finalize();
-    }
-
-    public void close() {
-        synchronized (this) {
-            if (session != null) {
-                if (session.isLive()) {
-                    session.logout();
-                }
-                session = null;
-            }
-        }
-    }
-
     @SuppressWarnings("unchecked")
     @Override
     public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {

Modified: sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java?rev=1656755&r1=1656754&r2=1656755&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java (original)
+++ sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java Tue Feb  3 15:13:12 2015
@@ -18,14 +18,11 @@
  */
 package org.apache.sling.validation.impl.setup;
 
-import org.apache.sling.api.resource.LoginException;
-import org.apache.sling.api.resource.PersistenceException;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.commons.testing.jcr.RepositoryProvider;
-import org.apache.sling.commons.testing.jcr.RepositoryUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
@@ -34,12 +31,15 @@ import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.servlet.http.HttpServletRequest;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.commons.testing.jcr.RepositoryProvider;
+import org.apache.sling.commons.testing.jcr.RepositoryUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class MockedResourceResolver implements ResourceResolver {
 
@@ -49,7 +49,6 @@ public class MockedResourceResolver impl
     private static final String[] SEARCH_PATHS = new String[] {"/apps/", "/libs/"};
 
     public final RepositoryProvider repoProvider;
-    private List<MockedResource> resources = new LinkedList<MockedResource>();
 
     private Session session;
 
@@ -112,13 +111,13 @@ public class MockedResourceResolver impl
         Session session;
         try {
             session = createSession();
-            session.getNode(path);
+            Node node = session.getNode(path);
+            return new MockedResource(this, node);
         } catch (PathNotFoundException e) {
             return null;
         } catch (RepositoryException e) {
             throw new RuntimeException("RepositoryException: " + e, e);
         }
-        return new MockedResource(this, path, "nt:unstructured");
     }
 
     public Resource getResource(Resource base, String path) {
@@ -147,7 +146,7 @@ public class MockedResourceResolver impl
                     Node next = nodes.nextNode();
                     try {
                         return new MockedResource(MockedResourceResolver.this,
-                                next.getPath(), "nt:unstructured");
+                                next);
                     } catch (RepositoryException e) {
                         throw new RuntimeException("RepositoryException: " + e,
                                 e);
@@ -197,11 +196,6 @@ public class MockedResourceResolver impl
     }
 
     public void close() {
-        Iterator<MockedResource> it = resources.iterator();
-        while (it.hasNext()) {
-            MockedResource r = it.next();
-            r.close();
-        }
         if (session != null) {
             if (session.isLive()) {
                 session.logout();
@@ -210,10 +204,6 @@ public class MockedResourceResolver impl
         }
     }
 
-    public void register(MockedResource mockedResource) {
-        resources.add(mockedResource);
-    }
-
     public String getUserID() {
         throw new UnsupportedOperationException("Not implemented");
     }
@@ -227,9 +217,6 @@ public class MockedResourceResolver impl
     }
 
     public void delete(Resource resource) throws PersistenceException {
-        if (resources.contains(resource)) {
-            resources.remove(resource);
-        }
         Node node = resource.adaptTo(Node.class);
         try {
             node.remove();
@@ -289,22 +276,22 @@ public class MockedResourceResolver impl
     }
 
     public String getParentResourceType(Resource resource) {
-        // TODO Auto-generated method stub
-        return null;
+        throw new UnsupportedOperationException("Not implemented");
     }
 
     public String getParentResourceType(String resourceType) {
-        // TODO Auto-generated method stub
-        return null;
+        throw new UnsupportedOperationException("Not implemented");
     }
 
     public boolean isResourceType(Resource resource, String resourceType) {
-        // TODO Auto-generated method stub
-        return false;
+        throw new UnsupportedOperationException("Not implemented");
     }
 
     public void refresh() {
-        // TODO Auto-generated method stub
-
+        try {
+            this.session.refresh(true);
+        } catch (RepositoryException e) {
+            LOG.warn("Could not refresh seesion", e);
+        }
     }
 }