FileDocCategorySizeDatePackage
TokenizerTest.javaAPI DocGlassfish v2 API7264Fri May 04 22:25:00 BST 2007com.sun.cli.util

TokenizerTest

public class TokenizerTest extends TestCase

Fields Summary
public static final String
WHITE_SPACE
public static final char
ESCAPE_CHAR
public static final String
ESCAPABLE_CHARS
private static final char
BACKSLASH
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) );
	
protected voidsetUp()

	
protected voidtearDown()

	
public voidtestEmpty()

		final Tokenizer tk	= create( "" );
		
		assertEquals( "expecting no tokens", 0, tk.getTokens().length );
		
		try
		{	
			tk.iterator().next();
			fail( "expecting exception trying to get token" );
		}
		catch( Exception e )
		{
		}
	
public voidtestEscapedNewlineCR()

	
	
		 
	
	
		final String	TEST	= "test" + BACKSLASH + "n" + BACKSLASH + "r";
		final String []	tokens	= create( TEST ).getTokens();
		
		assertEquals( 1, tokens.length );
		assertEquals( "test\n\r", 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 voidtestLeadingDelim()

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

		final String	input	= "hello" + WHITE_SPACE + "there" + 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 Iterator	iter	= create( input ).iterator();
		
		int		count	= 0;
		String	temp	= "";
		while ( iter.hasNext() )
		{
			++count;
			temp	= temp + " " + iter.next();
		}
		assertEquals( "expecting 7 tokens", 7, count );
		assertEquals( "expecting match", input, temp.substring( 1, temp.length() ));
	
public voidtestQuotedString()

		final String	input	= "\"hello there\" \"another\" \"3\" \"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 );
	
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 voidtestUnescapableChar()

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

		final String	input1	= "hello there 1 2 3 4 5";
		final String	input2	= "hello\tthere 1\t2 3 4\t5";
		final Iterator	iter1	= create( input1 ).iterator();
		final Iterator	iter2	= create( input2 ).iterator();
		
		while ( iter1.hasNext() )
		{
			assertEquals( "expecting equal results from different white space",
				iter1.next(), iter2.next() );
		}