public TestReport encode(URL srcURL, FileOutputStream fos) {
    DefaultTestReport report = new DefaultTestReport(this);
    try {
      ImageTranscoder transcoder = getTestImageTranscoder();
      TranscoderInput src = new TranscoderInput(svgURL.toString());
      TranscoderOutput dst = new TranscoderOutput(fos);
      transcoder.transcode(src, dst);
      return null;
    } 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()
                    }))
          });
    } 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);
    return report;
  }
Exemplo n.º 2
0
  /**
   * This method will only throw exceptions if some aspect of the test's internal operation fails.
   */
  public TestReport runImpl() throws Exception {
    DefaultTestReport report = new DefaultTestReport(this);

    SVGGraphics2D g2d = buildSVGGraphics2D();
    g2d.setSVGCanvasSize(CANVAS_SIZE);

    //
    // Generate SVG content
    //
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF-8");
    try {
      painter.paint(g2d);
      configureSVGGraphics2D(g2d);
      g2d.stream(osw);
      osw.flush();
      bos.flush();
      bos.close();
    } catch (Exception e) {
      StringWriter trace = new StringWriter();
      e.printStackTrace(new PrintWriter(trace));
      report.setErrorCode(ERROR_CANNOT_GENERATE_SVG);
      report.setDescription(
          new TestReport.Entry[] {
            new TestReport.Entry(
                Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                Messages.formatMessage(
                    ERROR_CANNOT_GENERATE_SVG,
                    new String[] {
                      painter == null ? "null" : painter.getClass().getName(),
                      e.getClass().getName(),
                      e.getMessage(),
                      trace.toString()
                    }))
          });
      report.setPassed(false);
      return report;
    }

    //
    // Compare with reference SVG
    //
    InputStream refStream = null;
    try {
      refStream = new BufferedInputStream(refURL.openStream());
    } catch (Exception e) {
      report.setErrorCode(ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE);
      report.setDescription(
          new TestReport.Entry[] {
            new TestReport.Entry(
                Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                Messages.formatMessage(
                    ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE,
                    new Object[] {
                      refURL != null ? refURL.toExternalForm() : "null", e.getMessage()
                    }))
          });
      report.setPassed(false);
      save(bos.toByteArray());
      return report;
    }

    InputStream newStream = new ByteArrayInputStream(bos.toByteArray());

    boolean accurate = true;
    String refLine = null;
    String newLine = null;
    int ln = 1;

    try {
      // accurate = compare(refStream, newStream);
      BufferedReader refReader = new BufferedReader(new InputStreamReader(refStream));
      BufferedReader newReader = new BufferedReader(new InputStreamReader(newStream));
      while ((refLine = refReader.readLine()) != null) {
        newLine = newReader.readLine();
        if (newLine == null || !refLine.equals(newLine)) {
          accurate = false;
          break;
        }
        ln++;
      }

      if (accurate) {
        // need to make sure newLine is null as well
        newLine = newReader.readLine();
        if (newLine != null) {
          accurate = false;
        }
      }

    } 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[] {refURL.toExternalForm(), e.getMessage()}))
          });
      report.setPassed(false);
      save(bos.toByteArray());
      return report;
    }

    if (!accurate) {
      save(bos.toByteArray());
      int cn = computeColumnNumber(refLine, newLine);
      String expectedChar = "eol";
      if (cn >= 0 && refLine != null && refLine.length() > cn) {
        expectedChar = (new Character(refLine.charAt(cn))).toString();
      }
      String foundChar = "null";
      if (cn >= 0 && newLine != null && newLine.length() > cn) {
        foundChar = (new Character(newLine.charAt(cn))).toString();
      }

      if (expectedChar.equals(" ")) {
        expectedChar = "' '";
      }
      if (foundChar.equals(" ")) {
        foundChar = "' '";
      }

      report.setErrorCode(ERROR_GENERATED_SVG_INACCURATE);
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_LINE_NUMBER, null), new Integer(ln));
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_NUMBER, null), new Integer(cn));
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_EXPECTED_VALUE, null), expectedChar);
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_FOUND_VALUE, null), foundChar);
      report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_REFERENCE_LINE, null), refLine);
      report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_NEW_LINE, null), newLine);
      report.setPassed(false);
    } else {
      report.setPassed(true);
    }

    return report;
  }