/** * Formats the given <code>{@link Description}</code> by surrounding its text value with square brackets and adding a space at * the end. * @param d the description to format. It can be {@code null}. * @return the formatted description, or an empty {@code String} if the the {@code Description} is {@code null}. */ public String format(Description d) { String s = (d != null) ? d.value() : null; if (isNullOrEmpty(s)) return ""; return String.format("[%s] ", s); }
/** * Sets a value in the property managed by this class. * @param value the value to set. * @throws ReflectionError if the given value cannot be set. */ public void set(T value) { try { descriptor.getWriteMethod().invoke(target, value); } catch (Exception e) { throw new ReflectionError(concat("Unable to update the value in property ", quote(propertyName)), e); } }
private static FilesException cannotCreateNewFile(String path, String reason, Exception cause) { String message = String.format("Unable to create the new file %s", quote(path)); if (!Strings.isNullOrEmpty(reason)) { message = concat(message, ": ", reason); } if (cause != null) { throw new FilesException(message, cause); } throw new FilesException(message); }
/** * When a test fails, this method takes a screenshot of the desktop and adds an hyperlink to the screenshot it in the * HTML test report. * @param result contains information about the failing test. */ @Override public void onTestFailure(ITestResult result) { if (!ready || !isGUITest(result)) return; String screenshotFileName = takeScreenshotAndReturnFileName(result); if (isEmpty(screenshotFileName)) return; logger.info(concat("Screenshot of desktop saved as: ", quote(screenshotFileName))); Reporter.setCurrentTestResult(result); Reporter.log(concat("<a href=\"", screenshotFileName, "\">Screenshot</a>")); }
/** * Returns the given {@code String} surrounded by single quotes, or {@code null} if the given {@code String} is * {@code null}. * * @param s the given {@code String}. * @return the given {@code String} surrounded by single quotes, or {@code null} if the given {@code String} is * {@code null}. */ public static String quote(String s) { return s != null ? concat("'", s, "'") : null; }
boolean hasPath() { return !isEmpty(path); }
/** * Returns the path of the system's temporary directory. This method appends the system's file separator at the end of * the path. * * @return the path of the system's temporary directory. */ public static String temporaryFolderPath() { return append(separator).to(System.getProperty("java.io.tmpdir")); }
static String screenshotFileNameFrom(ITestResult result) { String[] parts = namePartsFrom(result); return join(parts).with("."); }
/** * Returns the value of the property managed by this class. * @return the value of the property managed by this class. * @throws ReflectionError if the value of the property cannot be retrieved. */ @SuppressWarnings("unchecked") public T get() { try { return (T) descriptor.getReadMethod().invoke(target); } catch (Exception e) { throw new ReflectionError(concat("Unable to obtain the value in property " + quote(propertyName)), e); } }
private String expectedInnerClassName(String namespace) { return concat(namespace, "$", innerClassName); } }
private static void validateIsNotNullOrEmpty(String name) { if (name == null) throw new NullPointerException("The name of the field to access should not be null"); if (isEmpty(name)) throw new IllegalArgumentException("The name of the field to access should not be empty"); }
private static ReflectionError incorrectPropertyType(String name, Object target, Class<?> actual, Class<?> expected) { String typeName = target.getClass().getName(); String msg = concat("The type of the property ", quote(name), " in ", typeName, " should be <", expected.getName(), "> but was <", actual.getName(), ">"); throw new ReflectionError(msg); }
@Override public String toString() { String comparatorSimpleClassName = comparator.getClass().getSimpleName(); return quote(comparatorSimpleClassName.length() > 0 ? comparatorSimpleClassName : "anonymous comparator class"); }
/** * Appends the {@code String} specified in the constructor to the {@code String} passed as argument. * * @param s the target {@code String}. * @return a {@code String} containing the target {@code String} with the given {@code String} to append added to * the end. */ public String to(String s) { if (!s.endsWith(toAppend)) { return concat(s, toAppend); } return s; } }
private static void validateIsNotNullOrEmpty(String name) { if (name == null) throw new NullPointerException("The name of the property to access should not be null"); if (isEmpty(name)) throw new IllegalArgumentException("The name of the property to access should not be empty"); }
private AssertionError failureIfErrorMessageIsOverriden(AssertionInfo info) { String overridingErrorMessage = info.overridingErrorMessage(); return isNullOrEmpty(overridingErrorMessage) ? null : failure(MessageFormatter.instance().format(info.description(), overridingErrorMessage)); }
private static ReflectionError incorrectFieldType(Field field, Class<?> actual, Class<?> expected) { String fieldTypeName = field.getDeclaringClass().getName(); String message = concat("The type of the field ", quote(field.getName()), " in ", fieldTypeName, " should be <", expected.getName(), "> but was <", actual.getName(), ">"); throw new ReflectionError(message); }
@Override public String toString() { return format("%s[format=%s, arguments=%s]", getClass().getSimpleName(), quote(format), format(arguments)); }
private static <T> Constructor<T> constructor(Class<T> target, Class<?>... parameterTypes) { try { return target.getDeclaredConstructor(parameterTypes); } catch (Exception e) { throw new ReflectionError(concat("Unable to find constructor in type ", target.getName(), " with parameter types ", Arrays.toString(parameterTypes)), e); } }