### Question:
Dialogs { protected static String getFormattedErrorMessageForPlainText(String message, Throwable throwable){ StringBuilder sb = new StringBuilder(); if(!Strings.isNullOrEmpty(message)){ sb.append(message).append("\r\n"); } while (throwable != null && !Strings.isNullOrEmpty(throwable.getMessage())) { sb.append("\r\n\r\n").append(throwable.getMessage()).append("\r\n"); if (throwable.getStackTrace().length > 0) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); throwable.printStackTrace(pw); sb.append(sw.toString()); } throwable = throwable.getCause(); } return sb.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 GetFormattedErrorMessageForPlainText_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 plainText = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.\r\n\r\n\r\nParameter not set.\r\nException handling not configured.\r\nEat oranges they are great.\r\n\r\njava.lang.Throwable: Parameter not set.\r\nException handling not configured.\r\nEat oranges they are great.\r\n\n\tat XXX.yyy(BBB:99)\n"; assertThat("Message formatted for emailing copying", Dialogs.getFormattedErrorMessageForPlainText(message, t), is(plainText)); }
@Test public void GetFormattedErrorMessageForPlainText_WithMessageOnly() { String message = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly."; String plainText = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.\r\n\r\n\r\nThis is a detailed message\r\n"; Throwable t = new Throwable("This is a detailed message"); t.setStackTrace(new StackTraceElement[0]); assertThat("Message formatted without stacktrace fully present", Dialogs.getFormattedErrorMessageForPlainText(message, t), is(plainText)); }
@Test public void GetFormattedErrorMessageForPlainText_WithMessageOrThrowableMissing(){ String message = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly."; String plainText = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.\r\n"; assertThat("Message formatted without stacktrace fully present", Dialogs.getFormattedErrorMessageForPlainText(message, null), is(plainText)); plainText = ""; assertThat("Null message handled gracefully", Dialogs.getFormattedErrorMessageForPlainText(null, null), is(plainText)); Throwable t = new Throwable((String)null); t.setStackTrace(new StackTraceElement[0]); assertThat("Null message handled gracefully", Dialogs.getFormattedErrorMessageForPlainText(null, t), is(plainText)); }