StringUtils { @NonNull public static String defaultIfBlank(@Nullable final String primaryString, @NonNull final String defaultString) { return isBlank(primaryString) ? defaultString : primaryString; } private  StringUtils(); @NonNull static String defaultIfBlank(@Nullable final String primaryString, @NonNull final String defaultString);  }
@Test public void testThatDefaultIfBlankReturnsPrimaryStringIfItContainsAtLeastOneNonWhitespaceCharacter() { final String primaryString = "any string containing at least one non-whitespace character"; assert !primaryString.equals(DEFAULT_STRING); final String sanitizedString = StringUtils.defaultIfBlank(primaryString, DEFAULT_STRING); assertEquals( "defaultIfBlank should have returned the primary string", primaryString, sanitizedString); }
@Test public void testThatDefaultIfBlankReturnsDefaultStringIfPrimaryStringIsWhitespaceOnly() { final String whitespaceOnlyPrimaryString = " "; assert !DEFAULT_STRING.equals(whitespaceOnlyPrimaryString); final String sanitizedString = StringUtils.defaultIfBlank(whitespaceOnlyPrimaryString, DEFAULT_STRING); assertEquals( "defaultIfBlank should have returned the default string", DEFAULT_STRING, sanitizedString); }
@Test public void testThatDefaultIfBlankReturnsDefaultStringIfPrimaryStringIsEmpty() { final String emptyPrimaryString = ""; assert !DEFAULT_STRING.equals(emptyPrimaryString); final String sanitizedString = StringUtils.defaultIfBlank(emptyPrimaryString, DEFAULT_STRING); assertEquals( "defaultIfBlank should have returned the default string", DEFAULT_STRING, sanitizedString); }
@Test public void testThatDefaultIfBlankReturnsDefaultStringIfPrimaryStringIsNull() { final String nullPrimaryString = null; assert !DEFAULT_STRING.equals(nullPrimaryString); final String sanitizedString = StringUtils.defaultIfBlank(nullPrimaryString, DEFAULT_STRING); assertEquals( "defaultIfBlank should have returned the default string", DEFAULT_STRING, sanitizedString); }