Author: sseifert
Date: Sat Oct  8 07:45:40 2016
New Revision: 1763859

URL: http://svn.apache.org/viewvc?rev=1763859&view=rev
Log:
SLING-6116 Hamcrest: Add matchers for resource collections and iterators

Added:
    
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java
   (with props)
    
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java
   (with props)
    
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java
   (with props)
    
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java
   (with props)
    
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java
   (with props)
    
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java
   (with props)
Modified:
    sling/trunk/testing/hamcrest/pom.xml

Modified: sling/trunk/testing/hamcrest/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/pom.xml?rev=1763859&r1=1763858&r2=1763859&view=diff
==============================================================================
--- sling/trunk/testing/hamcrest/pom.xml (original)
+++ sling/trunk/testing/hamcrest/pom.xml Sat Oct  8 07:45:40 2016
@@ -40,6 +40,7 @@
     </scm>
     
     <dependencies>
+
         <dependency>
             <groupId>org.hamcrest</groupId>
             <artifactId>hamcrest-library</artifactId>
@@ -62,13 +63,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.testing.sling-mock</artifactId>
-            <version>1.6.2</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            
<artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
-            <version>1.1.12</version>
+            <version>1.8.0</version>
             <scope>test</scope>
         </dependency>
         <dependency>

Added: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java?rev=1763859&view=auto
==============================================================================
--- 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java
 (added)
+++ 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java
 Sat Oct  8 07:45:40 2016
@@ -0,0 +1,44 @@
+/*
+ * 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.Collection;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.hamcrest.matchers.ResourceCollectionMatcher;
+import org.hamcrest.Matcher;
+
+/**
+ * A collection of <tt>Matcher</tt>s for resource collections.
+ */
+public final class ResourceCollectionMatchers {
+    
+    private ResourceCollectionMatchers() {
+        // static methods only
+    }
+
+    /**
+     * Asserts that the given resource collection has resources with exactly 
the given paths in the given order.
+     * @param paths the expected resource paths
+     * @return a matcher instance
+     */
+    public static Matcher<Collection<Resource>> paths(String... paths) {
+        return new ResourceCollectionMatcher(Arrays.asList(paths));
+    }
+    
+}

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Oct  8 07:45:40 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java?rev=1763859&view=auto
==============================================================================
--- 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java
 (added)
+++ 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java
 Sat Oct  8 07:45:40 2016
@@ -0,0 +1,44 @@
+/*
+ * 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.Iterator;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.hamcrest.matchers.ResourceIteratorMatcher;
+import org.hamcrest.Matcher;
+
+/**
+ * A collection of <tt>Matcher</tt>s for resource iterators.
+ */
+public final class ResourceIteratorMatchers {
+    
+    private ResourceIteratorMatchers() {
+        // static methods only
+    }
+
+    /**
+     * Asserts that the given resource collection has resources with exactly 
the given paths in the given order.
+     * @param paths the expected resource paths
+     * @return a matcher instance
+     */
+    public static Matcher<Iterator<Resource>> paths(String... paths) {
+        return new ResourceIteratorMatcher(Arrays.asList(paths));
+    }
+    
+}

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Oct  8 07:45:40 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java?rev=1763859&view=auto
==============================================================================
--- 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java
 (added)
+++ 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java
 Sat Oct  8 07:45:40 2016
@@ -0,0 +1,64 @@
+/*
+ * 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.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * Ensures a collection of resources has exactly the given list of paths in 
the given order.
+ */
+public class ResourceCollectionMatcher extends 
TypeSafeMatcher<Collection<Resource>> {
+
+    // this should be "Iterable<? extends Resource>" instead of "?" but cannot 
until https://github.com/hamcrest/JavaHamcrest/issues/107 is solved
+    private final Matcher<?> iterarableMatcher;
+
+    public ResourceCollectionMatcher(List<String> paths) {
+        if ( paths == null || paths.isEmpty() ) {
+            throw new IllegalArgumentException("names is null or empty");
+        }
+
+        List<Matcher<? super Resource>> resourceMatchers = new 
ArrayList<Matcher<? super Resource>>();
+        for (String path : paths) {
+            resourceMatchers.add(new ResourcePathMatcher(path));
+        }
+
+        this.iterarableMatcher = 
org.hamcrest.collection.IsIterableContainingInOrder.contains(resourceMatchers);
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        iterarableMatcher.describeTo(description);
+    }
+
+    @Override
+    protected boolean matchesSafely(Collection<Resource> items) {
+        return iterarableMatcher.matches(items);
+    }
+
+    @Override
+    protected void describeMismatchSafely(Collection<Resource> items, 
Description mismatchDescription) {
+        iterarableMatcher.describeMismatch(items, mismatchDescription);
+    }
+
+}

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Oct  8 07:45:40 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java?rev=1763859&view=auto
==============================================================================
--- 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java
 (added)
+++ 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java
 Sat Oct  8 07:45:40 2016
@@ -0,0 +1,72 @@
+/*
+ * 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.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * Ensures an iterator of resources has exactly the given list of paths in the 
given order.
+ */
+public class ResourceIteratorMatcher extends 
TypeSafeMatcher<Iterator<Resource>> {
+
+    // this should be "Iterable<? extends Resource>" instead of "?" but cannot 
until https://github.com/hamcrest/JavaHamcrest/issues/107 is solved
+    private final Matcher<?> iterarableMatcher;
+
+    public ResourceIteratorMatcher(List<String> paths) {
+        if ( paths == null || paths.isEmpty() ) {
+            throw new IllegalArgumentException("names is null or empty");
+        }
+
+        List<Matcher<? super Resource>> resourceMatchers = new 
ArrayList<Matcher<? super Resource>>();
+        for (String path : paths) {
+            resourceMatchers.add(new ResourcePathMatcher(path));
+        }
+
+        this.iterarableMatcher = 
org.hamcrest.collection.IsIterableContainingInOrder.contains(resourceMatchers);
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        iterarableMatcher.describeTo(description);
+    }
+
+    @Override
+    protected boolean matchesSafely(Iterator<Resource> items) {
+        return iterarableMatcher.matches(toList(items));
+    }
+
+    @Override
+    protected void describeMismatchSafely(Iterator<Resource> items, 
Description mismatchDescription) {
+        iterarableMatcher.describeMismatch(toList(items), mismatchDescription);
+    }
+    
+    private List<Resource> toList(Iterator<Resource> items) {
+        List<Resource> list = new ArrayList<Resource>();
+        while (items.hasNext()) {
+            list.add(items.next());
+        }
+        return list;
+    }
+
+}

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Oct  8 07:45:40 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java?rev=1763859&view=auto
==============================================================================
--- 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java
 (added)
+++ 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java
 Sat Oct  8 07:45:40 2016
@@ -0,0 +1,78 @@
+/*
+ * 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 static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+public class ResourceCollectionMatchersTest {
+
+    @Rule
+    public final SlingContext context = new SlingContext();
+    
+    private List<Resource> list;
+    
+    @Before
+    public void setUp() {
+        list = ImmutableList.of(
+                context.create().resource("/content/1"),
+                context.create().resource("/content/2"),
+                context.create().resource("/content/3")
+        );
+    }
+    
+    @Test
+    public void testMatch() {
+        assertThat(list, ResourceCollectionMatchers.paths(
+                "/content/1",
+                "/content/2",
+                "/content/3"
+        ));
+    }
+
+    @Test
+    public void testMisMatch() {
+        assertThat(list, not(ResourceCollectionMatchers.paths(
+                "/content/1",
+                "/content/2",
+                "/content/3",
+                "/content/4"
+        )));
+        assertThat(list, not(ResourceCollectionMatchers.paths(
+                "/content/1",
+                "/content/2"
+        )));
+        assertThat(list, not(ResourceCollectionMatchers.paths(
+                "/content/1",
+                "/content/3",
+                "/content/2"
+        )));
+    }
+
+}

Propchange: 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Oct  8 07:45:40 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java?rev=1763859&view=auto
==============================================================================
--- 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java
 (added)
+++ 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java
 Sat Oct  8 07:45:40 2016
@@ -0,0 +1,78 @@
+/*
+ * 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 static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+public class ResourceIteratorMatchersTest {
+
+    @Rule
+    public final SlingContext context = new SlingContext();
+    
+    private List<Resource> list;
+    
+    @Before
+    public void setUp() {
+        list = ImmutableList.of(
+                context.create().resource("/content/1"),
+                context.create().resource("/content/2"),
+                context.create().resource("/content/3")
+        );
+    }
+    
+    @Test
+    public void testMatch() {
+        assertThat(list.iterator(), ResourceIteratorMatchers.paths(
+                "/content/1",
+                "/content/2",
+                "/content/3"
+        ));
+    }
+
+    @Test
+    public void testMisMatch() {
+        assertThat(list.iterator(), not(ResourceIteratorMatchers.paths(
+                "/content/1",
+                "/content/2",
+                "/content/3",
+                "/content/4"
+        )));
+        assertThat(list.iterator(), not(ResourceIteratorMatchers.paths(
+                "/content/1",
+                "/content/2"
+        )));
+        assertThat(list.iterator(), not(ResourceIteratorMatchers.paths(
+                "/content/1",
+                "/content/3",
+                "/content/2"
+        )));
+    }
+
+}

Propchange: 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Oct  8 07:45:40 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to