### Question:
Dialogs { protected static String getFormattedErrorMessageForDisplay(String message, Throwable throwable) { StringBuilder html = new StringBuilder(); if(!Strings.isNullOrEmpty(message)){ html.append("<b>").append(message.replace("\r\n","<br />") .replace("\n","<br />")).append("</b> <br /><br />"); } while(throwable != null && !Strings.isNullOrEmpty(throwable.getMessage())){ html.append(throwable.getMessage().replace("\r\n","<br />")) .append("<br /><br />"); throwable=throwable.getCause(); } return html.toString(); }  static void error(String title, final String friendlyMessage, final String errorMessage, final Throwable throwable, final Context context); static void alert(String title, String message, Context context); static void alert(String title, String message, Context context, final MessageBoxCallback msgCallback); static void alert(String title, String message, Context context, boolean includeCancel, final MessageBoxCallback msgCallback); static void progress(Context context, String title, String message); static void hideProgress(); static void autoCompleteText(final Context ctx, final String cacheKey, String title,
                                        String hint, String text,
                                        final AutoCompleteCallback callback);  }### Answer:
@Test public void GetFormattedErrorMessageForDisplay_WithMessageAndThrowable() { String message = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly."; Throwable t = new Throwable("Parameter not set.\r\nException handling not configured.\r\nEat oranges they are great.\r\n"); t.setStackTrace(new StackTraceElement[]{new StackTraceElement("XXX", "yyy", "BBB", 99)}); String expected = "<b>An error occurred.<br />220 ----------------- There was a problem<br />No username specified.<br />Server not configured properly.</b> <br /><br />Parameter not set.<br />Exception handling not configured.<br />Eat oranges they are great.<br /><br /><br />"; assertThat("Message formatted for error display", Dialogs.getFormattedErrorMessageForDisplay(message, t), is(expected)); }
@Test public void GetFormattedErrorMessageForDisplay_WithMessageOrThrowableMissing(){ String message = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly."; String expected = "<b>An error occurred.<br />220 ----------------- There was a problem<br />No username specified.<br />Server not configured properly.</b> <br /><br />"; assertThat("Message formatted even without exception object", Dialogs.getFormattedErrorMessageForDisplay(message, null), is(expected)); expected = ""; assertThat("Null message handled gracefully", Dialogs.getFormattedErrorMessageForDisplay(null, null), is(expected)); Throwable t = new Throwable((String)null); assertThat("Null message handled gracefully", Dialogs.getFormattedErrorMessageForDisplay(null, t), is(expected)); }
@Test public void GetFormattedErrorMessageForDisplay_NewLinesReplaced(){ String message = "An error occurred.\r\nThis is the next line."; String expected = "<b>An error occurred.<br />This is the next line.</b> <br /><br />"; assertThat("Backslash r backslash n replaced", Dialogs.getFormattedErrorMessageForDisplay(message, null), is(expected)); message = "An error occurred.\nThis is the next line."; assertThat("Backslash n replaced", Dialogs.getFormattedErrorMessageForDisplay(message, null), is(expected)); }