@Test public void testConvert() throws Exception { System.out.println("convert"); BooleanField instance = new BooleanField(); assertTrue(instance.convert("true")); assertFalse(instance.convert("false")); assertTrue(instance.convert(true)); assertFalse(instance.convert(false)); JSONObject jsonObject = new JSONObject(); jsonObject.put("true-key", true); jsonObject.put("false-key", false); assertTrue(instance.convert(jsonObject.get("true-key"))); assertFalse(instance.convert(jsonObject.get("false-key"))); try { instance.convert(null); fail("null incorrectly accepted as a valid input"); } catch(OperationException ex) { assertEquals(ErrorCode.DATATYPE_MISMATCH, ex.getErrorCode()); } try { instance.convert("something"); fail("incorrectly accepted a non boolean string as a value boolean value"); } catch(OperationException ex) { assertEquals(ErrorCode.DATATYPE_MISMATCH, ex.getErrorCode()); } try { instance.convert(new Integer(0)); fail("incorrectly accepted an Integer as a valid Boolean value"); } catch(OperationException ex) { assertEquals(ErrorCode.DATATYPE_MISMATCH, ex.getErrorCode()); } }
@Override public Boolean convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { switch (objValue.toString().toLowerCase()) { case "true": return true; case "false": return false; default: throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue.toString() + " to a boolean value"); } } else if (objValue instanceof Boolean) { return (Boolean) objValue; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot convert " + objValue + " to a valid boolean type"); }
BooleanField implements FieldType<Boolean> { @Override public Boolean convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { switch (objValue.toString().toLowerCase()) { case "true": return true; case "false": return false; default: throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue.toString() + " to a boolean value"); } } else if (objValue instanceof Boolean) { return (Boolean) objValue; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot convert " + objValue + " to a valid boolean type"); } }
BooleanField implements FieldType<Boolean> { @Override public Boolean convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { switch (objValue.toString().toLowerCase()) { case "true": return true; case "false": return false; default: throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue.toString() + " to a boolean value"); } } else if (objValue instanceof Boolean) { return (Boolean) objValue; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot convert " + objValue + " to a valid boolean type"); } BooleanField();  BooleanField(final Boolean defaultValue); }
BooleanField implements FieldType<Boolean> { @Override public Boolean convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { switch (objValue.toString().toLowerCase()) { case "true": return true; case "false": return false; default: throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue.toString() + " to a boolean value"); } } else if (objValue instanceof Boolean) { return (Boolean) objValue; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot convert " + objValue + " to a valid boolean type"); } BooleanField();  BooleanField(final Boolean defaultValue); @Override Types getType(); @Override Boolean getDefaultValue(); @Override boolean hasDefaultValue(); @Override Boolean convert(Object objValue); @Override JSONObject toJson(); }
BooleanField implements FieldType<Boolean> { @Override public Boolean convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { switch (objValue.toString().toLowerCase()) { case "true": return true; case "false": return false; default: throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue.toString() + " to a boolean value"); } } else if (objValue instanceof Boolean) { return (Boolean) objValue; } throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot convert " + objValue + " to a valid boolean type"); } BooleanField();  BooleanField(final Boolean defaultValue); @Override Types getType(); @Override Boolean getDefaultValue(); @Override boolean hasDefaultValue(); @Override Boolean convert(Object objValue); @Override JSONObject toJson();  }