List of usage examples for java.lang System setOut
public static void setOut(PrintStream out)
From source file:ape_test.CLITest.java
public void testMainWithEmptyString() { PrintStream originalOut = System.out; OutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os); System.setOut(ps); String[] arg = new String[1]; arg[0] = "";/*from w w w . j av a 2 s .c o m*/ Main.main(arg); System.setOut(originalOut); assertNotSame("", os.toString()); }
From source file:cz.muni.fi.mir.mathmlunificator.MathMLUnificatorCommandLineToolTest.java
@Test public void testMain_latexml_non_operators() throws Exception { String testFile = "latexml"; String[] argv = { getTestResourceAsFilepath(testFile + ".input.xml") }; ByteArrayOutputStream stdoutContent = new ByteArrayOutputStream(); PrintStream stdout = System.out; System.setOut(new PrintStream(stdoutContent)); MathMLUnificatorCommandLineTool.main(argv); System.setOut(stdout);// ww w . j av a 2 s.c o m String output = stdoutContent.toString(StandardCharsets.UTF_8.toString()); System.out.println("testMain_latexml_non_operators non-operators output:\n" + output); assertEquals( IOUtils.toString(getExpectedXMLTestResource(testFile + ".non-operator"), StandardCharsets.UTF_8), output); }
From source file:edu.vt.middleware.crypt.signature.SignatureCliTest.java
/** * @param partialLine Partial command line. * @param pubKey Public key file.//from w w w .jav a2 s . com * @param privKey Private key file. * * @throws Exception On test failure. */ @Test(groups = { "cli", "signature" }, dataProvider = "testdata") public void testSignatureCli(final String partialLine, final String pubKey, final String privKey) throws Exception { final String pubKeyPath = KEY_DIR_PATH + pubKey; final String privKeyPath = KEY_DIR_PATH + privKey; final PrintStream oldStdOut = System.out; try { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outStream)); // Compute signature and verify it String fullLine = partialLine + " -sign " + " -key " + privKeyPath + " -in " + TEST_PLAINTEXT; logger.info("Testing signature CLI sign operation with command line:\n\t" + fullLine); SignatureCli.main(CliHelper.splitArgs(fullLine)); final String sig = outStream.toString(); Assert.assertTrue(sig.length() > 0); // Write signature out to file for use in verify step new File(TEST_OUTPUT_DIR).mkdir(); final File sigFile = new File(TEST_OUTPUT_DIR + "sig.out"); final BufferedOutputStream sigOs = new BufferedOutputStream(new FileOutputStream(sigFile)); try { sigOs.write(outStream.toByteArray()); } finally { sigOs.close(); } outStream.reset(); // Verify signature fullLine = partialLine + " -verify " + sigFile + " -key " + pubKeyPath + " -in " + TEST_PLAINTEXT; logger.info("Testing signature CLI verify operation with command " + "line:\n\t" + fullLine); SignatureCli.main(CliHelper.splitArgs(fullLine)); final String result = outStream.toString(); AssertJUnit.assertTrue(result.indexOf("SUCCESS") != -1); } catch (Exception e) { e.printStackTrace(); } finally { // Restore STDOUT System.setOut(oldStdOut); } }
From source file:org.apache.hadoop.hive.ql.session.TestClearDanglingScratchDir.java
public void redirectStdOutErr() { stdout = new ByteArrayOutputStream(); PrintStream psStdout = new PrintStream(stdout); origStdoutPs = System.out;/*ww w. j av a2s . co m*/ System.setOut(psStdout); stderr = new ByteArrayOutputStream(); PrintStream psStderr = new PrintStream(stderr); origStderrPs = System.err; System.setErr(psStderr); }
From source file:net.sourceforge.seqware.pipeline.plugins.GenericMetadataSaverTest.java
@Before @Override/*from w ww . ja v a 2s.c o m*/ public void setUp() { super.setUp(); instance = new ModuleRunner(); instance.setMetadata(metadata); outStream = new ByteArrayOutputStream(); errStream = new ByteArrayOutputStream(); PrintStream pso = new PrintStream(outStream); PrintStream pse = new PrintStream(errStream) { @Override public PrintStream append(CharSequence csq) { return super.append(csq); } @Override public void print(String s) { super.print(s); } }; System.setOut(pso); System.setErr(pse); }
From source file:org.g_node.converter.ConvCliToolControllerTest.java
/** * Tests the run method of the {@link ConvCliToolController}. * @throws Exception//from w w w. ja v a2 s .c o m */ @Test public void runTest() throws Exception { // TODO check if this should be split up into different tests. // Set up environment final PrintStream stdout = System.out; final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outStream)); final PrintStream errout = System.err; final ByteArrayOutputStream errStream = new ByteArrayOutputStream(); System.setErr(new PrintStream(errStream)); Logger rootLogger = Logger.getRootLogger(); rootLogger.setLevel(Level.INFO); rootLogger.addAppender(new ConsoleAppender(new PatternLayout("[%-5p] %m%n"))); // Create test files final String tmpRoot = System.getProperty("java.io.tmpdir"); final String testFolderName = "fileservicetest"; final String testFileName = "test.txt"; final Path testFileFolder = Paths.get(tmpRoot, testFolderName); final File currTestFile = testFileFolder.resolve(testFileName).toFile(); FileUtils.write(currTestFile, "This is a normal test file"); final String testTurtleFileName = "test.ttl"; final String testTurtleDefaultOutName = "test_out.ttl"; final File currTurtleTestFile = testFileFolder.resolve(testTurtleFileName).toFile(); FileUtils.write(currTurtleTestFile, ""); final String testInvalidFileName = "test.rdf"; final File currInvalidTestFile = testFileFolder.resolve(testInvalidFileName).toFile(); FileUtils.write(currInvalidTestFile, "I shall crash!"); final String outFileName = testFileFolder.resolve("out.ttl").toString(); final CommandLineParser parser = new DefaultParser(); final Options useOptions = this.convCont.options(); String[] args; CommandLine cmd; // Test missing argument args = new String[1]; args[0] = "-i"; try { parser.parse(useOptions, args, false); } catch (MissingArgumentException e) { assertThat(e).hasMessage("Missing argument for option: i"); } // Test non existent input file args = new String[2]; args[0] = "-i"; args[1] = "iDoNotExist"; cmd = parser.parse(useOptions, args, false); this.convCont.run(cmd); assertThat(outStream.toString()).contains("Input file iDoNotExist does not exist"); outStream.reset(); // Test existing input file, unsupported file type args = new String[2]; args[0] = "-i"; args[1] = currTestFile.toString(); cmd = parser.parse(useOptions, args, false); this.convCont.run(cmd); assertThat(outStream.toString()) .contains(String.join("", "[ERROR] Input RDF file ", currTestFile.toString(), " cannot be read.")); outStream.reset(); // Test provide supported input file type, unsupported output RDF format args = new String[4]; args[0] = "-i"; args[1] = currTurtleTestFile.toString(); args[2] = "-f"; args[3] = "iDoNotExist"; cmd = parser.parse(useOptions, args, false); this.convCont.run(cmd); assertThat(outStream.toString()).contains("[ERROR] Unsupported output format: 'IDONOTEXIST'"); outStream.reset(); // Test provide supported input file type, test write output file w/o providing output filename args = new String[2]; args[0] = "-i"; args[1] = currTurtleTestFile.toString(); cmd = parser.parse(useOptions, args, false); this.convCont.run(cmd); assertThat(outStream.toString()).contains(String.join("", "[INFO ] Writing data to RDF file '", testFileFolder.resolve(testTurtleDefaultOutName).toString(), "' using format 'TTL'")); outStream.reset(); // Test invalid RDF input file args = new String[2]; args[0] = "-i"; args[1] = currInvalidTestFile.toString(); cmd = parser.parse(useOptions, args, false); this.convCont.run(cmd); assertThat(outStream.toString()).contains("[line: 1, col: 1 ] Content is not allowed in prolog."); outStream.reset(); // Test writing to provided output file does not match provided output format args = new String[6]; args[0] = "-i"; args[1] = currTurtleTestFile.toString(); args[2] = "-o"; args[3] = outFileName; args[4] = "-f"; args[5] = "JSON-LD"; cmd = parser.parse(useOptions, args, false); this.convCont.run(cmd); assertThat(outStream.toString()).contains(String.join("", "[INFO ] Writing data to RDF file '", outFileName, ".jsonld' using format 'JSON-LD'")); outStream.reset(); // Test writing to provided output file args = new String[4]; args[0] = "-i"; args[1] = currTurtleTestFile.toString(); args[2] = "-o"; args[3] = outFileName; cmd = parser.parse(useOptions, args, false); this.convCont.run(cmd); assertThat(outStream.toString()).contains( String.join("", "[INFO ] Writing data to RDF file '", outFileName, "' using format 'TTL'")); outStream.reset(); // Clean up if (Files.exists(testFileFolder)) { FileUtils.deleteDirectory(testFileFolder.toFile()); } rootLogger.removeAllAppenders(); System.setOut(stdout); System.setErr(errout); }
From source file:org.apache.hadoop.streaming.UtilTest.java
void redirectIfAntJunit() throws IOException { boolean fromAntJunit = System.getProperty("test.build.data") != null; if (fromAntJunit) { new File(antTestDir_).mkdirs(); File outFile = new File(antTestDir_, testName_ + ".log"); PrintStream out = new PrintStream(new FileOutputStream(outFile)); System.setOut(out); System.setErr(out);//from w w w.ja v a 2s . co m } }
From source file:org.apache.tika.cli.TikaCLIBatchIT.java
@After public void tearDown() throws Exception { System.setOut(new PrintStream(out, true, UTF_8.name())); System.setErr(new PrintStream(err, true, UTF_8.name())); //TODO: refactor to use our deleteDirectory with straight path //FileUtils.deleteDirectory(tempOutputDir.toFile()); }
From source file:functionaltests.TagCommandsFunctTest.java
@Test public void testListJobTaskIds() throws Exception { typeLine("listtasks(+" + jobId.longValue() + ")"); runCli();//from w w w .j a va 2 s . c om String out = this.capturedOutput.toString(); System.setOut(stdOut); System.out.println("------------- testListJobTaskIds:"); System.out.println(out); assertTrue(out.contains("T1#1")); assertTrue(out.contains("Print1#1")); assertTrue(out.contains("Print2#1")); assertTrue(out.contains("T2#1")); assertTrue(out.contains("T1#2")); assertTrue(out.contains("Print1#2")); assertTrue(out.contains("Print2#2")); assertTrue(out.contains("T2#2")); }
From source file:org.apache.wiki.util.CryptoUtilTest.java
public void testCommandLineVerify() throws Exception { // Save old printstream PrintStream oldOut = System.out; // Swallow System out and get command output OutputStream out = new ByteArrayOutputStream(); System.setOut(new PrintStream(out)); CryptoUtil.main(new String[] { "--verify", "testing123", "{SSHA}yfT8SRT/WoOuNuA6KbJeF10OznZmb28=" }); String output = new String(out.toString()); // Restore old printstream System.setOut(oldOut);// w w w .j av a 2 s.co m // Run our tests assertTrue(output.startsWith("true")); }