You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2019/07/22 09:08:39 UTC
[sling-org-apache-sling-contentparser-api] 02/11: SLING-8570 -
Extract a generic Content Parser API from
org.apache.sling.jcr.contentparser with pluggable implementations
This is an automated email from the ASF dual-hosted git repository.
radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-contentparser-api.git
commit 7538f20553a6c7a842fb2edcd64f5c8f806669da
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Thu Jul 11 15:07:22 2019 +0200
SLING-8570 - Extract a generic Content Parser API from org.apache.sling.jcr.contentparser with pluggable implementations
* corrected some typos
* added more unit tests
---
pom.xml | 2 +-
.../sling/contentparser/api/ParserHelper.java | 4 +-
.../sling/contentparser/api/ParserHelperTest.java | 47 +++++++++++++++++++++-
3 files changed, 49 insertions(+), 4 deletions(-)
diff --git a/pom.xml b/pom.xml
index 799b201..bdb4bab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,7 +31,7 @@
<name>Apache Sling Content Parser API</name>
<description>
- Parser API Apache Sling Resource trees stored in files (e.g. JSON, FileVault XML, etc.).
+ API for parsing Apache Sling Resource trees stored in files (e.g. JSON, FileVault XML, etc.).
</description>
<scm>
diff --git a/src/main/java/org/apache/sling/contentparser/api/ParserHelper.java b/src/main/java/org/apache/sling/contentparser/api/ParserHelper.java
index 7cb1196..ca7f2ca 100644
--- a/src/main/java/org/apache/sling/contentparser/api/ParserHelper.java
+++ b/src/main/java/org/apache/sling/contentparser/api/ParserHelper.java
@@ -74,6 +74,8 @@ public final class ParserHelper {
*
* @param values the multi-value property's values
* @return an object representation of the multi-value property
+ * @throws ParseException when the provided {@code values} array contains null values, {@link Map} values, or the values are not of the
+ * same type
*/
public static Object convertSingleTypeArray(Object[] values) {
if (values.length == 0) {
@@ -90,7 +92,7 @@ public final class ParserHelper {
if (itemType == null) {
itemType = value.getClass();
} else if (itemType != value.getClass()) {
- throw new ParseException("Multivalue array must not contain values with different types "
+ throw new ParseException("Multi-value array must not contain values with different types "
+ "(" + itemType.getName() + ", " + value.getClass().getName() + ").");
}
}
diff --git a/src/test/java/org/apache/sling/contentparser/api/ParserHelperTest.java b/src/test/java/org/apache/sling/contentparser/api/ParserHelperTest.java
index 2b2da48..7f57ff3 100644
--- a/src/test/java/org/apache/sling/contentparser/api/ParserHelperTest.java
+++ b/src/test/java/org/apache/sling/contentparser/api/ParserHelperTest.java
@@ -19,13 +19,17 @@
package org.apache.sling.contentparser.api;
import java.util.Calendar;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
-import static junit.framework.TestCase.assertEquals;
-import static junit.framework.TestCase.assertNull;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
public class ParserHelperTest {
@@ -54,4 +58,43 @@ public class ParserHelperTest {
}
}
}
+
+ @Test
+ public void convertSingleTypeArray() {
+ Object[] empty = new Object[]{};
+ assertEquals(empty, ParserHelper.convertSingleTypeArray(empty));
+
+ Object[] nullValues = new Object[] {"string", null};
+ ParseException nullValuesException = null;
+ try {
+ ParserHelper.convertSingleTypeArray(nullValues);
+ } catch (ParseException e) {
+ nullValuesException = e;
+ }
+ assertNotNull("Expected a ParseException when the Object array contains multiple types.", nullValuesException);
+
+ Object[] maps = new Object[] {Collections.emptyMap()};
+ ParseException mapsException = null;
+ try {
+ ParserHelper.convertSingleTypeArray(maps);
+ } catch (ParseException e) {
+ mapsException = e;
+ }
+ assertNotNull("Expected a ParseException when the Object array contains Map objects.", mapsException);
+
+ Object[] differentTypes = new Object[] {"string", 1, 1L, 1F, Boolean.TRUE};
+ ParseException differentTypesException = null;
+ try {
+ ParserHelper.convertSingleTypeArray(differentTypes);
+ } catch (ParseException e) {
+ differentTypesException = e;
+ }
+ assertNotNull("Expected a ParseException when the Object array contains multiple types.", differentTypesException);
+
+ Object[] values = new Object[] {1, 2, 3, 4, 5};
+ Object result = ParserHelper.convertSingleTypeArray(values);
+ assertTrue("Expected the resulting object to be an Integer array.", result instanceof Integer[]);
+ Integer[] typedResult = (Integer[]) result;
+ assertArrayEquals("Expected the arrays to be equal.", new Integer[] {1, 2, 3, 4, 5}, typedResult);
+ }
}