This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-hamcrest.git
commit 806d5680fdc1555b7a19a0651f68c9c679bbb5c3 Author: Stefan Seifert <[email protected]> AuthorDate: Sat Oct 8 07:45:40 2016 +0000 SLING-6116 Hamcrest: Add matchers for resource collections and iterators git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1763859 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 9 +-- .../sling/hamcrest/ResourceCollectionMatchers.java | 44 ++++++++++++ .../sling/hamcrest/ResourceIteratorMatchers.java | 44 ++++++++++++ .../matchers/ResourceCollectionMatcher.java | 64 ++++++++++++++++++ .../hamcrest/matchers/ResourceIteratorMatcher.java | 72 ++++++++++++++++++++ .../hamcrest/ResourceCollectionMatchersTest.java | 78 ++++++++++++++++++++++ .../hamcrest/ResourceIteratorMatchersTest.java | 78 ++++++++++++++++++++++ 7 files changed, 382 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 7366abc..3943567 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java b/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java new file mode 100644 index 0000000..ea67b37 --- /dev/null +++ b/src/main/java/org/apache/sling/hamcrest/ResourceCollectionMatchers.java @@ -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)); + } + +} diff --git a/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java b/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java new file mode 100644 index 0000000..fb633c8 --- /dev/null +++ b/src/main/java/org/apache/sling/hamcrest/ResourceIteratorMatchers.java @@ -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)); + } + +} diff --git a/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java new file mode 100644 index 0000000..1c3a312 --- /dev/null +++ b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceCollectionMatcher.java @@ -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); + } + +} diff --git a/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java new file mode 100644 index 0000000..37b37fd --- /dev/null +++ b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceIteratorMatcher.java @@ -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; + } + +} diff --git a/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java b/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java new file mode 100644 index 0000000..eb74766 --- /dev/null +++ b/src/test/java/org/apache/sling/hamcrest/ResourceCollectionMatchersTest.java @@ -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" + ))); + } + +} diff --git a/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java b/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java new file mode 100644 index 0000000..bb3bb78 --- /dev/null +++ b/src/test/java/org/apache/sling/hamcrest/ResourceIteratorMatchersTest.java @@ -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" + ))); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
