private Appendable defaultOutOrFailIfAlreadyUsed(String formatterString) { try { if (defaultOut != null) { defaultOutFormatter = formatterString; return defaultOut; } else { throw new CucumberException("Only one formatter can use STDOUT, now both " + defaultOutFormatter + " and " + formatterString + " use it. " + "If you use more than one formatter you must specify output path with PLUGIN:PATH_OR_URL"); } } finally { defaultOut = null; } }
private CucumberException withLocation(CucumberException exception) { exception.setStackTrace(new StackTraceElement[]{this.location}); return exception; }
private <T extends Plugin> T newInstance(Constructor<T> constructor, Object... ctorArgs) { try { return constructor.newInstance(ctorArgs); } catch (InstantiationException e) { throw new CucumberException(e); } catch (IllegalAccessException e) { throw new CucumberException(e); } catch (InvocationTargetException e) { throw new CucumberException(e.getTargetException()); } }
private CucumberException registerTypeInConfiguration(String expressionString, UndefinedParameterTypeException e) { return new CucumberException(String.format("" + "Could not create a cucumber expression for '%s'.\n" + "It appears you did not register parameter type. The details are in the stacktrace below.\n" + "You can find the documentation here: https://docs.cucumber.io/cucumber/cucumber-expressions/", expressionString ), e); }
private URL toUrl(String fileName) { try { return new URL(htmlReportDir, fileName); } catch (IOException e) { throw new CucumberException(e); } }
public FeatureBuilder() { try { this.md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new CucumberException(e); } }
@SuppressWarnings("unchecked") private static Class<? extends Plugin> loadClass(String className) { try { Class<?> aClass = Thread.currentThread().getContextClassLoader().loadClass(className); if (Plugin.class.isAssignableFrom(aClass)) { return (Class<? extends Plugin>) aClass; } throw new CucumberException("Couldn't load plugin class: " + className + ". It does not implement " + Plugin.class.getName()); } catch (ClassNotFoundException e) { throw new CucumberException("Couldn't load plugin class: " + className, e); } }
static String filePath(URL fileUrl) { if (!"file".equals(fileUrl.getProtocol())) { throw new CucumberException("Expected a file URL:" + fileUrl.toExternalForm()); } try { return fileUrl.toURI().getSchemeSpecificPart(); } catch (URISyntaxException e) { throw new CucumberException(e); } }
private <T> Constructor<T> findSingleArgConstructor(Class<T> pluginClass) { Constructor<T> constructor = null; for (Class ctorArgClass : CTOR_PARAMETERS) { try { Constructor<T> candidate = pluginClass.getConstructor(ctorArgClass); if (constructor != null) { throw new CucumberException(String.format("Plugin %s should only define a single one-argument constructor", pluginClass.getName())); } constructor = candidate; } catch (NoSuchMethodException ignore) { } } return constructor; }
public static URL toURL(String pathOrUrl) { try { if (!pathOrUrl.endsWith("/")) { pathOrUrl = pathOrUrl + "/"; } if (pathOrUrl.matches("^(file|http|https):.*")) { return new URL(pathOrUrl); } else { return new URL("file:" + pathOrUrl); } } catch (MalformedURLException e) { throw new CucumberException("Bad URL:" + pathOrUrl, e); } }
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); }
public <T> T newInstance(Class[] constructorParams, Object[] constructorArgs, Class<? extends T> clazz) { Constructor<? extends T> constructor = null; try { constructor = clazz.getConstructor(constructorParams); try { return constructor.newInstance(constructorArgs); } catch (Exception e) { String message = String.format("Failed to instantiate %s with %s", constructor.toGenericString(), Arrays.asList(constructorArgs)); throw new CucumberException(message, e); } } catch (NoSuchMethodException e) { throw new CucumberException(e); } }
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); } } }
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); }
public static SnippetType fromString(String name) { for (SnippetType snippetType : SnippetType.values()) { if (name.equalsIgnoreCase(snippetType.name)) { return snippetType; } } throw new CucumberException(String.format("Unrecognized SnippetType %s", name)); }
private void notifyContextManagerAboutTestClassStarted() { if (testContextManager != null) { try { testContextManager.beforeTestClass(); } catch (Exception e) { throw new CucumberException(e.getMessage(), e); } } }
private void notifyContextManagerAboutTestClassFinished() { if (testContextManager != null) { try { testContextManager.afterTestClass(); } catch (Exception e) { throw new CucumberException(e.getMessage(), e); } } }
@Override public Collection<? extends Backend> get() { Collection<? extends Backend> backends = loadBackends(); if (backends.isEmpty()) { throw new CucumberException("No backends were found. Please make sure you have a backend module on your CLASSPATH."); } return backends; }
private static OutputStream createReportFileOutputStream(URL url) { try { return new URLOutputStream(url); } catch (IOException e) { throw new CucumberException(e); } }
private static void checkNoComponentAnnotations(Class<?> type) { for (Annotation annotation : type.getAnnotations()) { if (hasComponentAnnotation(annotation)) { throw new CucumberException(String.format("" + "Glue class %1$s was annotated with @%2$s; marking it as a candidate for auto-detection by " + "Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by " + "spring may lead to duplicate bean definitions. Please remove the @%2$s annotation", type.getName(), annotation.annotationType().getSimpleName())); } } }