@Test public void testConvert() throws Exception { System.out.println("convert"); CollectionField instance; List<Integer> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { list.add(i); } instance = new CollectionField(Types.ARRAY, Types.INT); Object convertedObj = instance.convert(list); if(!(convertedObj instanceof List)) { fail("convert method giving a non List object for an ARRAY collection"); } instance = new CollectionField(Types.MULTISET, Types.INT); convertedObj = instance.convert(list); if(!(convertedObj instanceof Set)) { fail("convert method giving a non Set object for an MULTISET collection. Got: " + convertedObj.getClass().getName()); } JSONArray jsonArray = new JSONArray(); for (int i = 0; i < 10; i++) { jsonArray.put(i); } instance = new CollectionField(Types.ARRAY, Types.INT); convertedObj = instance.convert(jsonArray); if(!(convertedObj instanceof List)) { fail("convert method giving a non List object for an ARRAY collection"); } instance = new CollectionField(Types.MULTISET, Types.INT); convertedObj = instance.convert(jsonArray); if(!(convertedObj instanceof Set)) { fail("convert method giving a non Set object for an MULTISET collection"); } try { instance.convert(null); fail("null value not checked"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_MISMATCH, ex.getErrorCode()); } }
@Override public Object convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof JSONArray) { if (type == Types.ARRAY) { return listFromJsonArray((JSONArray) objValue); } else if (type == Types.MULTISET) { return setFromJsonArray((JSONArray) objValue); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else if (objValue instanceof Collection) { Collection collection = (Collection) objValue; if (type == Types.ARRAY) { if (objValue instanceof List) { return objValue; } return new ArrayList<>(collection); } else if (type == Types.MULTISET) { if (objValue instanceof Set) { return objValue; } return new HashSet<>(collection); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else { throw new OperationException(ErrorCode.DATATYPE_ERROR, "Cannot cast " + objValue + " to a valid collection type"); } }
CollectionField implements FieldType { @Override public Object convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof JSONArray) { if (type == Types.ARRAY) { return listFromJsonArray((JSONArray) objValue); } else if (type == Types.MULTISET) { return setFromJsonArray((JSONArray) objValue); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else if (objValue instanceof Collection) { Collection collection = (Collection) objValue; if (type == Types.ARRAY) { if (objValue instanceof List) { return objValue; } return new ArrayList<>(collection); } else if (type == Types.MULTISET) { if (objValue instanceof Set) { return objValue; } return new HashSet<>(collection); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else { throw new OperationException(ErrorCode.DATATYPE_ERROR, "Cannot cast " + objValue + " to a valid collection type"); } } }
CollectionField implements FieldType { @Override public Object convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof JSONArray) { if (type == Types.ARRAY) { return listFromJsonArray((JSONArray) objValue); } else if (type == Types.MULTISET) { return setFromJsonArray((JSONArray) objValue); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else if (objValue instanceof Collection) { Collection collection = (Collection) objValue; if (type == Types.ARRAY) { if (objValue instanceof List) { return objValue; } return new ArrayList<>(collection); } else if (type == Types.MULTISET) { if (objValue instanceof Set) { return objValue; } return new HashSet<>(collection); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else { throw new OperationException(ErrorCode.DATATYPE_ERROR, "Cannot cast " + objValue + " to a valid collection type"); } } CollectionField(final Types type, final Types subType); }
CollectionField implements FieldType { @Override public Object convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof JSONArray) { if (type == Types.ARRAY) { return listFromJsonArray((JSONArray) objValue); } else if (type == Types.MULTISET) { return setFromJsonArray((JSONArray) objValue); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else if (objValue instanceof Collection) { Collection collection = (Collection) objValue; if (type == Types.ARRAY) { if (objValue instanceof List) { return objValue; } return new ArrayList<>(collection); } else if (type == Types.MULTISET) { if (objValue instanceof Set) { return objValue; } return new HashSet<>(collection); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else { throw new OperationException(ErrorCode.DATATYPE_ERROR, "Cannot cast " + objValue + " to a valid collection type"); } } CollectionField(final Types type, final Types subType); @Override Types getType(); Types getSubType(); @Override Object getDefaultValue(); @Override boolean hasDefaultValue(); @Override Object convert(Object objValue); @Override JSONObject toJson(); }
CollectionField implements FieldType { @Override public Object convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof JSONArray) { if (type == Types.ARRAY) { return listFromJsonArray((JSONArray) objValue); } else if (type == Types.MULTISET) { return setFromJsonArray((JSONArray) objValue); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else if (objValue instanceof Collection) { Collection collection = (Collection) objValue; if (type == Types.ARRAY) { if (objValue instanceof List) { return objValue; } return new ArrayList<>(collection); } else if (type == Types.MULTISET) { if (objValue instanceof Set) { return objValue; } return new HashSet<>(collection); } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Data type " + type.name() + " is not a valid collection type"); } } else { throw new OperationException(ErrorCode.DATATYPE_ERROR, "Cannot cast " + objValue + " to a valid collection type"); } } CollectionField(final Types type, final Types subType); @Override Types getType(); Types getSubType(); @Override Object getDefaultValue(); @Override boolean hasDefaultValue(); @Override Object convert(Object objValue); @Override JSONObject toJson();  }