Search in sources :

Example 6 with XmlTest

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

the class TestNGXmlSuiteHelper method writeSuite.

public static File writeSuite(Map<String, Map<String, List<String>>> map, Map<String, String> testParams, String name, String rootPath, Logger logger) {
    File xmlFile;
    final XmlSuite xmlSuite = new XmlSuite();
    xmlSuite.setParameters(testParams);
    XmlTest xmlTest = new XmlTest(xmlSuite);
    xmlTest.setName(name);
    List<XmlClass> xmlClasses = new ArrayList<XmlClass>();
    int idx = 0;
    for (String className : map.keySet()) {
        final XmlClass xmlClass = new XmlClass(className, idx++, false);
        final Map<String, List<String>> collection = map.get(className);
        if (collection != null) {
            final ArrayList<XmlInclude> includedMethods = new ArrayList<XmlInclude>();
            int mIdx = 0;
            for (String methodName : collection.keySet()) {
                final List<Integer> includes = new ArrayList<Integer>();
                for (String include : collection.get(methodName)) {
                    try {
                        includes.add(Integer.parseInt(include));
                    } catch (NumberFormatException e) {
                        logger.log(e);
                    }
                }
                includedMethods.add(new XmlInclude(methodName, includes, mIdx++));
            }
            xmlClass.setIncludedMethods(includedMethods);
        }
        xmlClasses.add(xmlClass);
    }
    xmlTest.setXmlClasses(xmlClasses);
    xmlFile = new File(rootPath, "temp-testng-customsuite.xml");
    final String toXml = xmlSuite.toXml();
    writeToFile(logger, xmlFile, toXml);
    return xmlFile;
}
Also used : XmlSuite(org.testng.xml.XmlSuite) ArrayList(java.util.ArrayList) XmlInclude(org.testng.xml.XmlInclude) XmlTest(org.testng.xml.XmlTest) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) XmlClass(org.testng.xml.XmlClass)

Example 7 with XmlTest

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

the class TestNGTreeHierarchyTest method testOneTestMethodWithMultipleInvocationCount.

@Test
public void testOneTestMethodWithMultipleInvocationCount() 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", Arrays.asList(0, 1, 2), 0));
    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" + "\n" + "##teamcity[testStarted name='ATest.test1|[1|] (1)' locationHint='java:test://a.ATest.test1|[1|]']\n" + "\n" + "##teamcity[testFinished name='ATest.test1|[1|] (1)']\n" + "\n" + "##teamcity[testStarted name='ATest.test1|[2|] (2)' locationHint='java:test://a.ATest.test1|[2|]']\n" + "\n" + "##teamcity[testFinished name='ATest.test1|[2|] (2)']\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 8 with XmlTest

use of org.testng.xml.XmlTest 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 9 with XmlTest

use of org.testng.xml.XmlTest 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 10 with XmlTest

use of org.testng.xml.XmlTest 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)

Aggregations

XmlTest (org.testng.xml.XmlTest)16 XmlSuite (org.testng.xml.XmlSuite)12 XmlClass (org.testng.xml.XmlClass)11 XmlInclude (org.testng.xml.XmlInclude)6 ArrayList (java.util.ArrayList)3 File (java.io.File)2 List (java.util.List)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 Map (java.util.Map)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 IDEATestNGRemoteListener (org.testng.IDEATestNGRemoteListener)1 ISuite (org.testng.ISuite)1 ITestRunnerFactory (org.testng.ITestRunnerFactory)1 TestRunner (org.testng.TestRunner)1