Example usage for java.lang System setOut

List of usage examples for java.lang System setOut

Introduction

In this page you can find the example usage for java.lang System setOut.

Prototype

public static void setOut(PrintStream out) 

Source Link

Document

Reassigns the "standard" output stream.

Usage

From source file:net.sf.mpaxs.spi.computeHost.Host.java

@Override
public void configure(Configuration cfg) {
    settings = new Settings(cfg);
    Logger.getLogger(Host.class.getName()).log(Level.INFO, "Running ComputeHost at IP {0}",
            settings.getLocalIp());/*  w w  w  . ja  va2 s  .c  om*/
    File baseDir = new File(settings.getOption(ConfigurationKeys.KEY_COMPUTE_HOST_WORKING_DIR));
    try {
        //remove leftovers from previous run
        FileUtils.deleteDirectory(baseDir);
    } catch (IOException ex) {
        Logger.getLogger(Host.class.getName()).log(Level.SEVERE, null, ex);
    }
    baseDir.mkdirs();
    settings.setOption(ConfigurationKeys.KEY_COMPUTE_HOST_ERROR_FILE,
            new File(baseDir, "error.txt").getAbsolutePath());
    settings.setOption(ConfigurationKeys.KEY_COMPUTE_HOST_OUTPUT_FILE,
            new File(baseDir, "output.txt").getAbsolutePath());
    try {
        System.setErr(new PrintStream(
                new FileOutputStream(settings.getOption(ConfigurationKeys.KEY_COMPUTE_HOST_ERROR_FILE))));
        System.setOut(new PrintStream(
                new FileOutputStream(settings.getOption(ConfigurationKeys.KEY_COMPUTE_HOST_OUTPUT_FILE))));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Host.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.codice.ddf.catalog.pubsub.command.ListCommandTest.java

/**
 * Test subscriptions:list command with no args. Should return all registered subscriptions.
 *
 * @throws Exception/*from w w  w. j  ava 2 s . com*/
 */
@Test
public void testListNoArgsSubscriptionsFound() throws Exception {
    ListCommand listCommand = new ListCommand();

    BundleContext bundleContext = mock(BundleContext.class);
    listCommand.setBundleContext(bundleContext);

    ServiceReference mySubscription = mock(ServiceReference.class);
    when(mySubscription.getPropertyKeys()).thenReturn(new String[] { SUBSCRIPTION_ID_PROPERTY_KEY });
    when(mySubscription.getProperty("subscription-id")).thenReturn(MY_SUBSCRIPTION_ID);

    ServiceReference yourSubscription = mock(ServiceReference.class);
    when(yourSubscription.getPropertyKeys()).thenReturn(new String[] { SUBSCRIPTION_ID_PROPERTY_KEY });
    when(yourSubscription.getProperty(SUBSCRIPTION_ID_PROPERTY_KEY)).thenReturn(YOUR_SUBSCRIPTION_ID);

    ServiceReference[] refs = new ServiceReference[] { mySubscription, yourSubscription };
    when(bundleContext.getServiceReferences(eq(SubscriptionsCommand.SERVICE_PID), anyString()))
            .thenReturn(refs);

    PrintStream realSystemOut = System.out;

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    System.setOut(new PrintStream(buffer));

    // when
    listCommand.execute();

    /* cleanup */
    System.setOut(realSystemOut);

    // then
    List<String> linesWithText = getConsoleOutputText(buffer);
    assertThat(linesWithText.size(), is(4));
    assertThat(linesWithText, hasItems(containsString("Total subscriptions found: 2"),
            containsString(MY_SUBSCRIPTION_ID), containsString(YOUR_SUBSCRIPTION_ID)));

    buffer.close();
}

From source file:com.complexible.stardog.ext.spring.batch.TestSpringBatch.java

/**
 * We'll add 20 triples of the form:/*from  w w  w .java 2s. co m*/
 *   
 *   <urn:test:resource> <urn:test:predicate> "lit{0..20}"
 * 
 * @throws Exception
 */
@Before
public void setUp() throws Exception {
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));
    SnarlTemplate tmp = new SnarlTemplate();
    tmp.setDataSource(dataSource);
    tmp.doWithAdder(new AdderCallback<Boolean>() {
        @Override
        public Boolean add(Adder adder) throws StardogException {
            String uriA = "urn:test:resource";
            String uriB = "urn:test:predicate";

            for (int i = 0; i < 20; i++) {
                adder.statement(new URIImpl(uriA), new URIImpl(uriB), new LiteralImpl("lit" + i));
            }
            return true;
        }
    });

}

From source file:org.apache.solr.hadoop.MapReduceIndexerToolArgumentParserTest.java

@Before
public void setUp() throws Exception {
    super.setUp();
    AbstractZkTestCase.SOLRHOME = solrHomeDirectory;
    FileUtils.copyDirectory(MINIMR_INSTANCE_DIR, solrHomeDirectory);

    conf = new Configuration();
    parser = new MapReduceIndexerTool.MyArgumentParser();
    opts = new MapReduceIndexerTool.Options();
    oldSystemOut = System.out;//from  w  w w . j a  v  a  2s. c  o  m
    bout = new ByteArrayOutputStream();
    System.setOut(new PrintStream(bout, true, "UTF-8"));
    oldSystemErr = System.err;
    berr = new ByteArrayOutputStream();
    System.setErr(new PrintStream(berr, true, "UTF-8"));
}

From source file:edu.vt.middleware.crypt.digest.DigestCliTest.java

/**
 * @param  partialLine  Partial command line.
 * @param  expected  Expected result of digest operation.
 *
 * @throws  Exception  On test failure./*from   w w w. j  ava  2s  . co m*/
 */
@Test(groups = { "cli", "digest" }, dataProvider = "testdata")
public void testDigestCli(final String partialLine, final String expected) throws Exception {
    final PrintStream oldStdOut = System.out;
    try {
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outStream));

        final String fullLine = partialLine + " -in " + TEST_PLAINTEXT;
        logger.info("Testing digest CLI with command line:\n\t" + fullLine);
        DigestCli.main(CliHelper.splitArgs(fullLine));

        final String result = outStream.toString();
        AssertJUnit.assertTrue(result.length() > 0);
        if (expected != null) {
            AssertJUnit.assertEquals(expected, result.trim());
        }
    } finally {
        // Restore STDOUT
        System.setOut(oldStdOut);
    }
}

From source file:org.pentaho.di.kitchen.KitchenIT.java

@Test
public void testArchivedJobsExecution() throws Exception {
    String file = this.getClass().getResource("test-kjb.zip").getFile();
    String[] args = new String[] { "/file:zip:file://" + file + "!Job.kjb" };
    oldOut = System.out;//www.j ava 2 s.c o m
    oldErr = System.err;
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));
    try {
        Kitchen.main(args);
    } catch (SecurityException e) {
        System.setOut(oldOut);
        System.setErr(oldErr);
        System.out.println(outContent);
        assertFalse(outContent.toString().contains("result=[false]"));
        assertFalse(outContent.toString().contains("ERROR"));
    }
}

From source file:org.apache.hadoop.hive.metastore.tools.TestSchemaToolCatalogOps.java

@AfterClass
public static void removeDb() throws Exception {
    File metaStoreDir = new File(testMetastoreDB);
    if (metaStoreDir.exists()) {
        FileUtils.forceDeleteOnExit(metaStoreDir);
    }/*from ww  w . ja  v  a 2  s .c  om*/
    System.setOut(outStream);
    System.setErr(errStream);
}

From source file:ape_test.CLITest.java

public void testMainWithEmptyArray() {
    PrintStream originalOut = System.out;
    OutputStream os = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(os);
    System.setOut(ps);

    String[] arg = new String[0];
    Main.main(arg);/*  ww  w.ja  va  2  s .  c  om*/
    System.setOut(originalOut);
    assertNotSame("", os.toString());
}

From source file:org.apache.hadoop.hive.common.JavaUtils.java

public static void closeClassLoader(ClassLoader loader) throws IOException {
    if (loader instanceof Closeable) {
        ((Closeable) loader).close();
    } else if (SUN_MISC_UTIL_RELEASE != null && loader instanceof URLClassLoader) {
        PrintStream outputStream = System.out;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PrintStream newOutputStream = new PrintStream(byteArrayOutputStream);
        try {/*from  www.ja va 2  s.  c o m*/
            // SUN_MISC_UTIL_RELEASE.invoke prints to System.out
            // So we're changing the outputstream for that call,
            // and setting it back to original System.out when we're done
            System.setOut(newOutputStream);
            SUN_MISC_UTIL_RELEASE.invoke(null, loader);
            String output = byteArrayOutputStream.toString("UTF8");
            LOG.debug(output);
        } catch (InvocationTargetException e) {
            if (e.getTargetException() instanceof IOException) {
                throw (IOException) e.getTargetException();
            }
            throw new IOException(e.getTargetException());
        } catch (Exception e) {
            throw new IOException(e);
        } finally {
            System.setOut(outputStream);
            newOutputStream.close();
        }
    }
    LogFactory.release(loader);
}

From source file:org.marketcetera.util.file.WriterWrapperTest.java

private void testStandardOutputStreamUnicode() throws Exception {
    PrintStream stdOutSave = System.out;
    CloseableRegistry r = new CloseableRegistry();
    ByteArrayOutputStream stdOutByteArray = new ByteArrayOutputStream();
    try {/*from w  w w  .  ja  va 2  s  .  c  o m*/
        r.register(stdOutByteArray);
        PrintStream stdOut = new PrintStream(stdOutByteArray);
        r.register(stdOut);
        System.setOut(stdOut);
        WriterWrapper wrapper = new WriterWrapper(SpecialNames.STANDARD_OUTPUT, SignatureCharset.UTF8_UTF8);
        r.register(wrapper);
        assertTrue(wrapper.getSkipClose());
        assertNotNull(wrapper.getWriter());
        wrapper.getWriter().write(COMBO);
    } finally {
        System.setOut(stdOutSave);
        r.close();
    }
    assertArrayEquals(ArrayUtils.addAll(Signature.UTF8.getMark(), COMBO_UTF8), stdOutByteArray.toByteArray());
}