UserInitialsGeneratorImpl implements UserInitialsGenerator { @Override public String getInitials( Locale locale, String firstName, String middleName, String lastName) { String[] userNames = {firstName, middleName, lastName}; return Stream.of( _getUserInitialsFieldNames(locale) ).map( key -> userNames[_userNameIndexes.get(key)] ).filter( name -> Validator.isNotNull(name) ).limit( 2 ).map( name -> StringUtil.shorten(name, 1) ).map( initial -> StringUtil.toUpperCase(initial) ).collect( Collectors.joining() ); }  @Override String getInitials(
		Locale locale, String firstName, String middleName, String lastName); @Override String getInitials(User user);  }
@Test public void testFirstLast() throws Exception { _setUpLanguageUtil("first-name,last-name"); Assert.assertEquals( "FL", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( "F", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, null)); Assert.assertEquals( "L", _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( StringPool.BLANK, _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, null)); }
@Test public void testFirstMiddleLast() throws Exception { _setUpLanguageUtil("first-name,middle-name,last-name"); Assert.assertEquals( "FM", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( "FM", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, null)); Assert.assertEquals( "ML", _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( "FL", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, null, _LAST_NAME)); Assert.assertEquals( "F", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, null, null)); Assert.assertEquals( "M", _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, null)); Assert.assertEquals( "L", _userInitialsGenerator.getInitials( _LOCALE, null, null, _LAST_NAME)); }
@Test public void testLastFirst() throws Exception { _setUpLanguageUtil("last-name,first-name"); Assert.assertEquals( "LF", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( "F", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, null)); Assert.assertEquals( "L", _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( StringPool.BLANK, _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, null)); }
@Test public void testLastFirstMiddle() throws Exception { _setUpLanguageUtil("last-name,first-name,middle-name"); Assert.assertEquals( "LF", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( "FM", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, null)); Assert.assertEquals( "LM", _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( "LF", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, null, _LAST_NAME)); Assert.assertEquals( "F", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, null, null)); Assert.assertEquals( "M", _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, null)); Assert.assertEquals( "L", _userInitialsGenerator.getInitials( _LOCALE, null, null, _LAST_NAME)); }
@Test public void testNoPropertiesReturnedUsesDefaultValues() throws Exception { _setUpLanguageUtil(StringPool.BLANK); Assert.assertEquals( "FL", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( "F", _userInitialsGenerator.getInitials( _LOCALE, _FIRST_NAME, _MIDDLE_NAME, null)); Assert.assertEquals( "L", _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, _LAST_NAME)); Assert.assertEquals( StringPool.BLANK, _userInitialsGenerator.getInitials( _LOCALE, null, _MIDDLE_NAME, null)); }