StringToGrantedAuthority implements Transformer { public Object transform( final Object obj ) { if ( StringToGrantedAuthority.logger.isDebugEnabled() ) { StringToGrantedAuthority.logger.debug( Messages.getInstance().getString( "StringToGrantedAuthority.DEBUG_INPUT_TO_TRANSFORM", ( null != obj ) ? obj.toString() : "null" ) ); } Object transformed = obj; if ( obj instanceof String ) { transformed = transformItem( obj ); } else if ( obj instanceof Collection ) { transformed = new HashSet(); Set authSet = (Set) transformed; Iterator iter = ( (Collection) obj ).iterator(); while ( iter.hasNext() ) { authSet.add( transformItem( iter.next() ) ); } } else if ( obj instanceof Object[] ) { transformed = new HashSet(); Set authSet = (Set) transformed; Object[] objArray = (Object[]) obj; for ( Object element : objArray ) { authSet.add( transformItem( element ) ); } transformed = authSet.toArray(); } return transformed; } StringToGrantedAuthority(); Object transform( final Object obj ); void setConvertToUpperCase( final boolean convertToUpperCase ); void setRolePrefix( final String rolePrefix );  }
@Test public void testTransform() { StringToGrantedAuthority test = new StringToGrantedAuthority(); test.setConvertToUpperCase( false ); String admin = "admin"; Object transformed = test.transform( admin ); assertEquals( "ROLE_admin", transformed.toString() ); test.setConvertToUpperCase( true ); transformed = test.transform( admin ); assertEquals( "ROLE_ADMIN", transformed.toString() ); Integer zero = new Integer( 0 ); transformed = test.transform( zero ); assertEquals( zero, transformed ); Object[] input = new Object[] { new Integer( 0 ), new Long( 1 ), new Character( 'a' ) }; Object output = test.transform( input ); Assert.assertArrayEquals( (Object[]) output, input ); Collection<Object> inputCollection = new LinkedList<>(); output = test.transform( inputCollection ); assertTrue( ( (Collection) output ).isEmpty() ); inputCollection.add( new String( "admin" ) ); output = test.transform( inputCollection ); assertTrue( ( (Collection) output ).size() == 1 ); Object element = ( (Collection) output ).toArray()[0]; assertEquals( "ROLE_ADMIN", element.toString() ); }