Example usage for java.io PrintStream PrintStream

List of usage examples for java.io PrintStream PrintStream

Introduction

In this page you can find the example usage for java.io PrintStream PrintStream.

Prototype

public PrintStream(File file, Charset charset) throws IOException 

Source Link

Document

Creates a new print stream, without automatic line flushing, with the specified file and charset.

Usage

From source file:com.jayway.restassured.itest.java.FilterITest.java

@Test
public void supportsSpecifyingDefaultFilters() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    RestAssured.filters(asList(logErrorsTo(captor), logResponseTo(captor)));
    try {/* w  w w .j ava2 s .  c o m*/
        expect().body(equalTo("ERROR")).when().get("/409");
    } finally {
        RestAssured.reset();
    }
    String lineSeparator = System.getProperty("line.separator");
    assertThat(writer.toString(), is(
            "HTTP/1.1 409 Conflict\nContent-Type: text/plain;charset=utf-8\nContent-Length: 5\nServer: Jetty(9.3.2.v20150730)\n\nERROR"
                    + lineSeparator
                    + "HTTP/1.1 409 Conflict\nContent-Type: text/plain;charset=utf-8\nContent-Length: 5\nServer: Jetty(9.3.2.v20150730)\n\nERROR"
                    + lineSeparator));
}

From source file:io.restassured.itest.java.GivenWhenThenLoggingITest.java

@Test
public void logOnlyHeadersUsingResponseUsingLogSpecWithGivenWhenThenSyntax() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);

    given().config(RestAssuredConfig.config().logConfig(new LogConfig(captor, true)))
            .pathParam("firstName", "John").pathParam("lastName", "Doe").when().get("/{firstName}/{lastName}")
            .then().log().headers().body("fullName", equalTo("John Doe"));

    assertThat(writer.toString(), equalTo(
            "Content-Type: application/json;charset=utf-8\nContent-Length: 59\nServer: Jetty(9.3.2.v20150730)"
                    + LINE_SEPARATOR));//from w w w.j  a v a  2  s.c  o m
}

From source file:com.jayway.restassured.itest.java.GivenWhenThenLoggingITest.java

@Test
public void logOnlyHeadersUsingResponseUsingLogSpecWithGivenWhenThenSyntax() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);

    given().config(config().logConfig(new LogConfig(captor, true))).pathParam("firstName", "John")
            .pathParam("lastName", "Doe").when().get("/{firstName}/{lastName}").then().log().headers()
            .body("fullName", equalTo("John Doe"));

    assertThat(writer.toString(), equalTo(
            "Content-Type: application/json;charset=utf-8\nContent-Length: 59\nServer: Jetty(9.3.2.v20150730)"
                    + LINE_SEPARATOR));//from w w  w.  ja  va  2s.c om
}

From source file:com.jayway.restassured.itest.java.FileDownloadITest.java

@Test
public void loggingWithBinaryCharsetWorks() throws Exception {
    int expectedSize = IOUtils
            .toByteArray(getClass().getResourceAsStream("/powermock-easymock-junit-1.4.12.zip")).length;

    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);

    final InputStream inputStream = given().config(config().logConfig(new LogConfig(captor, false))).expect()
            .log().all().when().get("http://powermock.googlecode.com/files/powermock-easymock-junit-1.4.12.zip")
            .asInputStream();//from   ww  w .  j  a v  a2s  .co m

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, byteArrayOutputStream);
    IOUtils.closeQuietly(byteArrayOutputStream);
    IOUtils.closeQuietly(inputStream);

    assertThat(writer.toString(), not(isEmptyOrNullString()));
    assertThat(byteArrayOutputStream.size(), equalTo(expectedSize));
}

From source file:com.adaptris.core.MimeEncoderImpl.java

protected MimeBodyPart asMimePart(Exception e) throws Exception {
    MimeBodyPart p = new MimeBodyPart();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
            PrintStream printer = new PrintStream(out, true)) {
        e.printStackTrace(printer);//w w  w.ja  v  a2s.  co  m
        p.setDataHandler(new DataHandler(new ByteArrayDataSource(out.toByteArray())));
    }
    return p;
}