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 2017/10/18 23:28:30 UTC
[sling-org-apache-sling-testing-hamcrest] 09/21: SLING-6108
hamcrest: Match resources by path
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 25e42386451ae2a739eeb66bdbda63f908a7521e
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Oct 6 15:30:08 2016 +0000
SLING-6108 hamcrest: Match resources by path
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1763611 13f79535-47bb-0310-9956-ffa450edef68
---
.../apache/sling/hamcrest/ResourceMatchers.java | 19 ++++++++-
.../hamcrest/matchers/ResourcePathMatcher.java | 49 ++++++++++++++++++++++
.../hamcrest/ResourceMatchersTest.java | 13 +++++-
3 files changed, 78 insertions(+), 3 deletions(-)
diff --git a/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java b/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
index 353fbed..d993fa3 100644
--- a/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
+++ b/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
@@ -24,6 +24,7 @@ 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.ResourceNameMatcher;
+import org.apache.sling.hamcrest.matchers.ResourcePathMatcher;
import org.apache.sling.hamcrest.matchers.ResourcePropertiesMatcher;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
@@ -69,7 +70,7 @@ public final class ResourceMatchers {
* Matches resources which have exactly the children with the names given in <tt>children</tt>. The order is not validated.
*
* <pre>
- * assertThat(resource, containsChildren('child1', 'child2'));
+ * assertThat(resource, containsChildrenInAnyOrder('child1', 'child2'));
* </pre>
*
* @param children the expected children, not <code>null</code> or empty
@@ -80,10 +81,24 @@ public final class ResourceMatchers {
}
/**
+ * Matches only if the resource has the given path
+ *
+ * <pre>
+ * assertThat(resource, hasName('/a/b/c'));
+ * </pre>
+ *
+ * @param path the resources path, not <code>null</code> or empty
+ * @return a matcher instance
+ */
+ public static Matcher<Resource> resourceWithPath(String path) {
+ return new ResourcePathMatcher(path);
+ }
+
+ /**
* Matches only if the resource has the given name
*
* <pre>
- * assertThat(resource, hasName('resource1'));
+ * assertThat(resource, resourceWithName('resource1'));
* </pre>
*
* @param name the resources name, not <code>null</code> or empty
diff --git a/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePathMatcher.java b/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePathMatcher.java
new file mode 100644
index 0000000..f76cb70
--- /dev/null
+++ b/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePathMatcher.java
@@ -0,0 +1,49 @@
+/*
+ * 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 org.apache.sling.api.resource.Resource;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * Matcher which matches whenever the path of the given resource is equal to the path given in the constructor.
+ */
+public class ResourcePathMatcher extends TypeSafeMatcher<Resource> {
+
+ private final String path;
+
+ public ResourcePathMatcher(String path) {
+ this.path = path;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("Resource with path ").appendValue(path);
+ }
+
+ @Override
+ protected boolean matchesSafely(Resource resource) {
+ return path.equals(resource.getPath());
+ }
+
+ @Override
+ protected void describeMismatchSafely(Resource resource, Description mismatchDescription) {
+ mismatchDescription.appendText("was Resource with path ").appendValue(resource.getPath()).appendText(" (resource: ").appendValue(resource).appendText(")");
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/hamcrest/ResourceMatchersTest.java b/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
similarity index 92%
rename from src/test/java/org/apache/sling/testing/hamcrest/ResourceMatchersTest.java
rename to src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
index 5ba2bd5..0d0dcec 100644
--- a/src/test/java/org/apache/sling/testing/hamcrest/ResourceMatchersTest.java
+++ b/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.sling.testing.hamcrest;
+package org.apache.sling.hamcrest;
import java.util.HashMap;
import java.util.Map;
@@ -47,6 +47,16 @@ public class ResourceMatchersTest {
}
@Test
+ public void testResourceWithPath() throws PersistenceException {
+ MockHelper.create(context.resourceResolver())
+ .resource("/resource").commit();
+
+ Resource resource = context.resourceResolver().getResource("/resource");
+ Assert.assertThat(resource, ResourceMatchers.resourceWithPath("/resource"));
+ Assert.assertThat(resource, Matchers.not(ResourceMatchers.resourceWithPath("some/other/name")));
+ }
+
+ @Test
public void testResourceWithName() throws PersistenceException {
MockHelper.create(context.resourceResolver())
.resource("/resource").commit();
@@ -130,4 +140,5 @@ public class ResourceMatchersTest {
Assert.assertThat(resource, Matchers.not(ResourceMatchers.containsChildren("child2", "child1")));
Assert.assertThat(resource, Matchers.not(ResourceMatchers.containsChildren("child1", "child2", "child3")));
}
+
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.