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/11/07 10:17:21 UTC
[sling-org-apache-sling-testing-hamcrest] 01/18: SLING-5063 -
Create a set of Hamcrest matchers for JUnit tests
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.testing.hamcrest-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-hamcrest.git
commit 0e811f0598765fe3bee6fc66d4e7af0f7c638ff8
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Fri Sep 25 09:32:45 2015 +0000
SLING-5063 - Create a set of Hamcrest matchers for JUnit tests
Initial version
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/hamcrest@1705261 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 56 +++++++++++++++++++++
.../apache/sling/hamcrest/ResourceMatchers.java | 50 +++++++++++++++++++
.../hamcrest/matchers/ResourceChildrenMatcher.java | 52 ++++++++++++++++++++
.../sling/hamcrest/matchers/ResourceMatcher.java | 57 ++++++++++++++++++++++
4 files changed, 215 insertions(+)
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..86e7af0
--- /dev/null
+++ b/pom.xml
@@ -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>
diff --git a/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java b/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
new file mode 100644
index 0000000..7950a2b
--- /dev/null
+++ b/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
@@ -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
+ }
+}
diff --git a/src/main/java/org/apache/sling/hamcrest/matchers/ResourceChildrenMatcher.java b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceChildrenMatcher.java
new file mode 100644
index 0000000..9dc4465
--- /dev/null
+++ b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceChildrenMatcher.java
@@ -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
diff --git a/src/main/java/org/apache/sling/hamcrest/matchers/ResourceMatcher.java b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceMatcher.java
new file mode 100644
index 0000000..ed25bd6
--- /dev/null
+++ b/src/main/java/org/apache/sling/hamcrest/matchers/ResourceMatcher.java
@@ -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
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.