StringToObjectConverter { public Object prepareValue(String pExpectedClassName, Object pValue) { if (pValue == null) { return null; } else { Class expectedClass = ClassUtil.classForName(pExpectedClassName); Object param = null; if (expectedClass != null) { param = prepareValue(expectedClass,pValue); } if (param == null) { return convertFromString(pExpectedClassName, pValue.toString()); } return param; } }  Object prepareValue(String pExpectedClassName, Object pValue); static String convertSpecialStringTags(String pValue); Object convertFromString(String pType, String pValue);  }
@Test public void enumConversion() { ConfigKey key = (ConfigKey) converter.prepareValue(ConfigKey.class.getName(), "MAX_DEPTH"); assertEquals(key, ConfigKey.MAX_DEPTH); }
@Test(expectedExceptions = { IllegalArgumentException.class}) public void dateConversionFailed() { converter.prepareValue(Date.class.getName(),"illegal-date-format"); }
@Test public void prepareValue() { assertNull(converter.prepareValue("java.lang.String", null)); assertEquals(converter.prepareValue("java.lang.Long", 10L), 10L); assertEquals(converter.prepareValue("java.lang.Long", "10"), 10L); Map<String,String> map = new HashMap<String, String>(); map.put("euro","fcn"); assertTrue(converter.prepareValue("java.util.Map", map) == map); }
@Test(expectedExceptions = { IllegalArgumentException.class }) public void prepareValueInvalidClass() { converter.prepareValue("blubber.bla.hello",10L); }
@Test public void prepareValueListConversion1() { List<Boolean> list = new ArrayList<Boolean>(); list.add(true); list.add(false); boolean[] res = (boolean[]) converter.prepareValue("[Z",list); assertTrue(res[0]); assertFalse(res[1]); Assert.assertEquals(res.length,2); }
@Test public void prepareValueListConversion2() { List<Boolean> list = new ArrayList<Boolean>(); list.add(true); list.add(false); list.add(null); Boolean[] res = (Boolean[]) converter.prepareValue("[Ljava.lang.Boolean;",list); assertTrue(res[0]); assertFalse(res[1]); assertNull(res[2]); Assert.assertEquals(res.length,3); }
@Test(expectedExceptions = { IllegalArgumentException.class }) public void prepareValueWithException() { List<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(null); converter.prepareValue("[I",list); }
@Test public void prepareValueWithConstructor() { Object o = converter.prepareValue(this.getClass().getCanonicalName() + "$Example", "test"); assertTrue(o instanceof Example); assertEquals("test", ((Example)o).getValue()); }
@Test public void prepareValueWithConstructorList() { Object o = converter.prepareValue(this.getClass().getCanonicalName() + "$Example", Arrays.asList("test")); assertTrue(o instanceof Example); assertNull(((Example)o).getList()); assertEquals("[test]", ((Example)o).getValue()); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Cannot convert string test to type " + "org.jolokia.converter.object.StringToObjectConverterTest\\$PrivateExample " + "because no converter could be found") public void prepareValueWithPrivateExample() { converter.prepareValue(this.getClass().getCanonicalName() + "$PrivateExample", "test"); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Cannot convert string test to type " + "org.jolokia.converter.object.StringToObjectConverterTest\\$MultipleConstructorExample " + "because no converter could be found") public void prepareValueWithMultipleConstructors() { converter.prepareValue(this.getClass().getCanonicalName() + "$MultipleConstructorExample", "test"); }