StringToOpenTypeConverter extends OpenTypeConverter { @SuppressWarnings("unchecked") @Override public Object convertToObject(OpenType openType, Object pValue) { if (pValue == null) { return null; } else { for (OpenTypeConverter converter : converters) { if (converter.canConvert(openType)) { return converter.convertToObject(openType,pValue); } } throw new IllegalArgumentException( "Cannot convert " + pValue + " to " + openType + ": " + "No converter could be found"); } } StringToOpenTypeConverter(StringToObjectConverter pStringToObjectConverter); @SuppressWarnings("unchecked") @Override Object convertToObject(OpenType openType, Object pValue); StringToOpenTypeConverter makeForgiving();  }
@Test public void nullValue() throws OpenDataException { assertNull(converter.convertToObject(SimpleType.STRING,null)); }
@Test(expectedExceptions = { NumberFormatException.class }) public void simpleTypeFailed() { converter.convertToObject(SimpleType.INTEGER,"4.52"); }
@Test public void arrayType() throws OpenDataException, ParseException { ArrayType type = new ArrayType(2,STRING); String json = "[ \"hello\", \"world\" ]"; for (Object element : new Object[] { json, new JSONParser().parse(json) }) { Object[] data = (Object[]) converter.convertToObject(type,element); assertEquals(data.length,2); assertEquals(data[0],"hello"); assertEquals(data[1],"world"); } }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*JSONArray.*") public void arrayTypeWithWrongJson() throws OpenDataException { converter.convertToObject(new ArrayType(2,STRING),"{ \"hello\": \"world\"}"); }
@Test public void arrayTypeWithCompositeElementType() throws OpenDataException { CompositeTypeAndJson taj = new CompositeTypeAndJson( STRING,"verein","FCN" ); CompositeData[] result = (CompositeData[]) converter.convertToObject(new ArrayType(2,taj.getType()),"[" + taj.getJsonAsString() + "]"); assertEquals(result[0].get("verein"), "FCN"); assertEquals(result.length,1); }
@Test(expectedExceptions = UnsupportedOperationException.class,expectedExceptionsMessageRegExp = ".*Unsupported.*") public void arrayTypeWithWrongElementType() throws OpenDataException { TabularTypeAndJson taj = new TabularTypeAndJson( new String[] { "verein" }, new CompositeTypeAndJson( STRING,"verein","fcn", BOOLEAN,"absteiger",false ) ); JSONArray array = new JSONArray(); array.add(taj.getJson()); converter.convertToObject(new ArrayType(2, taj.getType()),array); }
@Test public void compositeType() throws OpenDataException, AttributeNotFoundException, ParseException { CompositeTypeAndJson taj = new CompositeTypeAndJson( STRING,"verein","FCN", INTEGER,"platz",6, STRING,"trainer",null, BOOLEAN,"absteiger",false ); for (Object input : new Object[] { taj.getJson(), taj.getJsonAsString() }) { CompositeData result = (CompositeData) converter.convertToObject(taj.getType(),input); assertEquals(result.get("verein"),"FCN"); assertEquals(result.get("trainer"),null); assertEquals(result.get("platz"),6); assertEquals(result.get("absteiger"),false); assertEquals(result.values().size(),4); } }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*JSONObject.*") public void compositeTypeWithWrongJson() throws OpenDataException { CompositeTypeAndJson taj = new CompositeTypeAndJson( STRING,"verein","FCN" ); converter.convertToObject(taj.getType(),"[ 12, 15, 16]"); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*praesident.*") public void compositeTypeWithWrongKey() throws OpenDataException { CompositeTypeAndJson taj = new CompositeTypeAndJson( STRING,"verein","FCN" ); converter.convertToObject(taj.getType(),"{ \"praesident\": \"hoeness\"}"); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*parse.*") public void invalidJson() throws OpenDataException { CompositeTypeAndJson taj = new CompositeTypeAndJson( STRING,"verein","FCN" ); converter.convertToObject(taj.getType(),"{ \"praesident\":"); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*JSONAware.*") public void invalidJson2() throws OpenDataException { CompositeTypeAndJson taj = new CompositeTypeAndJson( STRING,"verein","FCN" ); converter.convertToObject(taj.getType(),"2"); }
@Test public void tabularType() throws OpenDataException { TabularTypeAndJson taj = getSampleTabularType(); TabularData data = (TabularData) converter.convertToObject(taj.getType(),taj.getJsonAsString()); assertEquals(data.get(new String[] { "fcn" }).get("absteiger"),false); assertEquals(data.get(new String[] { "fcb" }).get("absteiger"),true); }
@Test public void tabularTypeForMXBeanMaps() throws OpenDataException { TabularTypeAndJson taj = getSampleTabularTypeForMXBeanMap(); String json = "{ \"keyOne\" : \"valueOne\", \"keyTwo\" : \"valueTwo\"}"; TabularData data = (TabularData) converter.convertToObject(taj.getType(),json); CompositeData col1 = data.get(new String[] { "keyOne" }); assertEquals(col1.get("key"),"keyOne"); assertEquals(col1.get("value"),"valueOne"); CompositeData col2 = data.get(new String[] { "keyTwo" }); assertEquals(col2.get("key"),"keyTwo"); assertEquals(col2.get("value"),"valueTwo"); }
@Test public void tabularTypeInFullRepresentation() throws OpenDataException, MalformedObjectNameException { TabularType type = getSampleTabularTypeForComplexTabularData(); String json = "{ \"indexNames\" : [ \"user\", \"street\" ], " + " \"values\" : [ " + " { \"user\" : { \"name\" : \"roland\", \"age\" : 44 }, " + " \"street\" : \"homestreet\", " + " \"oname\" : \"java.lang:type=Memory\" " + " }]" + "}"; TabularData data = (TabularData) converter.convertToObject(type,json); assertNotNull(data); Set keySet = data.keySet(); assertEquals(keySet.size(), 1); List keys = (List) keySet.iterator().next(); assertEquals(keys.size(),2); assertTrue(keys.contains("homestreet")); CompositeData cd = checkCompositeKey(keys); CompositeData row = data.get(new Object[] { cd, "homestreet"}); assertEquals(row.get("user"),cd); assertEquals(row.get("street"),"homestreet"); assertEquals(row.get("oname"),new ObjectName("java.lang:type=Memory")); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*index.*name.*") public void invalidIndexNameForComplexTabularDataConversion() throws OpenDataException { TabularType type = getSampleTabularTypeForComplexTabularData(); String json = "{ \"indexNames\" : [ \"user\", \"bla\" ], " + " \"values\" : [ " + " { \"user\" : { \"name\" : \"roland\", \"age\" : 44 }, " + " \"bla\" : \"homestreet\", " + " \"oname\" : \"java.lang:type=Memory\" " + " }]" + "}"; converter.convertToObject(type,json); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*index.*name.*") public void invalidIndexNameCountForComplexTabularDataConversion() throws OpenDataException { TabularType type = getSampleTabularTypeForComplexTabularData(); String json = "{ \"indexNames\" : [ \"user\", \"street\", \"bla\" ], " + " \"values\" : [ " + " { \"user\" : { \"name\" : \"roland\", \"age\" : 44 }, " + " \"street\" : \"homestreet\", " + " \"bla\" : \"blub\", " + " \"oname\" : \"java.lang:type=Memory\" " + " }]" + "}"; converter.convertToObject(type,json); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*array.*") public void invalidTypeComplexTabularDataConversion1() throws OpenDataException { TabularType type = getSampleTabularTypeForComplexTabularData(); String json = "{ \"indexNames\" : { \"user\" : \"bla\" }, " + " \"values\" : [ " + " { \"user\" : { \"name\" : \"roland\", \"age\" : 44 }, " + " \"street\" : \"homestreet\", " + " \"bla\" : \"blub\", " + " \"oname\" : \"java.lang:type=Memory\" " + " }]" + "}"; converter.convertToObject(type,json); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*array.*") public void invalidTypeComplexTabularDataConversion2() throws OpenDataException { TabularType type = getSampleTabularTypeForComplexTabularData(); String json = "{ \"indexNames\" : [ \"user\", \"street\" ], " + " \"values\" : " + " { \"user\" : { \"name\" : \"roland\", \"age\" : 44 }, " + " \"street\" : \"homestreet\", " + " \"bla\" : \"blub\", " + " \"oname\" : \"java.lang:type=Memory\" " + " }" + "}"; converter.convertToObject(type,json); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*object.*") public void invalidTypeComplexTabularDataConversion3() throws OpenDataException { TabularType type = getSampleTabularTypeForComplexTabularData(); String json = "{ \"indexNames\" : [ \"user\", \"street\"], " + " \"values\" : [ " + " [{ \"user\" : { \"name\" : \"roland\", \"age\" : 44 }, " + " \"street\" : \"homestreet\", " + " \"bla\" : \"blub\", " + " \"oname\" : \"java.lang:type=Memory\" " + " }]]" + "}"; converter.convertToObject(type,json); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*JSONObject.*") public void tabularTypeForMXBeanMapsFail() throws OpenDataException { TabularTypeAndJson taj = getSampleTabularTypeForMXBeanMap(); converter.convertToObject(taj.getType(), "[ { \"keyOne\" : \"valueOne\" } ]"); }
@Test public void tabularTypeForMXBeanMapsComplex() throws OpenDataException { TabularTypeAndJson inner = getSampleTabularTypeForMXBeanMap(); TabularTypeAndJson taj = new TabularTypeAndJson( new String[]{"key"}, new CompositeTypeAndJson( STRING, "key", null, inner.getType(), "value", null ) ); String json = "{ \"keyOne\" : { \"innerKeyOne\" : \"valueOne\" }, \"keyTwo\" : { \"innerKeyTwo\" : \"valueTwo\"}}"; TabularData data = (TabularData) converter.convertToObject(taj.getType(),json); CompositeData col1 = data.get(new String[] { "keyOne" }); assertEquals(col1.get("key"),"keyOne"); TabularData innerCol1 = (TabularData) col1.get("value"); CompositeData col1inner = innerCol1.get(new String[]{"innerKeyOne"}); assertEquals(col1inner.get("key"),"innerKeyOne"); assertEquals(col1inner.get("value"),"valueOne"); }
@Test public void multipleLevleTabularData() throws OpenDataException { JSONObject map = new JSONObject(); JSONObject inner = new JSONObject(); map.put("fcn",inner); JSONObject innerinner = new JSONObject(); inner.put("franconia",innerinner); innerinner.put("verein","fcn"); innerinner.put("region","franconia"); innerinner.put("absteiger",false); TabularType type = new TabularType("soccer","soccer", new CompositeType("row","row", new String[] { "verein", "region", "absteiger" }, new String[] { "verein","region","absteiger"}, new OpenType[] { STRING, STRING, BOOLEAN}), new String[] { "verein", "region" }); TabularData data = (TabularData) converter.convertToObject(type,map); CompositeData row = data.get(new Object[] { "fcn", "franconia" }); assertNotNull(row); assertFalse((Boolean) row.get("absteiger")); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*praesident.*") public void compositeTypeWithWrongType() throws OpenDataException { converter.convertToObject(getSampleTabularType().getType(),"{ \"praesident\": \"hoeness\"}"); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*JSONArray.*") public void compositeTypeWithWrongInnerType() throws OpenDataException { converter.convertToObject(getSampleTabularType().getType(),"[[{ \"praesident\": \"hoeness\"}]]"); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*No converter.*") public void unknownOpenType() throws OpenDataException { converter.convertToObject(new OpenType("java.util.Date","guenther","guenther") { @Override public boolean isValue(Object obj) { return false; } @Override public boolean equals(Object obj) { return false; } @Override public int hashCode() { return 0; } @Override public String toString() { return null; } },"bla"); }