private String resolveString(CoreEvent event, ValueResolver resolver) throws MuleException { try (ValueResolvingContext context = getResolvingContextFor(event)) { Object value = resolver.resolve(context); return value != null ? StringMessageUtils.toString(value) : null; } }
@Test public void toStringOnClassShouldReturnClassName() { Object test = getClass(); String result = StringMessageUtils.toString(test); assertEquals(getClass().getName(), result); }
@Test public void toStringOnListLargerThanMaximumOutputLengthShouldReturnAbbreviatedStringRepresentation() { // create a Collection that is too long to be printed List<Integer> list = new ArrayList<Integer>(100); for (int i = 0; i < 100; i++) { list.add(new Integer(i)); } // the String will contain not more than exactly MAX_ARRAY_LENGTH elements String result = StringMessageUtils.toString(list); assertTrue(result.endsWith("[..]]")); assertEquals(StringMessageUtils.MAX_ELEMENTS - 1, countMatches(result, ",")); }
@Test public void toStringOnByteArrayLargerThanMaximumOutputLengthShouldReturnAbbreviatedStringRepresentation() { // create an array that is too long to be printed byte[] test = new byte[StringMessageUtils.MAX_ELEMENTS + 100]; for (int i = 0; i < test.length; i++) { test[i] = (byte) i; } // the String will contain not more than exactly MAX_ARRAY_LENGTH elements String result = StringMessageUtils.toString(test); assertTrue(result.endsWith("[..]}")); assertEquals(StringMessageUtils.MAX_ELEMENTS - 1, countMatches(result, ",")); }
@Test public void toStringOnLongArrayShouldReturnStringRepresentation() { long[] test = new long[] {5068875495743534L, 457635546759674L}; Object result = StringMessageUtils.toString(test); assertEquals("{5068875495743534,457635546759674}", result); }
@Test public void toStringOnStringShouldReturnTheString() { String input = "Oscar"; String result = StringMessageUtils.toString(input); assertEquals(input, result); }
@Test public void toStringOnByteArrayShouldReturnStringRepresentation() { Object test = new byte[] {1, 2}; String result = StringMessageUtils.toString(test); assertEquals("{1,2}", result); }
@Test public void toStringOnDoubleArrayShouldReturnStringRepresentation() { double[] test = new double[] {1.1, 2.02}; String result = StringMessageUtils.toString(test); assertEquals("{1.1,2.02}", result); }
@Test public void toStringOnStringArrayShouldReturnStringRepresentation() { Object test = new String[] {"foo", "bar"}; String result = StringMessageUtils.toString(test); assertEquals("{foo,bar}", result); }
@Test public void toStringOnObjectShouldReturnObjectsToStringRepresentation() { // this class uses the default toString implementation Object test = new TestObject("Ernie"); String result = StringMessageUtils.toString(test); assertEquals(test.toString(), result); // this class has a custom toString implementation test = new AnotherTestObject("Bert"); result = StringMessageUtils.toString(test); assertEquals("Bert", result); }
public static String getFormattedMessage(String msg, Object[] arguments) { if (arguments != null) { for (int i = 0; i < arguments.length; i++) { arguments[i] = toString(arguments[i]); } } return MessageFormat.format(msg, arguments); }
@Test public void toStringOnListLargerThanMaximumOutputLengthShouldReturnAbbreviatedStringRepresentation() { // create a Collection that is too long to be printed List<Integer> list = new ArrayList<Integer>(100); for (int i = 0; i < 100; i++) { list.add(new Integer(i)); } // the String will contain not more than exactly MAX_ARRAY_LENGTH elements String result = StringMessageUtils.toString(list); assertTrue(result.endsWith("[..]]")); assertEquals(StringMessageUtils.MAX_ELEMENTS - 1, countMatches(result, ",")); }
@Test public void toStringOnClassShouldReturnClassName() { Object test = getClass(); String result = StringMessageUtils.toString(test); assertEquals(getClass().getName(), result); }
@Test public void toStringOnStringArrayShouldReturnStringRepresentation() { Object test = new String[] {"foo", "bar"}; String result = StringMessageUtils.toString(test); assertEquals("{foo,bar}", result); }
@Test public void toStringOnLongArrayShouldReturnStringRepresentation() { long[] test = new long[] {5068875495743534L, 457635546759674L}; Object result = StringMessageUtils.toString(test); assertEquals("{5068875495743534,457635546759674}", result); }
@Test public void toStringOnByteArrayShouldReturnStringRepresentation() { Object test = new byte[] {1, 2}; String result = StringMessageUtils.toString(test); assertEquals("{1,2}", result); }
@Test public void toStringOnByteArrayLargerThanMaximumOutputLengthShouldReturnAbbreviatedStringRepresentation() { // create an array that is too long to be printed byte[] test = new byte[StringMessageUtils.MAX_ELEMENTS + 100]; for (int i = 0; i < test.length; i++) { test[i] = (byte) i; } // the String will contain not more than exactly MAX_ARRAY_LENGTH elements String result = StringMessageUtils.toString(test); assertTrue(result.endsWith("[..]}")); assertEquals(StringMessageUtils.MAX_ELEMENTS - 1, countMatches(result, ",")); }
@Test public void toStringOnStringShouldReturnTheString() { String input = "Oscar"; String result = StringMessageUtils.toString(input); assertEquals(input, result); }
@Test public void toStringOnDoubleArrayShouldReturnStringRepresentation() { double[] test = new double[] {1.1, 2.02}; String result = StringMessageUtils.toString(test); assertEquals("{1.1,2.02}", result); }