diff options
Diffstat (limited to 'src/org/openoffice/tools/tests/CommandTest.java')
-rw-r--r-- | src/org/openoffice/tools/tests/CommandTest.java | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/org/openoffice/tools/tests/CommandTest.java b/src/org/openoffice/tools/tests/CommandTest.java new file mode 100644 index 0000000..06f4315 --- /dev/null +++ b/src/org/openoffice/tools/tests/CommandTest.java @@ -0,0 +1,134 @@ +package org.openoffice.tools.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openoffice.tools.options.Command; +import org.openoffice.tools.options.SubCommand; +import org.openoffice.tools.options.SubcommandLine; + +public class CommandTest { + + private Command mCommand; + + @Before + public void setUp() throws Exception { + mCommand = setupCommandLine(); + } + + @After + public void tearDown( ) throws Exception { + mCommand = null; + } + + @Test + public void testParseValid() { + String[] validArgs = new String[] { + "registration", + "-cpp", + "-s", + "TestImpl" + }; + + try { + SubcommandLine line = mCommand.parse( validArgs, new PosixParser() ); + assertNotNull( "Result of parsing is null", line.getCommandLine() ); + assertEquals( "Wrong subcommand", "registration", line.getSubcommand() ); + + // Check the presence of the last option and its value + assertTrue( "Option not recognized", line.getCommandLine().hasOption( "s" ) ); + assertEquals( "wrong value detected", "TestImpl", line.getCommandLine().getOptionValue( "s" ) ) ; + } catch (ParseException e) { + fail( "Unexcepted exception: " + e.getMessage() ); + } + } + + @Test + public void testParseSubcommandNameError() { + String[] validArgs = new String[] { + "failure", + "-cpp", + "-s", + "TestImpl" + }; + + try { + mCommand.parse( validArgs, new PosixParser() ); + fail( "Should have thrown an exception" ); + } catch (ParseException e) { + assertTrue( "Wrong exception message", e.getMessage().endsWith( "failure" ) ); + } + } + + @Test + public void testParseSubcommandArgsError() { + String[] validArgs = new String[] { + "registration", + "-cpp", + "--error", + "--err2" + }; + + try { + mCommand.parse( validArgs, new PosixParser() ); + fail( "Should have thrown an exception" ); + } catch (ParseException e) { + assertTrue( "Error message doesn't contain '--error'" , e.getMessage().contains( "--error" ) ); + } + } + + @Test( expected=ParseException.class ) + public void testParseSubcommandNoArg() throws Exception { + String[] validArgs = new String[] { + "uno-skeletonmaker" + }; + + mCommand.parse( validArgs, new PosixParser() ); + } + + private static Command setupCommandLine( ) { + Command cmdLine = new Command(); + + // General options + Options generalOptions = new Options(); + + OptionGroup langGroup = new OptionGroup(); + Option java5 = new Option( "java5", false, "generate output for Java 1.5 or later (is currently the default)" ); + Option java4 = new Option( "java4", false, "generate output for Java 1.4 or earlier" ); + Option cpp = new Option( "cpp", false, "generate output for C++" ); + langGroup.addOption( java4 ); + langGroup.addOption( java5 ); + langGroup.addOption( cpp ); + langGroup.setRequired( false ); + generalOptions.addOptionGroup( langGroup ); + + cmdLine.setCommonOptions( generalOptions ); + + + // component command options + Options compOptions = new Options( ); + compOptions.addOption( "t", true, "specifies an UNOIDL type name, e.g. com.sun.star.text.XText" ); + SubCommand component = new SubCommand( "component", compOptions, + "generates language specific code skeleton files using the implementation name as the file and class name" ); + cmdLine.addCommand( component ); + + // registration options + Options regOptions = new Options(); + regOptions.addOption( "s", true, "name of the implementation of a service to add to the registration system." ); + SubCommand registration = new SubCommand( "registration", regOptions, + "creates the registration code for a list of services implementations. The '-s' option has to be used at least once to specify the implementation name of a service to add to the registration system." ); + cmdLine.addCommand( registration ); + + return cmdLine; + } +} |