AbstractVersionedMain extends AbstractMain { @Override protected int run(CliModeObject mode) throws Exception { if (this.version) { assert (CliMode.ID_VERSION.equals(mode.getId())); getStandardOutput().println(getVersion()); return 0; } else if (CliMode.ID_DEFAULT.equals(mode.getId())) { return runDefaultMode(); } return EXIT_CODE_ILLEGAL_SYNTAX; }    }
@Test public void testHelp() { AbstractVersionedMain main = new TestMain(); StringBuilder errorBuffer = new StringBuilder(); main.setStandardError(errorBuffer); StringBuilder outputBuffer = new StringBuilder(); main.setStandardOutput(outputBuffer); int exitCode = main.run(new String[] { "--help" }); String error = errorBuffer.toString(); Assert.assertEquals("", error); Assert.assertEquals(0, exitCode); String help = outputBuffer.toString(); System.out.println(help); Assert.assertTrue(help.contains(main.getClass().getName())); Assert.assertTrue(help.contains(TestMain.USAGE)); }
@Test public void testVersion() { AbstractVersionedMain main = new TestMain(); StringBuilder errorBuffer = new StringBuilder(); main.setStandardError(errorBuffer); StringBuilder outputBuffer = new StringBuilder(); main.setStandardOutput(outputBuffer); int exitCode = main.run(new String[] { "--version" }); Assert.assertEquals(0, exitCode); String version = outputBuffer.toString(); Assert.assertEquals("42.1.2.3" + System.lineSeparator(), version); Assert.assertEquals("", errorBuffer.toString()); }
@Test public void testDefaultWithEndOptions() { AbstractVersionedMain main = new TestMain(); StringBuilder buffer = new StringBuilder(); main.setStandardError(buffer); main.setStandardOutput(buffer); String argumentValue = "-argument-"; int exitCode = main.run(new String[] { "--", argumentValue }); String output = buffer.toString(); Assert.assertEquals(output, 0, exitCode); Assert.assertEquals(argumentValue, output); }
@Test public void testDefault() { AbstractVersionedMain main = new TestMain(); StringBuilder buffer = new StringBuilder(); main.setStandardError(buffer); main.setStandardOutput(buffer); String argumentValue = "42argument42"; int exitCode = main.run(new String[] { argumentValue }); String output = buffer.toString(); Assert.assertEquals(output, 0, exitCode); Assert.assertEquals(argumentValue, output); }
@Test public void testDefaultWithCombinedOptions() { AbstractVersionedMain main = new TestMain(); StringBuilder buffer = new StringBuilder(); main.setStandardError(buffer); main.setStandardOutput(buffer); String argumentValue = "-argument-"; int exitCode = main.run(new String[] { "-fb", "--", argumentValue }); String output = buffer.toString(); Assert.assertEquals(output, 0, exitCode); Assert.assertEquals("foo bar " + argumentValue, output); }
@Test public void testDefaultWithLongOption() { AbstractVersionedMain main = new TestMain(); StringBuilder buffer = new StringBuilder(); main.setStandardError(buffer); main.setStandardOutput(buffer); String argumentValue = "-argument-"; int exitCode = main.run(new String[] { "--bar", "--", argumentValue }); String output = buffer.toString(); Assert.assertEquals(output, 0, exitCode); Assert.assertEquals("bar " + argumentValue, output); }