List of usage examples for java.lang System setIn
public static void setIn(InputStream in)
From source file:Main.java
public static void main(String[] args) throws Exception { System.setIn(new FileInputStream("file.txt")); char ret = (char) System.in.read(); System.out.println(ret);// w w w . java 2 s. c o m }
From source file:Main.java
public static void main(String[] args) { String str = "java2s.com"; ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); System.setIn(bais); Scanner scanner = new Scanner(System.in); String input = scanner.next(); System.out.println(input);//from ww w . j ava2 s. c om }
From source file:MainClass.java
public static void main(String[] args) throws IOException { PrintStream console = System.out; BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java")); PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out"))); System.setIn(in); System.setOut(out);/*from w w w. j a va 2 s. c o m*/ System.setErr(out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) System.out.println(s); out.close(); System.setOut(console); }
From source file:Redirect.java
public static void main(String args[]) throws Exception { PrintStream origOut = System.out; PrintStream origErr = System.err; InputStream stdin = null;//from w w w . j a v a 2 s . c o m stdin = new FileInputStream("Redirect.in"); PrintStream stdout = null; stdout = new PrintStream(new FileOutputStream("Redirect.out")); PrintStream stderr = null; stderr = new PrintStream(new FileOutputStream("Redirect.err")); origOut.println("1"); System.out.println("2"); origOut.println("3"); System.err.println("4"); origErr.println("5"); System.setIn(stdin); System.setOut(stdout); System.setErr(stderr); origOut.println("\nR"); System.out.println("T"); origOut.println("Tq"); System.err.println("Tqw"); origErr.println("Test"); origOut.println("\nRedirect: Round #3"); int inChar = 0; while (-1 != inChar) { try { inChar = System.in.read(); } catch (Exception e) { // Clean up the output and bail. origOut.print("\n"); break; } origOut.write(inChar); } stdin.close(); stdout.close(); stderr.close(); System.exit(0); }
From source file:Redirecting.java
public static void main(String[] args) throws IOException { PrintStream console = System.out; BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java")); PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out"))); System.setIn(in); System.setOut(out);/*from w w w .j a v a2s.co m*/ System.setErr(out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) System.out.println(s); out.close(); // Remember this! System.setOut(console); }
From source file:com.github.robozonky.cli.GoogleCredentialsFeatureTest.java
@BeforeEach void replaceSystemIn() { System.setIn(IOUtils.toInputStream("", Defaults.CHARSET)); }
From source file:com.github.robozonky.cli.GoogleCredentialsFeatureTest.java
@AfterEach void restoreSystemIn() { System.setIn(originalSystemIn); }
From source file:MyTest.java
@Test public void test() throws Exception { InputStream input = null;//from w ww.j a v a 2 s .c o m InputStream expectedOutput = null; int i = 0; do { input = Thread.currentThread().getContextClassLoader().getResourceAsStream("input" + String.valueOf(i)); expectedOutput = Thread.currentThread().getContextClassLoader() .getResourceAsStream("output" + String.valueOf(i)); if (input != null) { System.out.println("Running case #" + String.valueOf(i) + " input:"); System.out.println(IOUtils.toString(Thread.currentThread().getContextClassLoader() .getResourceAsStream("input" + String.valueOf(i)), "US-ASCII")); System.setIn(input); if (expectedOutput != null) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); PrintStream realOutput = new PrintStream(byteArrayOutputStream); PrintStream preservedOut = System.out; System.setOut(realOutput); long start = System.currentTimeMillis(); MySolution.main(null); long end = System.currentTimeMillis(); System.setOut(preservedOut); System.out.println("Output:"); System.out.println(byteArrayOutputStream.toString("US-ASCII")); System.out.println("Time: " + (end - start)); assertEquals(IOUtils.toString(expectedOutput, "US-ASCII"), byteArrayOutputStream.toString("US-ASCII")); } else { MySolution.main(null); } } i++; } while (input != null); }
From source file:net.jperf.LogParserTest.java
public void testLogParserMain() throws Exception { PrintStream realOut = System.out; ByteArrayOutputStream fakeOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(fakeOut, true)); try {/*from w ww . j a v a 2s .c om*/ //usage realOut.println("-- Usage Test --"); LogParser.runMain(new String[] { "--help" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("Usage") >= 0); fakeOut.reset(); //log on std in, write to std out InputStream realIn = System.in; ByteArrayInputStream fakeIn = new ByteArrayInputStream(testLog.getBytes()); System.setIn(fakeIn); try { realOut.println("-- Std in -> Std out Test --"); LogParser.runMain(new String[0]); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("tag") >= 0 && fakeOut.toString().indexOf("tag2") >= 0 && fakeOut.toString().indexOf("tag3") >= 0); fakeOut.reset(); } finally { System.setIn(realIn); } //Log from a file FileUtils.writeStringToFile(new File("./target/logParserTest.log"), testLog); //log from file, write to std out realOut.println("-- File in -> Std out Test --"); LogParser.runMain(new String[] { "./target/logParserTest.log" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("tag") >= 0 && fakeOut.toString().indexOf("tag2") >= 0 && fakeOut.toString().indexOf("tag3") >= 0); fakeOut.reset(); //CSV format test realOut.println("-- File in -> Std out Test with CSV --"); LogParser.runMain(new String[] { "-f", "csv", "./target/logParserTest.log" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("\"tag\",") >= 0 && fakeOut.toString().indexOf("\"tag2\",") >= 0 && fakeOut.toString().indexOf("\"tag3\",") >= 0); fakeOut.reset(); //log from file, write to file realOut.println("-- File in -> File out Test --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "./target/logParserTest.log" }); String statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); assertTrue( statsOut.indexOf("tag") >= 0 && statsOut.indexOf("tag2") >= 0 && statsOut.indexOf("tag3") >= 0); //log from file, write to file, different timeslice realOut.println("-- File in -> File out with different timeslice Test --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "--timeslice", "120000", "./target/logParserTest.log" }); statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); assertTrue( statsOut.indexOf("tag") >= 0 && statsOut.indexOf("tag2") >= 0 && statsOut.indexOf("tag3") >= 0); //missing param test realOut.println("-- Missing param test --"); assertEquals(1, LogParser.runMain(new String[] { "./target/logParserTest.log", "-o" })); //unknown arg test realOut.println("-- Unknown arg test --"); assertEquals(1, LogParser.runMain(new String[] { "./target/logParserTest.log", "--foo" })); realOut.println(fakeOut); assertTrue(fakeOut.toString().indexOf("Unknown") >= 0); //graphing test realOut.println("-- File in -> File out with graphing --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "-g", "./target/perfGraphs.out", "./src/test/resources/net/jperf/dummyLog.txt" }); statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); String graphsOut = FileUtils.readFileToString(new File("./target/perfGraphs.out")); realOut.println(graphsOut); assertTrue(graphsOut.indexOf("chtt=TPS") > 0 && graphsOut.indexOf("chtt=Mean") > 0); } finally { System.setOut(realOut); } }
From source file:edu.stanford.muse.email.VerifyEmailSetup.java
public static Pair<Boolean, String> run() { PrintStream savedOut = System.out; InputStream savedIn = System.in; try {/* www . j a va2 s. c o m*/ // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(true); String filename = System.getProperty("java.io.tmpdir") + File.separatorChar + "verifyEmailSetup"; PrintStream ps = new PrintStream(new FileOutputStream(filename)); System.setOut(ps); System.setErr(ps); // Get a Store object Store store = null; store = session.getStore("imaps"); store.connect("imap.gmail.com", 993, "checkmuse", ""); // not the real password. unfortunately, the checkmuse a/c will get blocked by google. // Folder folder = store.getFolder("[Gmail]/Sent Mail"); // int totalMessages = folder.getMessageCount(); // System.err.println (totalMessages + " messages!"); ps.close(); String contents = Util.getFileContents(filename); System.out.println(contents); return new Pair<Boolean, String>(Boolean.TRUE, contents); } catch (AuthenticationFailedException e) { /* its ok if auth failed. we only want to check if IMAPS network route is blocked. when network is blocked, we'll get something like javax.mail.MessagingException: No route to host; nested exception is: java.net.NoRouteToHostException: No route to host ... */ log.info("Verification succeeded: " + Util.stackTrace(e)); return new Pair<Boolean, String>(Boolean.TRUE, ""); } catch (Exception e) { log.warn("Verification failed: " + Util.stackTrace(e)); return new Pair<Boolean, String>(Boolean.FALSE, e.toString()); // stack track reveals too much about our code... Util.stackTrace(e)); } finally { System.setOut(savedOut); System.setIn(savedIn); } }