public static void setIn(InputStream in) { System.setIn(in); }
public static void setIn(InputStream in) { System.setIn(in); }
/** */ public static void main(String[] args) { LatticeXMLReader reader = new LatticeXMLReader(); try { System.setIn(new FileInputStream(args[0])); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } reader.load(System.in); int numLattices = 0; for(Lattice lattice : reader) { System.out.println(lattice.toString()); numLattices++; } System.out.printf("\nLoaded %d lattices\n", numLattices); } }
public static void main(String[] args) throws IOException { // 1. create the pipes PipedInputStream inPipe = new PipedInputStream(); PipedInputStream outPipe = new PipedInputStream(); // 2. set the System.in and System.out streams System.setIn(inPipe); System.setOut(new PrintStream(new PipedOutputStream(outPipe), true)); PrintWriter inWriter = new PrintWriter(new PipedOutputStream(inPipe), true); // 3. create the gui JFrame frame = new JFrame("\"Console\""); frame.add(console(outPipe, inWriter)); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); // 4. write some output (to JTextArea) System.out.println("Hello World!"); System.out.println("Test"); System.out.println("Test"); System.out.println("Test"); // 5. get some input (from JTextArea) Scanner s = new Scanner(System.in); System.out.printf("got from input: \"%s\"%n", s.nextLine()); }
protected static void resetStreamsAndSendOutput() { System.setOut(ORIGINAL_STDOUT); System.setErr(ORIGINAL_STDERR); System.setIn(ORIGINAL_STDIN); LOG.info("Sending stdout content through logger: \n\n{}\n\n", outContent.toString()); LOG.info("Sending stderr content through logger: \n\n{}\n\n", errContent.toString()); }
@After public void tearDown() throws Exception { System.setIn(stdin); StubJobLauncher.tearDown(); }
@Before public void setUp() throws Exception { JobExecution jobExecution = new JobExecution(null, new Long(1), null, null); ExitStatus exitStatus = ExitStatus.COMPLETED; jobExecution.setExitStatus(exitStatus); StubJobLauncher.jobExecution = jobExecution; stdin = System.in; System.setIn(new InputStream() { @Override public int read() { return -1; } }); }
@Test public void testInteractiveOptionReadsFromStdInMultiLinePrompt() { class App { @Option(names = "-x", description = {"Pwd%nline2", "ignored"}, interactive = true) int x; @Option(names = "-z") int z; } PrintStream out = System.out; InputStream in = System.in; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); System.setOut(new PrintStream(baos)); System.setIn(new ByteArrayInputStream("123".getBytes())); App app = new App(); CommandLine cmd = new CommandLine(app); cmd.parse("-x", "-z", "987"); String expectedPrompt = format("Enter value for -x (Pwd%nline2): "); assertEquals(expectedPrompt, baos.toString()); assertEquals(123, app.x); assertEquals(987, app.z); } finally { System.setOut(out); System.setIn(in); } }
@Test public void testInteractivePositionalReadsFromStdIn() { class App { @Parameters(index = "0", description = {"Pwd%nline2", "ignored"}, interactive = true) int x; @Parameters(index = "1") int z; } PrintStream out = System.out; InputStream in = System.in; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); System.setOut(new PrintStream(baos)); System.setIn(new ByteArrayInputStream("123".getBytes())); App app = new App(); CommandLine cmd = new CommandLine(app); cmd.parse("987"); String expectedPrompt = format("Enter value for position 0 (Pwd%nline2): "); assertEquals(expectedPrompt, baos.toString()); assertEquals(123, app.x); assertEquals(987, app.z); } finally { System.setOut(out); System.setIn(in); } }
System.setIn(in);
@Test public void testInteractivePositional2ReadsFromStdIn() { class App { @Parameters(index = "0") int a; @Parameters(index = "1", description = {"Pwd%nline2", "ignored"}, interactive = true) int x; @Parameters(index = "2") int z; } PrintStream out = System.out; InputStream in = System.in; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); System.setOut(new PrintStream(baos)); System.setIn(new ByteArrayInputStream("123".getBytes())); App app = new App(); CommandLine cmd = new CommandLine(app); cmd.parse("333", "987"); String expectedPrompt = format("Enter value for position 1 (Pwd%nline2): "); assertEquals(expectedPrompt, baos.toString()); assertEquals(333, app.a); assertEquals(123, app.x); assertEquals(987, app.z); } finally { System.setOut(out); System.setIn(in); } }
System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); System.setIn(in);
@Test public void testInteractiveOptionReadsFromStdIn() { class App { @Option(names = "-x", description = {"Pwd", "line2"}, interactive = true) int x; @Option(names = "-z") int z; } PrintStream out = System.out; InputStream in = System.in; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); System.setOut(new PrintStream(baos)); System.setIn(new ByteArrayInputStream("123".getBytes())); App app = new App(); CommandLine cmd = new CommandLine(app); cmd.parse("-x"); assertEquals("Enter value for -x (Pwd): ", baos.toString()); assertEquals(123, app.x); assertEquals(0, app.z); cmd.parse("-z", "678"); assertEquals(0, app.x); assertEquals(678, app.z); } finally { System.setOut(out); System.setIn(in); } }
@Test public void testWithStdinCommandLine() throws Throwable { System.setIn(new InputStream() { char[] input = (jobPath+"\n"+jobName+"\nfoo=bar\nspam=bucket").toCharArray(); int index = 0; @Override public int available() { return input.length - index; } @Override public int read() { return index<input.length-1 ? (int) input[index++] : -1; } }); CommandLineJobRunner.main(new String[0]); assertEquals(0, StubSystemExiter.status); assertEquals(2, StubJobLauncher.jobParameters.getParameters().size()); }
@Test public void testWithStdinCommandLineWithEmptyLines() throws Throwable { System.setIn(new InputStream() { char[] input = (jobPath+"\n"+jobName+"\nfoo=bar\n\nspam=bucket\n\n").toCharArray(); int index = 0; @Override public int available() { return input.length - index; } @Override public int read() { return index<input.length-1 ? (int) input[index++] : -1; } }); CommandLineJobRunner.main(new String[0]); assertEquals(0, StubSystemExiter.status); assertEquals(2, StubJobLauncher.jobParameters.getParameters().size()); }
@Test public void testWithStdinParameters() throws Throwable { String[] args = new String[] { jobPath, jobName }; System.setIn(new InputStream() { char[] input = ("foo=bar\nspam=bucket").toCharArray(); int index = 0; @Override public int available() { return input.length - index; } @Override public int read() { return index<input.length-1 ? (int) input[index++] : -1; } }); CommandLineJobRunner.main(args); assertEquals(0, StubSystemExiter.status); assertEquals(2, StubJobLauncher.jobParameters.getParameters().size()); }
@Test public void testWithInvalidStdin() throws Throwable { System.setIn(new InputStream() { @Override public int available() throws IOException { throw new IOException("Planned"); } @Override public int read() { return -1; } }); CommandLineJobRunner.main(new String[] { jobPath, jobName }); assertEquals(0, StubSystemExiter.status); assertEquals(0, StubJobLauncher.jobParameters.getParameters().size()); }