* Requests this <tt>Test</tt> to run and produce a
* report.
*
*/
public TestReport run(){
DefaultTestReport report
= new DefaultTestReport(this);
//
// First, do clean-up
//
if (candidateReference != null){
if (candidateReference.exists()){
candidateReference.delete();
}
}
//
// Render the SVG image into a raster. We use the
// ImageTranscoder to convert the SVG into a raster in
// a temporary file.
//
File tmpFile = null;
try{
if (candidateReference != null)
tmpFile = candidateReference;
else
tmpFile = File.createTempFile(TEMP_FILE_PREFIX,
TEMP_FILE_SUFFIX,
null);
}catch(IOException e){
report.setErrorCode(ERROR_CANNOT_CREATE_TEMP_FILE);
report.setDescription(new TestReport.Entry[] {
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_CANNOT_CREATE_TEMP_FILE,
new Object[]{e.getMessage()}))
});
report.setPassed(false);
return report;
}
FileOutputStream tmpFileOS = null;
try{
tmpFileOS = new FileOutputStream(tmpFile);
}catch(IOException e){
report.setErrorCode(ERROR_CANNOT_CREATE_TEMP_FILE_STREAM);
report.setDescription(new TestReport.Entry[] {
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_CANNOT_CREATE_TEMP_FILE_STREAM,
new String[]{tmpFile.getAbsolutePath(),
e.getMessage()})) });
report.setPassed(false);
tmpFile.deleteOnExit();
return report;
}
ImageTranscoder transcoder = getTestImageTranscoder();
TranscoderInput src = new TranscoderInput(svgURL.toString());
TranscoderOutput dst = new TranscoderOutput(tmpFileOS);
try{
transcoder.transcode(src, dst);
}catch(TranscoderException e){
StringWriter trace = new StringWriter();
e.printStackTrace(new PrintWriter(trace));
report.setErrorCode(ERROR_CANNOT_TRANSCODE_SVG);
report.setDescription(new TestReport.Entry[]{
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_CANNOT_TRANSCODE_SVG,
new String[]{svgURL.toString(),
e.getClass().getName(),
e.getMessage(),
trace.toString()
})) });
report.setPassed(false);
tmpFile.deleteOnExit();
return report;
}catch(Exception e){
StringWriter trace = new StringWriter();
e.printStackTrace(new PrintWriter(trace));
report.setErrorCode(ERROR_CANNOT_TRANSCODE_SVG);
report.setDescription(new TestReport.Entry[]{
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_CANNOT_TRANSCODE_SVG,
new String[]{svgURL.toString(),
e.getClass().getName(),
e.getMessage(),
trace.toString()
})) });
report.setPassed(false);
tmpFile.deleteOnExit();
return report;
}
//
// Do a binary comparison of the encoded images.
//
InputStream refStream = null;
InputStream newStream = null;
try {
refStream =
new BufferedInputStream(refImgURL.openStream());
}catch(IOException e){
report.setErrorCode(ERROR_CANNOT_OPEN_REFERENCE_IMAGE);
report.setDescription( new TestReport.Entry[]{
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_CANNOT_OPEN_REFERENCE_IMAGE,
new Object[]{refImgURL.toString(),
e.getMessage()})) });
report.setPassed(false);
// Try and save tmp file as a candidate variation
if (candidateReference == null){
tmpFile.delete();
}
return report;
}
try{
newStream =
new BufferedInputStream(new FileInputStream(tmpFile));
}catch(IOException e){
report.setErrorCode(ERROR_CANNOT_OPEN_GENERATED_IMAGE);
report.setDescription(new TestReport.Entry[]{
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_CANNOT_OPEN_GENERATED_IMAGE,
new Object[]{tmpFile.getAbsolutePath(),
e.getMessage()}))});
report.setPassed(false);
tmpFile.delete();
return report;
}
boolean accurate = false;
try{
accurate = compare(refStream, newStream);
} catch(IOException e) {
report.setErrorCode(ERROR_ERROR_WHILE_COMPARING_FILES);
report.setDescription(new TestReport.Entry[]{
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_ERROR_WHILE_COMPARING_FILES,
new Object[]{refImgURL.toString(),
tmpFile.getAbsolutePath(),
e.getMessage()}))});
report.setPassed(false);
if (candidateReference == null){
tmpFile.delete();
}
return report;
}
//
// If the files differ here, it means that even the variation does
// not account for the difference return an error
//
if(!accurate){
try{
BufferedImage ref = getImage(refImgURL);
BufferedImage gen = getImage(tmpFile);
BufferedImage diff = buildDiffImage(ref, gen);
//
// If there is an accepted variation, check if it equals the
// computed difference.
//
if(variationURL != null){
File tmpDiff = imageToFile(diff, IMAGE_TYPE_DIFF);
InputStream variationURLStream = null;
try{
variationURLStream = variationURL.openStream();
}catch(IOException e){
// Could not open variationURL stream. Just trace that
System.err.println(Messages.formatMessage(COULD_NOT_OPEN_VARIATION_URL,
new Object[]{variationURL.toString()}));
}
if(variationURLStream != null){
InputStream refDiffStream =
new BufferedInputStream(variationURLStream);
InputStream tmpDiffStream =
new BufferedInputStream(new FileInputStream(tmpDiff));
if(compare(refDiffStream, tmpDiffStream)){
// We accept the generated result.
accurate = true;
}
}
}
if(!accurate){
System.err.println(">>>>>>>>>>>>>>>>>>>>>> Rendering is not accurate");
if(saveVariation != null){
// There is a computed variation different from the
// referenced variation and there is a place where the new
// variation should be saved.
saveImage(diff, saveVariation);
}
// Build two images:
// a. One with the reference image and the newly generated image
// b. One with the difference between the two images and the set of
// different pixels.
BufferedImage cmp = makeCompareImage(ref, gen);
File cmpFile = imageToFile(cmp, IMAGE_TYPE_COMPARISON);
File diffFile = imageToFile(diff, IMAGE_TYPE_DIFF);
report.setErrorCode(ERROR_SVG_RENDERING_NOT_ACCURATE);
report.setDescription(new TestReport.Entry[]{
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_SVG_RENDERING_NOT_ACCURATE, null)),
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_REFERENCE_GENERATED_IMAGE_URI, null),
cmpFile),
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_DIFFERENCE_IMAGE, null),
diffFile) });
if (candidateReference == null){
tmpFile.delete();
}
report.setPassed(false);
return report;
}
}catch(Exception e){
report.setErrorCode(ERROR_SVG_RENDERING_NOT_ACCURATE);
StringWriter trace = new StringWriter();
e.printStackTrace(new PrintWriter(trace));
report.setDescription(new TestReport.Entry[]{
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
Messages.formatMessage(ERROR_SVG_RENDERING_NOT_ACCURATE, null)),
new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_INTERNAL_ERROR, null),
Messages.formatMessage(COULD_NOT_GENERATE_COMPARISON_IMAGES,
new Object[]{e.getClass().getName(),
e.getMessage(),
trace.toString()})) });
if (candidateReference == null){
tmpFile.delete();
}
report.setPassed(false);
return report;
}
}
//
// Yahooooooo! everything worked out well.
//
report.setPassed(true);
tmpFile.delete();
return report;
}