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.languagetool.commandline.MainTest.java

@Test
public void testEnglishParaMode() throws Exception {
    String test = "This is what I mean\nand you know it.";
    System.setIn(new ByteArrayInputStream(test.getBytes()));
    String[] args = { "-l", "en", "-a", "-" };

    Main.main(args);//from w w w .  ja v a2  s .  c  o m
    String output = new String(this.out.toByteArray());
    assertEquals("This is what I mean\nand you know it.", output);
}

From source file:org.languagetool.commandline.MainTest.java

@Test
public void testPolishStdInDefaultOff() throws Exception {
    String test = "To jest test, ktry zrobiem, ktry mi si podoba.";
    System.setIn(new ByteArrayInputStream(test.getBytes()));
    String[] args = { "-l", "pl", "-e", "PL_WORD_REPEAT", "-" };

    Main.main(args);//from   w  w w. j av a 2 s.  c  o  m
    String stdout = new String(this.out.toByteArray());
    String stderr = new String(this.err.toByteArray());
    assertTrue(stderr.indexOf("Expected text language: Polish") == 0);
    assertTrue(stderr.contains("Working on STDIN..."));
    assertTrue(stdout.contains("1.) Line 1, column 31, Rule ID: PL_WORD_REPEAT"));
}

From source file:org.languagetool.commandline.MainTest.java

@Test
public void testPolishApiStdInDefaultOff() throws Exception {
    String test = "To jest test, ktry zrobiem, ktry mi si podoba.";
    System.setIn(new ByteArrayInputStream(test.getBytes()));
    String[] args = { "--api", "-l", "pl", "-eo", "-e", "PL_WORD_REPEAT", "-" };
    Main.main(args);//from w ww  .  j  av  a  2  s .c  om
    String output = new String(this.out.toByteArray());
    assertThat(StringUtils.countMatches(output, "<error "), is(1));
    assertThat(StringUtils.countMatches(output, "<matches "), is(1));
    assertThat(StringUtils.countMatches(output, "</matches>"), is(1)); // https://github.com/languagetool-org/languagetool/issues/251
}

From source file:org.languagetool.commandline.MainTest.java

@Test
public void testPolishApiStdInDefaultOffNoErrors() throws Exception {
    String test = "To jest test.";
    System.setIn(new ByteArrayInputStream(test.getBytes()));
    String[] args = { "--api", "-l", "pl", "-e", "PL_WORD_REPEAT", "-" };
    Main.main(args);/*from  w  w w .j  a v a 2 s.co  m*/
    String output = new String(this.out.toByteArray());
    assertThat(StringUtils.countMatches(output, "<error "), is(0));
    assertThat(StringUtils.countMatches(output, "<matches "), is(1));
    assertThat(StringUtils.countMatches(output, "</matches>"), is(1));
}

From source file:org.languagetool.commandline.MainTest.java

@Test
public void testPolishSpelling() throws Exception {
    String test = "Zwuasdac?";
    System.setIn(new ByteArrayInputStream(test.getBytes()));
    String[] args = { "-l", "pl", "-e", "MORFOLOGIK_RULE_PL_PL", "-" };

    Main.main(args);//from w w w.  j  a va2s . c  o m
    String stdout = new String(this.out.toByteArray());
    String stderr = new String(this.err.toByteArray());
    assertTrue(stderr.indexOf("Expected text language: Polish") == 0);
    assertTrue(stderr.contains("Working on STDIN..."));
    assertTrue(stdout.contains("1.) Line 1, column 1, Rule ID: MORFOLOGIK_RULE_PL_PL"));
}

From source file:org.languagetool.commandline.MainTest.java

@Test
public void testEnglishFileFakeRuleEnabled() throws Exception {
    String test = "Zwuasdac?";
    System.setIn(new ByteArrayInputStream(test.getBytes()));
    String[] args = { "-l", "en", "-e", "FOO_BAR_BLABLA", "-" };
    Main.main(args);//from   w  w w .  j  a va  2  s  .c  om
    String stderr = new String(this.err.toByteArray());
    assertTrue(stderr.indexOf("Expected text language: English") == 0);
}

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

private void testStandardInputStreamUnicode() throws Exception {
    InputStream stdInSave = System.in;
    CloseableRegistry r = new CloseableRegistry();
    try {/*from w ww  .  j av a2s. c o m*/
        ByteArrayInputStream stdIn = new ByteArrayInputStream(
                ArrayUtils.addAll(Signature.UTF8.getMark(), COMBO_UTF8));
        r.register(stdIn);
        System.setIn(stdIn);
        ReaderWrapper wrapper = new ReaderWrapper(SpecialNames.STANDARD_INPUT, DecodingStrategy.SIG_REQ);
        r.register(wrapper);
        assertTrue(wrapper.getSkipClose());
        assertNotNull(wrapper.getReader());
        assertEquals(COMBO, IOUtils.toString(wrapper.getReader()));
    } finally {
        System.setIn(stdInSave);
        r.close();
    }
}

From source file:org.neo4j.shell.StartClientTest.java

@Test
public void givenShellClientWhenReadFromStdinThenExecutePipedCommands() throws IOException {
    // Given//from  w ww . ja va2  s .  c  om
    // an empty database

    // When
    InputStream realStdin = System.in;
    try {
        System.setIn(new ByteArrayInputStream("CREATE (n {foo:'bar'});".getBytes()));
        StartClient.main(new String[] { "-file", "-" });
    } finally {
        System.setIn(realStdin);
    }

    // Then
    try (Transaction tx = db.getGraphDatabaseAPI().beginTx()) {
        assertThat((String) db.getGraphDatabaseAPI().getNodeById(0).getProperty("foo"), equalTo("bar"));
        tx.success();
    }
}

From source file:org.nuxeo.build.ant.AntClient.java

public void run(File buildFile, List<String> targets) throws BuildException {
    PrintStream previousErr = System.err;
    PrintStream previousOut = System.out;
    InputStream in = System.in;
    InputStream newIn = null;/*w ww  . j  av a 2  s  . c om*/
    PrintStream newOut = null;
    PrintStream newErr = null;
    try {
        // Configure IO
        InputHandler handler = new DefaultInputHandler();
        project.setInputHandler(handler);
        if (allowInput) {
            project.setDefaultInputStream(System.in);
        }
        newIn = new DemuxInputStream(project);
        System.setIn(newIn);
        newOut = new PrintStream(new DemuxOutputStream(project, false));
        System.setOut(newOut);
        newErr = new PrintStream(new DemuxOutputStream(project, true));
        System.setErr(newErr);

        if (!buildFile.isAbsolute()) {
            buildFile = new File(project.getBaseDir(), buildFile.getPath());
        }
        project.setUserProperty("ant.file", buildFile.getPath());
        ProjectHelper.configureProject(project, buildFile);

        project.fireBuildStarted();
        if (targets != null) {
            project.getExecutor().executeTargets(project, targets.toArray(new String[targets.size()]));
        } else {
            project.getExecutor().executeTargets(project, new String[] { project.getDefaultTarget() });
        }
        project.fireBuildFinished(null);
    } catch (BuildException e) {
        project.fireBuildFinished(e);
        throw e;
    } finally {
        System.setOut(previousOut);
        System.setErr(previousErr);
        System.setIn(in);
        IOUtil.close(newIn);
        IOUtil.close(newOut);
        IOUtil.close(newErr);
    }
}

From source file:org.openanzo.test.client.cli.TestCommandLineInterface.java

/**
 * Isolates a java method call to the command line interface's main method. Mock System in, out and error are put around the method call and System.exit's
 * are managed and their status captured.
 * /*ww  w.  j a va 2  s  .  co  m*/
 * The System out, error and exit status are returned.
 * 
 */
private TestCommandResult runCommandTest(String command, InputStream input) throws Exception {
    InputStream defaultInput = System.in;
    PrintStream defaultOutput = System.out;
    PrintStream defaultError = System.err;
    SecurityManager defaultSecurityManager = System.getSecurityManager();

    int status = -1;
    String output = null;
    String error = null;
    try {
        System.setIn(input);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(new PrintStream(out)));

        ByteArrayOutputStream err = new ByteArrayOutputStream();
        System.setErr(new PrintStream(new PrintStream(err)));

        System.setSecurityManager(new ExitStatusManager());
        try {
            CommandLineInterface.main(command.split("\\s+"));

        } catch (ExitStatusException e) {
            status = e.getStatus();
        }

        output = out.toString("UTF-8");
        error = err.toString("UTF-8");
    } finally {
        System.setIn(defaultInput);
        System.setOut(defaultOutput);
        System.setErr(defaultError);
        System.setSecurityManager(defaultSecurityManager);
    }

    return new TestCommandResult(output, error, status);
}