From 814ea7bb140af6fbc559fa9944504e20516e28e5 Mon Sep 17 00:00:00 2001 From: Carlos Miranda Date: Tue, 21 Oct 2014 09:31:34 +0800 Subject: [PATCH] #36 Created unit test --- .../com/jcabi/log/VerboseProcessTest.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/test/java/com/jcabi/log/VerboseProcessTest.java b/src/test/java/com/jcabi/log/VerboseProcessTest.java index 6b18e22a..9e78e665 100644 --- a/src/test/java/com/jcabi/log/VerboseProcessTest.java +++ b/src/test/java/com/jcabi/log/VerboseProcessTest.java @@ -29,6 +29,10 @@ */ package com.jcabi.log; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; import java.io.StringWriter; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -42,6 +46,7 @@ import org.junit.Assume; import org.junit.Ignore; import org.junit.Test; +import org.mockito.Mockito; /** * Test case for {@link VerboseProcess}. @@ -204,4 +209,31 @@ public void stdoutQuietlyLogsErrors() throws Exception { ); } + /** + * VerboseProcess exits "gracefully" when it can't read from the process + * stream, and logs the error that is thrown. + * @throws Exception + */ + @Test + public void logsErrorWhenUnderlyingStreamIsClosed() throws Exception { + final StringWriter writer = new StringWriter(); + org.apache.log4j.Logger.getRootLogger().addAppender( + new WriterAppender(new SimpleLayout(), writer) + ); + final Process prc = Mockito.mock(Process.class); + final InputStream stdout = new FileInputStream( + File.createTempFile("temp", "test") + ); + stdout.close(); + Mockito.doReturn(stdout).when(prc).getInputStream(); + Mockito.doReturn(new ByteArrayInputStream(new byte[0])) + .when(prc).getErrorStream(); + new VerboseProcess(prc, Level.ALL, Level.ALL).stdout(); + MatcherAssert.assertThat( + writer.toString(), + Matchers.containsString("underlying process stream was closed") + ); + } + } +; \ No newline at end of file