Search in sources :

Example 1 with ExecResult

use of org.apache.camel.component.exec.ExecResult in project camel by apache.

the class ExecDocumentationExamplesTest method testExecWinAnt.

/**
     * The test assumes that Apache ant is installed
     */
@Test
@Ignore
public void testExecWinAnt() throws Exception {
    File f = new File(ANT_BUILD_FILE_NAME);
    f.createNewFile();
    FileUtils.writeStringToFile(f, ANT_BUILD_FILE_CONTENT);
    assertTrue("You must create a sample build file!", f.exists());
    ExecResult body = templateExecAnt.requestBody((Object) "test", ExecResult.class);
    String stdout = IOUtils.toString(body.getStdout());
    assertNull(body.getStderr());
    assertTrue("The ant script should print" + TEST_MSG, stdout.contains(TEST_MSG));
    f.delete();
}
Also used : File(java.io.File) ExecResult(org.apache.camel.component.exec.ExecResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with ExecResult

use of org.apache.camel.component.exec.ExecResult in project camel by apache.

the class ExecDocumentationExamplesTest method testWinJavaVersionWorkingDir.

@Test
@Ignore
public void testWinJavaVersionWorkingDir() throws Exception {
    ExecResult body = templateJavaVersionWorkingDir.requestBody((Object) "test", ExecResult.class);
    InputStream out = body.getStdout();
    InputStream err = body.getStderr();
    // Strange that Sun Java 1.5 writes the -version in the syserr
    assertNull(out);
    assertNotNull(err);
    String outerr = IOUtils.toString(err);
    log.info("Received stderr: " + outerr);
    assertTrue(outerr.contains("java version"));
}
Also used : InputStream(java.io.InputStream) ExecResult(org.apache.camel.component.exec.ExecResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ExecResult

use of org.apache.camel.component.exec.ExecResult in project camel by apache.

the class ExecDocumentationExamplesTest method testJavaVersion.

/**
     * The test assumes, that java is in the system path
     */
@Test
@Ignore
public void testJavaVersion() throws Exception {
    ExecResult body = templateJavaVersion.requestBody((Object) "test", ExecResult.class);
    InputStream out = body.getStdout();
    InputStream err = body.getStderr();
    // Strange that Sun Java 1.5 writes the -version in the syserr
    assertNull(out);
    assertNotNull(err);
    String outString = IOUtils.toString(err);
    log.info("Received stdout: " + outString);
    assertTrue(outString.contains("java version"));
}
Also used : InputStream(java.io.InputStream) ExecResult(org.apache.camel.component.exec.ExecResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with ExecResult

use of org.apache.camel.component.exec.ExecResult in project camel by apache.

the class ExecDocumentationExamplesTest method testExecLinuxWordCount.

@Test
@Ignore
public void testExecLinuxWordCount() throws Exception {
    // use type conversion here
    ExecResult body = templateWordCount.requestBody((Object) "test", ExecResult.class);
    assertNotNull(body);
}
Also used : ExecResult(org.apache.camel.component.exec.ExecResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with ExecResult

use of org.apache.camel.component.exec.ExecResult in project camel by apache.

the class ExecDocumentationExamplesTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            // word count
            from("direct:wordCount").to("exec:wc?args=--words /usr/share/dict/words").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // By default, the body is ExecResult instance
                    assertIsInstanceOf(ExecResult.class, exchange.getIn().getBody());
                    // Use the Camel Exec String type converter to
                    // convert the ExecResult to String
                    // In this case, the stdout is considered as output
                    String wordCountOutput = exchange.getIn().getBody(String.class);
                    // do something with the output
                    log.info(wordCountOutput);
                }
            });
            // example 1 in the component docs
            from("direct:javaVersion").to("exec:java?args=-version -server");
            // example 2 in the component docs
            from("direct:javaVersionWorkingDir").to("exec:" + buildJavaExecutablePath() + "?args=-version -Duser.name=Camel&workingDir=C:/temp");
            // advanced, test ant
            from("direct:execAnt").to("exec:ant.bat?args=-f " + ANT_BUILD_FILE_NAME);
            // advanced, test ant with out file
            from("direct:execAntWithOutFile").to("exec:ant.bat?args=-f " + ANT_BUILD_FILE_NAME + " -l " + ANT_OUT_FILE_NAME + "&outFile=" + ANT_OUT_FILE_NAME).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    InputStream outFile = exchange.getIn().getBody(InputStream.class);
                    // do something with the out file here
                    log.info(IOUtils.toString(outFile));
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) InputStream(java.io.InputStream) ExecResult(org.apache.camel.component.exec.ExecResult)

Aggregations

ExecResult (org.apache.camel.component.exec.ExecResult)6 InputStream (java.io.InputStream)4 Ignore (org.junit.Ignore)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 Exchange (org.apache.camel.Exchange)1 Processor (org.apache.camel.Processor)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 ExecDefaultExecutor (org.apache.camel.component.exec.ExecDefaultExecutor)1 ExecEndpoint (org.apache.camel.component.exec.ExecEndpoint)1 ExecException (org.apache.camel.component.exec.ExecException)1 CommandLine (org.apache.commons.exec.CommandLine)1 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)1 ExecuteException (org.apache.commons.exec.ExecuteException)1 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)1