StringObjectFormatter extends ObjectFormatter implements IValueFormatter { @Override public String toString(Object value, Map<String, Object> options) { int maxLength = getMaxStringLength(options); return String.format("\"%s\"", maxLength > 0 ? StringUtils.abbreviate(((StringReference) value).value(), maxLength) : ((StringReference) value).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 testToString() throws Exception { Value string = this.getLocalValue("str"); Map<String, Object> options = formatter.getDefaultOptions(); options.put(MAX_STRING_LENGTH_OPTION, 4); assertEquals("Should be able to format string type.", "\"s...\"", formatter.toString(string, options)); options.put(MAX_STRING_LENGTH_OPTION, 5); assertEquals("Should be able to format string type.", "\"st...\"", formatter.toString(string, options)); assertTrue("Should not trim long string by default", formatter.toString(string, new HashMap<>()).contains(((StringReference) string).value())); }