Skip to content

Commit

Permalink
#36 Created unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmiranda committed Oct 21, 2014
1 parent d4d7403 commit 814ea7b
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/test/java/com/jcabi/log/VerboseProcessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}.
Expand Down Expand Up @@ -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")
);
}

}
;

0 comments on commit 814ea7b

Please sign in to comment.