
com.github.mkolisnyk.cucumber.reporting.CucumberResultsCommon Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cucumber-reports Show documentation
Show all versions of cucumber-reports Show documentation
Library generating different Cucumber reports
package com.github.mkolisnyk.cucumber.reporting;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.JAXB;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.cedarsoftware.util.io.JsonObject;
import com.cedarsoftware.util.io.JsonReader;
import com.github.mkolisnyk.cucumber.reporting.types.result.CucumberFeatureResult;
//import com.google.common.io.Files;
public abstract class CucumberResultsCommon {
public static final int CHART_WIDTH = 450;
public static final int CHART_HEIGHT = 300;
public static final int CHART_THICKNESS = 20;
private String[] sourceFiles;
private String outputDirectory;
private String outputName;
private String pdfPageSize = "auto";
/**
* @return the outputDirectory
*/
public final String getOutputDirectory() {
return outputDirectory;
}
/**
* @param outputDirectoryValue the outputDirectory to set
*/
public final void setOutputDirectory(String outputDirectoryValue) {
this.outputDirectory = outputDirectoryValue;
}
/**
* @return the outputName
*/
public final String getOutputName() {
return outputName;
}
/**
* @param outputNameValue the outputName to set
*/
public final void setOutputName(String outputNameValue) {
this.outputName = outputNameValue;
}
/**
* @return the sourceFile
*/
public final String getSourceFile() {
return sourceFiles[0];
}
/**
* @param sourceFileValue the sourceFile to set
*/
public final void setSourceFile(String sourceFileValue) {
this.sourceFiles = new String[] {sourceFileValue};
}
public String[] getSourceFiles() {
return sourceFiles;
}
public void setSourceFiles(String[] sourceFilesValue) {
this.sourceFiles = sourceFilesValue;
}
public String getPdfPageSize() {
return pdfPageSize;
}
public void setPdfPageSize(String pdfPageSizeValue) {
this.pdfPageSize = pdfPageSizeValue;
}
public CucumberFeatureResult[] aggregateResults(CucumberFeatureResult[] input, boolean collapse) {
for (int i = 0; i < input.length; i++) {
input[i].setId("" + input[i].getLine() + "" + input[i].getId());
input[i].aggregateScenarioResults(collapse);
}
return input;
}
@SuppressWarnings("unchecked")
public CucumberFeatureResult[] readFileContent(String sourceFileValue, boolean aggregate) throws Exception {
FileInputStream fis = null;
JsonReader jr = null;
File file = new File(sourceFileValue);
if (!(file.exists() && file.isFile())) {
throw new FileNotFoundException();
}
fis = new FileInputStream(file);
jr = new JsonReader(fis, true);
JsonObject source = (JsonObject) jr.readObject();
Object[] objs = (Object[]) source.get("@items");
CucumberFeatureResult[] sources = new CucumberFeatureResult[objs.length];
for (int i = 0; i < objs.length; i++) {
sources[i] = new CucumberFeatureResult((JsonObject) objs[i]);
}
jr.close();
fis.close();
sources = aggregateResults(sources, aggregate);
return sources;
}
@SuppressWarnings("unchecked")
public CucumberFeatureResult[] readFileContent(boolean aggregate) throws Exception {
return readFileContent(this.getSourceFiles(), aggregate);
}
private CucumberFeatureResult[] readFileContent(String[] sourceFilesValue,
boolean aggregate) throws Exception {
CucumberFeatureResult[] output = {};
for (String sourceFile : sourceFilesValue) {
output = (CucumberFeatureResult[]) ArrayUtils.addAll(output, readFileContent(sourceFile, aggregate));
}
return output;
}
/*@SuppressWarnings("unchecked")
public T[] readFileContent(Class param) throws Exception {
FileInputStream fis = null;
JsonReader jr = null;
File file = new File(this.getSourceFile());
if (!(file.exists() && file.isFile())) {
throw new FileNotFoundException();
}
fis = new FileInputStream(file);
jr = new JsonReader(fis, true);
JsonObject source = (JsonObject) jr.readObject();
Object[] objs = (Object[]) source.get("@items");
T[] sources = (T[]) Array.newInstance(param, objs.length);
for (int i = 0; i < objs.length; i++) {
sources[i] = (T) param.getConstructors()[0].newInstance((JsonObject) objs[i]);
}
jr.close();
fis.close();
return sources;
}*/
public String replaceHtmlEntitiesWithCodes(String input) throws IOException {
String output = input;
Map entitiesMap = new HashMap();
InputStream is = this.getClass().getResourceAsStream("/html_entities_map.txt");
String[] result = IOUtils.toString(is).split("\n");
is.close();
for (String line : result) {
entitiesMap.put(line.split("(\\s+)")[0], line.split("(\\s+)")[1]);
}
for (Entry entry : entitiesMap.entrySet()) {
output = output.replace(entry.getKey(), entry.getValue());
}
return output;
}
public void dumpOverviewStats(File outFile, CucumberFeatureResult[] results) {
int[][] stats = getStatuses(results);
JAXB.marshal(stats, outFile);
}
private void convertSvgToPng(File svg, File png) throws Exception {
String svgUriInput = svg.toURI().toURL().toString();
TranscoderInput inputSvgImage = new TranscoderInput(svgUriInput);
//Step-2: Define OutputStream to PNG Image and attach to TranscoderOutput
OutputStream pngOStream = new FileOutputStream(png);
TranscoderOutput outputPngImage = new TranscoderOutput(pngOStream);
// Step-3: Create PNGTranscoder and define hints if required
PNGTranscoder myConverter = new PNGTranscoder();
// Step-4: Convert and Write output
myConverter.transcode(inputSvgImage, outputPngImage);
// Step 5- close / flush Output Stream
pngOStream.flush();
pngOStream.close();
}
public String replaceSvgWithPng(File htmlFile) throws Exception {
String tempPath = this.getOutputDirectory() + File.separator + "temp" + (new Date()).getTime();
File folder = new File(tempPath);
//Files.createTempDirectory("temp").toFile();
folder.mkdirs();
folder.deleteOnExit();
String htmlText = FileUtils.readFileToString(htmlFile);
Pattern p = Pattern.compile("
© 2015 - 2025 Weber Informatics LLC | Privacy Policy