protected JUnitTestMethod(JUnitTestClass owner, ConstructorOrMethod method, Object instance) { this(owner, method.getName(), method, instance); }
@Override public boolean accept(ConstructorOrMethod method) { return "setUp".equals(method.getName()); } }, cls);
@Override public boolean accept(ConstructorOrMethod method) { return "tearDown".equals(method.getName()); } }, cls);
@Override public boolean accept(ConstructorOrMethod method) { return method.getName().startsWith("test") && method.getParameterTypes().length == 0; } }, cls);
public String getSimpleName() { return m_method.getDeclaringClass().getSimpleName() + "." + m_method.getName(); }
private String computeSignature() { String classLong = m_method.getDeclaringClass().getName(); String cls = classLong.substring(classLong.lastIndexOf(".") + 1); StringBuilder result = new StringBuilder(cls).append(".").append(m_method.getName()).append("("); int i = 0; for (Class<?> p : m_method.getParameterTypes()) { if (i++ > 0) { result.append(", "); } result.append(p.getName()); } result.append(")"); result.append("[pri:").append(getPriority()).append(", instance:").append(m_instance).append("]"); return result.toString(); }
static StringBuilder stringify(String cls, ConstructorOrMethod method) { StringBuilder result = new StringBuilder(cls).append(".").append(method.getName()).append("("); return result.append(method.stringifyParameterTypes()).append(")"); }
@Override public String toString() { ConstructorOrMethod m = getConstructorOrMethod(); String cls = m.getDeclaringClass().getName(); StringBuilder result = new StringBuilder(cls).append(".").append( m.getName()).append("("); int i = 0; for (Class<?> p : m.getParameterTypes()) { if (i++ > 0) { result.append(", "); } result.append(p.getName()); } result.append(")"); return result.toString(); }
@Override public ITestNGMethod[] getBeforeTestMethods(Class cls) { return privateFindTestMethods(method -> "setUp".equals(method.getName()), cls); }
@Override public ITestNGMethod[] getAfterTestMethods(Class cls) { return privateFindTestMethods(method -> "tearDown".equals(method.getName()), cls); }
public String getSimpleName() { return m_method.getDeclaringClass().getSimpleName() + "." + m_method.getName(); }
private void generateExcludedMethodsReport(XmlSuite xmlSuite, ISuite suite) { Collection<ITestNGMethod> excluded = suite.getExcludedMethods(); StringBuilder sb2 = new StringBuilder("<h2>Methods that were not run</h2><table>\n"); for (ITestNGMethod method : excluded) { ConstructorOrMethod m = method.getConstructorOrMethod(); if (m != null) { sb2.append("<tr><td>") .append(m.getDeclaringClass().getName()).append(".").append(m.getName()); String description = method.getDescription(); if(isStringNotEmpty(description)) { sb2.append("<br/>").append(SP2).append("<i>").append(description).append("</i>"); } sb2.append("</td></tr>\n"); } } sb2.append("</table>"); Utils.writeFile(getOutputDirectory(xmlSuite), METHODS_NOT_RUN, sb2.toString()); }
/** * @param result * @param tm * @return true if a method by a similar name (and same hierarchy) already * exists */ private static boolean isMethodAlreadyPresent(List<ITestNGMethod> result, ITestNGMethod tm) { for (ITestNGMethod m : result) { ConstructorOrMethod jm1 = m.getConstructorOrMethod(); ConstructorOrMethod jm2 = tm.getConstructorOrMethod(); if (jm1.getName().equals(jm2.getName())) { // Same names, see if they are in the same hierarchy Class<?> c1 = jm1.getDeclaringClass(); Class<?> c2 = jm2.getDeclaringClass(); if (c1.isAssignableFrom(c2) || c2.isAssignableFrom(c1)) { return true; } } } return false; }
@Override public ITestNGMethod[] getTestMethods(Class cls, XmlTest xmlTest) { return privateFindTestMethods( method -> method.getName().startsWith("test") && method.getParameterTypes().length == 0, cls); }
private static MatchResults matchMethod(ITestNGMethod[] methods, String regexp) { MatchResults results = new MatchResults(); boolean usePackage = regexp.indexOf('.') != -1; Pattern pattern = Pattern.compile(regexp); for (ITestNGMethod method : methods) { ConstructorOrMethod thisMethod = method.getConstructorOrMethod(); String thisMethodName = thisMethod.getName(); String methodName = usePackage ? calculateMethodCanonicalName(method) : thisMethodName; Pair<String, String> cacheKey = Pair.create(regexp, methodName); Boolean match = MATCH_CACHE.get(cacheKey); if (match == null) { match = pattern.matcher(methodName).matches(); MATCH_CACHE.put(cacheKey, match); } if (match) { results.matchedMethods.add(method); results.foundAtLeastAMethod = true; } } return results; }
private String findDescription(ITestAnnotation testAnnotation, XmlTest xmlTest) { String result = testAnnotation.getDescription(); if (result == null) { List<XmlClass> classes = xmlTest.getXmlClasses(); for (XmlClass c : classes) { if (c.getName().equals(m_method.getMethod().getDeclaringClass().getName())) { for (XmlInclude include : c.getIncludedMethods()) { if (include.getName().equals(m_method.getName())) { result = include.getDescription(); if (result != null) { break; } } } } } } return result; }
@Test public void mainTest() { Assert.assertEquals(m_method.getName(), "mainTest"); ITestNGMethod[] methods = m_context.getAllTestMethods(); Assert.assertEquals(1, methods.length); Assert.assertEquals(methods[0].getConstructorOrMethod().getName(), "mainTest"); } }
@Test public void f(ITestContext tc) { Assert.assertNotNull(tc); ITestNGMethod[] allMethods = tc.getAllTestMethods(); Assert.assertEquals(allMethods.length, 1); Assert.assertEquals(allMethods[0].getConstructorOrMethod().getName(), "f"); } }
/** Returns the name of the executed test. */ private static String getTestName(IInvokedMethod method) { return StringUtils.substringAfterLast(method.getTestMethod().getTestClass().getName(), ".") + "#" + method.getTestMethod().getConstructorOrMethod().getName(); }
private static String getName(ITestResult result) { String testName = result.getName(); String methodName = result.getMethod().getConstructorOrMethod().getName(); String name; if (testName.contains(methodName)) { name = methodName; } else { name = testName + "#" + methodName; } if (result.getParameters().length != 0) { name = name + "(" + Joiner.on(",").useForNull("null").join(getParameterNames(result.getParameters())) + ")"; } return name; }