@Test public void testConvert() throws OperationException, JSONException { System.out.println("convert"); final byte[] expResult; byte[] result; final String sampleText = UUID.randomUUID().toString(); BinaryField instance = new BinaryField(Types.BLOB); JSONObject jsonObject = new JSONObject(); jsonObject.put("bytes", sampleText.getBytes()); expResult = sampleText.getBytes(); result = instance.convert(jsonObject.get("bytes")); assertArrayEquals(expResult, result); result = instance.convert(sampleText.getBytes()); assertArrayEquals(expResult, result); instance = new BinaryField(Types.BLOB, "36"); result = instance.convert(jsonObject.get("bytes")); assertArrayEquals(expResult, result); result = instance.convert(sampleText.getBytes()); assertArrayEquals(expResult, result); instance = new BinaryField(Types.BLOB, "35"); try { instance.convert(jsonObject.get("bytes")); fail("Length constraint violation not reported in the form of an OperationException"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, ex.getErrorCode()); } try { instance.convert(sampleText.getBytes()); fail("Length constraint violation not reported in the form of an OperationException"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, ex.getErrorCode()); } try { instance.convert(null); fail("null value not checked"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_MISMATCH, ex.getErrorCode()); } }
@Override public byte[] convert(Object objValue) throws OperationException { byte[] bytes; JSONArray jsonArray; if(objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof byte[]) { bytes = (byte[]) objValue; if (getLength() > 0 && bytes.length > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + bytes.length); } return bytes; } if (!(objValue instanceof JSONArray)) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not parse " + objValue + " to a valid byte []"); } jsonArray = (JSONArray) objValue; if (getLength() > 0 && jsonArray.length() > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + jsonArray.length()); } bytes = new byte[jsonArray.length()]; final int arrayLength = jsonArray.length(); for (int i = 0; i < arrayLength; i++) { try { bytes[i] = new Byte(jsonArray.getString(i)); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue + " to a byte []. " + "Failure at index " + i + " for casting " + jsonArray.getString(i) + " to a byte"); } } return bytes; }
BinaryField implements FieldType<byte[]> { @Override public byte[] convert(Object objValue) throws OperationException { byte[] bytes; JSONArray jsonArray; if(objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof byte[]) { bytes = (byte[]) objValue; if (getLength() > 0 && bytes.length > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + bytes.length); } return bytes; } if (!(objValue instanceof JSONArray)) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not parse " + objValue + " to a valid byte []"); } jsonArray = (JSONArray) objValue; if (getLength() > 0 && jsonArray.length() > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + jsonArray.length()); } bytes = new byte[jsonArray.length()]; final int arrayLength = jsonArray.length(); for (int i = 0; i < arrayLength; i++) { try { bytes[i] = new Byte(jsonArray.getString(i)); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue + " to a byte []. " + "Failure at index " + i + " for casting " + jsonArray.getString(i) + " to a byte"); } } return bytes; } }
BinaryField implements FieldType<byte[]> { @Override public byte[] convert(Object objValue) throws OperationException { byte[] bytes; JSONArray jsonArray; if(objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof byte[]) { bytes = (byte[]) objValue; if (getLength() > 0 && bytes.length > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + bytes.length); } return bytes; } if (!(objValue instanceof JSONArray)) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not parse " + objValue + " to a valid byte []"); } jsonArray = (JSONArray) objValue; if (getLength() > 0 && jsonArray.length() > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + jsonArray.length()); } bytes = new byte[jsonArray.length()]; final int arrayLength = jsonArray.length(); for (int i = 0; i < arrayLength; i++) { try { bytes[i] = new Byte(jsonArray.getString(i)); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue + " to a byte []. " + "Failure at index " + i + " for casting " + jsonArray.getString(i) + " to a byte"); } } return bytes; } BinaryField(Types type);  BinaryField(Types type, String lengthString); }
BinaryField implements FieldType<byte[]> { @Override public byte[] convert(Object objValue) throws OperationException { byte[] bytes; JSONArray jsonArray; if(objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof byte[]) { bytes = (byte[]) objValue; if (getLength() > 0 && bytes.length > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + bytes.length); } return bytes; } if (!(objValue instanceof JSONArray)) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not parse " + objValue + " to a valid byte []"); } jsonArray = (JSONArray) objValue; if (getLength() > 0 && jsonArray.length() > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + jsonArray.length()); } bytes = new byte[jsonArray.length()]; final int arrayLength = jsonArray.length(); for (int i = 0; i < arrayLength; i++) { try { bytes[i] = new Byte(jsonArray.getString(i)); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue + " to a byte []. " + "Failure at index " + i + " for casting " + jsonArray.getString(i) + " to a byte"); } } return bytes; } BinaryField(Types type);  BinaryField(Types type, String lengthString); @Override Types getType(); long getLength(); @Override byte[] getDefaultValue(); @Override boolean hasDefaultValue(); @Override byte[] convert(Object objValue); @Override JSONObject toJson(); }
BinaryField implements FieldType<byte[]> { @Override public byte[] convert(Object objValue) throws OperationException { byte[] bytes; JSONArray jsonArray; if(objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof byte[]) { bytes = (byte[]) objValue; if (getLength() > 0 && bytes.length > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + bytes.length); } return bytes; } if (!(objValue instanceof JSONArray)) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Could not parse " + objValue + " to a valid byte []"); } jsonArray = (JSONArray) objValue; if (getLength() > 0 && jsonArray.length() > getLength()) { throw new OperationException(ErrorCode.DATATYPE_CONSTRAINT_VIOLATION, "Maximum permitted length " + getLength() + ", but found data of length " + jsonArray.length()); } bytes = new byte[jsonArray.length()]; final int arrayLength = jsonArray.length(); for (int i = 0; i < arrayLength; i++) { try { bytes[i] = new Byte(jsonArray.getString(i)); } catch (NumberFormatException ex) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast " + objValue + " to a byte []. " + "Failure at index " + i + " for casting " + jsonArray.getString(i) + " to a byte"); } } return bytes; } BinaryField(Types type);  BinaryField(Types type, String lengthString); @Override Types getType(); long getLength(); @Override byte[] getDefaultValue(); @Override boolean hasDefaultValue(); @Override byte[] convert(Object objValue); @Override JSONObject toJson();  }