Search in sources :

Example 1 with CucumberException

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) CucumberException(cucumber.runtime.CucumberException) TransformerException(javax.xml.transform.TransformerException)

Example 2 with CucumberException

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);
}
Also used : DecimalFormat(java.text.DecimalFormat) CucumberException(cucumber.runtime.CucumberException)

Example 3 with CucumberException

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);
}
Also used : CucumberException(cucumber.runtime.CucumberException)

Example 4 with CucumberException

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) CucumberException(cucumber.runtime.CucumberException) URL(java.net.URL)

Example 5 with CucumberException

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));
            }
        }
    }
}
Also used : DataTable(cucumber.api.DataTable) Utils.mapKeyType(cucumber.runtime.Utils.mapKeyType) Utils.listItemType(cucumber.runtime.Utils.listItemType) Utils.mapValueType(cucumber.runtime.Utils.mapValueType) Type(java.lang.reflect.Type) SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) CucumberException(cucumber.runtime.CucumberException)

Aggregations

CucumberException (cucumber.runtime.CucumberException)37 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 SingleValueConverter (cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter)4 IOException (java.io.IOException)4 Step (gherkin.formatter.model.Step)3 ObjectFactory (cucumber.api.java.ObjectFactory)2 StepdefBody (cucumber.api.java8.StepdefBody)2 RuntimeOptionsFactory (cucumber.runtime.RuntimeOptionsFactory)2 Resource (cucumber.runtime.io.Resource)2 Java8StepDefinition (cucumber.runtime.java.Java8StepDefinition)2 AssetManager (android.content.res.AssetManager)1 DataTable (cucumber.api.DataTable)1 PendingException (cucumber.api.PendingException)1 Scenario (cucumber.api.Scenario)1 After (cucumber.api.java.After)1 Cucumber (cucumber.api.junit.Cucumber)1 OsgiClassFinder (cucumber.java.runtime.osgi.OsgiClassFinder)1 PaxExamObjectFactory (cucumber.java.runtime.osgi.PaxExamObjectFactory)1 Backend (cucumber.runtime.Backend)1