Search in sources :

Example 6 with XmlClass

use of org.testng.xml.XmlClass in project intellij-community by JetBrains.

the class TestNGTreeHierarchyTest method testOneTestMethod.

@Test
public void testOneTestMethod() throws Exception {
    final XmlSuite suite = new XmlSuite();
    final XmlTest test = new XmlTest();
    final XmlClass xmlClass = new XmlClass("a.ATest", false);
    xmlClass.getIncludedMethods().add(new XmlInclude("test1"));
    test.getClasses().add(xmlClass);
    suite.getTests().add(test);
    doTest(suite, "##teamcity[enteredTheMatrix]\n" + "\n" + "##teamcity[testSuiteStarted name ='ATest' locationHint = 'java:suite://a.ATest']\n" + "\n" + "##teamcity[testStarted name='ATest.test1|[0|]' locationHint='java:test://a.ATest.test1|[0|]']\n" + "\n" + "##teamcity[testFinished name='ATest.test1|[0|]']\n");
}
Also used : XmlInclude(org.testng.xml.XmlInclude) XmlSuite(org.testng.xml.XmlSuite) XmlTest(org.testng.xml.XmlTest) XmlClass(org.testng.xml.XmlClass) Test(org.junit.Test) XmlTest(org.testng.xml.XmlTest)

Example 7 with XmlClass

use of org.testng.xml.XmlClass in project druid by druid-io.

the class TestNG method initializeSuitesAndJarFile.

public void initializeSuitesAndJarFile() {
    // so don't initialize suites twice.
    if (m_isInitialized) {
        return;
    }
    m_isInitialized = true;
    if (m_suites.size() > 0) {
        //to parse the suite files (<suite-file>), if any
        for (XmlSuite s : m_suites) {
            for (String suiteFile : s.getSuiteFiles()) {
                try {
                    Collection<XmlSuite> childSuites = getParser(suiteFile).parse();
                    for (XmlSuite cSuite : childSuites) {
                        cSuite.setParentSuite(s);
                        s.getChildSuites().add(cSuite);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace(System.out);
                } catch (ParserConfigurationException e) {
                    e.printStackTrace(System.out);
                } catch (SAXException e) {
                    e.printStackTrace(System.out);
                } catch (IOException e) {
                    e.printStackTrace(System.out);
                }
            }
        }
        return;
    }
    //
    for (String suitePath : m_stringSuites) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("suiteXmlPath: \"" + suitePath + "\"");
        }
        try {
            Collection<XmlSuite> allSuites = getParser(suitePath).parse();
            for (XmlSuite s : allSuites) {
                // If test names were specified, only run these test names
                if (m_testNames != null) {
                    m_suites.add(extractTestNames(s, m_testNames));
                } else {
                    m_suites.add(s);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace(System.out);
        } catch (IOException e) {
            e.printStackTrace(System.out);
        } catch (ParserConfigurationException e) {
            e.printStackTrace(System.out);
        } catch (SAXException e) {
            e.printStackTrace(System.out);
        } catch (Exception ex) {
            // Probably a Yaml exception, unnest it
            Throwable t = ex;
            while (t.getCause() != null) {
                t = t.getCause();
            }
            //        t.printStackTrace();
            if (t instanceof TestNGException) {
                throw (TestNGException) t;
            } else {
                throw new TestNGException(t);
            }
        }
    }
    // inside that jar path
    if (m_jarPath != null && m_stringSuites.size() > 0) {
        StringBuilder suites = new StringBuilder();
        for (String s : m_stringSuites) {
            suites.append(s);
        }
        Utils.log("TestNG", 2, "Ignoring the XML file inside " + m_jarPath + " and using " + suites + " instead");
        return;
    }
    if (isStringEmpty(m_jarPath)) {
        return;
    }
    // We have a jar file and no XML file was specified: try to find an XML file inside the jar
    File jarFile = new File(m_jarPath);
    try {
        Utils.log("TestNG", 2, "Trying to open jar file:" + jarFile);
        JarFile jf = new JarFile(jarFile);
        //      System.out.println("   result: " + jf);
        Enumeration<JarEntry> entries = jf.entries();
        List<String> classes = Lists.newArrayList();
        boolean foundTestngXml = false;
        while (entries.hasMoreElements()) {
            JarEntry je = entries.nextElement();
            if (je.getName().equals(m_xmlPathInJar)) {
                Parser parser = getParser(jf.getInputStream(je));
                m_suites.addAll(parser.parse());
                foundTestngXml = true;
                break;
            } else if (je.getName().endsWith(".class")) {
                int n = je.getName().length() - ".class".length();
                classes.add(je.getName().replace("/", ".").substring(0, n));
            }
        }
        if (!foundTestngXml) {
            Utils.log("TestNG", 1, "Couldn't find the " + m_xmlPathInJar + " in the jar file, running all the classes");
            XmlSuite xmlSuite = new XmlSuite();
            xmlSuite.setVerbose(0);
            xmlSuite.setName("Jar suite");
            XmlTest xmlTest = new XmlTest(xmlSuite);
            List<XmlClass> xmlClasses = Lists.newArrayList();
            for (String cls : classes) {
                XmlClass xmlClass = new XmlClass(cls);
                xmlClasses.add(xmlClass);
            }
            xmlTest.setXmlClasses(xmlClasses);
            m_suites.add(xmlSuite);
        }
    } catch (ParserConfigurationException ex) {
        ex.printStackTrace();
    } catch (SAXException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : XmlSuite(org.testng.xml.XmlSuite) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ParameterException(com.beust.jcommander.ParameterException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Parser(org.testng.xml.Parser) XmlTest(org.testng.xml.XmlTest) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JarFile(java.util.jar.JarFile) File(java.io.File) XmlClass(org.testng.xml.XmlClass)

Example 8 with XmlClass

use of org.testng.xml.XmlClass in project druid by druid-io.

the class TestNG method createCommandLineSuitesForClasses.

private List<XmlSuite> createCommandLineSuitesForClasses(Class[] classes) {
    //
    // See if any of the classes has an xmlSuite or xmlTest attribute.
    // If it does, create the appropriate XmlSuite, otherwise, create
    // the default one
    //
    XmlClass[] xmlClasses = Utils.classesToXmlClasses(classes);
    Map<String, XmlSuite> suites = Maps.newHashMap();
    IAnnotationFinder finder = m_configuration.getAnnotationFinder();
    for (int i = 0; i < classes.length; i++) {
        Class c = classes[i];
        ITestAnnotation test = finder.findAnnotation(c, ITestAnnotation.class);
        String suiteName = getDefaultSuiteName();
        String testName = getDefaultTestName();
        boolean isJUnit = false;
        if (test != null) {
            suiteName = defaultIfStringEmpty(test.getSuiteName(), suiteName);
            testName = defaultIfStringEmpty(test.getTestName(), testName);
        } else {
            if (m_isMixed && JUnitTestFinder.isJUnitTest(c)) {
                isJUnit = true;
                testName = c.getName();
            }
        }
        XmlSuite xmlSuite = suites.get(suiteName);
        if (xmlSuite == null) {
            xmlSuite = new XmlSuite();
            xmlSuite.setName(suiteName);
            suites.put(suiteName, xmlSuite);
        }
        if (m_dataProviderThreadCount != null) {
            xmlSuite.setDataProviderThreadCount(m_dataProviderThreadCount);
        }
        XmlTest xmlTest = null;
        for (XmlTest xt : xmlSuite.getTests()) {
            if (xt.getName().equals(testName)) {
                xmlTest = xt;
                break;
            }
        }
        if (xmlTest == null) {
            xmlTest = new XmlTest(xmlSuite);
            xmlTest.setName(testName);
            xmlTest.setJUnit(isJUnit);
        }
        xmlTest.getXmlClasses().add(xmlClasses[i]);
    }
    return new ArrayList<XmlSuite>(suites.values());
}
Also used : XmlSuite(org.testng.xml.XmlSuite) ITestAnnotation(org.testng.annotations.ITestAnnotation) IAnnotationFinder(org.testng.internal.annotations.IAnnotationFinder) XmlTest(org.testng.xml.XmlTest) ArrayList(java.util.ArrayList) XmlClass(org.testng.xml.XmlClass) XmlClass(org.testng.xml.XmlClass)

Example 9 with XmlClass

use of org.testng.xml.XmlClass in project druid by druid-io.

the class TestNG method createCommandLineSuitesForMethods.

/**
   * @param commandLineMethods a string with the form "com.example.Foo.f1,com.example.Bar.f2"
   *
   * @return a list of XmlSuite objects that represent the list of classes and methods passed
   * in parameter.
   */
private List<XmlSuite> createCommandLineSuitesForMethods(List<String> commandLineMethods) {
    //
    // Create the <classes> tag
    //
    Set<Class> classes = Sets.newHashSet();
    for (String m : commandLineMethods) {
        Class c = ClassHelper.forName(splitMethod(m)[0]);
        if (c != null) {
            classes.add(c);
        }
    }
    List<XmlSuite> result = createCommandLineSuitesForClasses(classes.toArray(new Class[0]));
    //
    // Add the method tags
    //
    List<XmlClass> xmlClasses = Lists.newArrayList();
    for (XmlSuite s : result) {
        for (XmlTest t : s.getTests()) {
            xmlClasses.addAll(t.getClasses());
        }
    }
    for (XmlClass xc : xmlClasses) {
        for (String m : commandLineMethods) {
            String[] split = splitMethod(m);
            String className = split[0];
            if (xc.getName().equals(className)) {
                XmlInclude includedMethod = new XmlInclude(split[1]);
                xc.getIncludedMethods().add(includedMethod);
            }
        }
    }
    return result;
}
Also used : XmlInclude(org.testng.xml.XmlInclude) XmlSuite(org.testng.xml.XmlSuite) XmlTest(org.testng.xml.XmlTest) XmlClass(org.testng.xml.XmlClass) XmlClass(org.testng.xml.XmlClass)

Example 10 with XmlClass

use of org.testng.xml.XmlClass in project buck by facebook.

the class TestNGRunner method createXmlSuite.

private XmlSuite createXmlSuite(Class<?> c) {
    XmlSuite xmlSuite = new XmlSuite();
    xmlSuite.setName("TmpSuite");
    xmlSuite.setTimeOut(String.valueOf(defaultTestTimeoutMillis));
    XmlTest xmlTest = new XmlTest(xmlSuite);
    xmlTest.setName("TmpTest");
    xmlTest.setXmlClasses(Collections.singletonList(new XmlClass(c)));
    return xmlSuite;
}
Also used : XmlSuite(org.testng.xml.XmlSuite) XmlTest(org.testng.xml.XmlTest) XmlClass(org.testng.xml.XmlClass)

Aggregations

XmlClass (org.testng.xml.XmlClass)11 XmlTest (org.testng.xml.XmlTest)11 XmlSuite (org.testng.xml.XmlSuite)8 XmlInclude (org.testng.xml.XmlInclude)6 ArrayList (java.util.ArrayList)3 File (java.io.File)2 Test (org.junit.Test)2 ParameterException (com.beust.jcommander.ParameterException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 IDEATestNGRemoteListener (org.testng.IDEATestNGRemoteListener)1 ITestAnnotation (org.testng.annotations.ITestAnnotation)1 IAnnotationFinder (org.testng.internal.annotations.IAnnotationFinder)1 Parser (org.testng.xml.Parser)1 SAXException (org.xml.sax.SAXException)1