public class OutputTester
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private ResourceReader |
classLoaderReader |
private ResourceReader |
classResourceReader |
private boolean |
dumpMismatchedOutputToFile |
private java.lang.String |
expectedOutputEncoding |
private java.lang.String |
expectedOutputsDirPath |
private ResourceReader |
fileReader |
private boolean |
normalizeLineSeparators |
private java.lang.StringBuilder |
out |
private java.lang.String |
packageName |
private java.io.File |
resourceDir |
private java.lang.String |
testResourcesFolder |
private java.lang.Class<?> |
testRoot |
private boolean |
updateExpectedOutputs |
Constructor and Description |
---|
OutputTester(java.lang.Class<?> testRoot,
java.lang.String expectedOutputsDirPath)
Creates an output tester to validate outputs produced by test methods of a given class.
|
OutputTester(java.lang.Class<?> testRoot,
java.lang.String expectedOutputsDirPath,
java.lang.String expectedOutputEncoding)
Creates an output tester to validate outputs produced by test methods of a given class.
|
OutputTester(java.lang.String expectedOutputsDirPath)
Creates an output tester to validate outputs produced by test methods of a subclass
|
OutputTester(java.lang.String expectedOutputsDirPath,
java.lang.String expectedOutputEncoding)
Creates an output tester to validate outputs produced by test methods of a subclass
|
Modifier and Type | Method and Description |
---|---|
private java.lang.String |
cleanup(java.lang.String content) |
void |
clear()
Discards any output stored in the internal buffer.
|
private java.io.File |
getExpectedOutputDir() |
private java.lang.String |
getOutputAndClear() |
java.io.File |
getResourceDir()
Returns the project's actual resource directory, where the expected output files are located.
|
private java.io.InputStream |
getResultData(java.lang.String className,
java.lang.String testMethod) |
java.lang.String |
getTestResourcesFolder()
Returns the project's test resources folder (relative to the project root), where the expected output files are located.
|
boolean |
getUpdateExpectedOutputs()
Returns a flag indicating whether all tests of the test class should have their outputs updated.
|
boolean |
isDumpMismatchedOutputToFile()
Indicates whether the output produced by a given test method should be written into a temporary file.
|
boolean |
isNormalizeLineSeparators()
Indicates whether line separators in both produced and expected outputs will be normalized (i.e.
|
private boolean |
isTestMethod(java.lang.String className,
java.lang.String methodName) |
private void |
performValidation(boolean validate,
boolean print,
java.lang.String className,
java.lang.String method,
java.lang.Object[] methodArgs,
java.lang.String producedOutput,
java.io.File expectedOutputDir) |
void |
print(java.lang.Object contents)
Appends some content to the output.
|
void |
print(java.lang.StringBuilder out,
java.lang.Object contents)
Appends some content to the output.
|
private void |
print(java.lang.String output,
java.lang.String className,
java.lang.String method) |
void |
printAndDontValidate(java.lang.CharSequence output,
java.lang.Object... methodArgs)
Prints the result to the standard output without validating its contents
|
void |
printAndDontValidate(java.lang.Object... methodArgs)
Prints the result to the standard output without validating its contents
|
void |
printAndValidate(java.lang.CharSequence output,
java.lang.Object... methodArgs)
Prints the result to the standard output and validates it against the expected output
stored in {expectedOutputsDirPath}/{test_class_name}/{test_method_name}
|
void |
printAndValidate(java.lang.Object... methodArgs)
Prints the result to the standard output and validates it against the expected output
stored in {expectedOutputsDirPath}/{test_class_name}/{test_method_name}
|
private void |
printAndValidateOutput(boolean validate,
boolean print,
java.lang.String producedOutput,
java.io.File expectedOutputDir,
java.lang.Object[] methodArgs)
Finds out the test method being executed and compares the output against
the expected output in
expectedOutputsDirPath . |
private void |
printAndValidateOutput(boolean validate,
boolean print,
java.lang.String producedOutput,
java.lang.Object[] methodArgs)
Finds out the test method being executed and compares the output against
the expected output in
expectedOutputsDirPath . |
void |
println()
Appends a newline to the output
|
void |
println(java.lang.Object contents)
Appends some content to the output and adds a newline at the end.
|
void |
println(java.lang.StringBuilder out)
Appends a newline to the output
|
void |
println(java.lang.StringBuilder out,
java.lang.Object contents)
Appends some content to the output and adds a newline at the end.
|
void |
setDumpMismatchedOutputToFile(boolean dumpMismatchedOutputToFile)
Defines whether the output produced by a given test method should be written into a temporary file.
|
void |
setNormalizeLineSeparators(boolean normalizeLineSeparators)
Enables/disables normalization of line separators.
|
void |
setResourceDir(java.io.File resourceDir)
Defines the project's actual resource directory, where the expected output files are located.
|
void |
setTestResourcesFolder(java.lang.String testResourcesFolder)
Defines the project's test resources folder (relative to the project root), where the expected output files are located.
|
void |
setUpdateExpectedOutputs(boolean updateExpectedOutputs)
Used to indicate when all tests of the test class should have their outputs updated.
|
void |
updateExpectedOutput(java.lang.CharSequence output,
java.lang.Object... methodArgs)
Updates or creates the expected output file under the given expected output directory.
|
void |
updateExpectedOutput(java.lang.Object... methodArgs)
Updates or creates the expected output file under the given expected output directory.
|
private void |
updateExpectedOutput(java.lang.String className,
java.lang.String testMethod,
java.lang.String producedOutput,
java.io.File expectedOutputDir) |
void |
validate(java.lang.CharSequence output,
java.lang.Object... methodArgs)
Validates the result against the expected output
stored in {expectedOutputsDirPath}/{test_class_name}/{test_method_name}
|
void |
validate(java.lang.Object... methodArgs)
Validates the result against the expected output
stored in {expectedOutputsDirPath}/{test_class_name}/{test_method_name}
|
private void |
validateExampleOutput(java.lang.String className,
java.lang.String testMethod,
java.lang.String producedOutput,
java.io.File expectedOutputDir) |
private java.lang.StringBuilder out
private final java.lang.Class<?> testRoot
private final java.lang.String packageName
private final java.lang.String expectedOutputsDirPath
private final java.lang.String expectedOutputEncoding
private boolean normalizeLineSeparators
private boolean dumpMismatchedOutputToFile
private java.io.File resourceDir
private java.lang.String testResourcesFolder
private boolean updateExpectedOutputs
private final ResourceReader classLoaderReader
private final ResourceReader classResourceReader
private final ResourceReader fileReader
public OutputTester(java.lang.String expectedOutputsDirPath)
expectedOutputsDirPath
- the path to a file or classpath resource that contains the expected outputspublic OutputTester(java.lang.String expectedOutputsDirPath, java.lang.String expectedOutputEncoding)
expectedOutputsDirPath
- the path to a file or classpath resource that contains the expected outputsexpectedOutputEncoding
- the encoding of the files stored in the given pathpublic OutputTester(java.lang.Class<?> testRoot, java.lang.String expectedOutputsDirPath)
testRoot
- the test class whose test methods' outputs will be validatedexpectedOutputsDirPath
- the path to a file or classpath resource that contains the expected outputspublic OutputTester(java.lang.Class<?> testRoot, java.lang.String expectedOutputsDirPath, java.lang.String expectedOutputEncoding)
testRoot
- the test class whose test methods' outputs will be validatedexpectedOutputsDirPath
- the path to a file or classpath resource that contains the expected outputsexpectedOutputEncoding
- the encoding of the files stored in the given pathpublic void setUpdateExpectedOutputs(boolean updateExpectedOutputs)
getUpdateExpectedOutputs()
method evaluates to true
, all calls to validate(Object...)
or printAndValidate(Object...)
will trigger the updateExpectedOutput(Object...)
method internally. No tests will fail but error messages will be
printed out to the standard output to remind users that the outputs are not being validated.updateExpectedOutputs
- flag to indicate whether all expected outputs of a test case should be updated.public boolean getUpdateExpectedOutputs()
true
,
all calls to validate(Object...)
or printAndValidate(Object...)
will trigger the
updateExpectedOutput(Object...)
method internally. No tests will fail but error messages will be
printed out to the standard output to remind users that the outputs are not being validated.true
to indicate whether all expected outputs of a test case should be updated, or false
if
the test case outputs will be validated.public void printAndDontValidate(java.lang.CharSequence output, java.lang.Object... methodArgs)
output
- the result of the test case.methodArgs
- arguments passed to the test method. Used when testing with data providerspublic void printAndDontValidate(java.lang.Object... methodArgs)
methodArgs
- arguments passed to the test method. Used when testing with data providerspublic void printAndValidate(java.lang.CharSequence output, java.lang.Object... methodArgs)
output
- the result of the test case to be validated against the expected output.methodArgs
- arguments passed to the test method. Used when testing with data providerspublic void printAndValidate(java.lang.Object... methodArgs)
methodArgs
- arguments passed to the test method. Used when testing with data providerspublic void validate(java.lang.CharSequence output, java.lang.Object... methodArgs)
output
- the result of the test case to be validated against the expected output.methodArgs
- arguments passed to the test method. Used when testing with data providerspublic void validate(java.lang.Object... methodArgs)
methodArgs
- arguments passed to the test method. Used when testing with data providerspublic java.io.File getResourceDir()
updateExpectedOutput(Object...)
is called to determine where to update/create expected output files.public void setResourceDir(java.io.File resourceDir)
updateExpectedOutput(Object...)
is called to determine where to update/create expected output files.resourceDir
- project's resource directory.public java.lang.String getTestResourcesFolder()
updateExpectedOutput(Object...)
is called to determine where to update/create expected output files, and getResourceDir()
evaluates to null
Defaults to "src/test/resources"public void setTestResourcesFolder(java.lang.String testResourcesFolder)
updateExpectedOutput(Object...)
is called to determine where to update/create expected output files, and getResourceDir()
evaluates to null
;
Defaults to "src/test/resources"testResourcesFolder
- the test resources folder.public void updateExpectedOutput(java.lang.Object... methodArgs)
methodArgs
- arguments passed to the test method. Used when testing with data providerspublic void updateExpectedOutput(java.lang.CharSequence output, java.lang.Object... methodArgs)
output
- the actual output whose contents will be used to generate/update the expected output file.methodArgs
- arguments passed to the test method. Used when testing with data providersprivate java.io.File getExpectedOutputDir()
public void println(java.lang.StringBuilder out, java.lang.Object contents)
out
- the output to have content appended to.contents
- the contents to be appendedpublic void println(java.lang.Object contents)
contents
- the contents to be appendedpublic void println(java.lang.StringBuilder out)
out
- the output to have a newline appendedpublic void println()
public void print(java.lang.StringBuilder out, java.lang.Object contents)
out
- the output to have content appended to.contents
- the contents to be appendedpublic void print(java.lang.Object contents)
contents
- the contents to be appendedprivate void printAndValidateOutput(boolean validate, boolean print, java.lang.String producedOutput, java.lang.Object[] methodArgs)
expectedOutputsDirPath
.validate
- flag to indicate whether the output should be validatedprint
- flag that indicates whether or not to print the outputproducedOutput
- the output produced by an examplemethodArgs
- arguments passed to the test method. Used when testing with data providersprivate void printAndValidateOutput(boolean validate, boolean print, java.lang.String producedOutput, java.io.File expectedOutputDir, java.lang.Object[] methodArgs)
expectedOutputsDirPath
. If expectedOutputToUpdate
is not null,
the expected output file will be generated/updated to store the expected output, and the the test method will fail.validate
- flag to indicate whether the output should be validatedprint
- flag that indicates whether or not to print the outputproducedOutput
- the output produced by an exampleexpectedOutputDir
- directory of the expected output file to generate/updatemethodArgs
- arguments passed to the test method. Used when testing with data providersprivate void performValidation(boolean validate, boolean print, java.lang.String className, java.lang.String method, java.lang.Object[] methodArgs, java.lang.String producedOutput, java.io.File expectedOutputDir)
private boolean isTestMethod(java.lang.String className, java.lang.String methodName)
private void print(java.lang.String output, java.lang.String className, java.lang.String method)
private java.io.InputStream getResultData(java.lang.String className, java.lang.String testMethod)
private void validateExampleOutput(java.lang.String className, java.lang.String testMethod, java.lang.String producedOutput, java.io.File expectedOutputDir)
private void updateExpectedOutput(java.lang.String className, java.lang.String testMethod, java.lang.String producedOutput, java.io.File expectedOutputDir)
private java.lang.String cleanup(java.lang.String content)
public void clear()
private java.lang.String getOutputAndClear()
public boolean isNormalizeLineSeparators()
'\n'
).public void setNormalizeLineSeparators(boolean normalizeLineSeparators)
'\n'
.normalizeLineSeparators
- flag to enable or disable normalization of line separatorspublic boolean isDumpMismatchedOutputToFile()
">> Output dumped into temporary file: <tmp_dir>/<method_name>_<random_number>.txt"
will be
produced before the assertion error is thrown.public void setDumpMismatchedOutputToFile(boolean dumpMismatchedOutputToFile)
">> Output dumped into temporary file: <tmp_dir>/<method_name>_<random_number>.txt"
will be
produced before the assertion error is thrown.dumpMismatchedOutputToFile
- a flag indicating whether the output produced by failing test methods should
be saved into a temporary file.