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:21:49 UTC
[sling-org-apache-sling-testing-resourceresolver-mock] 03/05:
SLING-6416 MockValueMap does not permit to get a BigDecimal value
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.testing.resourceresolver-mock-1.1.16
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-resourceresolver-mock.git
commit 66101c4c372554f51a82407ac6043509fc3f015e
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Wed Dec 21 14:19:14 2016 +0000
SLING-6416 MockValueMap does not permit to get a BigDecimal value
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/resourceresolver-mock@1775430 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 14 +-
.../sling/testing/resourceresolver/DateUtils.java | 98 +++++++++
.../DeepReadModifiableValueMapDecorator.java | 4 +-
.../testing/resourceresolver/MockValueMap.java | 21 +-
.../testing/resourceresolver/ObjectConverter.java | 184 ++++++++++++++++
.../resourceresolver/ValueMapDecorator.java | 178 +++++++++++++++
.../sling/testing/resourceresolver/Convert.java | 242 +++++++++++++++++++++
.../resourceresolver/ObjectConverterTest.java | 233 ++++++++++++++++++++
.../SlingCrudResourceResolverTest.java | 3 -
.../resourceresolver/ValueMapDecoratorTest.java | 173 +++++++++++++++
10 files changed, 1124 insertions(+), 26 deletions(-)
diff --git a/pom.xml b/pom.xml
index 3dd2e87..24c1878 100644
--- a/pom.xml
+++ b/pom.xml
@@ -83,7 +83,7 @@
<version>2.5.3</version>
<scope>compile</scope>
</dependency>
-
+ <!-- testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -95,6 +95,18 @@
<version>15.0</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>2.3.7</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.2</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<!-- Profiles to run unit tests against different Sling API versions -->
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/DateUtils.java b/src/main/java/org/apache/sling/testing/resourceresolver/DateUtils.java
new file mode 100644
index 0000000..e78e621
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/DateUtils.java
@@ -0,0 +1,98 @@
+/*
+ * 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.testing.resourceresolver;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.jackrabbit.util.ISO8601;
+
+/**
+ * This is copied from org.apache.sling.api.wrappers.impl.DateUtils
+ * to avoid dependency to latest Sling API.
+ */
+final class DateUtils {
+
+ private DateUtils() {
+ // static methods only
+ }
+
+ /**
+ * @param date Date value
+ * @return Calendar value or null
+ */
+ public static Calendar toCalendar(Date input) {
+ if (input == null) {
+ return null;
+ }
+ Calendar result = Calendar.getInstance();
+ result.setTime(input);
+ return result;
+ }
+
+ /**
+ * @param calendar Calendar value
+ * @return Date value or null
+ */
+ public static Date toDate(Calendar input) {
+ if (input == null) {
+ return null;
+ }
+ return input.getTime();
+ }
+
+ /**
+ * @param input Date value
+ * @return ISO8601 string representation or null
+ */
+ public static String dateToString(Date input) {
+ return calendarToString(toCalendar(input));
+ }
+
+ /**
+ * @param input Calendar value
+ * @return ISO8601 string representation or null
+ */
+ public static String calendarToString(Calendar input) {
+ if (input == null) {
+ return null;
+ }
+ return ISO8601.format(input);
+ }
+
+ /**
+ * @param input ISO8601 string representation
+ * @return Date value or null
+ */
+ public static Date dateFromString(String input) {
+ return toDate(calendarFromString(input));
+ }
+
+ /**
+ * @param input ISO8601 string representation
+ * @return Calendar value or null
+ */
+ public static Calendar calendarFromString(String input) {
+ if (input == null) {
+ return null;
+ }
+ return ISO8601.parse(input);
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/DeepReadModifiableValueMapDecorator.java b/src/main/java/org/apache/sling/testing/resourceresolver/DeepReadModifiableValueMapDecorator.java
index 2c90cc3..6ad8d13 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/DeepReadModifiableValueMapDecorator.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/DeepReadModifiableValueMapDecorator.java
@@ -22,10 +22,10 @@ import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
-import org.apache.sling.api.wrappers.ValueMapDecorator;
/**
- * This is copied from org.apache.sling.api.wrappers.DeepReadValueMapDecorator and DeepReadModifiableValueMapDecorator.
+ * This is copied from org.apache.sling.api.wrappers.DeepReadValueMapDecorator and DeepReadModifiableValueMapDecorator
+ * to avoid dependency to latest Sling API.
*/
class DeepReadModifiableValueMapDecorator extends ValueMapDecorator implements ModifiableValueMap {
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
index a5e2536..5932506 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
@@ -27,10 +27,8 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
-import org.apache.jackrabbit.util.ISO8601;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.wrappers.ValueMapDecorator;
/**
* ValueMap for mocked resources to mimick JCR-like behavior.
@@ -54,24 +52,7 @@ public class MockValueMap extends DeepReadModifiableValueMapDecorator implements
@Override
public <T> T get(String name, Class<T> type) {
- if (type == Calendar.class) {
- // Support conversion of String to Calendar if value conforms to ISO8601 date format
- Object value = get(name);
- if (value instanceof String) {
- return (T)ISO8601.parse((String)value);
- }
- }
- else if (type == Date.class) {
- // Support conversion from Calendar to Date
- Calendar calendar = get(name, Calendar.class);
- if (calendar != null) {
- return (T)calendar.getTime();
- }
- else {
- return null;
- }
- }
- else if (type == InputStream.class) {
+ if (type == InputStream.class) {
// Support conversion from byte array to InputStream
byte[] data = get(name, byte[].class);
if (data!=null) {
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/ObjectConverter.java b/src/main/java/org/apache/sling/testing/resourceresolver/ObjectConverter.java
new file mode 100644
index 0000000..f405499
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/ObjectConverter.java
@@ -0,0 +1,184 @@
+/*
+ * 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.testing.resourceresolver;
+
+import java.lang.reflect.Array;
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * This is copied from org.apache.sling.api.wrappers.impl.ObjectConverter
+ * to avoid dependency to latest Sling API.
+ */
+final class ObjectConverter {
+
+ private ObjectConverter() {
+ // static methods only
+ }
+
+ /**
+ * Converts the object to the given type.
+ * @param obj object
+ * @param type type
+ * @return the converted object
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T convert(Object obj, Class<T> type) {
+ if (obj == null) {
+ return null;
+ }
+
+ // check if direct assignment is possible
+ if (type.isAssignableFrom(obj.getClass())) {
+ return (T)obj;
+ }
+
+ // convert array elements individually
+ if (type.isArray()) {
+ return (T)convertToArray(obj, type.getComponentType());
+ }
+
+ // convert Calendar in Date and vice versa
+ if (Calendar.class.isAssignableFrom(type) && obj instanceof Date) {
+ return (T)DateUtils.toCalendar((Date)obj);
+ }
+ if (type == Date.class && obj instanceof Calendar) {
+ return (T)DateUtils.toDate((Calendar)obj);
+ }
+
+ // no direct conversion - format to string and try to parse to target type
+ String result = getSingleValue(obj);
+ if (result == null) {
+ return null;
+ }
+ if (type == String.class) {
+ return (T)result.toString();
+ }
+ if (type == Boolean.class) {
+ // do not rely on Boolean.parseBoolean to avoid converting nonsense to "false" without noticing
+ if ("true".equalsIgnoreCase(result)) {
+ return (T)Boolean.TRUE;
+ }
+ else if ("false".equalsIgnoreCase(result)) {
+ return (T)Boolean.FALSE;
+ }
+ else {
+ return null;
+ }
+ }
+ try {
+ if (type == Byte.class) {
+ return (T)(Byte)Byte.parseByte(result);
+ }
+ if (type == Short.class) {
+ return (T)(Short)Short.parseShort(result);
+ }
+ if (type == Integer.class) {
+ return (T)(Integer)Integer.parseInt(result);
+ }
+ if (type == Long.class) {
+ return (T)(Long)Long.parseLong(result);
+ }
+ if (type == Float.class) {
+ return (T)(Float)Float.parseFloat(result);
+ }
+ if (type == Double.class) {
+ return (T)(Double)Double.parseDouble(result);
+ }
+ if (type == BigDecimal.class) {
+ return (T)new BigDecimal(result);
+ }
+ }
+ catch (NumberFormatException e) {
+ return null;
+ }
+ if (Calendar.class.isAssignableFrom(type)) {
+ return (T)DateUtils.calendarFromString(result);
+ }
+ if (type == Date.class) {
+ return (T)DateUtils.dateFromString(result);
+ }
+ return null;
+ }
+
+ /**
+ * Gets a single value of String from the object. If the object is an array it returns it's first element.
+ * @param obj object or object array.
+ * @return result of <code>toString()</code> on object or first element of an object array. If @param obj is null
+ * or it's an array with first element that is null, then null is returned.
+ */
+ private static String getSingleValue(Object obj) {
+ final String result;
+ if (obj == null) {
+ result = null;
+ }
+ else if (obj.getClass().isArray()) {
+ if (Array.getLength(obj) == 0) {
+ result = null;
+ }
+ else {
+ result = getSingleValue(Array.get(obj, 0));
+ }
+ }
+ else if (obj instanceof Calendar) {
+ result = DateUtils.calendarToString((Calendar)obj);
+ }
+ else if (obj instanceof Date) {
+ result = DateUtils.dateToString((Date)obj);
+ }
+ else {
+ result = obj.toString();
+ }
+ return result;
+ }
+
+ /**
+ * Converts the object to an array of the given type
+ * @param obj the object or object array
+ * @param type the component type of the array
+ * @return and array of type T
+ */
+ @SuppressWarnings("unchecked")
+ private static <T> T[] convertToArray(Object obj, Class<T> type) {
+ if (obj.getClass().isArray()) {
+ List<Object> resultList = new ArrayList<Object>();
+ for (int i = 0; i < Array.getLength(obj); i++) {
+ T singleValueResult = convert(Array.get(obj, i), type);
+ if (singleValueResult != null) {
+ resultList.add(singleValueResult);
+ }
+ }
+ return resultList.toArray((T[])Array.newInstance(type, resultList.size()));
+ }
+ else {
+ final T singleValueResult = convert(obj, type);
+ // return null for type conversion errors instead of single element array with value null
+ if (singleValueResult == null) {
+ return (T[])Array.newInstance(type, 0);
+ }
+ final T[] arrayResult = (T[])Array.newInstance(type, 1);
+ arrayResult[0] = singleValueResult;
+ return arrayResult;
+ }
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/ValueMapDecorator.java b/src/main/java/org/apache/sling/testing/resourceresolver/ValueMapDecorator.java
new file mode 100644
index 0000000..52c7f36
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/ValueMapDecorator.java
@@ -0,0 +1,178 @@
+/*
+ * 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.testing.resourceresolver;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.sling.api.resource.ValueMap;
+
+/**
+ * This is copied from org.apache.sling.api.wrappers.ValueMapDectorator
+ * to avoid dependency to latest Sling API.
+ */
+class ValueMapDecorator implements ValueMap {
+
+ /**
+ * underlying map
+ */
+ private final Map<String, Object> base;
+
+ /**
+ * Creates a new wrapper around a given map.
+ * @param base wrapped object
+ */
+ public ValueMapDecorator(Map<String, Object> base) {
+ this.base = base;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public <T> T get(String name, Class<T> type) {
+ if (base instanceof ValueMap) {
+ // shortcut if decorated map is ValueMap
+ return ((ValueMap)base).get(name, type);
+ }
+ return ObjectConverter.convert(get(name), type);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("unchecked")
+ public <T> T get(String name, T defaultValue) {
+ if (base instanceof ValueMap) {
+ // shortcut if decorated map is ValueMap
+ return ((ValueMap)base).get(name, defaultValue);
+ }
+ if (defaultValue == null) {
+ return (T)get(name);
+ }
+ T value = get(name, (Class<T>) defaultValue.getClass());
+ return value == null ? defaultValue : value;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int size() {
+ return base.size();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isEmpty() {
+ return base.isEmpty();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean containsKey(Object key) {
+ return base.containsKey(key);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean containsValue(Object value) {
+ return base.containsValue(value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object get(Object key) {
+ return base.get(key);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object put(String key, Object value) {
+ return base.put(key, value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object remove(Object key) {
+ return base.remove(key);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void putAll(Map<? extends String, ?> t) {
+ base.putAll(t);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void clear() {
+ base.clear();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Set<String> keySet() {
+ return base.keySet();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Collection<Object> values() {
+ return base.values();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Set<Entry<String, Object>> entrySet() {
+ return base.entrySet();
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + " : " + this.base.toString();
+ }
+
+ @Override
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return base.hashCode();
+ }
+
+ @Override
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ return base.equals(obj);
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/Convert.java b/src/test/java/org/apache/sling/testing/resourceresolver/Convert.java
new file mode 100644
index 0000000..bdcd924
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/Convert.java
@@ -0,0 +1,242 @@
+/*
+ * 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.testing.resourceresolver;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.lang.reflect.Array;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.commons.lang3.ClassUtils;
+
+/**
+ * This is copied from org.apache.sling.api.wrappers.impl.Convert
+ */
+final class Convert {
+
+ private Convert() {
+ // static methods only
+ }
+
+ @SuppressWarnings("unchecked")
+ public static class ConversionAssert<T,U> {
+ private final T input1;
+ private final T input2;
+ private Class<T> inputType;
+ private U expected1;
+ private U expected2;
+ private U nullValue;
+ private Class<U> expectedType;
+
+ private ConversionAssert(T input1, T input2, boolean inputTypePrimitive) {
+ this.input1 = input1;
+ this.input2 = input2;
+ this.inputType = (Class<T>)input1.getClass();
+ if (inputTypePrimitive) {
+ this.inputType = (Class<T>)ClassUtils.wrapperToPrimitive(this.inputType);
+ }
+ }
+
+ private void expected(U expected1, U expected2, boolean expectedTypePrimitive) {
+ this.expected1 = expected1;
+ this.expected2 = expected2;
+ this.expectedType = (Class<U>)expected1.getClass();
+ if (expectedTypePrimitive) {
+ expectedType = (Class<U>)ClassUtils.wrapperToPrimitive(this.expectedType);
+ }
+ }
+
+ /**
+ * @param expected1 Singleton or first array expected result value
+ * @param expected2 Second array expected result value
+ * @return this
+ */
+ public ConversionAssert<T,U> to(U expected1, U expected2) {
+ expected(expected1, expected2, false);
+ return this;
+ }
+
+ /**
+ * @param expected1 Singleton or first array expected result value
+ * @param expected2 Second array expected result value
+ * @return this
+ */
+ public ConversionAssert<T,U> toPrimitive(U expected1, U expected2) {
+ expected(expected1, expected2, true);
+ return this;
+ }
+
+ /**
+ * @param expected1 Singleton or first array expected result value
+ * @param expected2 Second array expected result value
+ * @return this
+ */
+ public ConversionAssert<T,U> toNull(Class<U> expectedType) {
+ expected1 = null;
+ expected2 = null;
+ this.expectedType = expectedType;
+ return this;
+ }
+
+ /**
+ * @param nullValue Result value in case of null
+ */
+ public ConversionAssert<T,U> nullValue(U nullValue) {
+ this.nullValue = nullValue;
+ return this;
+ }
+
+ /**
+ * Do assertion
+ */
+ public void test() {
+ Class<U[]> expectedArrayType = (Class<U[]>)Array.newInstance(this.expectedType, 0).getClass();
+ assertPermuations(input1, input2, inputType, expected1, expected2, nullValue, expectedType, expectedArrayType);
+ }
+ }
+
+ /**
+ * @param input1 Singleton or first array input value
+ * @param input2 Second array input value
+ */
+ public static <T,U> ConversionAssert<T,U> from(T input1, T input2) {
+ return new ConversionAssert<T,U>(input1, input2, false);
+ }
+
+ /**
+ * @param input1 Singleton or first array input value
+ * @param input2 Second array input value
+ */
+ public static <T,U> ConversionAssert<T,U> fromPrimitive(T input1, T input2) {
+ return new ConversionAssert<T,U>(input1, input2, true);
+ }
+
+ private static <T,U> void assertPermuations(T input1, T input2, Class<T> inputType,
+ U expected1, U expected2, U nullValue, Class<U> expectedType, Class<U[]> expectedArrayType) {
+
+ // single value to single value
+ assertConversion(expected1, input1, expectedType);
+
+ // single value to array
+ Object expectedSingletonArray;
+ if (expected1 == null && expected2 == null) {
+ expectedSingletonArray = Array.newInstance(expectedType, 0);
+ }
+ else {
+ expectedSingletonArray = Array.newInstance(expectedType, 1);
+ Array.set(expectedSingletonArray, 0, expected1);
+ }
+ assertConversion(expectedSingletonArray, input1, expectedArrayType);
+
+ // array to array
+ Object inputDoubleArray = Array.newInstance(inputType, 2);
+ Array.set(inputDoubleArray, 0, input1);
+ Array.set(inputDoubleArray, 1, input2);
+ Object expectedDoubleArray;
+ if (expected1 == null && expected2 == null) {
+ expectedDoubleArray = Array.newInstance(expectedType, 0);
+ }
+ else {
+ expectedDoubleArray = Array.newInstance(expectedType, 2);
+ Array.set(expectedDoubleArray, 0, expected1);
+ Array.set(expectedDoubleArray, 1, expected2);
+ }
+ assertConversion(expectedDoubleArray, inputDoubleArray, expectedArrayType);
+
+ // array to single (first) value
+ assertConversion(expected1, inputDoubleArray, expectedType);
+
+ // null to single value
+ assertConversion(nullValue, null, expectedType);
+
+ // null to array
+ assertConversion(null, null, expectedArrayType);
+
+ // empty array to single value
+ Object inputEmptyArray = Array.newInstance(inputType, 0);
+ assertConversion(nullValue, inputEmptyArray, expectedType);
+
+ // empty array to array
+ Object expectedEmptyArray = Array.newInstance(expectedType, 0);
+ assertConversion(expectedEmptyArray, inputEmptyArray, expectedArrayType);
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T,U> void assertConversion(Object expected, Object input, Class<U> type) {
+ U result = ObjectConverter.convert(input, type);
+ String msg = "Convert '" + toString(input) + "' to " + type.getSimpleName();
+ if (expected == null) {
+ assertNull(msg, result);
+ }
+ else if (expected.getClass().isArray()) {
+ assertArrayEquals(msg, (U[])toStringIfDate(expected), (U[])toStringIfDate(result));
+ }
+ else {
+ assertEquals(msg, toStringIfDate(expected), toStringIfDate(result));
+ }
+ }
+
+ private static String toString(Object input) {
+ if (input == null) {
+ return "null";
+ }
+ else if (input.getClass().isArray()) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("[");
+ for (int i=0; i<Array.getLength(input); i++) {
+ if (i > 0) {
+ sb.append(",");
+ }
+ sb.append(toString(Array.get(input, i)));
+ }
+ sb.append("]");
+ return sb.toString();
+ }
+ else {
+ return toStringIfDate(input).toString();
+ }
+ }
+
+ private static Object toStringIfDate(Object input) {
+ if (input == null) {
+ return null;
+ }
+ if (input instanceof Calendar) {
+ return "(Calendar)" + DateUtils.calendarToString((Calendar)input);
+ }
+ if (input instanceof Date) {
+ return "(Date)" + DateUtils.dateToString((Date)input);
+ }
+ if (input.getClass().isArray()) {
+ if (Calendar.class.isAssignableFrom(input.getClass().getComponentType())
+ || input.getClass().getComponentType() == Date.class) {
+ Object[] resultArray = new String[Array.getLength(input)];
+ for (int i=0; i<Array.getLength(input); i++) {
+ resultArray[i] = toStringIfDate(Array.get(input, i));
+ }
+ return resultArray;
+ }
+ }
+ return input;
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/ObjectConverterTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/ObjectConverterTest.java
new file mode 100644
index 0000000..8b9e4cc
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/ObjectConverterTest.java
@@ -0,0 +1,233 @@
+/*
+ * 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.testing.resourceresolver;
+
+import static org.apache.sling.testing.resourceresolver.DateUtils.calendarToString;
+import static org.apache.sling.testing.resourceresolver.DateUtils.toCalendar;
+import static org.apache.sling.testing.resourceresolver.DateUtils.toDate;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNull;
+
+import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import org.junit.Test;
+
+/**
+ * This is copied from org.apache.sling.testing.resourceresolver.ObjectConverterTest
+ */
+public class ObjectConverterTest {
+
+ private static final String STRING_1 = "item1";
+ private static final String STRING_2 = "item2";
+ private static final boolean BOOLEAN_1 = true;
+ private static final boolean BOOLEAN_2 = false;
+ private static final byte BYTE_1 = (byte)0x01;
+ private static final byte BYTE_2 = (byte)0x02;
+ private static final short SHORT_1 = (short)12;
+ private static final short SHORT_2 = (short)34;
+ private static final int INT_1 = 55;
+ private static final int INT_2 = -123;
+ private static final long LONG_1 = 1234L;
+ private static final long LONG_2 = -4567L;
+ private static final float FLOAT_1 = 1.23f;
+ private static final float FLOAT_2 = -4.56f;
+ private static final double DOUBLE_1 = 12.34d;
+ private static final double DOUBLE_2 = -45.67d;
+ private static final BigDecimal BIGDECIMAL_1 = new BigDecimal("12345.67");
+ private static final BigDecimal BIGDECIMAL_2 = new BigDecimal("-23456.78");
+ private static final Calendar CALENDAR_1 = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
+ private static final Calendar CALENDAR_2 = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
+ {
+ CALENDAR_1.set(2016, 10, 15, 8, 20, 30);
+ CALENDAR_2.set(2015, 6, 31, 19, 10, 20);
+ }
+ private static final Date DATE_1 = toDate(CALENDAR_1);
+ private static final Date DATE_2 = toDate(CALENDAR_2);
+
+ @Test
+ public void testDateToString() {
+ Convert.from(STRING_1, STRING_2).to(STRING_1, STRING_2).test();
+ Convert.fromPrimitive(BOOLEAN_1, BOOLEAN_2).to(Boolean.toString(BOOLEAN_1), Boolean.toString(BOOLEAN_2)).test();
+ Convert.from(BOOLEAN_1, BOOLEAN_2).to(Boolean.toString(BOOLEAN_1), Boolean.toString(BOOLEAN_2)).test();
+ Convert.fromPrimitive(BYTE_1, BYTE_2).to(Byte.toString(BYTE_1), Byte.toString(BYTE_2)).test();
+ Convert.from(BYTE_1, BYTE_2).to(Byte.toString(BYTE_1), Byte.toString(BYTE_2)).test();
+ Convert.fromPrimitive(SHORT_1, SHORT_2).to(Short.toString(SHORT_1), Short.toString(SHORT_2)).test();
+ Convert.from(SHORT_1, SHORT_2).to(Short.toString(SHORT_1), Short.toString(SHORT_2)).test();
+ Convert.fromPrimitive(INT_1, INT_2).to(Integer.toString(INT_1), Integer.toString(INT_2)).test();
+ Convert.from(INT_1, INT_2).to(Integer.toString(INT_1), Integer.toString(INT_2)).test();
+ Convert.fromPrimitive(LONG_1, LONG_2).to(Long.toString(LONG_1), Long.toString(LONG_2)).test();
+ Convert.from(LONG_1, LONG_2).to(Long.toString(LONG_1), Long.toString(LONG_2)).test();
+ Convert.fromPrimitive(FLOAT_1, FLOAT_2).to(Float.toString(FLOAT_1), Float.toString(FLOAT_2)).test();
+ Convert.from(FLOAT_1, FLOAT_2).to(Float.toString(FLOAT_1), Float.toString(FLOAT_2)).test();
+ Convert.fromPrimitive(DOUBLE_1, DOUBLE_2).to(Double.toString(DOUBLE_1), Double.toString(DOUBLE_2)).test();
+ Convert.from(DOUBLE_1, DOUBLE_2).to(Double.toString(DOUBLE_1), Double.toString(DOUBLE_2)).test();
+ Convert.from(BIGDECIMAL_1, BIGDECIMAL_2).to(BIGDECIMAL_1.toString(), BIGDECIMAL_2.toString()).test();
+ Convert.from(CALENDAR_1, CALENDAR_2).to(calendarToString(CALENDAR_1), calendarToString(CALENDAR_2)).test();
+ Convert.from(DATE_1, DATE_2).to(calendarToString(toCalendar(DATE_1)), calendarToString(toCalendar(DATE_2))).test();
+ }
+
+ @Test
+ public void testToBoolean() {
+ Convert.fromPrimitive(BOOLEAN_1, BOOLEAN_2).to(BOOLEAN_1, BOOLEAN_2).test();
+ Convert.from(BOOLEAN_1, BOOLEAN_2).to(BOOLEAN_1, BOOLEAN_2).test();
+ Convert.from(Boolean.toString(BOOLEAN_1), Boolean.toString(BOOLEAN_2)).to(BOOLEAN_1, BOOLEAN_2).test();
+
+ // test other types that should not be converted
+ Convert.<Integer,Boolean>from(INT_1, INT_2).toNull(Boolean.class).test();
+ Convert.<Date,Boolean>from(DATE_1, DATE_2).toNull(Boolean.class).test();
+ }
+
+ @Test
+ public void testToByte() {
+ Convert.fromPrimitive(BYTE_1, BYTE_2).to(BYTE_1, BYTE_2).test();
+ Convert.from(BYTE_1, BYTE_2).to(BYTE_1, BYTE_2).test();
+ Convert.from(Byte.toString(BYTE_1), Byte.toString(BYTE_2)).to(BYTE_1, BYTE_2).test();
+
+ // test conversion from other number types
+ Convert.from(INT_1, INT_2).to((byte)INT_1, (byte)INT_2).test();
+ Convert.fromPrimitive(INT_1, INT_2).to((byte)INT_1, (byte)INT_2).test();
+
+ // test other types that should not be converted
+ Convert.<Date,Byte>from(DATE_1, DATE_2).toNull(Byte.class).test();
+ }
+
+ @Test
+ public void testToShort() {
+ Convert.fromPrimitive(SHORT_1, SHORT_2).to(SHORT_1, SHORT_2).test();
+ Convert.from(SHORT_1, SHORT_2).to(SHORT_1, SHORT_2).test();
+ Convert.from(Short.toString(SHORT_1), Short.toString(SHORT_2)).to(SHORT_1, SHORT_2).test();
+
+ // test conversion from other number types
+ Convert.from(INT_1, INT_2).to((short)INT_1, (short)INT_2).test();
+ Convert.fromPrimitive(INT_1, INT_2).to((short)INT_1, (short)INT_2).test();
+
+ // test other types that should not be converted
+ Convert.<Date,Short>from(DATE_1, DATE_2).toNull(Short.class).test();
+ }
+
+ @Test
+ public void testToInteger() {
+ Convert.fromPrimitive(INT_1, INT_2).to(INT_1, INT_2).test();
+ Convert.from(INT_1, INT_2).to(INT_1, INT_2).test();
+ Convert.from(Integer.toString(INT_1), Integer.toString(INT_2)).to(INT_1, INT_2).test();
+
+ // test conversion from other number types
+ Convert.from(SHORT_1, SHORT_2).to((int)SHORT_1, (int)SHORT_2).test();
+ Convert.fromPrimitive(SHORT_1, SHORT_2).to((int)SHORT_1, (int)SHORT_2).test();
+
+ // test other types that should not be converted
+ Convert.<Date,Integer>from(DATE_1, DATE_2).toNull(Integer.class).test();
+ }
+
+ @Test
+ public void testToLong() {
+ Convert.fromPrimitive(LONG_1, LONG_2).to(LONG_1, LONG_2).test();
+ Convert.from(LONG_1, LONG_2).to(LONG_1, LONG_2).test();
+ Convert.from(Long.toString(LONG_1), Long.toString(LONG_2)).to(LONG_1, LONG_2).test();
+
+ // test conversion from other number types
+ Convert.from(SHORT_1, SHORT_2).to((long)SHORT_1, (long)SHORT_2).test();
+ Convert.fromPrimitive(SHORT_1, SHORT_2).to((long)SHORT_1, (long)SHORT_2).test();
+
+ // test other types that should not be converted
+ Convert.<Date,Long>from(DATE_1, DATE_2).toNull(Long.class).test();
+ }
+
+ @Test
+ public void testToFloat() {
+ Convert.fromPrimitive(FLOAT_1, FLOAT_2).to(FLOAT_1, FLOAT_2).test();
+ Convert.from(FLOAT_1, FLOAT_2).to(FLOAT_1, FLOAT_2).test();
+ Convert.from(Float.toString(FLOAT_1), Float.toString(FLOAT_2)).to(FLOAT_1, FLOAT_2).test();
+
+ // test conversion from other number types
+ Convert.from(SHORT_1, SHORT_2).to((float)SHORT_1, (float)SHORT_2).test();
+ Convert.fromPrimitive(SHORT_1, SHORT_2).to((float)SHORT_1, (float)SHORT_2).test();
+
+ // test other types that should not be converted
+ Convert.<Date,Float>from(DATE_1, DATE_2).toNull(Float.class).test();
+ }
+
+ @Test
+ public void testToDouble() {
+ Convert.fromPrimitive(DOUBLE_1, DOUBLE_2).to(DOUBLE_1, DOUBLE_2).test();
+ Convert.from(DOUBLE_1, DOUBLE_2).to(DOUBLE_1, DOUBLE_2).test();
+ Convert.from(Double.toString(DOUBLE_1), Double.toString(DOUBLE_2)).to(DOUBLE_1, DOUBLE_2).test();
+
+ // test conversion from other number types
+ Convert.from(SHORT_1, SHORT_2).to((double)SHORT_1, (double)SHORT_2).test();
+ Convert.fromPrimitive(SHORT_1, SHORT_2).to((double)SHORT_1, (double)SHORT_2).test();
+
+ // test other types that should not be converted
+ Convert.<Date,Double>from(DATE_1, DATE_2).toNull(Double.class).test();
+ }
+
+ @Test
+ public void testToBigDecimal() {
+ Convert.from(BIGDECIMAL_1, BIGDECIMAL_2).to(BIGDECIMAL_1, BIGDECIMAL_2).test();
+ Convert.from(BIGDECIMAL_1.toString(), BIGDECIMAL_2.toString()).to(BIGDECIMAL_1, BIGDECIMAL_2).test();
+
+ // test conversion from other number types
+ Convert.from(LONG_1, LONG_2).to(BigDecimal.valueOf(LONG_1), BigDecimal.valueOf(LONG_2)).test();
+ Convert.fromPrimitive(LONG_1, LONG_2).to(BigDecimal.valueOf(LONG_1), BigDecimal.valueOf(LONG_2)).test();
+ Convert.from(DOUBLE_1, DOUBLE_2).to(BigDecimal.valueOf(DOUBLE_1), BigDecimal.valueOf(DOUBLE_2)).test();
+ Convert.fromPrimitive(DOUBLE_1, DOUBLE_2).to(BigDecimal.valueOf(DOUBLE_1), BigDecimal.valueOf(DOUBLE_2)).test();
+
+ // test other types that should not be converted
+ Convert.<Date,BigDecimal>from(DATE_1, DATE_2).toNull(BigDecimal.class).test();
+ }
+
+ @Test
+ public void testToCalendar() {
+ Convert.from(CALENDAR_1, CALENDAR_2).to(CALENDAR_1, CALENDAR_2).test();
+ Convert.from(DateUtils.calendarToString(CALENDAR_1), DateUtils.calendarToString(CALENDAR_2)).to(CALENDAR_1, CALENDAR_2).test();
+
+ // test conversion from other date types
+ Convert.from(DATE_1, DATE_2).to(toCalendar(DATE_1), toCalendar(DATE_2)).test();
+
+ // test other types that should not be converted
+ Convert.<String,Calendar>from(STRING_1, STRING_2).toNull(Calendar.class).test();
+ Convert.<Boolean,Calendar>from(BOOLEAN_1, BOOLEAN_2).toNull(Calendar.class).test();
+ }
+
+ @Test
+ public void testToDate() {
+ Convert.from(DATE_1, DATE_2).to(DATE_1, DATE_2).test();
+ Convert.from(DateUtils.dateToString(DATE_1), DateUtils.dateToString(DATE_2)).to(DATE_1, DATE_2).test();
+
+ // test conversion from other date types
+ Convert.from(CALENDAR_1, CALENDAR_2).to(toDate(CALENDAR_1), toDate(CALENDAR_2)).test();
+
+ // test other types that should not be converted
+ Convert.<String,Date>from(STRING_1, STRING_2).toNull(Date.class).test();
+ Convert.<Boolean,Date>from(BOOLEAN_1, BOOLEAN_2).toNull(Date.class).test();
+ }
+
+ @Test
+ public void testPrimitiveByteArray() {
+ byte[] array = new byte[] { 0x01, 0x02, 0x03 };
+ assertArrayEquals(array, ObjectConverter.convert(array, byte[].class));
+ assertArrayEquals(new byte[0], ObjectConverter.convert(new byte[0], byte[].class));
+ assertNull(ObjectConverter.convert(null, byte[].class));
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
index 9e4a350..3cd8007 100644
--- a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
@@ -43,7 +43,6 @@ import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
import com.google.common.collect.ImmutableMap;
@@ -127,12 +126,10 @@ public class SlingCrudResourceResolverTest {
}
@Test
- @Ignore // TODO: enable this test when resourceresolver-mock implementation supports BigDecimal conversion (SLING-6416)
public void testSimpleProperties_DecimalConversion() throws IOException {
Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
ValueMap props = ResourceUtil.getValueMap(resource1);
- // TODO: enable this test when resourceresolver-mock implementation supports BigDecimal conversion (SLING-6416)
assertEquals(new BigDecimal(DOUBLE_VALUE).doubleValue(), props.get("doubleProp", BigDecimal.class).doubleValue(), 0.0001d);
assertEquals(BIGDECIMAL_VALUE.doubleValue() , props.get("bigDecimalProp", Double.class), 0.0001d);
}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapDecoratorTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapDecoratorTest.java
new file mode 100644
index 0000000..0b78f50
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/ValueMapDecoratorTest.java
@@ -0,0 +1,173 @@
+/*
+ * 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.testing.resourceresolver;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.api.resource.ValueMap;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * This is copied from org.apache.sling.api.wrappers.ValueMapDecoratorTest
+ */
+public class ValueMapDecoratorTest {
+
+ private Map<String, Object> map;
+ private ValueMap valueMap;
+
+ @Before
+ public void setUp() {
+ map = new HashMap<String, Object>();
+ valueMap = new ValueMapDecorator(map);
+ }
+
+ // SLING-4178
+ @Test
+ public void testIncompatibleTypeInArray() {
+ map.put("prop1", new String[] { "test", "test2" });
+ map.put("prop2", "test");
+ Assert.assertArrayEquals("Not convertible type should return empty array", new Integer[0], valueMap.get("prop1", Integer[].class));
+ Assert.assertArrayEquals("Not convertible type should return empt array", new Integer[0], valueMap.get("prop2", Integer[].class));
+ }
+
+ // SLING-662
+ @Test
+ public void testGettingArraysFromSingleValueEntries() {
+ map.put("prop1", "test");
+ map.put("prop2", "1");
+ Assert.assertArrayEquals(
+ "Even though the underlying entry is single-value if should be enclosed in a single element array",
+ new String[] { "test" }, valueMap.get("prop1", String[].class));
+ Assert.assertArrayEquals(
+ "Even though the underlying entry is single-value if should be enclosed in a single element array",
+ new Integer[] { 1 }, valueMap.get("prop2", Integer[].class));
+ }
+
+ @Test
+ public void testGettingArraysFromMultiValueEntries() {
+ map.put("prop1", new String[] { "test", "test2" });
+ map.put("prop2", new String[] { "1", "2" });
+ Assert.assertArrayEquals("Could not get values from underlying array", new String[] { "test", "test2" },
+ valueMap.get("prop1", String[].class));
+ Assert.assertArrayEquals("Conversion to Integer was not possible", new Integer[] { 1, 2 },
+ valueMap.get("prop2", Integer[].class));
+ }
+
+ @Test
+ public void testGettingSingleValuesFromMultiValueEntries() {
+ map.put("prop1", new String[] { "test", "test2" });
+ map.put("prop2", new String[] { "1", "2" });
+ Assert.assertEquals("First element from underlying array should be returned", "test",
+ valueMap.get("prop1", String.class));
+ Assert.assertEquals("First element from underlying array should be returned", Integer.valueOf(1),
+ valueMap.get("prop2", Integer.class));
+ }
+
+ @Test
+ public void testGettingInvalidEntryWithDefaultValue() {
+ Assert.assertEquals(Integer.valueOf(1), valueMap.get("prop1", 1));
+ Assert.assertEquals("test", valueMap.get("prop1", "test"));
+ }
+
+ @Test
+ public void testPrimitiveTypes() {
+ map.put("prop1", new String[] { "1", "2" });
+ Assert.assertNull("ValueMap should not support conversion to primitive type", valueMap.get("prop1", int.class));
+ }
+ @Test(expected=ClassCastException.class)
+ public void testPrimitiveTypesArray() {
+ map.put("prop1", new String[] { "1", "2" });
+ Assert.assertArrayEquals("ValueMap should not support conversion to array of primitive type",
+ new int[0], valueMap.get("prop1", int[].class));
+ }
+
+ @Test
+ public void testEqualsAndHashCodeOfEqualValueMapsWithNonArrayTypes() {
+ map.put("prop1", "some string");
+ ValueMapDecorator valueMap2 = new ValueMapDecorator(map);
+ Assert.assertTrue("Two ValueMapDecorators based on the same map should be equal", valueMap.equals(valueMap2));
+ Assert.assertEquals("Two equal ValueMapDecorators should have the same hash code", valueMap.hashCode(),
+ valueMap2.hashCode());
+
+ ValueMapDecorator valueMap3 = new ValueMapDecorator(new HashMap<String, Object>());
+ valueMap3.put("prop1", "some string");
+ Assert.assertEquals(valueMap, valueMap3);
+ Assert.assertEquals("Two equal ValueMapDecorators should have the same hash code", valueMap.hashCode(),
+ valueMap3.hashCode());
+
+ Assert.assertEquals(map, valueMap);
+ Assert.assertEquals(valueMap, map);
+ }
+
+ @Ignore("SLING-4784")
+ @Test
+ public void testEqualsAndHashCodeOfEqualValueMapsWithArrayTypes() {
+ map.put("prop1", new String[] { "1", "2" });
+ ValueMapDecorator valueMap2 = new ValueMapDecorator(map);
+ Assert.assertTrue("Two ValueMapDecorators based on the same map should be equal", valueMap.equals(valueMap2));
+ Assert.assertEquals("Two equal ValueMapDecorators should have the same hash code", valueMap.hashCode(),
+ valueMap2.hashCode());
+
+ ValueMapDecorator valueMap3 = new ValueMapDecorator(new HashMap<String, Object>());
+ valueMap3.put("prop1", new String[] { "1", "2" });
+ Assert.assertEquals(valueMap, valueMap3);
+ Assert.assertEquals("Two equal ValueMapDecorators should have the same hash code", valueMap.hashCode(),
+ valueMap3.hashCode());
+ }
+
+ @Test
+ public void testEqualsOfInequalValueMapsWithNonArrayTypes() {
+ valueMap.put("prop", "value");
+ ValueMapDecorator valueMap2 = new ValueMapDecorator(new HashMap<String, Object>());
+ valueMap2.put("prop", "value2");
+ Assert.assertFalse("Two ValueMapDecorators based on maps with different entries should not be equal",
+ valueMap.equals(valueMap2));
+
+ }
+
+ @Test
+ public void testEqualsOfInequalValueMapsWithArrayTypes() {
+ valueMap.put("prop", new String[] { "1", "2" });
+ ValueMapDecorator valueMap2 = new ValueMapDecorator(new HashMap<String, Object>());
+ valueMap2.put("prop", new String[] { "3", "4" });
+ Assert.assertFalse("Two ValueMapDecorators based on maps with different entries should not be equal",
+ valueMap.equals(valueMap2));
+ }
+
+ @Test
+ public void testDelegateToValueMap() {
+ ValueMap original = mock(ValueMap.class);
+ ValueMap decorated = new ValueMapDecorator(original);
+
+ decorated.get("prop1", String.class);
+ verify(original, times(1)).get("prop1", String.class);
+
+ decorated.get("prop1", "defValue");
+ verify(original, times(1)).get("prop1", "defValue");
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.