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));
}
}
}
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();
}
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);
}
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);
}
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();
}
}
Aggregations