You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2015/09/25 11:32:45 UTC

svn commit: r1705261 - in /sling/trunk/testing/hamcrest: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/sling/ src/main/java/org/apache/sling/hamcrest/ src/main/java/org/apache/sling/hamcrest/matc...

Author: rombert
Date: Fri Sep 25 09:32:45 2015
New Revision: 1705261

URL: http://svn.apache.org/viewvc?rev=1705261&view=rev
Log:
SLING-5063 - Create a set of Hamcrest matchers for JUnit tests

Initial version

Added:
    sling/trunk/testing/hamcrest/
    sling/trunk/testing/hamcrest/pom.xml
    sling/trunk/testing/hamcrest/src/
    sling/trunk/testing/hamcrest/src/main/
    sling/trunk/testing/hamcrest/src/main/java/
    sling/trunk/testing/hamcrest/src/main/java/org/
    sling/trunk/testing/hamcrest/src/main/java/org/apache/
    sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/
    sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/
    sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
    sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/
    sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceChildrenMatcher.java
    sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceMatcher.java

Added: sling/trunk/testing/hamcrest/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/pom.xml?rev=1705261&view=auto
==============================================================================
--- sling/trunk/testing/hamcrest/pom.xml (added)
+++ sling/trunk/testing/hamcrest/pom.xml Fri Sep 25 09:32:45 2015
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>24</version>
+        <relativePath />
+    </parent>
+
+    <artifactId>org.apache.sling.testing.hamcrest</artifactId>
+    <version>0.9.0-SNAPSHOT</version>
+
+    <name>Apache Sling Testing Hamcrest</name>
+    <description>Hamcrest matchers tailored for Apache Sling</description>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/testing/hamcrest</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/hamcrest</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest</url>
+    </scm>
+    
+    <dependencies>
+        <dependency>
+            <groupId>org.hamcrest</groupId>
+            <artifactId>hamcrest-core</artifactId>
+            <version>1.3</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.4.0</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>

Added: sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java?rev=1705261&view=auto
==============================================================================
--- sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java (added)
+++ sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java Fri Sep 25 09:32:45 2015
@@ -0,0 +1,50 @@
+/*
+ * 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.hamcrest;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.hamcrest.matchers.ResourceChildrenMatcher;
+import org.apache.sling.hamcrest.matchers.ResourceMatcher;
+import org.hamcrest.Matcher;
+
+/**
+ * A collection of <tt>Matcher</tt>s that work on the Resource API level
+ *
+ */
+public final class ResourceMatchers {
+    
+    public static Matcher<Resource> hasChildren(String... children) {
+        return new ResourceChildrenMatcher(Arrays.asList(children));
+    }
+    
+    public static Matcher<Resource> resourceOfType(String resourceType) {
+        return new ResourceMatcher(Collections.<String, Object> singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, resourceType));
+    }
+
+    public static Matcher<Resource> resourceWithProps(Map<String, Object> properties) {
+        return new ResourceMatcher(properties);
+    }
+    
+    private ResourceMatchers() {
+        // prevent instantiation
+    }
+}

Added: sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceChildrenMatcher.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceChildrenMatcher.java?rev=1705261&view=auto
==============================================================================
--- sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceChildrenMatcher.java (added)
+++ sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceChildrenMatcher.java Fri Sep 25 09:32:45 2015
@@ -0,0 +1,52 @@
+/*
+ * 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.hamcrest.matchers;
+
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+
+public class ResourceChildrenMatcher extends TypeSafeMatcher<Resource> {
+    
+    private final List<String> childNames;
+    
+    public ResourceChildrenMatcher(List<String> childNames) {
+        if ( childNames == null || childNames.isEmpty() ) {
+            throw new IllegalArgumentException("childNames is null or empty");
+        }
+        
+        this.childNames = childNames;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText("Resource with children ").appendValueList("[", ",", "]", childNames);
+    }
+
+    @Override
+    protected boolean matchesSafely(Resource item) {
+        for ( String childName : childNames ) {
+            if ( item.getChild(childName) == null ) {
+                return false;
+            }
+        }
+        return true;
+    }
+    
+}
\ No newline at end of file

Added: sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceMatcher.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceMatcher.java?rev=1705261&view=auto
==============================================================================
--- sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceMatcher.java (added)
+++ sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceMatcher.java Fri Sep 25 09:32:45 2015
@@ -0,0 +1,57 @@
+/*
+ * 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.hamcrest.matchers;
+
+import java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+
+public class ResourceMatcher extends TypeSafeMatcher<Resource> {
+
+    private final Map<String, Object> properties;
+
+    public ResourceMatcher(Map<String, Object> properties) {
+
+        if (properties == null || properties.isEmpty()) {
+            throw new IllegalArgumentException("properties is null or empty");
+        }
+
+        this.properties = properties;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText("Resource with properties ").appendValueList("[", ",", "]", properties.entrySet());
+    }
+
+    @Override
+    protected boolean matchesSafely(Resource item) {
+
+        for (Map.Entry<String, Object> prop : properties.entrySet()) {
+            Object value = item.adaptTo(ValueMap.class).get(prop.getKey());
+            if ( value == null || !value.equals(prop.getValue())) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+}
\ No newline at end of file