This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-hamcrest.git
The following commit(s) were added to refs/heads/master by this push:
new 707201b SLING-12831 fix resource type matching for special resources
707201b is described below
commit 707201b6049cb2a7a3edaa403d15e6672f3bcf05
Author: Konrad Windszus <[email protected]>
AuthorDate: Tue Jun 17 13:58:20 2025 +0200
SLING-12831 fix resource type matching for special resources
Not all resources expose their resource type via properties yet
(SLING-12781). Therefore use dedicated methods instead of checking
properties.
---
pom.xml | 2 +-
.../apache/sling/hamcrest/ResourceMatchers.java | 27 +++++++++--
.../matchers/ResourcePropertiesMatcher.java | 8 +---
.../hamcrest/matchers/ResourceTypeMatcher.java | 55 ++++++++++++++++++++++
.../sling/hamcrest/ResourceMatchersTest.java | 28 ++++++++++-
5 files changed, 107 insertions(+), 13 deletions(-)
diff --git a/pom.xml b/pom.xml
index 48fbe5d..496c417 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
</parent>
<artifactId>org.apache.sling.testing.hamcrest</artifactId>
- <version>1.0.3-SNAPSHOT</version>
+ <version>1.1.0-SNAPSHOT</version>
<name>Apache Sling Testing Hamcrest</name>
<description>Hamcrest matchers tailored for Apache Sling</description>
diff --git a/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
b/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
index b35a40d..e8055c6 100644
--- a/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
+++ b/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
@@ -17,15 +17,14 @@
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.ResourceNameMatcher;
import org.apache.sling.hamcrest.matchers.ResourcePathMatcher;
import org.apache.sling.hamcrest.matchers.ResourcePropertiesMatcher;
+import org.apache.sling.hamcrest.matchers.ResourceTypeMatcher;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
@@ -109,16 +108,34 @@ public final class ResourceMatchers {
}
/**
- * Matches resources with a resource type set to the specified {@code
resourceType}
+ * Matches resources with a resource type set to the specified {@code
resourceType} (exactly).
+ * In order to check for resource types allowing more specific ones use
{@link #resourceTypeOrDerived(String)}.
*
* <pre>
- * assertThat(resource, resourceOfType('my/app'));
+ * assertThat(resource, resourceType('my/app'));
* </pre>
* @param resourceType the resource type to match
* @return a matcher instance
*/
public static Matcher<Resource> resourceType(String resourceType) {
- return new ResourcePropertiesMatcher(Collections.<String, Object>
singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, resourceType));
+ return new ResourceTypeMatcher(resourceType, false);
+ }
+
+ /**
+ * Matches resources with a resource type set to the specified {@code
resourceType} or one of its sub types.
+ * In order to check for exact resource types only use {@link
#resourceType(String)}.
+ *
+ * <pre>
+ * assertThat(resource, resourceTypeOrDerived('my/app'));
+ * </pre>
+ * @param resourceType the resource type to match
+ * @return a matcher instance
+ * @since 1.1.0
+ * @see Resource#isResourceType(String)
+ * @see #resourceType(String)
+ */
+ public static Matcher<Resource> resourceTypeOrDerived(String resourceType)
{
+ return new ResourceTypeMatcher(resourceType, true);
}
/**
diff --git
a/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java
b/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java
index 0344d08..4bbba9e 100644
---
a/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java
+++
b/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java
@@ -46,7 +46,7 @@ public class ResourcePropertiesMatcher extends
TypeSafeMatcher<Resource> {
@Override
protected boolean matchesSafely(Resource item) {
- ValueMap givenProps = item.adaptTo(ValueMap.class);
+ ValueMap givenProps = item.getValueMap();
for (Map.Entry<String, Object> prop : expectedProps.entrySet()) {
Object givenValue = givenProps.get(prop.getKey());
Object expectedValue = prop.getValue();
@@ -93,11 +93,7 @@ public class ResourcePropertiesMatcher extends
TypeSafeMatcher<Resource> {
@Override
protected void describeMismatchSafely(Resource item, Description
mismatchDescription) {
- Map<String, Object> actualProperties = item.adaptTo(ValueMap.class);
- if (actualProperties == null) {
- mismatchDescription.appendText("was Resource which does not expose
a value map via adaptTo(ValueMap.class)");
- return;
- }
+ Map<String, Object> actualProperties = item.getValueMap();
mismatchDescription.appendText("was Resource with properties ")
.appendValueList("[", ",", "]",
convertArraysToStrings(actualProperties).entrySet())
.appendText(" (resource: ")
diff --git
a/src/main/java/org/apache/sling/hamcrest/matchers/ResourceTypeMatcher.java
b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceTypeMatcher.java
new file mode 100644
index 0000000..0d239c4
--- /dev/null
+++ b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceTypeMatcher.java
@@ -0,0 +1,55 @@
+/*
+ * 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 type of the given resource is equal to
the type in the constructor (optionally allowing also sub types).
+ */
+public class ResourceTypeMatcher extends TypeSafeMatcher<Resource> {
+
+ private final String type;
+ private final boolean allowSubtypes;
+
+ public ResourceTypeMatcher(String type, boolean allowSubtypes) {
+ this.type = type;
+ this.allowSubtypes = allowSubtypes;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("Resource with type ").appendValue(type);
+ }
+
+ @Override
+ protected boolean matchesSafely(Resource resource) {
+ if (allowSubtypes) {
+ return resource.isResourceType(type);
+ } else {
+ return type.equals(resource.getResourceType());
+ }
+ }
+
+ @Override
+ protected void describeMismatchSafely(Resource resource, Description
mismatchDescription) {
+ mismatchDescription.appendText("was Resource with type
").appendValue(resource.getResourceType()).appendText(" (resource:
").appendValue(resource).appendText(")");
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
b/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
index dde21c8..f65ccfe 100644
--- a/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
+++ b/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
@@ -22,9 +22,9 @@ import java.util.Map;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.SyntheticResource;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.hamcrest.Matchers;
-
import org.junit.Rule;
import org.junit.Test;
@@ -32,6 +32,9 @@ import com.google.common.collect.ImmutableMap;
public class ResourceMatchersTest {
+ // only defined in newer versions of Sling API
+ private static final String PROPERTY_RESOURCE_SUPER_TYPE =
"sling:resourceSuperType";
+
@Rule
public final SlingContext context = new SlingContext();
@@ -44,6 +47,29 @@ public class ResourceMatchersTest {
Resource resource =
context.resourceResolver().getResource("/resource");
assertThat(resource, ResourceMatchers.resourceType("some/type"));
assertThat(resource,
Matchers.not(ResourceMatchers.resourceType("some/other/type")));
+
+ SyntheticResource syntheticResource = new SyntheticResource(
+ context.resourceResolver(), "/synthetic", "some/type");
+ assertThat(syntheticResource,
ResourceMatchers.resourceType("some/type"));
+ assertThat(syntheticResource,
Matchers.not(ResourceMatchers.resourceType("some/other/type")));
+ }
+
+ @Test
+ public void testResourceTypeOrDerived() {
+ context.build().resource("/resource",
+ ResourceResolver.PROPERTY_RESOURCE_TYPE, "some/type",
+ PROPERTY_RESOURCE_SUPER_TYPE, "some/base/type",
+ "some other key", "some other value");
+
+ Resource resource =
context.resourceResolver().getResource("/resource");
+ assertThat(resource,
ResourceMatchers.resourceTypeOrDerived("some/type"));
+ assertThat(resource,
ResourceMatchers.resourceTypeOrDerived("some/base/type"));
+ assertThat(resource,
Matchers.not(ResourceMatchers.resourceTypeOrDerived("some/other/type")));
+
+ SyntheticResource syntheticResource = new SyntheticResource(
+ context.resourceResolver(), "/synthetic", "some/type");
+ assertThat(syntheticResource,
ResourceMatchers.resourceType("some/type"));
+ assertThat(syntheticResource,
Matchers.not(ResourceMatchers.resourceType("some/other/type")));
}
@Test