FileDocCategorySizeDatePackage
TokenizerTest.javaAPI DocGlassfish v2 API11276Fri May 04 22:26:00 BST 2007com.sun.enterprise.admin.util

TokenizerTest

public class TokenizerTest extends TestCase

Fields Summary
public static final char
TAB
public static final char
BACKSLASH
public static final char
QUOTE_CHAR
public static final String
WHITE_SPACE
public static final char
ESCAPE_CHAR
public static final String
ESCAPABLE_CHARS
static final char
QUOTE
Constructors Summary
public TokenizerTest(String name)

		super( name );
	
Methods Summary
Tokenizercreate(java.lang.String input, boolean multipleDelimsCountAsOne)

	
		
	     
		 
	
		return( new TokenizerImpl( input,
			WHITE_SPACE, multipleDelimsCountAsOne, ESCAPE_CHAR, ESCAPABLE_CHARS) );
	
Tokenizercreate(java.lang.String input)

		return( new TokenizerImpl( input,
			WHITE_SPACE, false, ESCAPE_CHAR, ESCAPABLE_CHARS) );
	
static java.lang.Stringquote(java.lang.String s)

		 
	   
	
		return( QUOTE + s + QUOTE );
	
protected voidsetUp()

	
protected voidtearDown()

	
public voidtestAdjacentQuotedStrings()

		final String	input	= quote( "xxx" ) + quote( "yyy" ) + quote( "" );
		final String []	tokens	= create( input ).getTokens();
		
		assertEquals( 1, tokens.length );
		assertEquals( "xxxyyy", tokens[ 0 ] );
	
public voidtestEmpty()

		final Tokenizer tk	= create( "" );
		
		assertEquals( "expecting no tokens", 0, tk.getTokens().length );
	
public voidtestEmptyQuotedString()

		final String	input	= quote( "" );
		final String []	tokens	= create( input ).getTokens();
		
		assertEquals( 1, tokens.length );
		assertEquals( "", tokens[ 0 ] );
	
public voidtestEmptyQuotedStrings()

		final String	input	= quote( "" ) + quote( "" ) + quote( "" ) + quote( "" );
		final String []	tokens	= create( input ).getTokens();
		
		assertEquals( 1, tokens.length );
		assertEquals( "", tokens[ 0 ] );
	
public voidtestEmptyQuotedStringsSeparatedByDelim()

		final String	input	= quote( "" ) + WHITE_SPACE.charAt( 0 ) + quote( "" );
		final String []	tokens	= create( input ).getTokens();
		
		assertEquals( 2, tokens.length );
	
public voidtestEscapedNewlineCR()

		final String	TEST	= "test" + BACKSLASH + "n" + BACKSLASH + "r" + BACKSLASH + "t";
		final String []	tokens	= create( TEST ).getTokens();
		
		assertEquals( 1, tokens.length );
		assertEquals( "test\n\r\t", tokens[ 0 ] );
	
public voidtestEscaping()

		// create a String which each escapable character is represented
		final StringBuffer	b	= new StringBuffer();
		for( int i = 0; i < ESCAPABLE_CHARS.length(); ++i )
		{
			b.append( "\\" + ESCAPABLE_CHARS.charAt( i ) );
		}
		final String []	tokens	= create( b.toString() ).getTokens();
		
		assertEquals( "expecting 1 token", 1, tokens.length );
		assertEquals( "expecting match", ESCAPABLE_CHARS, tokens[ 0 ] );
	
public voidtestIllegalEscapeSequence()

		final String	input	= BACKSLASH + "x";
		try
		{
			final String []	tokens	= create( input ).getTokens();
			fail( "expected to fail: " + input);
		}
		catch( TokenizerException e )
		{
		}
	
public voidtestIllegalUnicodeSequence()

		final String	input1	= BACKSLASH + "u";
		final String	input2	= input1 + "zzzz";
		final String	input3	= input1 + "abcx";
		
		try
		{
			create( input1 ).getTokens();
			fail( "expected to fail: " + input1);
			create( input2 ).getTokens();
			fail( "expected to fail: " + input2);
			create( input3 ).getTokens();
			fail( "expected to fail: " + input3);
		}
		catch( TokenizerException e )
		{
		}
	
public voidtestLeadingDelim()

		final String	input	= WHITE_SPACE.charAt( 0 ) + "hello";
		final String []	tokens	= create( input, false ).getTokens();
		
		assertEquals( 2, tokens.length );
		assertEquals( "", tokens[ 0 ] );
		assertEquals( "hello", tokens[ 1 ] );
	
public voidtestLegalUnicodeSequence()

		final String	input	= BACKSLASH + "u0020";	// unicode for the space char
		
		final String[]	tokens	= create( input ).getTokens();
		assertEquals( 1, tokens.length );
		assertEquals( " ", tokens[ 0 ] );
	
public voidtestMultipleDelimsAsOne()

		final String	input	= "HELLO" + WHITE_SPACE + "THERE" + WHITE_SPACE + WHITE_SPACE;
		final String []	tokens	= create( input, true ).getTokens();
		
		assertEquals( 3, tokens.length );
	
public voidtestMultipleDelimsWithNoData()

		final String	input	= "" + WHITE_SPACE.charAt( 0 );
		final String []	tokens	= create( input, false ).getTokens();
		
		assertEquals( input.length() + 1, tokens.length );
	
public voidtestMultipleTokens()

		final String	input	= "hello there 1 2 3 4 5";
		final String[]	tokens	= create( input ).getTokens();
		
		assertEquals( "expecting 7 tokens", 7, tokens.length );
		assertEquals( tokens[ 0 ], "hello" );
		assertEquals( tokens[ 1 ], "there" );
		assertEquals( tokens[ 2 ], "1" );
		assertEquals( tokens[ 3 ], "2" );
		assertEquals( tokens[ 4 ], "3" );
		assertEquals( tokens[ 5 ], "4" );
		assertEquals( tokens[ 6 ], "5" );
	
public voidtestOnlyDelims()

		final String	input	= WHITE_SPACE + WHITE_SPACE + WHITE_SPACE;
		final String []	tokens	= create( input, false ).getTokens();
		
		assertEquals( (WHITE_SPACE.length() * 3) + 1, tokens.length );
	
public voidtestQuotedDelim()

		final String	input	= quote( " " ) + " " + quote( " " ) + BACKSLASH + " ";
		final String []	tokens	= create( input ).getTokens();
		
		assertEquals( 2, tokens.length );
		assertEquals( " ", tokens[ 0 ] );
		assertEquals( "  ", tokens[ 1 ] );
	
public voidtestQuotedString()

		final String	input	= quote( "hello there" ) + " " + quote( "another" ) + " " +
			quote( "3" ) + " " + quote( "words" ) + " ";
		final String []	tokens	= create( input ).getTokens();
		
		assertEquals( 5, tokens.length );
		assertEquals( "hello there", tokens[ 0 ] );
		assertEquals( "another", tokens[ 1 ] );
		assertEquals( "3", tokens[ 2 ] );
		assertEquals( "words", tokens[ 3 ] );
		assertEquals( "", tokens[ 4 ] );
	
public voidtestSingleDelimOnly()

		final String	input	= "" + WHITE_SPACE.charAt( 0 );
		final String []	tokens	= create( input, false ).getTokens();
		
		assertEquals( 2, tokens.length );
		assertEquals( "", tokens[ 0 ] );
		assertEquals( "", tokens[ 1 ] );
	
public voidtestSingleToken()

		final String	input	= "hello";
		final String []	tokens	= create( input, false ).getTokens();
		
		assertEquals( "expecting 1 token", 1, tokens.length );
		assertEquals( "expecting " + input, input, tokens[ 0 ] );
	
public voidtestTrailingDelimiter1()

		final String	input	= "hello" + WHITE_SPACE.charAt( 0 );
		final String []	tokens	= create( input, false ).getTokens();
		
		assertEquals( 2, tokens.length );
	
public voidtestTrailingDelimiter2()

		final String	input	= "hello" + WHITE_SPACE.charAt( 0 ) +
							"\"there\"" + WHITE_SPACE.charAt( 0 );
		final String []	tokens	= create( input, false ).getTokens();
		
		assertEquals( 3, tokens.length );
	
public voidtestTrailingEscapedDelim()

		final String	input	= "x." + BACKSLASH + ".";
		final String[]	tokens	= new TokenizerImpl( input,
									".", false, BACKSLASH, BACKSLASH + "." ).getTokens();
									
		assertEquals( 2, tokens.length );
		assertEquals( "x", tokens[ 0 ] );
		assertEquals( ".", tokens[ 1 ] );
	
public voidtestUnterminatedLiteralString()

		final String	input1	= "" + QUOTE;
		final String	input2	= QUOTE + "xxx";
		final String	input3	= "xxx" + QUOTE;
		try
		{
			create( input1 ).getTokens();
			fail( "expected to fail: " + input1);
			
			create( input2 ).getTokens();
			fail( "expected to fail: " + input2);
			
			create( input3 ).getTokens();
			fail( "expected to fail: " + input3);
		}
		catch( TokenizerException e )
		{
		}
	
public voidtestWhiteSpaceEquality()

		final String	input1	= "hello there 1 2 3 4 5";
		final String	input2	= "hello\tthere 1\t2 3 4\t5";
		final String[]	tokens1	= create( input1 ).getTokens();
		final String[]	tokens2	= create( input2 ).getTokens();
		
		
		assertEquals( tokens1.length, tokens2.length );
		for( int i = 0; i < tokens1.length; ++i )
		{
			assertEquals( tokens1[ i ], tokens2[ i ] );
		}