// 2012-10-08 johnpfeiffer
package net.kittyandbear.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.HashMap;
import org.junit.Before;
import org.junit.Test;
public class JohnStringUtilsTest
{
private static final String CLASSVERSION = "0.35";
@Before
public void setUp() throws Exception
{
}
@Test
public void testGetVersion()
{
assertEquals( CLASSVERSION , JohnStringUtils.CLASSVERSION );
}
@Test
public void testSafeHTML()
{
final char AMPERSAND = '&';
final char LEFTANGLEBRACKET = '<';
final char RIGHTANGLEBRACKET = '>';
final char DOUBLEQUOTE = '"';
final char SINGLEQUOTE = '\'';
final char FORWARDSLASH = '/';
String illegalChars = "" + AMPERSAND + LEFTANGLEBRACKET + RIGHTANGLEBRACKET + DOUBLEQUOTE + SINGLEQUOTE + FORWARDSLASH;
String expected = "&" + "<" + ">" + """ + "'" + "/";
assertEquals( expected , JohnStringUtils.safeHTML( illegalChars ) );
}
@Test
public void testSafeHTMLNull()
{
try
{
JohnStringUtils.safeHTML( null );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: safeHTML source String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testContainsWhiteSpace()
{
assertTrue( JohnStringUtils.containsWhiteSpace( " containswhitespace" ) );
assertTrue( JohnStringUtils.containsWhiteSpace( "containswhitespace " ) );
assertTrue( JohnStringUtils.containsWhiteSpace( "contains whitespace" ) );
assertTrue( JohnStringUtils.containsWhiteSpace( "contains whitespace" ) );
assertFalse( JohnStringUtils.containsWhiteSpace( "containswhitespace" ) );
}
@Test
public void testContainsWhiteSpaceNull()
{
try
{
JohnStringUtils.containsWhiteSpace( null );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: containsWhiteSpace source String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testIsDigits()
{
assertTrue( JohnStringUtils.isDigits( "1234567890" ) );
assertFalse( JohnStringUtils.isDigits( "1234567890a" ) );
assertFalse( JohnStringUtils.isDigits( "a" ) );
assertFalse( JohnStringUtils.isDigits( "~\"\'!@#$%^&*()_+=-[]\\{}|;:<>" ) );
}
@Test
public void testIsDigitsNull()
{
try
{
JohnStringUtils.isDigits( null );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: isDigits source String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testIsDigitsEmpty()
{
try
{
JohnStringUtils.isDigits( "" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: isDigits source String cannot be empty" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
assertFalse( JohnStringUtils.isDigits( "" ) );
}
@Test
public void testContainsIllegalCharacter()
{
HashMap <Character , Character> illegalCharacterMap = new HashMap <Character , Character>();
illegalCharacterMap.put( 'c' , 'c' );
illegalCharacterMap.put( '-' , '-' );
assertTrue( JohnStringUtils.containsIllegalCharacter( "cccccc" , illegalCharacterMap ) );
assertTrue( JohnStringUtils.containsIllegalCharacter( "dash-illegal" , illegalCharacterMap ) );
assertTrue( JohnStringUtils.containsIllegalCharacter( "-illegal" , illegalCharacterMap ) );
assertTrue( JohnStringUtils.containsIllegalCharacter( "illegal-" , illegalCharacterMap ) );
assertFalse( JohnStringUtils.containsIllegalCharacter( "defghijklmnopqrstuvwxyz" , illegalCharacterMap ) );
}
@Test
public void testContainsIllegalCharacterSourceNull()
{
HashMap <Character , Character> illegalCharacterMap = new HashMap <Character , Character>();
try
{
JohnStringUtils.containsIllegalCharacter( null , illegalCharacterMap );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: containsIllegalCharacter source String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testContainsIllegalCharacterMapNull()
{
try
{
JohnStringUtils.containsIllegalCharacter( "source" , null );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: containsIllegalCharacter HashMap parameter cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testReverse()
{
String source = "this reversed";
String expected = "desrever siht";
assertEquals( expected , JohnStringUtils.reverse( source ) );
}
@Test
public void testReverseNull()
{
try
{
JohnStringUtils.reverse( null );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: reverse source String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testReverseSpellings()
{
String source = "this reversed";
String expected = "siht desrever";
assertEquals( expected , JohnStringUtils.reverseSpellings( source , " " ) );
}
@Test
public void testReverseSpellingsNull()
{
try
{
JohnStringUtils.reverseSpellings( null , "delimiter" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: reverseSpellings String parameter cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testReplaceTargetWithOneChar()
{
String source = "dc\\=domain,dc\\=example";
String target = "\\=";
char replacement = '=';
String expected = "dc=domain,dc=example";
assertEquals( expected , JohnStringUtils.replaceTargetWithOneChar( source , target , replacement ) );
}
@Test
public void testReplaceTargetWithOneCharMinimalInput()
{
String source = "\\=";
String target = "\\=";
char replacement = '=';
String expected = "=";
assertEquals( expected , JohnStringUtils.replaceTargetWithOneChar( source , target , replacement ) );
}
@Test
public void testReplaceTargetWithOneCharSourceNull()
{
try
{
JohnStringUtils.replaceTargetWithOneChar( null , "\\=" , '=' );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: replaceTargetWithOneChar source String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testReplaceTargetWithOneCharSourceEmpty()
{
assertEquals( "" , JohnStringUtils.replaceTargetWithOneChar( "" , "-" , '=' ) );
}
@Test
public void testReplaceTargetWithOneCharTargetNull()
{
try
{
JohnStringUtils.replaceTargetWithOneChar( "source" , null , '=' );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: replaceTargetWithOneChar target String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testReplaceTargetWithOneCharTargetEmpty()
{
try
{
JohnStringUtils.replaceTargetWithOneChar( "long string" , "" , '=' );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: replaceTargetWithOneChar target String cannot be empty" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testSourceToDelimitedList()
{
ArrayList <String> tokenList = JohnStringUtils.sourceToDelimitedList( "csv,type,format,source" , "," );
assertEquals( 4 , tokenList.size() );
assertEquals( "csv" , tokenList.get( 0 ) );
assertEquals( "type" , tokenList.get( 1 ) );
assertEquals( "format" , tokenList.get( 2 ) );
assertEquals( "source" , tokenList.get( 3 ) );
}
@Test
public void testSourceToDelimitedListSourceNull()
{
try
{
JohnStringUtils.sourceToDelimitedList( null , "delimiter" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: delimitedList source String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testSourceToDelimitedListSourceEmpty()
{
try
{
JohnStringUtils.sourceToDelimitedList( "" , "delimiter" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: delimiter is longer than source" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testSourceToDelimitedListSourceSmallerThanDelimiter()
{
try
{
JohnStringUtils.sourceToDelimitedList( "source" , "delimiter" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: delimiter is longer than source" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testSourceToDelimitedListDelimiterNull()
{
try
{
JohnStringUtils.sourceToDelimitedList( "source" , null );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: delimitedList delimiter String cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testSourceToDelimitedListDelimiterEmpty()
{
try
{
JohnStringUtils.sourceToDelimitedList( "source" , "" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: delimitedList delimiter String cannot be empty" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testSourceToDelimitedListSourceDoesNotContainDelimiter()
{
ArrayList <String> tokenList = JohnStringUtils.sourceToDelimitedList( "sourcesource" , "delimiter" );
assertEquals( 0 , tokenList.size() );
}
@Test
public void testSourceToDelimitedListSourceIsDelimiter()
{
ArrayList <String> tokenList = JohnStringUtils.sourceToDelimitedList( "delimiter" , "delimiter" );
assertEquals( 2 , tokenList.size() );
assertEquals( "" , tokenList.get( 0 ) );
assertEquals( "" , tokenList.get( 1 ) );
}
@Test
public void testSourceToDelimitedListLeadingTrailingDelimiter()
{
ArrayList <String> tokenList = JohnStringUtils.sourceToDelimitedList( "-dash-format-" , "-" );
assertEquals( 4 , tokenList.size() );
assertEquals( "" , tokenList.get( 0 ) );
assertEquals( "dash" , tokenList.get( 1 ) );
assertEquals( "format" , tokenList.get( 2 ) );
assertEquals( "" , tokenList.get( 3 ) );
}
@Test
public void testGetFirstStringContainsKeywordFromList()
{
ArrayList <String> source = new ArrayList <String>();
String keyword = "identifier";
String value = "identifier=value";
String skipStartWith = "#";
source.add( "#identifier=wrongvalue" );
source.add( value );
source.add( "identifier = value" );
source.add( "#identifier=wrongvalue" );
String result = JohnStringUtils.getFirstStringContainsKeywordFromList( source , keyword , skipStartWith );
assertEquals( value , result );
}
@Test
public void testGetSubstringAfterDelimiter()
{
String source = "identifier=value";
String value = "value";
String result = JohnStringUtils.getSubstringAfterDelimiter( source , "=" );
assertEquals( value , result );
}
@Test
public void testGetSubstringAfterDelimiterSourceNull()
{
try
{
JohnStringUtils.getSubstringAfterDelimiter( null , "=" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: source cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testGetSubstringAfterDelimiterSourceEmpty()
{
try
{
JohnStringUtils.getSubstringAfterDelimiter( "" , "=" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: source cannot be empty" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testGetSubstringAfterDelimiterDelimiterNull()
{
try
{
JohnStringUtils.getSubstringAfterDelimiter( "source" , null );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: delimiter cannot be null" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testGetSubstringAfterDelimiterDelimiterEmpty()
{
try
{
JohnStringUtils.getSubstringAfterDelimiter( "source" , "" );
}catch( IllegalArgumentException e )
{
assertEquals( "ERROR: delimiter cannot be empty" , e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testGetSubstringAfterDelimiterSourceSmallerThanDelimiter()
{
String source = "=";
String result = JohnStringUtils.getSubstringAfterDelimiter( source , "==" );
assertEquals( "" , result );
}
}// end class