@Override public int execute( final OverthereExecutionOutputHandler stdoutHandler, final OverthereExecutionOutputHandler stderrHandler, final CmdLine commandLine) { final OverthereProcess process = startProcess(commandLine); Thread stdoutReaderThread = null; Thread stderrReaderThread = null; final CountDownLatch latch = new CountDownLatch(2); try { stdoutReaderThread = getThread("stdout", commandLine.toString(), stdoutHandler, process.getStdout(), latch); stdoutReaderThread.start(); stderrReaderThread = getThread("stderr", commandLine.toString(), stderrHandler, process.getStderr(), latch); stderrReaderThread.start(); try { latch.await(); return process.waitFor(); } catch (InterruptedException exc) { Thread.currentThread().interrupt(); logger.info("Execution interrupted, destroying the process."); process.destroy(); throw new RuntimeIOException("Execution interrupted", exc); } } finally { quietlyJoinThread(stdoutReaderThread); quietlyJoinThread(stderrReaderThread); } }
/** Test normal arguments. */ public void testNormal() throws Exception { final String[] args = new String[] {B1, I1, "1", SA1, "hello", "world", S1, "goodnight", "arg1", "arg2"}; cmdLine.parse(args); assertCorrectValues(); cmdLine.parse( "@" + new File(Files.toFile("src/test/resources", getClass()), "CmdLineTest.txt")); assertCorrectValues(); }
/** Test missing argument. */ public void testMissingArg() throws IOException { final CmdLine cl = new CmdLine(getClass(), "Mangotiger Login Server"); cl.addInteger("localport", "Port on local machine to listen to"); cl.addInteger("dataport", "Port to redirect new users to"); cl.addString( "datahost", "Host to redirect to (machine name or dotted quad)", InetAddress.getLocalHost().getHostName()); try { cl.parse(new String[] {""}); fail(); } catch (CliException e) { // expected } }
/** Test no arguments. */ public void testNoArguments() throws Exception { try { cmdLine.parse(Strings.EMPTY_ARRAY); fail(); } catch (CliException e) { // expected } }
/** Test bad file. */ public void testBadFile() { try { cmdLine.parse(new String[] {"@bad-file"}); fail(); } catch (IOException expected) { assertTrue(expected.getMessage().indexOf("bad-file") != -1); } }
static OptionInfo createFromField(Field field) { Preconditions.checkNotNull(field); CmdLine cmdLine = field.getAnnotation(CmdLine.class); if (cmdLine == null) { throw new Configuration.ConfigurationException( "No @CmdLine Arg annotation for field " + field); } @SuppressWarnings("unchecked") OptionInfo optionInfo = new OptionInfo( checkValidName(cmdLine.name()), cmdLine.help(), ArgumentInfo.getArgForField(field), TypeUtil.getTypeParamTypeToken(field), field.getDeclaringClass().getCanonicalName(), Arrays.asList(field.getAnnotations()), cmdLine.parser()); return optionInfo; }
/** Test bad switch. */ public void testBadSwitch() throws IOException { try { final String[] args = new String[] { "-bad-switch", B1, I1, "1", SA1, "hello", "world", S1, "goodnight", "arg1", "arg2" }; cmdLine.parse(args); fail(); } catch (CliException e) { assertTrue(e.getMessage().indexOf("Unrecognized switch name (-bad-switch).") != -1); } }
@Override protected void setUp() throws Exception { super.setUp(); cmdLine = new CmdLine(getClass(), CL_DESCRIPTION); b1 = cmdLine.addBoolean(B1.substring(1), B1_DESCRIPTION); b2 = cmdLine.addBoolean(B2.substring(1), B2_DESCRIPTION, B2_DEFAULT); b3 = cmdLine.addBoolean(B3.substring(1), B3_DESCRIPTION, B3_DEFAULT); i1 = cmdLine.addInteger(I1.substring(1), I1_DESCRIPTION); i2 = cmdLine.addInteger(I2.substring(1), I2_DESCRIPTION, I2_DEFAULT); i3 = cmdLine.addInteger(I3.substring(1), I3_DESCRIPTION, I3_DEFAULT); sa1 = cmdLine.addStringArray(SA1.substring(1), SA1_DESCRIPTION); sa2 = cmdLine.addStringArray(SA2.substring(1), SA2_DESCRIPTION, SA2_DEFAULT); s1 = cmdLine.addString(S1.substring(1), S1_DESCRIPTION); s2 = cmdLine.addString(S2.substring(1), S2_DESCRIPTION, S2_DEFAULT); }
private void assertCorrectValues() { assertEquals(Boolean.TRUE, b1.getValue()); assertEquals(B2_DEFAULT, b2.value()); assertEquals(B3_DEFAULT, b3.value()); assertEquals(1, i1.value()); assertEquals(I2_DEFAULT, i2.value()); assertEquals(I3_DEFAULT, i3.value()); assertEquals(Arrays.asList(new String[] {"hello", "world"}), Arrays.asList(sa1.value())); assertEquals(Arrays.asList(SA2_DEFAULT), Arrays.asList(sa2.value())); assertEquals(s1.value(), "goodnight"); assertEquals(s2.value(), S2_DEFAULT); assertEquals(cmdLine.getArguments(), Arrays.asList(new String[] {"arg1", "arg2"})); assertEquals(Boolean.TRUE.toString(), b1.toString()); assertEquals(S2_DEFAULT, s2.toString()); final String expected = getClass().getName() + "{?=false" + ", b1=true, b2=true, b3=false" + ", i1=1, i2=2, i3=-1" + ", s1=goodnight, s2=s2-default" + ", sa1=[hello, world], sa2=[1, 2, 3]}" + "[arg1, arg2]"; assertEquals(expected, cmdLine.toString()); }
/** Test help. */ public void testHelp() throws IOException { try { cmdLine.parse(new String[] {"-?"}); fail(); } catch (CliException e) { final String expect = getClass().getName() + " [-?] [-b1] [-b2] [-b3] {-i1} [-i2] [-i3] {-s1} " + "[-s2] [-sa1 ...] [-sa2 ...]" + "\n\tdescription" + "\n\t-? Print usage text. Optional. No arguments." + "\n\t-b1 b1-description. Optional. No arguments." + "\n\t-b2 b2-description. Optional. No arguments." + "\n\t-b3 b3-description. Optional. No arguments." + "\n\t-i1 i1-description. Required. One argument." + "\n\t-i2 i2-description. Optional. One argument." + "\n\t-i3 i3-description. Optional. One argument." + "\n\t-s1 s1-description. Required. One argument." + "\n\t-s2 s2-description. Optional. One argument." + "\n\t-sa1 sa1-description. Optional. Zero or more arguments." + "\n\t-sa2 sa2-description. Optional. Zero or more arguments."; assertEquals(expect, cmdLine.getUsage()); } }
/** Simple test. */ public void test() { cmdLine = new CmdLine(getClass(), "description"); cmdLine.addString("string", "string description"); cmdLine.addStringArray("stringArray", "stringArray description"); cmdLine.addStringArray("stringArray2", "stringArray2 description", new String[] {"1", "2"}); }
/** Test empty array index. */ public void testEmptyArrayIndex() throws Exception { final String[] args = new String[] {B1, I1, "1", SA1, S1, "goodnight", "arg1", "arg2"}; cmdLine.parse(args); assertEquals(0, sa1.value().length); }