Example usage for java.lang System setIn

List of usage examples for java.lang System setIn

Introduction

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

Prototype

public static void setIn(InputStream in) 

Source Link

Document

Reassigns the "standard" input stream.

Usage

From source file:org.apache.hadoop.hdfs.server.namenode.TestNameNodeFormat.java

/**
 * Test namenode format with -format when the name directory exists and enter
 * N when prompted. Format should be aborted.
 * /*from   w  w  w  . jav a  2  s. c o  m*/
 * @throws IOException
 * @throws InterruptedException
 */
@Test
public void testFormatWithoutForceEnterN() throws IOException, InterruptedException {

    // create the directory
    if (!hdfsDir.mkdirs()) {
        fail("Failed to create dir " + hdfsDir.getPath());
    }

    // capture the input stream
    InputStream origIn = System.in;
    ByteArrayInputStream bins = new ByteArrayInputStream("N\n".getBytes());
    System.setIn(bins);
    String[] argv = { "-format" };
    try {
        NameNode.createNameNode(argv, config);
        fail("createNameNode() did not call System.exit()");
    } catch (ExitException e) {
        assertEquals("Format should not have succeeded", 1, e.status);
    }

    System.setIn(origIn);

    // check if the version file does not exists.
    File version = new File(hdfsDir, "current/VERSION");
    assertFalse("Check version should not exist", version.exists());
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestNameNodeFormat.java

/**
 * Test namenode format with -format option when the name directory exists and
 * enter Y when prompted. Format should succeed.
 * /*from   ww  w  . j  a  v a 2 s.co  m*/
 * @throws IOException
 * @throws InterruptedException
 */
@Test
public void testFormatWithoutForceEnterY() throws IOException, InterruptedException {

    // create the directory
    if (!hdfsDir.mkdirs()) {
        fail("Failed to create dir " + hdfsDir.getPath());
    }

    // capture the input stream
    InputStream origIn = System.in;
    ByteArrayInputStream bins = new ByteArrayInputStream("Y\n".getBytes());
    System.setIn(bins);
    String[] argv = { "-format" };
    try {
        NameNode.createNameNode(argv, config);
        fail("createNameNode() did not call System.exit()");
    } catch (ExitException e) {
        assertEquals("Format should have succeeded", 0, e.status);
    }

    System.setIn(origIn);

    // check if the version file does exist.
    File version = new File(hdfsDir, "current/VERSION");
    assertTrue("Check version file should exist", version.exists());
}

From source file:org.apache.hadoop.hdfs.tools.TestDFSHAAdmin.java

/**
 * Setup System.in with a stream that feeds a "yes" answer on the
 * next prompt./*  ww  w . ja  va2 s.co m*/
 */
private static void setupConfirmationOnSystemIn() {
    // Answer "yes" to the prompt about transition to active
    System.setIn(new ByteArrayInputStream("yes\n".getBytes()));
}

From source file:org.apache.pig.test.TestParamSubPreproc.java

@Test
public void testGruntWithParamSub() throws Exception {
    log.info("Starting test testGruntWithParamSub()");
    File inputFile = Util.createFile(new String[] { "daniel\t10", "jenny\t20" });
    File outputFile = File.createTempFile("tmp", "");
    outputFile.delete();//  w  ww  .j  a v a2 s.co  m
    PigContext pc = new PigContext(ExecType.LOCAL, new Properties());
    String command = "%default agelimit `echo 15`\n" + "rmf $outputFile;\n" + "a = load '"
            + Util.generateURI(inputFile.toString(), pc) + "' as ($param1:chararray, $param2:int);\n"
            + "b = filter a by age > $agelimit;" + "store b into '$outputFile';\n" + "quit\n";
    System.setProperty("jline.WindowsTerminal.directConsole", "false");
    System.setIn(new ByteArrayInputStream(command.getBytes()));
    org.apache.pig.PigRunner.run(new String[] { "-x", "local", "-p", "param1=name", "-p", "param2=age", "-p",
            "outputFile=" + Util.generateURI(outputFile.toString(), pc) }, null);
    File[] partFiles = outputFile.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.startsWith("part");
        }
    });
    String resultContent = Util.readFile(partFiles[0]);
    assertEquals(resultContent, "jenny\t20\n");
}

From source file:org.apache.pig.test.TestParamSubPreproc.java

@Test
public void testGruntMultilineDefine() throws Exception {
    log.info("Starting test testGruntMultilineDefine()");
    File inputFile = Util.createFile(new String[] { "daniel\t10", "jenny\t20" });
    File outputFile = File.createTempFile("tmp", "");
    outputFile.delete();/*from   w w w  .j a  v a2 s .c o m*/
    PigContext pc = new PigContext(ExecType.LOCAL, new Properties());
    String command = "DEFINE process(input_file) returns data {\n"
            + "$data = load '$input_file' using PigStorage(',');};\n" + "b = process('"
            + Util.generateURI(inputFile.toString(), pc) + "');\n" + "store b into '"
            + Util.generateURI(outputFile.toString(), pc) + "';" + "quit\n";
    System.setProperty("jline.WindowsTerminal.directConsole", "false");
    System.setIn(new ByteArrayInputStream(command.getBytes()));
    org.apache.pig.PigRunner.run(new String[] { "-x", "local" }, null);
    File[] partFiles = outputFile.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.startsWith("part");
        }
    });
    String resultContent = Util.readFile(partFiles[0]);
    assertEquals(resultContent, "daniel\t10\njenny\t20\n");
}

From source file:org.apache.taverna.scufl2.wfdesc.TestConvertToWfdesc.java

@After
public void restoreStd() {
    System.setIn(origIn);
    System.setOut(origOut);
    System.setErr(origErr);
}

From source file:org.apache.taverna.scufl2.wfdesc.TestConvertToWfdesc.java

@Test
public void stdin() throws Exception {
    byte[] input = IOUtils.toByteArray(getClass().getResourceAsStream("/" + HELLOWORLD_T2FLOW));
    assertTrue(input.length > 0);/*from  ww  w  . j a v a  2 s  . c  om*/
    InputStream in = new ByteArrayInputStream(input);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream outBuf = new PrintStream(out);
    try {
        System.setIn(in);
        System.setOut(outBuf);
        ConvertToWfdesc.main(new String[] {});
    } finally {
        restoreStd();
    }
    out.flush();
    out.close();
    String turtle = out.toString("utf-8");
    //System.out.println(turtle);
    assertTrue(turtle.contains("Hello_World"));
    assertTrue(turtle.contains("processor/hello/out/value"));
}

From source file:org.azyva.dragom.test.integration.IntegrationTestSuite.java

/**
 * Main method./*from   w  w  w .j a v a 2  s  .c  om*/
 *
 * @param args Arguments.
 */
public static void main(String[] args) {
    Set<String> setTestCategory;
    boolean indAllTests;

    System.setSecurityManager(new NoExitSecurityManager());

    EclipseSynchronizeErrOut.fix();

    IntegrationTestSuite.inputStreamDouble = new InputStreamDouble();
    System.setIn(IntegrationTestSuite.inputStreamDouble);

    if (args.length == 0) {
        IntegrationTestSuite.pathTestWorkspace = Paths.get(System.getProperty("user.dir"))
                .resolve("test-workspace");
        System.out.println(
                "Test workspace directory not specified. Using \"test-workspace\" subdirectory of current directory "
                        + IntegrationTestSuite.pathTestWorkspace + '.');
    } else {
        IntegrationTestSuite.pathTestWorkspace = Paths.get(args[0]);
        System.out.println(
                "Using specified test workspace directory " + IntegrationTestSuite.pathTestWorkspace + '.');

        args = Arrays.copyOfRange(args, 1, args.length);
    }

    setTestCategory = new HashSet<String>(Arrays.asList(args));
    indAllTests = setTestCategory.contains("all");

    if (indAllTests || setTestCategory.contains("DragomToolInvoker")) {
        IntegrationTestSuiteDragomToolInvoker.testDragomToolInvoker();
    }

    if (indAllTests || setTestCategory.contains("ExecContextManagerTool")) {
        IntegrationTestSuiteExecContextManagerTool.testExecContextManagerTool();
    }

    if (indAllTests || setTestCategory.contains("RootManagerTool")) {
        IntegrationTestSuiteRootManagerTool.testRootManagerTool();
    }

    if (indAllTests || setTestCategory.contains("CredentialManagerTool")) {
        IntegrationTestSuiteCredentialManagerTool.testCredentialManagerTool();
    }

    if (indAllTests || setTestCategory.contains("GenericRootModuleVersionJobInvokerTool")) {
        IntegrationTestSuiteGenericRootModuleVersionJobInvokerTool.testGenericRootModuleVersionJobInvokerTool();
    }

    if (indAllTests || setTestCategory.contains("CheckoutToolBase")) {
        IntegrationTestSuiteCheckoutToolBase.testCheckoutToolBase();
    }

    if (indAllTests || setTestCategory.contains("CheckoutToolConflict")) {
        IntegrationTestSuiteCheckoutToolConflict.testCheckoutToolConflict();
    }

    if (indAllTests || setTestCategory.contains("CheckoutToolSwitch")) {
        IntegrationTestSuiteCheckoutToolSwitch.testCheckoutToolSwitch();
    }

    if (indAllTests || setTestCategory.contains("CheckoutToolMultipleBase")) {
        IntegrationTestSuiteCheckoutToolMultipleBase.testCheckoutToolMultipleBase();
    }

    if (indAllTests || setTestCategory.contains("CheckoutToolMultipleConflict")) {
        IntegrationTestSuiteCheckoutToolMultipleConflict.testCheckoutToolMultipleConflict();
    }

    if (indAllTests || setTestCategory.contains("CheckoutToolMultipleSwitch")) {
        IntegrationTestSuiteCheckoutToolMultipleSwitch.testCheckoutToolMultipleSwitch();
    }

    if (indAllTests || setTestCategory.contains("CheckoutToolMultipleVersions")) {
        IntegrationTestSuiteCheckoutToolMultipleVersions.testCheckoutToolMultipleVersions();
    }

    if (indAllTests || setTestCategory.contains("WorkspaceManagerToolBase")) {
        IntegrationTestSuiteWorkspaceManagerToolBase.testWorkspaceManagerToolBase();
    }

    if (indAllTests || setTestCategory.contains("WorkspaceManagerToolStatusUpdateCommit")) {
        IntegrationTestSuiteWorkspaceManagerToolStatusUpdateCommit.testWorkspaceManagerToolStatusUpdateCommit();
    }

    if (indAllTests || setTestCategory.contains("WorkspaceManagerToolClean")) {
        IntegrationTestSuiteWorkspaceManagerToolClean.testWorkspaceManagerToolClean();
    }

    if (indAllTests || setTestCategory.contains("WorkspaceManagerToolBuildClean")) {
        IntegrationTestSuiteWorkspaceManagerToolBuildClean.testWorkspaceManagerToolBuildClean();
    }

    if (indAllTests || setTestCategory.contains("BuildToolBase")) {
        IntegrationTestSuiteBuildToolBase.testBuildToolBase();
    }

    if (indAllTests || setTestCategory.contains("BuildToolUserSystemMode")) {
        IntegrationTestSuiteBuildToolUserSystemMode.testBuildToolUserSystemMode();
    }

    if (indAllTests || setTestCategory.contains("BuildToolMavenBuilderPluginImplConfig")) {
        IntegrationTestSuiteBuildToolMavenBuilderPluginImplConfig.testBuildToolMavenBuilderPluginImplConfig();
    }

    if (indAllTests || setTestCategory.contains("ReferenceGraphReportToolBase")) {
        IntegrationTestSuiteReferenceGraphReportToolBase.testReferenceGraphReportToolBase();
    }

    if (indAllTests || setTestCategory.contains("ReferenceGraphReportToolReport")) {
        IntegrationTestSuiteReferenceGraphReportToolReport.testReferenceGraphReportToolReport();
    }

    if (indAllTests || setTestCategory.contains("SwitchToDynamicVersionToolBase")) {
        IntegrationTestSuiteSwitchToDynamicVersionToolBase.testSwitchToDynamicVersionToolBase();
    }

    if (indAllTests || setTestCategory.contains("SwitchToDynamicVersionToolRecurse")) {
        IntegrationTestSuiteSwitchToDynamicVersionToolRecurse.testSwitchToDynamicVersionToolRecurse();
    }

    if (indAllTests || setTestCategory.contains("SwitchToDynamicVersionToolReferenceChange")) {
        IntegrationTestSuiteSwitchToDynamicVersionToolReferenceChange
                .testSwitchToDynamicVersionToolReferenceChange();
    }

    if (indAllTests || setTestCategory.contains("SwitchToDynamicVersionToolHotfix")) {
        IntegrationTestSuiteSwitchToDynamicVersionToolHotfix.testSwitchToDynamicVersionToolHotfix();
    }

    if (indAllTests || setTestCategory.contains("SwitchToDynamicVersionToolPhase")) {
        IntegrationTestSuiteSwitchToDynamicVersionToolPhase.testSwitchToDynamicVersionToolPhase();
    }

    if (indAllTests || setTestCategory.contains("ReleaseToolBase")) {
        IntegrationTestSuiteReleaseToolBase.testReleaseToolBase();
    }

    if (indAllTests || setTestCategory.contains("ReleaseVersionToolRecurse")) {
        IntegrationTestSuiteReleaseToolRecurse.testReleaseToolRecurse();
    }

    if (indAllTests || setTestCategory.contains("ReleaseVersionToolSemantic")) {
        IntegrationTestSuiteReleaseToolSemantic.testReleaseToolSemantic();
        //??? incomplete. ANd there seems to be a bug with main workspace directory concept.
    }
    /*
    TODO:
        if (indAllTests || setTestCategory.contains("ReleaseVersionToolPhase")) {
          IntegrationTestSuiteCreateStaticVersionToolPhase.testCreateStaticVersionToolPhase();
        }
    */

    if (indAllTests || setTestCategory.contains("ReleaseToolMainModuleVersion")) {
        //TODO:
        IntegrationTestSuiteReleaseToolMainModuleVersion.testReleaseToolMainModuleVersion();
    }

    if (indAllTests || setTestCategory.contains("ReleaseToolMainModuleVersion")) {
        //TODO:
        IntegrationTestSuiteReleaseToolMainModuleVersion.testReleaseToolMainModuleVersion();
    }

    if (indAllTests || setTestCategory.contains("MergeMainToolBase")) {
        IntegrationTestSuiteMergeMainToolBase.testMergeMainToolBase();
    }

    if (indAllTests || setTestCategory.contains("SetupJenkinsJobsToolBase")) {
        IntegrationTestSuiteSetupJenkinsJobsToolBase.testSetupJenkinsJobsToolBase();
    }

    if (indAllTests || setTestCategory.contains("MutableModelSimpleConfig")) {
        IntegrationTestSuiteMutableModelSimpleConfig.testMutableModelSimpleConfig();
    }

    //    build-remote
    //    change-reference-to-module-version
    //    merge-main
    //    merge-reference-graph
}

From source file:org.commonjava.sjbi.sant.SimpleAntMechanism.java

private void build(final File buildFile, final String[] targets, final SimpleAntResult result) {
    final InputStream origIn = System.in;
    final PrintStream origOut = System.out;
    final PrintStream origErr = System.err;

    final Project project = new Project();

    Throwable error = null;//  w ww. j  a  v  a2 s .c  om
    try {
        project.addBuildListener(new DefaultLogger());
        project.setDefaultInputStream(System.in);

        System.setIn(new DemuxInputStream(project));
        System.setOut(new PrintStream(new DemuxOutputStream(project, false)));
        System.setErr(new PrintStream(new DemuxOutputStream(project, true)));

        project.fireBuildStarted();
        project.init();

        // TODO: Handle properties, via context...?
        // project.setUserProperty(arg, value);

        project.setUserProperty(MagicNames.ANT_FILE, buildFile.getAbsolutePath());
        project.setUserProperty(MagicNames.ANT_FILE_TYPE, MagicNames.ANT_FILE_TYPE_FILE);

        project.setKeepGoingMode(false);

        ProjectHelper.configureProject(project, buildFile);
        project.executeTargets(new Vector<String>(Arrays.asList(targets)));

    } catch (final RuntimeException e) {
        error = e;
        result.addError(new SJBIException("Error occurred in Ant build: %s\nTargets: [%s]\nBuild file: %s", e,
                e.getMessage(), join(targets, ", "), buildFile));
    } catch (final Error e) {
        error = e;
        result.addError(new SJBIException("Error occurred in Ant build: %s\nTargets: [%s]\nBuild file: %s", e,
                e.getMessage(), join(targets, ", "), buildFile));
    } finally {
        project.fireBuildFinished(error);

        System.setIn(origIn);
        System.setOut(origOut);
        System.setErr(origErr);
    }
}

From source file:org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.java

private static void redirectStandardStreams() throws FileNotFoundException {
    in = System.in;/*ww  w .j  a v  a 2s. co m*/
    out = System.out;
    err = System.err;
    System.setIn(new ByteArrayInputStream(new byte[0]));
    boolean isDebug = Boolean.getBoolean("jdt.ls.debug");
    if (isDebug) {
        String id = "jdt.ls-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        File workspaceFile = root.getRawLocation().makeAbsolute().toFile();
        File rootFile = new File(workspaceFile, ".metadata");
        rootFile.mkdirs();
        File outFile = new File(rootFile, ".out-" + id + ".log");
        FileOutputStream stdFileOut = new FileOutputStream(outFile);
        System.setOut(new PrintStream(stdFileOut));
        File errFile = new File(rootFile, ".error-" + id + ".log");
        FileOutputStream stdFileErr = new FileOutputStream(errFile);
        System.setErr(new PrintStream(stdFileErr));
    } else {
        System.setOut(new PrintStream(new ByteArrayOutputStream()));
        System.setErr(new PrintStream(new ByteArrayOutputStream()));
    }
}