Search in sources :

Example 1 with StringWriter

use of java.io.StringWriter in project jetty.project by eclipse.

the class FileSystemResourceTest method testInputStream.

@Test
public void testInputStream() throws Exception {
    Path dir = testdir.getPath().normalize().toRealPath();
    Files.createDirectories(dir);
    Path file = dir.resolve("foo");
    String content = "Foo is here";
    try (StringReader reader = new StringReader(content);
        BufferedWriter writer = Files.newBufferedWriter(file)) {
        IO.copy(reader, writer);
    }
    try (Resource base = newResource(dir.toFile())) {
        Resource foo = base.addPath("foo");
        try (InputStream stream = foo.getInputStream();
            InputStreamReader reader = new InputStreamReader(stream);
            StringWriter writer = new StringWriter()) {
            IO.copy(reader, writer);
            assertThat("Stream", writer.toString(), is(content));
        }
    }
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) StringReader(java.io.StringReader) Matchers.containsString(org.hamcrest.Matchers.containsString) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 2 with StringWriter

use of java.io.StringWriter in project elasticsearch by elastic.

the class ExceptionsHelper method stackTrace.

public static String stackTrace(Throwable e) {
    StringWriter stackTraceStringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stackTraceStringWriter);
    e.printStackTrace(printWriter);
    return stackTraceStringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 3 with StringWriter

use of java.io.StringWriter in project jetty.project by eclipse.

the class JavaUtilLogTest method testDebugOutput.

@Test
public void testDebugOutput() {
    jul.clear();
    // Common Throwable (for test)
    Throwable th = new Throwable("Message");
    // Capture raw string form
    StringWriter tout = new StringWriter();
    th.printStackTrace(new PrintWriter(tout));
    String ths = tout.toString();
    // Tests
    JavaUtilLog log = new JavaUtilLog("test.de.bug");
    setJulLevel("test.de.bug", Level.FINE);
    log.debug("Simple debug");
    log.debug("Debug with {} parameter", 1);
    log.debug("Debug with {} {} parameters", 2, "spiffy");
    log.debug("Debug with throwable", th);
    log.debug(th);
    // jul.dump();
    jul.assertContainsLine("FINE|test.de.bug|Simple debug");
    jul.assertContainsLine("FINE|test.de.bug|Debug with 1 parameter");
    jul.assertContainsLine("FINE|test.de.bug|Debug with 2 spiffy parameters");
    jul.assertContainsLine("FINE|test.de.bug|Debug with throwable");
    jul.assertContainsLine(ths);
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 4 with StringWriter

use of java.io.StringWriter in project jetty.project by eclipse.

the class JavaUtilLogTest method testInfoOutput.

@Test
public void testInfoOutput() {
    jul.clear();
    // Common Throwable (for test)
    Throwable th = new Throwable("Message");
    // Capture raw string form
    StringWriter tout = new StringWriter();
    th.printStackTrace(new PrintWriter(tout));
    String ths = tout.toString();
    // Tests
    JavaUtilLog log = new JavaUtilLog("test.in.fo");
    setJulLevel("test.in.fo", Level.INFO);
    log.info("Simple info");
    log.info("Info with {} parameter", 1);
    log.info("Info with {} {} parameters", 2, "spiffy");
    log.info("Info with throwable", th);
    log.info(th);
    // jul.dump();
    jul.assertContainsLine("INFO|test.in.fo|Simple info");
    jul.assertContainsLine("INFO|test.in.fo|Info with 1 parameter");
    jul.assertContainsLine("INFO|test.in.fo|Info with 2 spiffy parameters");
    jul.assertContainsLine("INFO|test.in.fo|Info with throwable");
    jul.assertContainsLine(ths);
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 5 with StringWriter

use of java.io.StringWriter in project jetty.project by eclipse.

the class BadRequestLogHandlerTest method testLogHandler.

@Test(timeout = 4000)
public void testLogHandler() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    CaptureLog captureLog = new CaptureLog();
    RequestLogHandler requestLog = new RequestLogHandler();
    requestLog.setRequestLog(captureLog);
    requestLog.setHandler(new HelloHandler());
    server.setHandler(requestLog);
    try {
        server.start();
        String host = connector.getHost();
        if (host == null) {
            host = "localhost";
        }
        InetAddress destAddr = InetAddress.getByName(host);
        int port = connector.getLocalPort();
        SocketAddress endpoint = new InetSocketAddress(destAddr, port);
        Socket socket = new Socket();
        socket.setSoTimeout(1000);
        socket.connect(endpoint);
        try (OutputStream out = socket.getOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            InputStream in = socket.getInputStream();
            InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
            StringReader request = new StringReader(requestHeader);
            IO.copy(request, writer);
            writer.flush();
            StringWriter response = new StringWriter();
            IO.copy(reader, response);
            LOG.info("Response: {}", response);
        } finally {
            socket.close();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServerConnector(org.eclipse.jetty.server.ServerConnector) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) OutputStreamWriter(java.io.OutputStreamWriter) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

StringWriter (java.io.StringWriter)8271 PrintWriter (java.io.PrintWriter)2550 Test (org.junit.Test)1750 IOException (java.io.IOException)1431 Writer (java.io.Writer)617 StringReader (java.io.StringReader)546 StreamResult (javax.xml.transform.stream.StreamResult)467 File (java.io.File)423 HashMap (java.util.HashMap)394 Transformer (javax.xml.transform.Transformer)323 InputStreamReader (java.io.InputStreamReader)314 InputStream (java.io.InputStream)311 ArrayList (java.util.ArrayList)310 DOMSource (javax.xml.transform.dom.DOMSource)279 Map (java.util.Map)278 Marshaller (javax.xml.bind.Marshaller)220 ByteArrayInputStream (java.io.ByteArrayInputStream)218 BufferedReader (java.io.BufferedReader)216 HttpServletRequest (javax.servlet.http.HttpServletRequest)197 HttpServletResponse (javax.servlet.http.HttpServletResponse)195