use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class JUnitFormatter method done.
@Override
public void done() {
try {
// set up a transformer
rootElement.setAttribute("name", JUnitFormatter.class.getName());
rootElement.setAttribute("failures", String.valueOf(rootElement.getElementsByTagName("failure").getLength()));
rootElement.setAttribute("skipped", String.valueOf(rootElement.getElementsByTagName("skipped").getLength()));
rootElement.setAttribute("time", sumTimes(rootElement.getElementsByTagName("testcase")));
if (rootElement.getElementsByTagName("testcase").getLength() == 0) {
// to avoid failed Jenkins jobs
addDummyTestCase();
}
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(out);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
} catch (TransformerException e) {
throw new CucumberException("Error while transforming.", e);
}
}
use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class JUnitFormatter method sumTimes.
private String sumTimes(NodeList testCaseNodes) {
double totalDurationSecondsForAllTimes = 0.0d;
for (int i = 0; i < testCaseNodes.getLength(); i++) {
try {
double testCaseTime = Double.parseDouble(testCaseNodes.item(i).getAttributes().getNamedItem("time").getNodeValue());
totalDurationSecondsForAllTimes += testCaseTime;
} catch (NumberFormatException e) {
throw new CucumberException(e);
} catch (NullPointerException e) {
throw new CucumberException(e);
}
}
DecimalFormat nfmt = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
nfmt.applyPattern("0.######");
return nfmt.format(totalDurationSecondsForAllTimes);
}
use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class TestNGFormatter method getTotalDuration.
private String getTotalDuration(NodeList testCaseNodes) {
long totalDuration = 0;
for (int i = 0; i < testCaseNodes.getLength(); i++) {
try {
String duration = testCaseNodes.item(i).getAttributes().getNamedItem("duration-ms").getNodeValue();
totalDuration += Long.parseLong(duration);
} catch (NumberFormatException e) {
throw new CucumberException(e);
} catch (NullPointerException e) {
throw new CucumberException(e);
}
}
return String.valueOf(totalDuration);
}
use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class Helpers method jarFilePath.
static String jarFilePath(URL jarUrl) {
String urlFile = jarUrl.getFile();
int separatorIndex = urlFile.indexOf("!/");
if (separatorIndex == -1) {
throw new CucumberException("Expected a jar URL: " + jarUrl.toExternalForm());
}
try {
URL fileUrl = new URL(urlFile.substring(0, separatorIndex));
return filePath(fileUrl);
} catch (MalformedURLException e) {
throw new CucumberException(e);
}
}
use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class TableConverter method convert.
/**
* This method converts a {@link cucumber.api.DataTable} to abother type.
* When a Step Definition is passed a Gherkin Data Table, the runtime will use this method to convert the
* {@link cucumber.api.DataTable} to the declared type before invoking the Step Definition.
* <p/>
* This method uses reflection to inspect the type and delegates to the appropriate {@code toXxx} method.
*
* @param dataTable the table to convert
* @param type the type to convert to
* @param transposed whether the table should be transposed first.
* @return the transformed object.
*/
public <T> T convert(DataTable dataTable, Type type, boolean transposed) {
if (transposed) {
dataTable = dataTable.transpose();
}
if (type == null || (type instanceof Class && ((Class) type).isAssignableFrom(DataTable.class))) {
return (T) dataTable;
}
Type mapKeyType = mapKeyType(type);
if (mapKeyType != null) {
Type mapValueType = mapValueType(type);
return (T) toMap(dataTable, mapKeyType, mapValueType);
}
Type itemType = listItemType(type);
if (itemType == null) {
throw new CucumberException("Not a Map or List type: " + type);
}
Type listItemType = listItemType(itemType);
if (listItemType != null) {
return (T) toLists(dataTable, listItemType);
} else {
SingleValueConverter singleValueConverter = xStream.getSingleValueConverter(itemType);
if (singleValueConverter != null) {
return (T) toList(dataTable, singleValueConverter);
} else {
if (itemType instanceof Class) {
if (Map.class.equals(itemType)) {
// Non-generic map
return (T) toMaps(dataTable, String.class, String.class);
} else {
return (T) toListOfComplexType(dataTable, (Class) itemType);
}
} else {
return (T) toMaps(dataTable, mapKeyType(itemType), mapValueType(itemType));
}
}
}
}
Aggregations