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 b6b2190c91b35e64165be3d82c91b068f14bef54 Author: Stefan Seifert <[email protected]> AuthorDate: Sat Oct 8 08:09:18 2016 +0000 SLING-6117 Hamcrest: Simplify ResourceMatchers method signatures git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/hamcrest@1763865 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/sling/hamcrest/MapUtil.java | 64 +++++++++++ .../apache/sling/hamcrest/ResourceMatchers.java | 49 ++++++++- .../org/apache/sling/hamcrest/MapUtilTest.java | 38 +++++++ .../sling/hamcrest/ResourceMatchersTest.java | 122 ++++++++++++++++----- 4 files changed, 240 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/apache/sling/hamcrest/MapUtil.java b/src/main/java/org/apache/sling/hamcrest/MapUtil.java new file mode 100644 index 0000000..4f38c20 --- /dev/null +++ b/src/main/java/org/apache/sling/hamcrest/MapUtil.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; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.Map; + +/** + * Map util methods. + */ +final class MapUtil { + + private MapUtil() { + // static methods only + } + + /** + * Convert key/value pairs to map + * @param args Key/value pairs + * @return Map + */ + @SuppressWarnings("unchecked") + public static Map<String, Object> toMap(Object... args) { + if (args == null || args.length == 0) { + return Collections.emptyMap(); + } + if (args.length == 1) { + if (args[0] instanceof Map) { + return (Map)args[0]; + } + else if (args[0] instanceof Dictionary) { + return toMap((Dictionary)args[0]); + } + } + if (args.length % 2 != 0) { + throw new IllegalArgumentException("args must be an even number of name/values:" + Arrays.asList(args)); + } + final Map<String, Object> result = new HashMap<String, Object>(); + for (int i=0 ; i < args.length; i+=2) { + result.put(args[i].toString(), args[i+1]); + } + return result; + } + +} diff --git a/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java b/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java index d993fa3..ebd4cba 100644 --- a/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java +++ b/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java @@ -90,7 +90,7 @@ public final class ResourceMatchers { * @param path the resources path, not <code>null</code> or empty * @return a matcher instance */ - public static Matcher<Resource> resourceWithPath(String path) { + public static Matcher<Resource> withPath(String path) { return new ResourcePathMatcher(path); } @@ -104,7 +104,7 @@ public final class ResourceMatchers { * @param name the resources name, not <code>null</code> or empty * @return a matcher instance */ - public static Matcher<Resource> resourceWithName(String name) { + public static Matcher<Resource> withName(String name) { return new ResourceNameMatcher(name); } @@ -117,7 +117,7 @@ public final class ResourceMatchers { * @param resourceType the resource type to match * @return a matcher instance */ - public static Matcher<Resource> resourceOfType(String resourceType) { + public static Matcher<Resource> ofType(String resourceType) { return new ResourcePropertiesMatcher(Collections.<String, Object> singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, resourceType)); } @@ -136,11 +136,30 @@ public final class ResourceMatchers { * @param properties the properties to match * @return a matcher instance */ - public static Matcher<Resource> resourceWithProps(Map<String, Object> properties) { + public static Matcher<Resource> withProps(Map<String, Object> properties) { return new ResourcePropertiesMatcher(properties); } /** + * Matches resources which has at least the specified <tt>properties</tt> defined with matching values + * + * <p>Values not declared in the the <tt>properties</tt> parameter are not validated.</p> + * <pre> + * Map<String, Object> expectedProperties = new HashMap<>(); + * expectedProperties.put("jcr:title", "Node title"); + * expectedProperties.put("jcr:text", "Some long text"); + * + * assertThat(resource, resourceWithProps(expectedProperties)); + * </pre> + * + * @param properties the properties to match + * @return a matcher instance + */ + public static Matcher<Resource> withProps(Object... properties) { + return withProps(MapUtil.toMap(properties)); + } + + /** * Matches resources which has the given name and at least the specified <tt>properties</tt> defined with matching values * * <p>Values not declared in the the <tt>properties</tt> parameter are not validated.</p> @@ -156,10 +175,30 @@ public final class ResourceMatchers { * @param properties the properties to match * @return a matcher instance */ - public static Matcher<Resource> resourceWithNameAndProps(String name, Map<String, Object> properties) { + public static Matcher<Resource> withNameAndProps(String name, Map<String, Object> properties) { return Matchers.allOf(new ResourceNameMatcher(name), new ResourcePropertiesMatcher(properties)); } + /** + * Matches resources which has the given name and at least the specified <tt>properties</tt> defined with matching values + * + * <p>Values not declared in the the <tt>properties</tt> parameter are not validated.</p> + * <pre> + * Map<String, Object> expectedProperties = new HashMap<>(); + * expectedProperties.put("jcr:title", "Node title"); + * expectedProperties.put("jcr:text", "Some long text"); + * + * assertThat(resource, resourceWithProps(expectedProperties)); + * </pre> + * + * @param name the expected name of the resource + * @param properties the properties to match + * @return a matcher instance + */ + public static Matcher<Resource> withNameAndProps(String name, Object... properties) { + return withNameAndProps(name, MapUtil.toMap(properties)); + } + private ResourceMatchers() { // prevent instantiation } diff --git a/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java b/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java new file mode 100644 index 0000000..9257fbf --- /dev/null +++ b/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java @@ -0,0 +1,38 @@ +/* + * 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.junit.Assert.assertEquals; + +import java.util.Map; + +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; + +public class MapUtilTest { + + @Test + public void testMapObjectVarargs() { + Map<String, Object> convertedMap = MapUtil.toMap("param1", "var1", "param2", 123, "param3", true); + + assertEquals(ImmutableMap.<String, Object>of("param1", "var1", "param2", 123, "param3", true), convertedMap); + } + +} diff --git a/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java b/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java index c98cb5a..9e900ce 100644 --- a/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java +++ b/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java @@ -16,7 +16,6 @@ */ package org.apache.sling.hamcrest; -import java.util.HashMap; import java.util.Map; import org.apache.sling.api.resource.Resource; @@ -27,61 +26,98 @@ import org.junit.Assert; import org.junit.Rule; import org.junit.Test; +import com.google.common.collect.ImmutableMap; + public class ResourceMatchersTest { @Rule public final SlingContext context = new SlingContext(); @Test - public void testResourceOfType() { + public void testOfType() { context.build().resource("/resource", ResourceResolver.PROPERTY_RESOURCE_TYPE, "some/type", "some other key", "some other value"); Resource resource = context.resourceResolver().getResource("/resource"); - Assert.assertThat(resource, ResourceMatchers.resourceOfType("some/type")); - Assert.assertThat(resource, Matchers.not(ResourceMatchers.resourceOfType("some/other/type"))); + Assert.assertThat(resource, ResourceMatchers.ofType("some/type")); + Assert.assertThat(resource, Matchers.not(ResourceMatchers.ofType("some/other/type"))); } @Test - public void testResourceWithPath() { + public void testWithPath() { context.build().resource("/resource"); Resource resource = context.resourceResolver().getResource("/resource"); - Assert.assertThat(resource, ResourceMatchers.resourceWithPath("/resource")); - Assert.assertThat(resource, Matchers.not(ResourceMatchers.resourceWithPath("some/other/name"))); + Assert.assertThat(resource, ResourceMatchers.withPath("/resource")); + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withPath("some/other/name"))); } @Test - public void testResourceWithName() { + public void testWithName() { context.build().resource("/resource"); Resource resource = context.resourceResolver().getResource("/resource"); - Assert.assertThat(resource, ResourceMatchers.resourceWithName("resource")); - Assert.assertThat(resource, Matchers.not(ResourceMatchers.resourceWithName("some/other/name"))); + Assert.assertThat(resource, ResourceMatchers.withName("resource")); + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withName("some/other/name"))); } @Test - public void testResourceWithProps() { + public void testWithProps() { context.build().resource("/resource", "key1", "value1", "key2", "value2", "key3", "value3"); - Map<String, Object> expectedProperties = new HashMap<String, Object>(); - expectedProperties.put("key1", "value1"); - expectedProperties.put("key2", "value2"); + Map<String, Object> expectedProperties = ImmutableMap.<String, Object>builder() + .put("key1", "value1") + .put("key2", "value2") + .build(); Resource resource = context.resourceResolver().getResource("/resource"); - Assert.assertThat(resource, ResourceMatchers.resourceWithProps(expectedProperties)); + Assert.assertThat(resource, ResourceMatchers.withProps(expectedProperties)); + + // test existing key with not matching value + expectedProperties = ImmutableMap.<String, Object>builder() + .put("key1", "value1") + .put("key2", "value3") + .build(); + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withProps(expectedProperties))); + + // test non-existing key + expectedProperties = ImmutableMap.<String, Object>builder() + .put("key4", "value4") + .build(); + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withProps(expectedProperties))); + } + + @Test + public void testWithPropsVarargs() { + context.build().resource("/resource", + "key1", "value1", + "key2", "value2", + "key3", "value3"); + + Object[] expectedProperties = new Object[] { + "key1", "value1", + "key2", "value2" + }; + + Resource resource = context.resourceResolver().getResource("/resource"); + Assert.assertThat(resource, ResourceMatchers.withProps(expectedProperties)); + // test existing key with not matching value - expectedProperties.put("key2", "value3"); - Assert.assertThat(resource, Matchers.not(ResourceMatchers.resourceWithProps(expectedProperties))); + expectedProperties = new Object[] { + "key1", "value1", + "key2", "value3" + }; + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withProps(expectedProperties))); // test non-existing key - expectedProperties.clear(); - expectedProperties.put("key4", "value4"); - Assert.assertThat(resource, Matchers.not(ResourceMatchers.resourceWithProps(expectedProperties))); + expectedProperties = new Object[] { + "key4", "value4" + }; + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withProps(expectedProperties))); } @Test @@ -95,25 +131,55 @@ public class ResourceMatchersTest { } @Test - public void testResourceWithNameAndProps() { + public void testWithNameAndProps() { context.build().resource("/resource", "key1", "value1", "key2", "value2", "key3", "value3"); - Map<String, Object> expectedProperties = new HashMap<String, Object>(); - expectedProperties.put("key1", "value1"); - expectedProperties.put("key2", "value2"); + Map<String, Object> expectedProperties = ImmutableMap.<String, Object>builder() + .put("key1", "value1") + .put("key2", "value2") + .build(); Resource resource = context.resourceResolver().getResource("/resource"); - Assert.assertThat(resource, ResourceMatchers.resourceWithNameAndProps("resource", expectedProperties)); + Assert.assertThat(resource, ResourceMatchers.withNameAndProps("resource", expectedProperties)); // test not matching name - Assert.assertThat(resource, Matchers.not(ResourceMatchers.resourceWithNameAndProps("resource1", expectedProperties))); + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withNameAndProps("resource1", expectedProperties))); // test existing key with not matching value - expectedProperties.put("key2", "value3"); - Assert.assertThat(resource, Matchers.not(ResourceMatchers.resourceWithNameAndProps("resource", expectedProperties))); + expectedProperties = ImmutableMap.<String, Object>builder() + .put("key1", "value1") + .put("key2", "value3") + .build(); + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withNameAndProps("resource", expectedProperties))); + } + + @Test + public void testWithNameAndPropsVarargs() { + context.build().resource("/resource", + "key1", "value1", + "key2", "value2", + "key3", "value3"); + + Object[] expectedProperties = new Object[] { + "key1", "value1", + "key2", "value2" + }; + + Resource resource = context.resourceResolver().getResource("/resource"); + Assert.assertThat(resource, ResourceMatchers.withNameAndProps("resource", expectedProperties)); + + // test not matching name + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withNameAndProps("resource1", expectedProperties))); + + // test existing key with not matching value + expectedProperties = new Object[] { + "key1", "value1", + "key2", "value3" + }; + Assert.assertThat(resource, Matchers.not(ResourceMatchers.withNameAndProps("resource", expectedProperties))); } @Test -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
