Skip to content

Commit

Permalink
Streamline xml serialisation to string
Browse files Browse the repository at this point in the history
Closes #3177
  • Loading branch information
krmahadevan committed Feb 20, 2025
1 parent df04c65 commit 44b0f23
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current (7.12.0)
Fixed: GITHUB-3177: Method org.testng.xml.XmlSuite#toXml do not save new properties like "share-thread-pool-for-data-providers" (Krishnan Mahadevan)
Fixed: GITHUB-3179: ClassCastException when use shouldUseGlobalThreadPool(true) property (Krishnan Mahadevan)
Fixed: GITHUB-2765: Test timeouts using existing Executor now propagate the stack trace to the ThreadTimeoutException (Charlie Hayes)

Expand Down
10 changes: 10 additions & 0 deletions testng-core-api/src/main/java/org/testng/xml/DefaultXmlWeaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public String asXml(XmlSuite xmlSuite) {
if (parallel != null && !XmlSuite.DEFAULT_PARALLEL.equals(parallel)) {
p.setProperty("parallel", parallel.toString());
}
XmlUtils.setProperty(
p,
"use-global-thread-pool",
String.valueOf(xmlSuite.useGlobalThreadPool()),
DEFAULT_SHARE_THREAD_POOL.toString());
XmlUtils.setProperty(
p,
"share-thread-pool-for-data-providers",
String.valueOf(xmlSuite.isShareThreadPoolForDataProviders()),
DEFAULT_SHARE_THREAD_POOL_FOR_DATA_PROVIDERS.toString());
XmlUtils.setProperty(
p,
"group-by-instances",
Expand Down
6 changes: 4 additions & 2 deletions testng-core-api/src/main/java/org/testng/xml/XmlSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ public String toString() {
public static final Boolean DEFAULT_SKIP_FAILED_INVOCATION_COUNTS = Boolean.FALSE;
private Boolean m_skipFailedInvocationCounts = DEFAULT_SKIP_FAILED_INVOCATION_COUNTS;

private boolean shareThreadPoolForDataProviders = false;
public static final Boolean DEFAULT_SHARE_THREAD_POOL_FOR_DATA_PROVIDERS = Boolean.FALSE;
private boolean shareThreadPoolForDataProviders = DEFAULT_SHARE_THREAD_POOL_FOR_DATA_PROVIDERS;

private boolean useGlobalThreadPool = false;
public static final Boolean DEFAULT_SHARE_THREAD_POOL = Boolean.FALSE;
private boolean useGlobalThreadPool = DEFAULT_SHARE_THREAD_POOL;

/** The thread count. */
public static final Integer DEFAULT_THREAD_COUNT = 5;
Expand Down
28 changes: 28 additions & 0 deletions testng-core/src/test/java/test/xml/XmlVerifyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.StringReader;
import java.lang.reflect.Method;
import java.util.Collections;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.testng.Assert;
import org.testng.ITestNGListener;
import org.testng.TestListenerAdapter;
Expand All @@ -14,6 +17,11 @@
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import test.SimpleBaseTest;

public class XmlVerifyTest extends SimpleBaseTest {
Expand Down Expand Up @@ -41,6 +49,26 @@ public void testToXmlWithoutComments() {
assertThat(xml).doesNotContain(DEFAULT_SUITE);
}

@Test(description = "GITHUB-3177")
public void testThreadPoolRelatedAttributesPresentInXml() throws Exception {
XmlSuite suite = createSuite();
suite.setShareThreadPoolForDataProviders(true);
suite.shouldUseGlobalThreadPool(true);
String xml = suite.toXml();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
document.getDocumentElement().normalize();
NodeList allSuites = document.getElementsByTagName("suite");
assertThat(allSuites.getLength()).isEqualTo(1);
Node xmlSuite = allSuites.item(0);
assertThat(xmlSuite.getNodeType()).isEqualTo(Node.ELEMENT_NODE);
Element element = (Element) xmlSuite;
assertThat(element.getAttribute("use-global-thread-pool")).isEqualTo(Boolean.TRUE.toString());
assertThat(element.getAttribute("share-thread-pool-for-data-providers"))
.isEqualTo(Boolean.TRUE.toString());
}

@AfterMethod
public void reset(Method method) {
if (method.getName().equals("testToXmlWithoutComments")) {
Expand Down

0 comments on commit 44b0f23

Please sign in to comment.