diff --git a/testng-core/src/main/java/org/testng/xml/LaunchSuite.java b/testng-core/src/main/java/org/testng/xml/LaunchSuite.java deleted file mode 100644 index 48fd6450c..000000000 --- a/testng-core/src/main/java/org/testng/xml/LaunchSuite.java +++ /dev/null @@ -1,384 +0,0 @@ -package org.testng.xml; - -import static org.testng.internal.Utils.isStringNotBlank; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.nio.charset.Charset; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import org.testng.collections.Lists; -import org.testng.log4testng.Logger; -import org.testng.reporters.XMLStringBuffer; -import org.testng.xml.internal.Parser; - -/** - * This class is used to encapsulate a launch. Various synthetic XML files are created depending on - * whether the user is trying to launch a suite, a class, a method, etc... - */ -public abstract class LaunchSuite { - /** This class's log4testng Logger. */ - private static final Logger LOGGER = Logger.getLogger(LaunchSuite.class); - - protected boolean m_temporary; - - /** - * Constructs a LaunchSuite - * - * @param isTemp the temporary status - */ - protected LaunchSuite(boolean isTemp) { - m_temporary = isTemp; - } - - /** - * Returns the temporary state. - * - * @return the temporary state. - */ - public boolean isTemporary() { - return m_temporary; - } - - /** - * Saves the suite file in the specified directory and returns the file pathname. - * - * @param directory the directory where the suite file is to be saved. - * @return the file pathname of the saved file. - */ - public abstract File save(File directory); - - public abstract XMLStringBuffer getSuiteBuffer(); - - /** ExistingSuite is a non-temporary LaunchSuite based on an existing file. */ - public static class ExistingSuite extends LaunchSuite { - - /** The existing suite path (either relative to the project root or an absolute path) */ - private File m_suitePath; - - /** - * Constructs a ExistingSuite based on an existing file - * - * @param path the path to the existing Launch suite. - */ - public ExistingSuite(File path) { - super(false); - - m_suitePath = path; - } - - @Override - public XMLStringBuffer getSuiteBuffer() { - throw new UnsupportedOperationException("Not implemented yet"); - } - - /** Trying to run an existing XML file: copy its content to where the plug-in expects it. */ - @Override - public File save(File directory) { - return m_suitePath; - } - } - - /** CustomizedSuite TODO cquezel JavaDoc. */ - private abstract static class CustomizedSuite extends LaunchSuite { - protected String m_projectName; - protected String m_suiteName; - - /** The annotation type. May be null. */ - protected Map m_parameters; - - /** The string buffer used to write the XML file. */ - private XMLStringBuffer m_suiteBuffer; - - private CustomizedSuite( - final String projectName, final String className, final Map parameters) { - super(true); - - m_projectName = projectName; - m_suiteName = className; - m_parameters = parameters; - } - - /** - * TODO cquezel JavaDoc - * - * @return - */ - protected XMLStringBuffer createContentBuffer() { - XMLStringBuffer suiteBuffer = new XMLStringBuffer(); - suiteBuffer.setDocType("suite SYSTEM \"" + Parser.HTTPS_TESTNG_DTD_URL + "\""); - - Properties attrs = new Properties(); - attrs.setProperty("parallel", XmlSuite.ParallelMode.NONE.toString()); - attrs.setProperty("name", m_suiteName); - suiteBuffer.push("suite", attrs); - - if (m_parameters != null) { - for (Map.Entry entry : m_parameters.entrySet()) { - Properties paramAttrs = new Properties(); - paramAttrs.setProperty("name", entry.getKey()); - paramAttrs.setProperty("value", entry.getValue()); - suiteBuffer.push("parameter", paramAttrs); - suiteBuffer.pop("parameter"); - } - } - - initContentBuffer(suiteBuffer); - - suiteBuffer.pop("suite"); - - return suiteBuffer; - } - - /** - * TODO cquezel JavaDoc - * - * @return - */ - @Override - public XMLStringBuffer getSuiteBuffer() { - if (null == m_suiteBuffer) { - m_suiteBuffer = createContentBuffer(); - } - - return m_suiteBuffer; - } - - /** - * Initializes the content of the xml string buffer. - * - * @param suiteBuffer the string buffer to initialize. - */ - protected abstract void initContentBuffer(XMLStringBuffer suiteBuffer); - - /** - * {@inheritDoc} This implementation saves the suite to the "temp-testng-customsuite.xml" file - * in the specified directory. - */ - @Override - public File save(File directory) { - final File suiteFile = new File(directory, "temp-testng-customsuite.xml"); - - saveSuiteContent(suiteFile, getSuiteBuffer()); - - return suiteFile; - } - - /** - * Saves the content of the string buffer to the specified file. - * - * @param file the file to write to. - * @param content the content to write to the file. - */ - protected void saveSuiteContent(final File file, final XMLStringBuffer content) { - - try (OutputStreamWriter fw = - new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8"))) { - fw.write(content.getStringBuffer().toString()); - } catch (IOException ioe) { - // TODO CQ is this normal to swallow exception here - LOGGER.error("IO Exception", ioe); - } - } - } - - /** A MethodsSuite is a suite made up of methods. */ - static class MethodsSuite extends CustomizedSuite { - protected Collection m_methodNames; - protected String m_className; - protected int m_logLevel; - - MethodsSuite( - final String projectName, - final String className, - final Collection methodNames, - final Map parameters, - final String annotationType, - final int logLevel) { - super(projectName, className, parameters); - - m_className = className; - m_methodNames = methodNames; - m_logLevel = logLevel; - } - - /** {@inheritDoc} */ - @Override - protected void initContentBuffer(XMLStringBuffer suiteBuffer) { - Properties testAttrs = new Properties(); - testAttrs.setProperty("name", m_className); - testAttrs.setProperty("verbose", String.valueOf(m_logLevel)); - - suiteBuffer.push("test", testAttrs); - - suiteBuffer.push("classes"); - - Properties classAttrs = new Properties(); - classAttrs.setProperty("name", m_className); - - if ((null != m_methodNames) && (!m_methodNames.isEmpty())) { - suiteBuffer.push("class", classAttrs); - - suiteBuffer.push("methods"); - - for (String methodName : m_methodNames) { - Properties methodAttrs = new Properties(); - methodAttrs.setProperty("name", methodName); - suiteBuffer.addEmptyElement("include", methodAttrs); - } - - suiteBuffer.pop("methods"); - suiteBuffer.pop("class"); - } else { - suiteBuffer.addEmptyElement("class", classAttrs); - } - suiteBuffer.pop("classes"); - suiteBuffer.pop("test"); - } - } - - static class ClassesAndMethodsSuite extends CustomizedSuite { - protected Map> m_classes; - protected int m_logLevel; - - ClassesAndMethodsSuite( - final String projectName, - final Map> classes, - final Map parameters, - final String annotationType, - final int logLevel) { - super(projectName, "Custom suite", parameters); - m_classes = classes; - m_logLevel = logLevel; - } - - /** {@inheritDoc} */ - @Override - protected void initContentBuffer(XMLStringBuffer suiteBuffer) { - Properties testAttrs = new Properties(); - testAttrs.setProperty("name", m_projectName); - testAttrs.setProperty("verbose", String.valueOf(m_logLevel)); - - suiteBuffer.push("test", testAttrs); - - suiteBuffer.push("classes"); - - for (Map.Entry> entry : m_classes.entrySet()) { - Properties classAttrs = new Properties(); - classAttrs.setProperty("name", entry.getKey()); - - Collection methodNames = sanitize(entry.getValue()); - if ((null != methodNames) && (!methodNames.isEmpty())) { - suiteBuffer.push("class", classAttrs); - - suiteBuffer.push("methods"); - - for (String methodName : methodNames) { - Properties methodAttrs = new Properties(); - methodAttrs.setProperty("name", methodName); - suiteBuffer.addEmptyElement("include", methodAttrs); - } - - suiteBuffer.pop("methods"); - suiteBuffer.pop("class"); - } else { - suiteBuffer.addEmptyElement("class", classAttrs); - } - } - suiteBuffer.pop("classes"); - suiteBuffer.pop("test"); - } - - private Collection sanitize(Collection source) { - if (null == source) { - return null; - } - - List result = Lists.newArrayList(); - for (String name : source) { - if (isStringNotBlank(name)) { - result.add(name); - } - } - - return result; - } - } - - /** ClassListSuite TODO cquezel JavaDoc. */ - static class ClassListSuite extends CustomizedSuite { - protected Collection m_packageNames; - protected Collection m_classNames; - protected Collection m_groupNames; - protected int m_logLevel; - - ClassListSuite( - final String projectName, - final Collection packageNames, - final Collection classNames, - final Collection groupNames, - final Map parameters, - final String annotationType, - final int logLevel) { - super(projectName, "Custom suite", parameters); - - m_packageNames = packageNames; - m_classNames = classNames; - m_groupNames = groupNames; - m_logLevel = logLevel; - } - - /** {@inheritDoc} */ - @Override - protected void initContentBuffer(XMLStringBuffer suiteBuffer) { - Properties testAttrs = new Properties(); - testAttrs.setProperty("name", m_projectName); - testAttrs.setProperty("verbose", String.valueOf(m_logLevel)); - - suiteBuffer.push("test", testAttrs); - - if (null != m_groupNames) { - suiteBuffer.push("groups"); - suiteBuffer.push("run"); - - for (String groupName : m_groupNames) { - Properties includeAttrs = new Properties(); - includeAttrs.setProperty("name", groupName); - suiteBuffer.addEmptyElement("include", includeAttrs); - } - - suiteBuffer.pop("run"); - suiteBuffer.pop("groups"); - } - - // packages belongs to suite according to the latest DTD - if ((m_packageNames != null) && (!m_packageNames.isEmpty())) { - suiteBuffer.push("packages"); - - for (String packageName : m_packageNames) { - Properties packageAttrs = new Properties(); - packageAttrs.setProperty("name", packageName); - suiteBuffer.addEmptyElement("package", packageAttrs); - } - suiteBuffer.pop("packages"); - } - - if ((m_classNames != null) && (!m_classNames.isEmpty())) { - suiteBuffer.push("classes"); - - for (String className : m_classNames) { - Properties classAttrs = new Properties(); - classAttrs.setProperty("name", className); - suiteBuffer.addEmptyElement("class", classAttrs); - } - - suiteBuffer.pop("classes"); - } - suiteBuffer.pop("test"); - } - } -} diff --git a/testng-core/src/main/java/org/testng/xml/SuiteGenerator.java b/testng-core/src/main/java/org/testng/xml/SuiteGenerator.java deleted file mode 100644 index 20f885033..000000000 --- a/testng-core/src/main/java/org/testng/xml/SuiteGenerator.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.testng.xml; - -import java.io.File; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -/** - * Factory to create custom suites. - * - * @author Hani Suleiman Date: Jul 25, 2005 Time: 1:12:18 PM - */ -public class SuiteGenerator { - private static final Collection EMPTY_CLASS_LIST = Collections.emptyList(); - - public static LaunchSuite createProxiedXmlSuite(final File xmlSuitePath) { - return new LaunchSuite.ExistingSuite(xmlSuitePath); - } - - public static LaunchSuite createSuite( - String projectName, - Collection packageNames, - Map> classAndMethodNames, - Collection groupNames, - Map parameters, - String annotationType, - int logLevel) { - - LaunchSuite result; - Collection classes = - classAndMethodNames != null ? classAndMethodNames.keySet() : EMPTY_CLASS_LIST; - if ((null != groupNames) && !groupNames.isEmpty()) { - // - // Create a suite from groups - // - result = - new LaunchSuite.ClassListSuite( - projectName, packageNames, classes, groupNames, parameters, annotationType, logLevel); - } else if (packageNames != null && !packageNames.isEmpty()) { - // - // Create a suite from packages - // - result = - new LaunchSuite.ClassListSuite( - projectName, packageNames, classes, groupNames, parameters, annotationType, logLevel); - } else { - // - // Default suite creation - // - result = - new LaunchSuite.ClassesAndMethodsSuite( - projectName, classAndMethodNames, parameters, annotationType, logLevel); - } - - return result; - } -}