FileDocCategorySizeDatePackage
ArgHelperTest.javaAPI DocGlassfish v2 API10677Fri May 04 22:24:48 BST 2007com.sun.cli.jmx.cmd

ArgHelperTest

public class ArgHelperTest extends TestCase

Fields Summary
Constructors Summary
public ArgHelperTest(String name)

		super( name );
	
Methods Summary
private static voiddm(java.lang.Object msg)

		System.out.println( SmartStringifier.toString( msg ) );
	
protected voidsetUp()

	
ArgHelpersetupArgHelper(int startOffset, java.lang.String options, java.lang.String input)

		final ArgHelperOptionsInfo optionsInfo	= new ArgHelperOptionsInfo();
		
		if ( options != null )
		{
			optionsInfo.addOptions( options );
		}
		
		final String []	tokens	= (input.length() == 0) ?
							new String [0] : input.split( "" + ArgHelperOptionsInfo.MULTIPLE_DELIM );
		
		final ListIterator	iter	= Arrays.asList( tokens ).listIterator( startOffset );
		final ArgHelper		argHelper = new ArgHelperImpl( iter, optionsInfo );
		
		return( argHelper );
	
ArgHelpersetupArgHelper(java.lang.String options, java.lang.String input)

		return( setupArgHelper( 0, options, input ) );
	
protected voidtearDown()

	
public voidtestBooleanFailureWithEquals()

		final String	booleanOptions	= "b1";
		final String	input			= "--b1=hello";
		
		try
		{
			ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
			argHelper.getBoolean( "b1", null );
			fail( "expecte this input to fail for boolean: " + input );
		}
		catch( Exception e )
		{
		}
	
public voidtestBooleanFailureWithSeparateArg()

		final String	booleanOptions	= "testbool";
		final String	input			= "--testbool false";// 'false' is an operand
		
		final ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
		
		final Boolean	value	= argHelper.getBoolean( booleanOptions, null );
		assertEquals( "expected 1 operand from: " + input, 1, argHelper.getOperands().length );
		assertEquals( "expected 1 option from: " + input, 1, argHelper.countOptions() );
	
public voidtestBooleanFailureWithSeparateArgTrue()

		final String	booleanOptions	= "testbool";
		final String	input			= "--testbool true";// 'true' is an operand
		
		final ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
		
		final Boolean	value	= argHelper.getBoolean( booleanOptions, null );
		assertEquals( "expected 1 operand from: " + input, 1, argHelper.getOperands().length );
		assertEquals( "expected 1 option from: " + input, 1, argHelper.countOptions() );
	
public voidtestBooleanWithInlineArgFalse()

		final String	booleanOptions	= "testbool";
		final String	input			= "--testbool=false";
		
		final ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
		
		final Boolean	value	= argHelper.getBoolean( booleanOptions, null );
		assertEquals( "expected FALSE from " + input, Boolean.FALSE, value );
	
public voidtestBooleanWithInlineArgTrue()

		final String	booleanOptions	= "testbool";
		final String	input			= "--testbool=true";
		
		final ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
		
		final Boolean	value	= argHelper.getBoolean( booleanOptions, null );
		assertEquals( "expected TRUE from " + input, Boolean.TRUE, value );
	
public voidtestBooleanWithoutArg()

		final String	booleanOptions	= "testbool";
		final String	input			= "--testbool";
		
		final ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
		
		final Boolean	value	= argHelper.getBoolean( booleanOptions, null );
		assertEquals( "expected true from " + input, Boolean.TRUE, value );
	
public voidtestIllegalOptionsFailure()

		try
		{
			final ArgHelper	argHelper	= setupArgHelper( "", "--option1=1" );
		}
		catch( Exception e )
		{
			// we expected to get here
		}
	
public voidtestInteger0()

		final ArgHelper	argHelper	= setupArgHelper( "i1,1", "--i1=0" );
		
		final Integer	value	= argHelper.getInteger( "i1" );
		assertEquals(Integer.valueOf(0), value);
	
public voidtestIntegerNegative()

		final ArgHelper	argHelper	= setupArgHelper( "i1,1", "--i1=-9999" );
		
		final Integer	value	= argHelper.getInteger( "i1" );
		assertEquals(Integer.valueOf(-9999), value);
	
public voidtestIntegerPositive()

		final ArgHelper	argHelper	= setupArgHelper( "i1,1", "--i1=9999" );
		
		final Integer	value	= argHelper.getInteger( "i1" );
		assertEquals(Integer.valueOf(9999), value);
	
public voidtestMultipleArgsWithEquals()

		final ArgHelper	argHelper	= setupArgHelper( "a1,4", "--a1=1,2,hello,there op1 op2" );
		
		final String []	values	= argHelper.getArgValues( "a1" );
		assertEquals( "expected 4 options", values.length, 4 );
		assertEquals( "expected 2 operands", 2, argHelper.getOperands().length );
	
public voidtestMultipleArgsWithSpaces()

		final ArgHelper	argHelper	= setupArgHelper( "a1,4", "--a1 1 2 hello there op1 op2" );
		
		final String []	values	= argHelper.getArgValues( "a1" );
		assertEquals( values.length, 4 );
		assertEquals( "expected 2 operands", 2, argHelper.getOperands().length );
	
public voidtestMultipleBooleans()

		final String	booleanOptions	= "b1 b2 b3 b4";
		final String	input			= "--b1 --b2=false --b3 --b4=false";
		
		final ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
		
		assertEquals( "expected b1 to be true from: " + input,
			Boolean.TRUE, argHelper.getBoolean( "b1", null ) );
			
		assertEquals( "expected b2 to be false from: " + input,
			Boolean.FALSE, argHelper.getBoolean( "b2", null ) );
			
		assertEquals( "expected b3 to be true from: " + input,
			Boolean.TRUE, argHelper.getBoolean( "b3", null ) );
			
		assertEquals( "expected b4 to be false from: " + input,
			Boolean.FALSE, argHelper.getBoolean( "b4", null ) );
	
public voidtestNoOperands()

		final ArgHelper	argHelper	= setupArgHelper( "b1 b2 s1,1", "--b1 --b2 --s1=hello" );
		
		assertEquals( "expecting 0 operands", 0, argHelper.getOperands().length );
		assertEquals( "expecting 3 options", 3, argHelper.countOptions() );
		
		final String	value	= argHelper.getString( "s1", null);
		assertEquals( "hello", value );
	
public voidtestNoOptionsNoOperands()

		final ArgHelper	argHelper	= setupArgHelper( "s1,1", "--s1=" );
		
		final String	value	= argHelper.getString( "s1", null);
		assertEquals( "", value );
	
public voidtestNoOptionsNoOperandsWithCmd()

		final ArgHelper	argHelper	= setupArgHelper( 1, null, "cmd" );
		
		assertEquals( "expecting no options", 0, argHelper.countOptions() );
		assertEquals( "expecting no operands", 0, argHelper.getOperands().length );
	
public voidtestNoOptionsOrOperands()

		final ArgHelper	argHelper	= setupArgHelper( null, "" );
		
		assertEquals( "expected no options", 0, argHelper.countOptions() );
		assertEquals( "expected no operands", 0, argHelper.getOperands().length );
	
public voidtestOnlyOperands()

		final ArgHelper	argHelper	= setupArgHelper( "", "op1 op2" );
		
		assertEquals( "expecting 2 operands", 2, argHelper.getOperands().length );
		assertEquals( "expecting 0 options", 0, argHelper.countOptions() );
	
public voidtestSingleCharBoolean()

		final String	booleanOptions	= "b";
		final String	input	= "-b";
			
		try
		{
			final ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
			
			assertEquals( "expected TRUE for: " + input,
				Boolean.TRUE, argHelper.getBoolean( booleanOptions, null) );
		}
		catch( Exception e )
		{
			// we expected to get here
		}
	
public voidtestSingleCharBooleanWithValue()

		final String	booleanOptions	= "b";
		final String	input	= "-b=false";
			
		try
		{
			final ArgHelper	argHelper	= setupArgHelper( booleanOptions, input );
			
			assertEquals( "expected FALSE for: " + input,
				Boolean.FALSE, argHelper.getBoolean( booleanOptions, null) );
		}
		catch( Exception e )
		{
			// we expected to get here
		}