### Question:
EscapingReader extends Reader { @Override public int read() throws IOException { int fromDelegate = delegate.read(); if (fromDelegate < 0) { return fromDelegate; } char c = (char) fromDelegate; while (invalidCharsPattern.matcher(String.valueOf(c)).find()) { fromDelegate = delegate.read(); if (fromDelegate < 0) { return fromDelegate; } c = (char) fromDelegate; } return fromDelegate; } EscapingReader(Reader delegate);  EscapingReader(Object lock, Reader delegate); @Override int read(); @Override int read(char[] cbuf, int off, int len); @Override long skip(long n); @Override boolean ready(); @Override boolean markSupported(); @Override void mark(int readAheadLimit); @Override void reset(); @Override void close();  }### Answer:
@Test public void read() throws IOException { when(mockDelegate.read()).thenReturn((int) 'y', (int) 'a', (int) 'y', -1); assertEquals((int) 'y', cut.read()); assertEquals((int) 'a', cut.read()); assertEquals((int) 'y', cut.read()); assertEquals(-1, cut.read()); }
@Test public void readWithLock() throws IOException { when(mockDelegate.read()).thenReturn((int) 'y', (int) 'a', (int) 'y', -1); final Object lock = new Object(); cut = new EscapingReader(lock, mockDelegate); assertEquals((int) 'y', cut.read()); assertEquals((int) 'a', cut.read()); assertEquals((int) 'y', cut.read()); assertEquals(-1, cut.read()); }
@Test public void readWithIllegalAtEnd() throws IOException { when(mockDelegate.read()).thenReturn((int) 'y', 0x00, -1); assertEquals((int) 'y', cut.read()); assertEquals(-1, cut.read()); }
@Test public void readWithIllegalNotAtEnd() throws IOException { when(mockDelegate.read()).thenReturn((int) 'y', 0x00, (int) 'y', -1); assertEquals((int) 'y', cut.read()); assertEquals((int) 'y', cut.read()); assertEquals(-1, cut.read()); }
@Test public void readWithCharArrayAndDelegateEOF() throws IOException { when(mockDelegate.read((char[]) Mockito.anyVararg(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(-1); final char[] buffer = new char[10]; assertEquals(-1, cut.read(buffer)); }
@Test public void readWithCharArrayAndLegalText() throws IOException { cut = new EscapingReader(new StringReader("legal")); final char[] buffer = new char[10]; assertEquals(5, cut.read(buffer)); assertEquals("legal", String.valueOf(buffer, 0, 5)); }
@Test public void readWithCharArrayAndIllegalText() throws IOException { cut = new EscapingReader(new StringReader("bad\0char")); final char[] buffer = new char[10]; assertEquals(7, cut.read(buffer)); assertEquals("badchar", String.valueOf(buffer, 0, 7)); }
@Test public void readWithCharBufferAndLegalText() throws IOException { cut = new EscapingReader(new StringReader("legal")); final CharBuffer buffer = CharBuffer.allocate(10); assertEquals(5, cut.read(buffer)); buffer.flip(); assertEquals("legal", buffer.toString()); }
@Test public void readWithCharBufferAndIllegalText() throws IOException { cut = new EscapingReader(new StringReader("bad\0char")); final CharBuffer buffer = CharBuffer.allocate(10); assertEquals(7, cut.read(buffer)); buffer.flip(); assertEquals("badchar", buffer.toString()); }