SLING-9171 - use an enum for property value types
diff --git a/src/main/java/org/apache/sling/repoinit/parser/operations/PropertyLine.java b/src/main/java/org/apache/sling/repoinit/parser/operations/PropertyLine.java
index 3ff5924..fff4e7f 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/operations/PropertyLine.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/operations/PropertyLine.java
@@ -17,38 +17,56 @@
package org.apache.sling.repoinit.parser.operations;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
/** A single "set property" line */
public class PropertyLine {
private final String name;
- private final String type;
+ private final PropertyType propertyType;
private final List<String> values;
private boolean isDefault = false;
+ /** Valid types for these properties */
+ public static enum PropertyType {
+ String,
+ Long,
+ Double,
+ Date,
+ Boolean
+ }
+
/**
- * Operation that sets property on a node.
+ * Stores data for one line of a "set property" block
* @param name name of the property
- * @param type property type
+ * @param typeString property type, as a String
* @param values values of the property
*/
- public PropertyLine(String name, String type, List<String> values, boolean isDefault) {
+ public PropertyLine(String name, String typeString, List<String> values, boolean isDefault) {
this.name = name;
- this.type = type;
- this.values = values;
+ this.propertyType = typeString == null ? PropertyType.String : PropertyType.valueOf(typeString);
+ this.values = values == null ? new ArrayList<>() : values;
this.isDefault = isDefault;
}
+ /** @return the name of the property to set */
public String getPropertyName() {return name;};
- public String getPropertyType() {return type;};
+ /** @return the type of the property to set */
+ public PropertyType getPropertyType() {return propertyType;};
+ /** @return the list ot values of the property to set */
public List<String> getPropertyValues() {
- return values;
+ return Collections.unmodifiableList(values);
}
+ /** True if this line is a "default" as opposed to a "set" instruction.
+ * @return true if a previously existing value of this property is kept, instead
+ * of being overwritten like a "set" instruction does
+ */
public boolean isDefault() { return isDefault; }
@Override
@@ -59,15 +77,14 @@
if(isDefault()) {
sb.append("default ");
}
+
sb.append(name);
sb.append("=");
- if (type != null) {
- sb.append("{");
- sb.append(type);
- sb.append("}");
- }
+ sb.append("{");
+ sb.append(propertyType.toString());
+ sb.append("}");
sb.append(values);
return sb.toString();
}
-}
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
index 81c58bc..ad65548 100644
--- a/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
@@ -113,7 +113,7 @@
add(new Object[] { "set properties on appsWithoutSlash \n set sling:ResourceType{String} to /x/y/z \n end", ParseException.class });
add(new Object[] { "set properties on /apps \n set dob{Date} to 13-10-2019 \n end", ParseException.class });
add(new Object[] { "set properties on /pathA/b \n set someProp{inValidType} to abc \n end", ParseException.class });
- add(new Object[] { "set properties on /pathA/b \n set smallcasetype{string} to abc \n end", ParseException.class });
+ add(new Object[] { "set properties on /pathA/b \n set lowercasetype{string} to abc \n end", ParseException.class });
add(new Object[] { "set properties on /pathA/b \n set {String} to missingPropertyName \n end", ParseException.class });
add(new Object[] { "set properties on /pathA/b \n set somepProp{String} withoutTo \n end", ParseException.class });
}};
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/PropertyLineTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/PropertyLineTest.java
new file mode 100644
index 0000000..3f401c7
--- /dev/null
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/PropertyLineTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.repoinit.parser.test;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.sling.repoinit.parser.operations.PropertyLine;
+import org.junit.Test;
+
+public class PropertyLineTest {
+
+ @Test
+ public void testDefaultPropertyType() {
+ final PropertyLine p = new PropertyLine("someName", null, null, false);
+ assertEquals(PropertyLine.PropertyType.String, p.getPropertyType());
+ }
+
+ @Test
+ public void testValidPropertyType() {
+ final PropertyLine p = new PropertyLine("someName", "Boolean", null, false);
+ assertEquals(PropertyLine.PropertyType.Boolean, p.getPropertyType());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidPropertyType() {
+ new PropertyLine("someName", "invalidTypeName", null, false);
+ }
+}