...18import static org.junit.Assert.assertEquals;19import org.junit.Test;20import org.junit.runner.RunWith;21import org.junit.runners.JUnit4;22import org.openqa.selenium.io.CircularOutputStream;23import java.io.ByteArrayOutputStream;24import java.io.PrintStream;25import java.io.PrintWriter;26@RunWith(JUnit4.class)27public class CircularOutputStreamTest {28 @Test29 public void testShouldReturnTheEntireWrittenContentIfSmallerThanTheBufferSize() throws Exception {30 String expected = "foo";31 int maxSize = expected.getBytes().length;32 try (CircularOutputStream os = new CircularOutputStream(maxSize)) {33 os.write(expected.getBytes());34 String seen = os.toString();35 assertEquals(expected, seen);36 }37 }38 @Test39 public void testShouldReturnJustTheWrittenOutputIfBufferIsTooLarge() throws Exception {40 String expected = "foo";41 // Note, this makes the buffer larger than what we write to it42 int maxSize = expected.getBytes().length + 1;43 try (CircularOutputStream os = new CircularOutputStream(maxSize)) {44 os.write(expected.getBytes());45 String seen = os.toString();46 assertEquals(expected, seen);47 }48 }49 @Test50 public void testShouldTruncateOutputToMatchTheSizeOfTheBuffer() throws Exception {51 String expected = "oo";52 int maxSize = expected.getBytes().length;53 try (CircularOutputStream os = new CircularOutputStream(maxSize)) {54 os.write("foo".getBytes());55 String seen = os.toString();56 assertEquals(expected, seen);57 }58 }59 @Test60 public void testShouldReturnContentInTheCorrectOrder() throws Exception {61 String expected = "234";62 int maxSize = expected.getBytes().length;63 try (CircularOutputStream os = new CircularOutputStream(maxSize)) {64 os.write("1234".getBytes());65 String seen = os.toString();66 assertEquals(expected, seen);67 }68 }69 @Test70 public void testLongerMultiLineOutputPreservesJustTheEnd() throws Exception {71 int maxSize = 64;72 ByteArrayOutputStream baos = new ByteArrayOutputStream();73 PrintStream bps = new PrintStream(baos, true);74 Throwable throwable = new Throwable();75 throwable.printStackTrace(bps);76 String expected = baos.toString();77 expected = expected.substring(expected.length() - maxSize);78 bps.close();79 CircularOutputStream os = new CircularOutputStream(maxSize);80 PrintStream cops = new PrintStream(os, true);81 throwable.printStackTrace(cops);82 String seen = os.toString();83 cops.close();84 assertEquals(expected, seen);85 }86 @Test87 public void testCircularness() {88 CircularOutputStream os = new CircularOutputStream(5);89 try (PrintWriter pw = new PrintWriter(os, true)) {90 pw.write("12345");91 pw.flush();92 assertEquals("12345", os.toString());93 pw.write("6");94 pw.flush();95 assertEquals("23456", os.toString());96 pw.write("789");97 pw.flush();98 assertEquals("56789", os.toString());99 }100 }101}...