T - Type of the wrapped (contained) Reader to use in
getWrappedReader().public class DiagnosticReader<T extends Reader> extends FilterReader
A decorating Reader that detects and log useful debug
informations about the stream passed in the constructor, and detects wrong
usage patterns.
Reader methods accessed after invocation of
close().close() method.close() invocation. Stream being garbage collected
without close() being called.
It normally acts as a FilterReader simply forwarding all the calls to
the Reader passed in the constructor, but also keeping track of
the usage of the methods.
Errors are both logged at WARN level and available through the standard class interface. Future version will allow the customization of this behavior disable the logging.
It is designed to detect also errors that happens during object finalization, but to detect these errors in tests you must be very careful on your test design (see example). Errors in finalizers are available trough the getFinalizationErrors() method.
It's an useful tool in unit tests to detect wrong handling of the streams, but it can be used in main applications too since it adds a very little overhead in standard situations.
Sample Usage (in Junit 4):
@org.junit.Test
public void testWarnDoubleClose() throws Exception {
Reader myTestData = ....
DiagnosticReader<Reader> diagIs =
new DiagnosticReader<Reader>(myTestData);
//The class and the method under test
MyClassUnderTest.myMethodUnderTest(diagIs);
final String[] instanceWarnings = diagIs.getInstanceWarnings();
assertTrue("No problems" + diagIs.getStatusMessage(),
instanceWarnings.length == 0);
}
If your code free resources in finalize() methods, or the
libraries you use do so you must use a more complex testing strategy because
the references to to the active DiagnosticReader instance in
your Junit prevents the class from being garbage collected. See the wiki for
details and getFinalizationErrors().
in| Constructor and Description |
|---|
DiagnosticReader(T in)
Constructor for DiagnosticReader.
|
DiagnosticReader(T reader,
int logDepth)
Constructor for DiagnosticReader.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clearInstanceWarnings()
clearInstanceWarnings
|
void |
close() |
void |
finalize() |
int |
getCloseCount()
Returns the number of times that close was called on this stream.
|
byte[] |
getContent()
Return the current captured bytes, if capture was enabled.
|
static String[] |
getFinalizationErrors()
Returns an array of descriptions of finalization errors.
|
String[] |
getInstanceWarnings()
getInstanceWarnings
|
String |
getStatusMessage()
Returns a string representation of the usage errors of the stream until
now.
|
T |
getWrappedReader()
Returns the wrapped (original)
Reader passed in the
constructor. |
boolean |
isMethodCalledAfterClose()
isMethodCalledAfterClose check if some operation on the current reader
was attempted after the method close() was invoked.
|
void |
mark(int readlimit) |
boolean |
markSupported() |
int |
read() |
int |
read(char[] b) |
int |
read(char[] b,
int off,
int len) |
void |
reset() |
static void |
resetFinalizationErrors()
resetFinalizationErrors
|
static void |
setDefaultLogDepth(int defaultFrameDepth)
Setter for the field
defaultLogDepth. |
long |
skip(long n) |
readypublic DiagnosticReader(T in)
Constructor for DiagnosticReader.
in - the source Reader.public DiagnosticReader(T reader, int logDepth)
Constructor for DiagnosticReader.
Reader - the source ReaderlogDepth - Number of stack frames to log. It overrides the default static
value.public static String[] getFinalizationErrors()
Returns an array of descriptions of finalization errors. For instance when the stream is finalized but it was not closed.
String objects.public static void resetFinalizationErrors()
resetFinalizationErrors
public static void setDefaultLogDepth(int defaultFrameDepth)
Setter for the field defaultLogDepth.
defaultFrameDepth - a int.public void clearInstanceWarnings()
clearInstanceWarnings
public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class FilterReaderIOExceptionpublic void finalize()
throws Throwable
public int getCloseCount()
public byte[] getContent()
Return the current captured bytes, if capture was enabled.
The capture buffer might be truncated if maxCapture is set.
public String[] getInstanceWarnings()
getInstanceWarnings
String objects.public String getStatusMessage()
Returns a string representation of the usage errors of the stream until now. Null if no error happened yet.
public T getWrappedReader()
Returns the wrapped (original) Reader passed in the
constructor. Any calls made to the returned stream will not be tracked by
DiagnosticReader, so this method should be used with care,
and close() and read() methods should'nt be
called on the returned Reader. Instead these methods should
be called on DiagnosticReader that simply forwards them to
the underlying stream.
Reader passed in the constructorpublic boolean isMethodCalledAfterClose()
isMethodCalledAfterClose check if some operation on the current reader was attempted after the method close() was invoked.
public void mark(int readlimit)
throws IOException
mark in class FilterReaderIOExceptionpublic boolean markSupported()
markSupported in class FilterReaderpublic int read()
throws IOException
read in class FilterReaderIOExceptionpublic int read(char[] b)
throws IOException
read in class ReaderIOExceptionpublic int read(char[] b,
int off,
int len)
throws IOException
read in class FilterReaderIOExceptionpublic void reset()
throws IOException
reset in class FilterReaderIOExceptionpublic long skip(long n)
throws IOException
skip in class FilterReaderIOExceptionCopyright © 2008–2016. All rights reserved.