All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.masterthought.cucumber.json.Embedded Maven / Gradle / Ivy

Go to download

Provides pretty html reports for Cucumber (Behaviour-Driven Development). It works by generating html from the cucumber json report formatter. So can be used anywhere a json report is generated. Current use is in the cucumber jenkins plugin and a maven mojo to generate the same report from mvn command line when running locally.

There is a newer version: 5.8.4
Show newest version
package net.masterthought.cucumber.json;

import org.apache.commons.io.Charsets;
import org.codehaus.plexus.util.Base64;

/**
 * @author Damian Szczepanik (damianszczepanik@github)
 */
public class Embedded {

    // Start: attributes from JSON file report
    private final String mime_type = null;
    private final String data = null;
    // End: attributes from JSON file report

    /**
     * Generates HTML code with attachment.
     * 
     * @param index
     *            number of the attachment for given step
     * @return generated HTML page
     */
    public String render(int index) {

        final String contentId = "embedding_" + hashCode();

        switch (mime_type) {
        case "image/png":
            return buildImage("png", contentId, index);
        case "image/bmp":
            return buildImage("bmp", contentId, index);
        case "image/jpeg":
            return buildImage("jpeg", contentId, index);
        case "text/plain":
            return buildPlainText("plain text", contentId, index);
        case "text/html":
            return buildhHTML(contentId, index);
        default:
            return buildUnknown(mime_type, contentId, index);
        }
    }

    private String buildImage(String imgType, String contentId, int index) {
        final String encodedImageContent = "data:image/" + imgType + ";base64," + this.data;

        return toExpandable(contentId, index, imgType,
                String.format("", contentId, encodedImageContent));
    }

    private String buildPlainText(String mimeType, String contentId, int index) {
        return toExpandable(contentId, index, mimeType, String.format("
%s
", decodeDataFromBase())); } private String buildhHTML(String contentId, int index) { return toExpandable(contentId, index, "HTML", decodeDataFromBase()); } private String buildUnknown(String mimeType, String contentId, int index) { return toExpandable(contentId, index, mimeType, "File the bug so support for this mimetype can be added."); } private static String toExpandable(String contentId, int index, String mimeType, String content) { return String.format( "", contentId, index + 1, mimeType, contentId, content); } private String decodeDataFromBase() { return new String(Base64.decodeBase64(data.getBytes(Charsets.UTF_8)), Charsets.UTF_8); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy