Class ResourceMatchers


  • public final class ResourceMatchers
    extends Object
    A collection of Matchers that work on the Resource API level
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> containsChildren​(String... children)
      Matches resources which have exactly the children with the names given in children.
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> containsChildrenInAnyOrder​(String... children)
      Matches resources which have exactly the children with the names given in children.
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> hasChildren​(String... children)
      Matches resources which have amongst their children the specified children.
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> name​(String name)
      Matches only if the resource has the given name
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> nameAndProps​(String name, Object... properties)
      Matches resources which has the given name and at least the specified properties defined with matching values
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> nameAndProps​(String name, Map<String,​Object> properties)
      Matches resources which has the given name and at least the specified properties defined with matching values
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> path​(String path)
      Matches only if the resource has the given path
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> props​(Object... properties)
      Matches resources which has at least the specified properties defined with matching values
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> props​(Map<String,​Object> properties)
      Matches resources which has at least the specified properties defined with matching values
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> resourceType​(String resourceType)
      Matches resources with a resource type set to the specified resourceType (exactly).
      static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> resourceTypeOrDerived​(String resourceType)
      Matches resources with a resource type set to the specified resourceType or one of its sub types.
    • Method Detail

      • hasChildren

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> hasChildren​(String... children)
        Matches resources which have amongst their children the specified children. Child resources not contained in the specified children are not validated. The order of the children does not matter.
         assertThat(resource, hasChildren('child1', 'child2'));
         
        Parameters:
        children - the expected children, not null or empty
        Returns:
        a matcher instance
      • containsChildren

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> containsChildren​(String... children)
        Matches resources which have exactly the children with the names given in children. The order is validated as well.
         assertThat(resource, containsChildren('child1', 'child2'));
         
        Parameters:
        children - the expected children, not null or empty
        Returns:
        a matcher instance
      • containsChildrenInAnyOrder

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> containsChildrenInAnyOrder​(String... children)
        Matches resources which have exactly the children with the names given in children. The order is not validated.
         assertThat(resource, containsChildrenInAnyOrder('child1', 'child2'));
         
        Parameters:
        children - the expected children, not null or empty
        Returns:
        a matcher instance
      • path

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> path​(String path)
        Matches only if the resource has the given path
         assertThat(resource, hasName('/a/b/c'));
         
        Parameters:
        path - the resources path, not null or empty
        Returns:
        a matcher instance
      • name

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> name​(String name)
        Matches only if the resource has the given name
         assertThat(resource, resourceWithName('resource1'));
         
        Parameters:
        name - the resources name, not null or empty
        Returns:
        a matcher instance
      • resourceType

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> resourceType​(String resourceType)
        Matches resources with a resource type set to the specified resourceType (exactly). In order to check for resource types allowing more specific ones use resourceTypeOrDerived(String).
         assertThat(resource, resourceType('my/app'));
         
        Parameters:
        resourceType - the resource type to match
        Returns:
        a matcher instance
      • resourceTypeOrDerived

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> resourceTypeOrDerived​(String resourceType)
        Matches resources with a resource type set to the specified resourceType or one of its sub types. In order to check for exact resource types only use resourceType(String).
         assertThat(resource, resourceTypeOrDerived('my/app'));
         
        Parameters:
        resourceType - the resource type to match
        Returns:
        a matcher instance
        Since:
        1.1.0
        See Also:
        Resource.isResourceType(String), resourceType(String)
      • props

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> props​(Map<String,​Object> properties)
        Matches resources which has at least the specified properties defined with matching values

        Values not declared in the the properties parameter are not validated.

         Map<String, Object> expectedProperties = new HashMap<>();
         expectedProperties.put("jcr:title", "Node title");
         expectedProperties.put("jcr:text",  "Some long text");
        
         assertThat(resource, resourceWithProps(expectedProperties));
         
        Parameters:
        properties - the properties to match
        Returns:
        a matcher instance
      • props

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> props​(Object... properties)
        Matches resources which has at least the specified properties defined with matching values

        Values not declared in the the properties parameter are not validated.

         Map<String, Object> expectedProperties = new HashMap<>();
         expectedProperties.put("jcr:title", "Node title");
         expectedProperties.put("jcr:text",  "Some long text");
        
         assertThat(resource, resourceWithProps(expectedProperties));
         
        Parameters:
        properties - the properties to match
        Returns:
        a matcher instance
      • nameAndProps

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> nameAndProps​(String name,
                                                                                                Map<String,​Object> properties)
        Matches resources which has the given name and at least the specified properties defined with matching values

        Values not declared in the the properties parameter are not validated.

         Map<String, Object> expectedProperties = new HashMap<>();
         expectedProperties.put("jcr:title", "Node title");
         expectedProperties.put("jcr:text",  "Some long text");
        
         assertThat(resource, resourceWithProps(expectedProperties));
         
        Parameters:
        name - the expected name of the resource
        properties - the properties to match
        Returns:
        a matcher instance
      • nameAndProps

        public static org.hamcrest.Matcher<org.apache.sling.api.resource.Resource> nameAndProps​(String name,
                                                                                                Object... properties)
        Matches resources which has the given name and at least the specified properties defined with matching values

        Values not declared in the the properties parameter are not validated.

         Map<String, Object> expectedProperties = new HashMap<>();
         expectedProperties.put("jcr:title", "Node title");
         expectedProperties.put("jcr:text",  "Some long text");
        
         assertThat(resource, resourceWithProps(expectedProperties));
         
        Parameters:
        name - the expected name of the resource
        properties - the properties to match
        Returns:
        a matcher instance