StringToObjectConverter { public Object convertFromString(String pType, String pValue) { String value = convertSpecialStringTags(pValue); if (value == null) { return null; } if (pType.startsWith("[") && pType.length() >= 2) { return convertToArray(pType, value); } Parser parser = PARSER_MAP.get(pType); if (parser != null) { return parser.extract(value); } Object cValue = convertByConstructor(pType, pValue); if (cValue != null) { return cValue; } throw new IllegalArgumentException( "Cannot convert string " + value + " to type " + pType + " because no converter could be found"); }  Object prepareValue(String pExpectedClassName, Object pValue); static String convertSpecialStringTags(String pValue); Object convertFromString(String pType, String pValue);  }
@Test public void simpleConversions() { Object obj = converter.convertFromString(int.class.getCanonicalName(),"10"); assertEquals("Int conversion",10,obj); obj = converter.convertFromString(Integer.class.getCanonicalName(),"10"); assertEquals("Integer conversion",10,obj); obj = converter.convertFromString(Short.class.getCanonicalName(),"10"); assertEquals("Short conversion",(short) 10,obj); obj = converter.convertFromString(short.class.getCanonicalName(),"10"); assertEquals("short conversion",Short.parseShort("10"),obj); obj = converter.convertFromString(Long.class.getCanonicalName(),"10"); assertEquals("long conversion",10L,obj); obj = converter.convertFromString(long.class.getCanonicalName(),"10"); assertEquals("Long conversion",10L,obj); obj = converter.convertFromString(Byte.class.getCanonicalName(),"10"); assertEquals("Byte conversion",(byte) 10,obj); obj = converter.convertFromString(byte.class.getCanonicalName(),"10"); assertEquals("byte conversion",Byte.parseByte("10"),obj); obj = converter.convertFromString(Float.class.getCanonicalName(),"10.5"); assertEquals("Float conversion",10.5f,obj); obj = converter.convertFromString(float.class.getCanonicalName(),"21.3"); assertEquals("float conversion",new Float(21.3f),obj); obj = converter.convertFromString(Double.class.getCanonicalName(),"10.5"); assertEquals("Double conversion",10.5d,obj); obj = converter.convertFromString(double.class.getCanonicalName(),"21.3"); assertEquals("double conversion",21.3d,obj); obj = converter.convertFromString(BigDecimal.class.getCanonicalName(),"83.4e+4"); assertEquals("BigDecimal conversion", new BigDecimal("8.34e+5"), obj); obj = converter.convertFromString(BigInteger.class.getCanonicalName(),"47110815471108154711"); assertEquals("BigInteger conversion", new BigInteger("47110815471108154711"), obj); obj = converter.convertFromString(Boolean.class.getCanonicalName(),"false"); assertEquals("Boolean conversion",false,obj); obj = converter.convertFromString(boolean.class.getCanonicalName(),"true"); assertEquals("boolean conversion",true,obj); obj = converter.convertFromString(char.class.getCanonicalName(),"a"); assertEquals("Char conversion",'a',obj); obj = converter.convertFromString("java.lang.String","10"); assertEquals("String conversion","10",obj); }
@Test public void jsonConversion() { JSONObject json = new JSONObject(); json.put("name","roland"); json.put("kind","jolokia"); Object object = converter.convertFromString(JSONObject.class.getName(),json.toString()); assertEquals(json,object); JSONArray array = new JSONArray(); array.add("roland"); array.add("jolokia"); object = converter.convertFromString(JSONArray.class.getName(),array.toString()); assertEquals(array,object); try { converter.convertFromString(JSONObject.class.getName(),"{bla:blub{"); fail(); } catch (IllegalArgumentException exp) { } }
@Test public void urlConversion(){ URL url = null; try { url = new URL("http: } catch (MalformedURLException e) {} Object object = converter.convertFromString(URL.class.getCanonicalName(),"http: assertEquals("URL conversion", url, object); }
@Test public void dateConversion() { Date date = (Date) converter.convertFromString(Date.class.getName(),"0"); assertEquals(date.getTime(),0); Date now = new Date(); date = (Date) converter.convertFromString(Date.class.getName(), DateUtil.toISO8601(now)); assertEquals(date.getTime() / 1000,now.getTime() / 1000); }
@Test public void objectNameConversion() throws MalformedObjectNameException { String name = "JOLOKIA:class=Conversion,type=builder,name=jlk"; ObjectName objName = new ObjectName(name); ObjectName testName = (ObjectName)converter.convertFromString(ObjectName.class.getName(), name); assertEquals(objName, testName); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*parse.*ObjectName.*") public void objectNameConversionFailed() { converter.convertFromString(ObjectName.class.getName(),"bla:blub:InvalidName"); }
@Test public void arrayConversions() { Object obj = converter.convertFromString(new int[0].getClass().getName(),"10,20,30"); int expected[] = new int[] { 10,20,30}; for (int i = 0;i < expected.length;i++) { assertEquals(expected[i],((int[]) obj)[i]); } obj = converter.convertFromString(new Integer[0].getClass().getName(),"10,20,30"); for (int i = 0;i < expected.length;i++) { assertEquals(expected[i],(int) ((Integer[]) obj)[i]); } String[] strings = (String[]) converter.convertFromString(new String[0].getClass().getName(),"hallo!,hans!!,wu!!rs!t"); assertEquals(strings.length,2); assertEquals("hallo,hans!",strings[0]); assertEquals("wu!rst",strings[1]); try { obj = converter.convertFromString("[Lbla;","10,20,30"); fail("Unknown object type"); } catch (IllegalArgumentException exp) {} try { obj = converter.convertFromString("[X","10,20,30"); fail("Unknown object type"); } catch (IllegalArgumentException exp) {} }
@Test public void checkNull() { Object obj = converter.convertFromString(new int[0].getClass().getName(),"[null]"); assertNull("Null check",obj); }
@Test public void checkEmptyString() { Object obj = converter.convertFromString("java.lang.String","\"\""); assertEquals("Empty String check",0,((String) obj).length()); try { obj = converter.convertFromString("java.lang.Integer","\"\""); fail("Empty string conversion only for string"); } catch (IllegalArgumentException exp) {} }
@Test public void unknownExtractor() { try { Object obj = converter.convertFromString(this.getClass().getName(),"bla"); fail("Unknown extractor"); } catch (IllegalArgumentException exp) {}; }
@Test public void dateConversionNotByConstructor() throws ParseException { final String dateStr = "2015-11-20T00:00:00+00:00"; try { new Date(dateStr); fail("Should have throw IllegalArgumentException"); } catch (IllegalArgumentException ignore) {} Object obj = converter.convertFromString(Date.class.getCanonicalName(), dateStr); assertNotNull(obj); assertTrue(obj instanceof Date); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); Date expectedDate = sdf.parse(dateStr.replaceFirst("\\+(0\\d)\\:(\\d{2})$", "+$1$2")); assertEquals(expectedDate, obj); }