Author: sseifert
Date: Sat Oct 8 08:09:18 2016
New Revision: 1763865
URL: http://svn.apache.org/viewvc?rev=1763865&view=rev
Log:
SLING-6117 Hamcrest: Simplify ResourceMatchers method signatures
Added:
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/MapUtil.java
(with props)
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java
(with props)
Modified:
sling/trunk/contrib/extensions/resourcemerger/pom.xml
sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/CommonMergedResourceProviderTest.java
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
Modified: sling/trunk/contrib/extensions/resourcemerger/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/pom.xml?rev=1763865&r1=1763864&r2=1763865&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/pom.xml (original)
+++ sling/trunk/contrib/extensions/resourcemerger/pom.xml Sat Oct 8 08:09:18
2016
@@ -113,13 +113,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock</artifactId>
- <version>1.6.0</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>
Modified:
sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/CommonMergedResourceProviderTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/CommonMergedResourceProviderTest.java?rev=1763865&r1=1763864&r2=1763865&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/CommonMergedResourceProviderTest.java
(original)
+++
sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/CommonMergedResourceProviderTest.java
Sat Oct 8 08:09:18 2016
@@ -133,8 +133,8 @@ public class CommonMergedResourceProvide
// all overlay resource are still exposed, because hiding children by
wildcard only hides children from underlying resources
Assert.assertThat(iterable, Matchers.containsInAnyOrder(
- ResourceMatchers.resourceWithNameAndProps("child1",
Collections.singletonMap("property1", (Object)"fromoverlay")),
- ResourceMatchers.resourceWithNameAndProps("child3",
Collections.singletonMap("property1", (Object)"fromoverlay"))
+ ResourceMatchers.withNameAndProps("child1",
Collections.singletonMap("property1", (Object)"fromoverlay")),
+ ResourceMatchers.withNameAndProps("child3",
Collections.singletonMap("property1", (Object)"fromoverlay"))
));
// now hide by explicit value
@@ -144,8 +144,8 @@ public class CommonMergedResourceProvide
// child1 is no longer exposed from overlay, because hiding children
by name hides children from underlying as well as from local resources, child2
is exposed from base
iterable = new IteratorIterable<Resource>(provider.listChildren(ctx,
mergedResource), true);
Assert.assertThat(iterable, Matchers.containsInAnyOrder(
- ResourceMatchers.resourceWithName("child2"),
- ResourceMatchers.resourceWithName("child3")));
+ ResourceMatchers.withName("child2"),
+ ResourceMatchers.withName("child3")));
// now hide by negated value (hide all underlying children except for
the one with name child2)
properties.put(MergedResourceConstants.PN_HIDE_CHILDREN, new
String[]{"!child2", "*", "child3"});
@@ -153,8 +153,8 @@ public class CommonMergedResourceProvide
iterable = new IteratorIterable<Resource>(provider.listChildren(ctx,
mergedResource), true);
Assert.assertThat(iterable, Matchers.containsInAnyOrder(
- ResourceMatchers.resourceWithName("child2"),
- ResourceMatchers.resourceWithNameAndProps("child1",
Collections.singletonMap("property1", (Object)"fromoverlay"))
+ ResourceMatchers.withName("child2"),
+ ResourceMatchers.withNameAndProps("child1",
Collections.singletonMap("property1", (Object)"fromoverlay"))
));
}
@@ -177,7 +177,7 @@ public class CommonMergedResourceProvide
IteratorIterable<Resource> iterable = new
IteratorIterable<Resource>(provider.listChildren(ctx, mergedResource), true);
// the resource named "!child3" should be hidden
- Assert.assertThat(iterable,
Matchers.contains(ResourceMatchers.resourceWithNameAndProps("!child1",
Collections.singletonMap("property1", (Object)"fromoverlay"))));
+ Assert.assertThat(iterable,
Matchers.contains(ResourceMatchers.withNameAndProps("!child1",
Collections.singletonMap("property1", (Object)"fromoverlay"))));
}
@Test
@@ -196,7 +196,7 @@ public class CommonMergedResourceProvide
Resource mergedResource = this.provider.getResource(ctx, "/merged",
ResourceContext.EMPTY_CONTEXT, null);
// the child was hidden on the parent (but only for the underlying
resource), the local child from the overlay is still exposed
- Assert.assertThat(provider.getResource(ctx, "/merged/child",
ResourceContext.EMPTY_CONTEXT, mergedResource),
ResourceMatchers.resourceWithNameAndProps("child",
Collections.singletonMap("property1", (Object)"fromoverlay")));
+ Assert.assertThat(provider.getResource(ctx, "/merged/child",
ResourceContext.EMPTY_CONTEXT, mergedResource),
ResourceMatchers.withNameAndProps("child",
Collections.singletonMap("property1", (Object)"fromoverlay")));
}
@Test
@@ -217,9 +217,9 @@ public class CommonMergedResourceProvide
Resource mergedResource = this.provider.getResource(ctx, "/merged",
ResourceContext.EMPTY_CONTEXT, null);
// property1 is still exposed from overlay, because hiding properties
by wildcard only hides children from underlying resources
- Assert.assertThat(mergedResource,
ResourceMatchers.resourceWithProps(expectedProperties));
+ Assert.assertThat(mergedResource,
ResourceMatchers.withProps(expectedProperties));
// all properties from underlying resource are hidden!
- Assert.assertThat(mergedResource,
Matchers.not(ResourceMatchers.resourceWithProps(properties)));
+ Assert.assertThat(mergedResource,
Matchers.not(ResourceMatchers.withProps(properties)));
// make sure no special properties are exposed
Assert.assertFalse(mergedResource.getValueMap().containsKey(MergedResourceConstants.PN_HIDE_CHILDREN));
Assert.assertFalse(mergedResource.getValueMap().containsKey(MergedResourceConstants.PN_HIDE_PROPERTIES));
@@ -230,9 +230,9 @@ public class CommonMergedResourceProvide
expectedProperties.put("property2", "frombase");
expectedProperties.remove("property1");
// property2 and property 3 are still exposed
- Assert.assertThat(mergedResource,
ResourceMatchers.resourceWithProps(expectedProperties));
+ Assert.assertThat(mergedResource,
ResourceMatchers.withProps(expectedProperties));
// property1 is no longer exposed from overlay nor base, because
hiding properties by name also hides local properties
- Assert.assertThat(mergedResource,
Matchers.not(ResourceMatchers.resourceWithProps(Collections.singletonMap("property1",
(Object)"fromoverlay"))));
+ Assert.assertThat(mergedResource,
Matchers.not(ResourceMatchers.withProps(Collections.singletonMap("property1",
(Object)"fromoverlay"))));
// make sure no special properties are exposed
Assert.assertFalse(mergedResource.getValueMap().containsKey(MergedResourceConstants.PN_HIDE_CHILDREN));
@@ -257,7 +257,7 @@ public class CommonMergedResourceProvide
// convert the iterator returned by list children into an iterable (to
be able to perform some tests)
IteratorIterable<Resource> iterable = new
IteratorIterable<Resource>(provider.listChildren(ctx, mergedResource), true);
- Assert.assertThat(iterable,
Matchers.contains(ResourceMatchers.resourceWithName("child1"),ResourceMatchers.resourceWithName("child4"),
ResourceMatchers.resourceWithName("child2"),
ResourceMatchers.resourceWithName("child3")));
+ Assert.assertThat(iterable,
Matchers.contains(ResourceMatchers.withName("child1"),ResourceMatchers.withName("child4"),
ResourceMatchers.withName("child2"), ResourceMatchers.withName("child3")));
}
@Test
@@ -273,7 +273,7 @@ public class CommonMergedResourceProvide
// convert the iterator returned by list children into an iterable (to
be able to perform some tests)
IteratorIterable<Resource> iterable = new
IteratorIterable<Resource>(provider.listChildren(ctx, mergedResource), true);
- Assert.assertThat(iterable,
Matchers.contains(ResourceMatchers.resourceWithName("child1"),ResourceMatchers.resourceWithName("child2"),
ResourceMatchers.resourceWithName("child3"),
ResourceMatchers.resourceWithName("child4")));
+ Assert.assertThat(iterable,
Matchers.contains(ResourceMatchers.withName("child1"),ResourceMatchers.withName("child2"),
ResourceMatchers.withName("child3"), ResourceMatchers.withName("child4")));
}
@Test
@@ -288,6 +288,6 @@ public class CommonMergedResourceProvide
// convert the iterator returned by list children into an iterable (to
be able to perform some tests)
IteratorIterable<Resource> iterable = new
IteratorIterable<Resource>(provider.listChildren(ctx, mergedResource), true);
- Assert.assertThat(iterable,
Matchers.contains(ResourceMatchers.resourceWithName("child1"),ResourceMatchers.resourceWithName("child3"),
ResourceMatchers.resourceWithName("child2")));
+ Assert.assertThat(iterable,
Matchers.contains(ResourceMatchers.withName("child1"),ResourceMatchers.withName("child3"),
ResourceMatchers.withName("child2")));
}
}
Added:
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/MapUtil.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/MapUtil.java?rev=1763865&view=auto
==============================================================================
---
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/MapUtil.java
(added)
+++
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/MapUtil.java
Sat Oct 8 08:09:18 2016
@@ -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;
+ }
+
+}
Propchange:
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/MapUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/MapUtil.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Oct 8 08:09:18 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange:
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/MapUtil.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java?rev=1763865&r1=1763864&r2=1763865&view=diff
==============================================================================
---
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
(original)
+++
sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/ResourceMatchers.java
Sat Oct 8 08:09:18 2016
@@ -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
}
Added:
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java?rev=1763865&view=auto
==============================================================================
---
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java
(added)
+++
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java
Sat Oct 8 08:09:18 2016
@@ -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);
+ }
+
+}
Propchange:
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Oct 8 08:09:18 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange:
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/MapUtilTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java?rev=1763865&r1=1763864&r2=1763865&view=diff
==============================================================================
---
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
(original)
+++
sling/trunk/testing/hamcrest/src/test/java/org/apache/sling/hamcrest/ResourceMatchersTest.java
Sat Oct 8 08:09:18 2016
@@ -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