Bert Leunis pushed to branch feature/CHANNELMGR-1388 at cms-community / 
hippo-addon-channel-manager


Commits:
70c3ca73 by Bert Leunis at 2018-03-05T13:31:54+01:00
CHANNELMGR-1391 boolean field type now correctly treats invalid input

The Boolean#parseBoolean() method regards any input not true or TRUE as false. 
We need an explicit test on the input string.

- - - - -


2 changed files:

- 
content-service/src/main/java/org/onehippo/cms/channelmanager/content/documenttype/field/type/BooleanFieldType.java
- 
content-service/src/test/java/org/onehippo/cms/channelmanager/content/documenttype/field/type/BooleanFieldTypeTest.java


Changes:

=====================================
content-service/src/main/java/org/onehippo/cms/channelmanager/content/documenttype/field/type/BooleanFieldType.java
=====================================
--- 
a/content-service/src/main/java/org/onehippo/cms/channelmanager/content/documenttype/field/type/BooleanFieldType.java
+++ 
b/content-service/src/main/java/org/onehippo/cms/channelmanager/content/documenttype/field/type/BooleanFieldType.java
@@ -17,10 +17,6 @@
 package org.onehippo.cms.channelmanager.content.documenttype.field.type;
 
 import javax.jcr.PropertyType;
-import javax.jcr.ValueFormatException;
-
-import org.onehippo.cms.channelmanager.content.error.BadRequestException;
-import org.onehippo.cms.channelmanager.content.error.ErrorWithPayloadException;
 
 public class BooleanFieldType extends PrimitiveFieldType {
     private static final String DEFAULT_VALUE = "false";
@@ -41,6 +37,9 @@ public class BooleanFieldType extends PrimitiveFieldType {
 
     @Override
     protected String fieldSpecificConversion(final String input) {
+        if (!"true".equalsIgnoreCase(input) && 
!"false".equalsIgnoreCase(input)) {
+            throw new IllegalArgumentException("BooleanFieldType value must be 
'true' or 'false'.");
+        }
         return Boolean.parseBoolean(input) + "";
     }
 }


=====================================
content-service/src/test/java/org/onehippo/cms/channelmanager/content/documenttype/field/type/BooleanFieldTypeTest.java
=====================================
--- 
a/content-service/src/test/java/org/onehippo/cms/channelmanager/content/documenttype/field/type/BooleanFieldTypeTest.java
+++ 
b/content-service/src/test/java/org/onehippo/cms/channelmanager/content/documenttype/field/type/BooleanFieldTypeTest.java
@@ -22,6 +22,8 @@ import java.util.List;
 import java.util.Optional;
 
 import javax.jcr.Node;
+import javax.jcr.PropertyType;
+import javax.jcr.Value;
 
 import org.hippoecm.repository.util.JcrUtils;
 import org.junit.Before;
@@ -91,6 +93,43 @@ public class BooleanFieldTypeTest {
         assertThat(node.getProperty(PROPERTY).getString(), equalTo("" + 
newValue));
     }
 
+    @Test
+    public void writeIncorrectValueDoesNotOverwriteExistingValue() throws 
Exception {
+        final Node node = MockNode.root();
+        final PrimitiveFieldType fieldType = new BooleanFieldType();
+        final Boolean oldValue = Boolean.TRUE;
+        final String invalidValue = "foo";
+
+        fieldType.setId(PROPERTY);
+        node.setProperty(PROPERTY, oldValue);
+
+        fieldType.writeTo(node, Optional.of(listOf(valueOf(invalidValue))));
+        assertThat(node.getProperty(PROPERTY).getBoolean(), equalTo(oldValue));
+    }
+
+    @Test
+    public void writeIncorrectValuesDoesNotOverwriteExistingValues() throws 
Exception {
+        final Node node = MockNode.root();
+        final PrimitiveFieldType fieldType = new BooleanFieldType();
+
+        fieldType.setId(PROPERTY);
+        fieldType.setMultiple(true);
+        fieldType.setMaxValues(2);
+
+        final Boolean oldValue1 = Boolean.TRUE;
+        final Boolean oldValue2 = Boolean.TRUE;
+        node.setProperty(PROPERTY, new String[] {oldValue1 + "", oldValue2 + 
""}, PropertyType.BOOLEAN);
+
+        final String invalidValue1 = "foo";
+        final List<FieldValue> es = Arrays.asList(valueOf(invalidValue1), 
valueOf(oldValue2 + ""));
+        fieldType.writeTo(node, Optional.of(es));
+
+        final Value[] values = node.getProperty(PROPERTY).getValues();
+        assertThat(values[0].getBoolean(), equalTo(oldValue1));
+        assertThat(values[1].getBoolean(), equalTo(oldValue2));
+    }
+
+
     private static List<FieldValue> listOf(final FieldValue value) {
         return Collections.singletonList(value);
     }



View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-addon-channel-manager/commit/70c3ca73ab5c449247416a8749b41fab564024e8

---
View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-addon-channel-manager/commit/70c3ca73ab5c449247416a8749b41fab564024e8
You're receiving this email because of your account on code.onehippo.org.
_______________________________________________
Hippocms-svn mailing list
Hippocms-svn@lists.onehippo.org
https://lists.onehippo.org/mailman/listinfo/hippocms-svn

Reply via email to