StringObjectFormatter extends ObjectFormatter implements IValueFormatter { @Override public Value valueOf(String value, Type type, Map<String, Object> options) { if (value == null || NullObjectFormatter.NULL_STRING.equals(value)) { return null; } if (value.length() >= 2 && value.startsWith(QUOTE_STRING) && value.endsWith(QUOTE_STRING)) { return type.virtualMachine().mirrorOf(StringUtils.substring(value, 1, -1)); } return type.virtualMachine().mirrorOf(value); } StringObjectFormatter(); @Override Map<String, Object> getDefaultOptions(); @Override String toString(Object value, Map<String, Object> options); @Override boolean acceptType(Type type, Map<String, Object> options); @Override Value valueOf(String value, Type type, Map<String, Object> options); static final String MAX_STRING_LENGTH_OPTION; }
@Test public void testValueOf() throws Exception { Map<String, Object> options = formatter.getDefaultOptions(); Value string = this.getLocalValue("str"); StringReference newValue = (StringReference) formatter.valueOf("aaa", string.type(), options); assertNotNull("StringObjectFormatter should be able to create string.", newValue); assertEquals("Should create a String with right value.", "aaa", newValue.value()); newValue = (StringReference) formatter.valueOf("\"aaa\"", string.type(), options); assertNotNull("StringObjectFormatter should be able to create string.", newValue); assertEquals("Should create a String with right value.", "aaa", newValue.value()); newValue = (StringReference) formatter.valueOf("\"aaa", string.type(), options); assertNotNull("StringObjectFormatter should be able to create string.", newValue); assertEquals("Should create a String with right value.", "\"aaa", newValue.value()); assertNull("StringObjectFormatter should be able to create null string.", formatter.valueOf("null", string.type(), options)); assertNull("StringObjectFormatter should be able to create null string.", formatter.valueOf(null, string.type(), options)); }