Search in sources :

Example 1 with Parser

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

the class TestNG method getParser.

private Parser getParser(String path) {
    Parser result = new Parser(path);
    initProcessor(result);
    return result;
}
Also used : Parser(org.testng.xml.Parser)

Example 2 with Parser

use of org.testng.xml.Parser 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 3 with Parser

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

the class TestNG method getParser.

private Parser getParser(InputStream is) {
    Parser result = new Parser(is);
    initProcessor(result);
    return result;
}
Also used : Parser(org.testng.xml.Parser)

Example 4 with Parser

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

the class SearchingForTestsTask method composeTestSuiteFromXml.

private void composeTestSuiteFromXml() throws CantRunException {
    final Map<String, String> buildTestParams = buildTestParameters();
    try {
        if (buildTestParams.isEmpty()) {
            String path = new File(myData.getSuiteName()).getAbsolutePath() + "\n";
            FileUtil.writeToFile(myTempFile, path.getBytes(CharsetToolkit.UTF8_CHARSET), true);
            return;
        }
        final Parser parser = new Parser(myData.getSuiteName());
        parser.setLoadClasses(false);
        final Collection<XmlSuite> suites = parser.parse();
        for (XmlSuite suite : suites) {
            Map<String, String> params = suite.getParameters();
            params.putAll(buildTestParams);
            final String fileId = FileUtil.sanitizeFileName(myProject.getName() + '_' + suite.getName() + '_' + Integer.toHexString(suite.getName().hashCode()) + ".xml");
            final File suiteFile = new File(PathManager.getSystemPath(), fileId);
            FileWriter fileWriter = new FileWriter(suiteFile);
            try {
                fileWriter.write(suite.toXml());
            } finally {
                fileWriter.close();
            }
            String path = suiteFile.getAbsolutePath() + "\n";
            FileUtil.writeToFile(myTempFile, path.getBytes(CharsetToolkit.UTF8_CHARSET), true);
        }
    } catch (Exception e) {
        throw new CantRunException("Unable to parse suite: " + e.getMessage());
    }
}
Also used : CantRunException(com.intellij.execution.CantRunException) XmlSuite(org.testng.xml.XmlSuite) ExecutionException(com.intellij.execution.ExecutionException) CantRunException(com.intellij.execution.CantRunException) Parser(org.testng.xml.Parser)

Example 5 with Parser

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

the class TestNGTestSuite method checkConfiguration.

@Override
public void checkConfiguration() throws RuntimeConfigurationException {
    final TestData data = myConfig.getPersistantData();
    try {
        final Parser parser = new Parser(data.getSuiteName());
        parser.setLoadClasses(false);
        synchronized (PARSE_LOCK) {
            //try to parse suite.xml
            parser.parse();
        }
    } catch (Exception e) {
        throw new RuntimeConfigurationException("Unable to parse '" + data.getSuiteName() + "' specified");
    }
}
Also used : RuntimeConfigurationException(com.intellij.execution.configurations.RuntimeConfigurationException) RuntimeConfigurationException(com.intellij.execution.configurations.RuntimeConfigurationException) CantRunException(com.intellij.execution.CantRunException) Parser(org.testng.xml.Parser)

Aggregations

Parser (org.testng.xml.Parser)5 CantRunException (com.intellij.execution.CantRunException)2 XmlSuite (org.testng.xml.XmlSuite)2 ParameterException (com.beust.jcommander.ParameterException)1 ExecutionException (com.intellij.execution.ExecutionException)1 RuntimeConfigurationException (com.intellij.execution.configurations.RuntimeConfigurationException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XmlClass (org.testng.xml.XmlClass)1 XmlTest (org.testng.xml.XmlTest)1 SAXException (org.xml.sax.SAXException)1