Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor discrepancy fixes #3026

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,7 @@ public static String replaceSpecialCharacters(String fileNameParameter) {
}

public static <T> String join(List<T> objects, String separator) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < objects.size(); i++) {
if (i > 0) {
result.append(separator);
}
result.append(objects.get(i).toString());
}
return result.toString();
return objects.stream().map(Object::toString).collect(Collectors.joining(separator));
}

/* Make sure that either we have an instance or if not, that the method is static */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class XmlInclude {

private String m_name;
private Set<Integer> m_invocationNumbers;
private final Set<Integer> m_invocationNumbers;
private final int m_index;
private String m_description;
private final Map<String, String> m_parameters = Maps.newHashMap();
Expand Down Expand Up @@ -76,7 +76,7 @@ public String toXml(String indent) {
Properties p = new Properties();
p.setProperty("name", getName());
List<Integer> invocationNumbers = getInvocationNumbers();
if (invocationNumbers != null && invocationNumbers.size() > 0) {
if (invocationNumbers != null && !invocationNumbers.isEmpty()) {
p.setProperty("invocation-numbers", XmlClass.listToString(invocationNumbers));
}

Expand Down Expand Up @@ -108,8 +108,6 @@ public boolean equals(Object obj) {
if (obj == null) return XmlSuite.f();
if (getClass() != obj.getClass()) return XmlSuite.f();
XmlInclude other = (XmlInclude) obj;
// if (m_index != other.m_index)
// return XmlSuite.f();
if (m_invocationNumbers == null) {
if (other.m_invocationNumbers != null) return XmlSuite.f();
} else if (!m_invocationNumbers.equals(other.m_invocationNumbers)) return XmlSuite.f();
Expand Down
29 changes: 7 additions & 22 deletions testng-core-api/src/main/java/org/testng/xml/XmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,23 +223,16 @@ public int getVerbose() {
result = getSuite().getVerbose();
}

if (null != result) {
return result;
} else {
return 1;
}
return Optional.ofNullable(result).orElse(1);
}

public boolean getGroupByInstances() {
Boolean result = m_groupByInstances;
if (result == null || XmlSuite.DEFAULT_GROUP_BY_INSTANCES.equals(m_groupByInstances)) {
result = getSuite().getGroupByInstances();
}
if (result != null) {
return result;
} else {
return XmlSuite.DEFAULT_GROUP_BY_INSTANCES;
}

return Optional.ofNullable(result).orElse(XmlSuite.DEFAULT_GROUP_BY_INSTANCES);
}

public void setGroupByInstances(boolean f) {
Expand Down Expand Up @@ -384,7 +377,7 @@ private void setTimeOut(String timeOut) {

public void setScript(XmlScript script) {
List<XmlMethodSelector> selectors = getMethodSelectors();
if (selectors.size() > 0) {
if (!selectors.isEmpty()) {
XmlMethodSelector xms = selectors.get(0);
xms.setScript(script);
} else if (script != null) {
Expand Down Expand Up @@ -425,8 +418,7 @@ public Object clone() {
result.setParallel(getParallel());
result.setThreadCount(getThreadCount());
result.setVerbose(getVerbose());
Map<String, String> localParameters = new HashMap<>();
localParameters.putAll(getLocalParameters());
Map<String, String> localParameters = Maps.newHashMap(getLocalParameters());
result.setParameters(localParameters);
result.setXmlPackages(getXmlPackages());
result.setTimeOut(getTimeOut());
Expand All @@ -452,22 +444,15 @@ public List<Integer> getInvocationNumbers(String method) {
for (XmlClass c : getXmlClasses()) {
for (XmlInclude xi : c.getIncludedMethods()) {
List<Integer> invocationNumbers = xi.getInvocationNumbers();
if (invocationNumbers.size() > 0) {
if (!invocationNumbers.isEmpty()) {
String methodName = c.getName() + "." + xi.getName();
m_failedInvocationNumbers.put(methodName, invocationNumbers);
}
}
}
}

List<Integer> result = m_failedInvocationNumbers.get(method);
if (result == null) {
// Don't use emptyList here since this list might end up receiving values if
// the test run fails.
return Lists.newArrayList();
} else {
return result;
}
return Optional.ofNullable(m_failedInvocationNumbers.get(method)).orElse(Lists.newArrayList());
}

public void setPreserveOrder(Boolean preserveOrder) {
Expand Down
2 changes: 1 addition & 1 deletion testng-core/src/main/java/org/testng/TestNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ protected void configure(CommandLineArgs cla) {
addReporter(reporterConfig);
}

if (cla.commandLineMethods.size() > 0) {
if (!cla.commandLineMethods.isEmpty()) {
m_commandLineMethods = cla.commandLineMethods;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Object[] getInstances(boolean create, String errorMsgPrefix) {
};
}
}
if (m_instances.size() > 0) {
if (!m_instances.isEmpty()) {
result = m_instances.toArray(new Object[0]);
} else {
Object defaultInstance = getDefaultInstance(create, errorMsgPrefix);
Expand Down
17 changes: 12 additions & 5 deletions testng-core/src/main/java/org/testng/internal/Yaml.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.testng.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
Expand Down Expand Up @@ -54,7 +53,7 @@ public static XmlSuite parse(String filePath, InputStream is, boolean loadClasse

org.yaml.snakeyaml.Yaml y = new org.yaml.snakeyaml.Yaml(constructor);
if (is == null) {
is = new FileInputStream(new File(filePath));
is = new FileInputStream(filePath);
}
XmlSuite result = y.load(is);

Expand Down Expand Up @@ -180,11 +179,19 @@ private static void toYaml(StringBuilder result, XmlTest t) {
result.append(sp2).append(sp2).append("xmlDependencyGroups:\n");
t.getXmlDependencyGroups()
.forEach(
(k, v) -> result.append(sp2).append(sp2).append(sp2).append(k + ": " + v + "\n"));
(k, v) ->
result
.append(sp2)
.append(sp2)
.append(sp2)
.append(k)
.append(": ")
.append(v)
.append("\n"));
}

Map<String, List<String>> mg = t.getMetaGroups();
if (mg.size() > 0) {
if (!mg.isEmpty()) {
result.append(sp2).append("metaGroups: { ");
boolean first = true;
for (Map.Entry<String, List<String>> entry : mg.entrySet()) {
Expand Down Expand Up @@ -279,7 +286,7 @@ private static void generateIncludeExclude(
}

private static void mapToYaml(Map<String, String> map, StringBuilder out) {
if (map.size() > 0) {
if (!map.isEmpty()) {
out.append("{ ");
boolean first = true;
for (Map.Entry<String, String> e : map.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public int compareTo(@Nonnull IWorker<ITestNGMethod> other) {
/** The priority of a worker is the priority of the first method it's going to run. */
@Override
public int getPriority() {
return m_methodInstances.size() > 0 ? m_methodInstances.get(0).getMethod().getPriority() : 0;
return !m_methodInstances.isEmpty() ? m_methodInstances.get(0).getMethod().getPriority() : 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected void generateFailureSuite(XmlSuite xmlSuite, ISuite suite, String outp
testContext.getSkippedTests().getAllResults());
}

if (null != failedSuite.getTests() && failedSuite.getTests().size() > 0) {
if (null != failedSuite.getTests() && !failedSuite.getTests().isEmpty()) {
if (xmlSuite.getParentSuite() != null
&& !xmlSuite.getParentSuite().getLocalListeners().isEmpty()) {
List<String> merged =
Expand All @@ -95,7 +95,7 @@ private void generateXmlTest(
Set<ITestResult> skippedTests) {
// Note: we can have skipped tests and no failed tests
// if a method depends on nonexistent groups
if (skippedTests.size() > 0 || failedTests.size() > 0) {
if (!skippedTests.isEmpty() || !failedTests.isEmpty()) {
Set<ITestNGMethod> methodsToReRun = Sets.newHashSet();

// Get the transitive closure of all the failed methods and the methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@ public class JUnitXMLReporter implements IResultListener2 {
private int m_numFailed = 0;
private Queue<ITestResult> m_allTests = new ConcurrentLinkedDeque<>();
private Queue<ITestResult> m_configIssues = new ConcurrentLinkedDeque<>();
private Map<String, String> m_fileNameMap = Maps.newHashMap();
private final Map<String, String> m_fileNameMap = Maps.newHashMap();
private int m_fileNameIncrementer = 0;

@Override
public void onTestStart(ITestResult result) {}

@Override
public void beforeConfiguration(ITestResult tr) {}

/** Invoked each time a test succeeds. */
@Override
public void onTestSuccess(ITestResult tr) {
Expand Down Expand Up @@ -123,7 +117,7 @@ protected void generateReport(ITestContext context) {
// ignore
}
Set<String> packages = getPackages(context);
if (packages.size() > 0) {
if (!packages.isEmpty()) {
attrs.setProperty(XMLConstants.ATTR_NAME, context.getCurrentXmlTest().getName());
// attrs.setProperty(XMLConstants.ATTR_PACKAGE, packages.iterator().next());
}
Expand Down Expand Up @@ -219,7 +213,7 @@ private void createFailureElement(XMLStringBuffer doc, ITestResult tr) {
if (t != null) {
attrs.setProperty(XMLConstants.ATTR_TYPE, t.getClass().getName());
String message = t.getMessage();
if ((message != null) && (message.length() > 0)) {
if ((message != null) && (!message.isEmpty())) {
attrs.setProperty(XMLConstants.ATTR_MESSAGE, encodeAttr(message)); // ENCODE
}
doc.push(XMLConstants.FAILURE, attrs);
Expand All @@ -235,24 +229,24 @@ private void createSkipElement(XMLStringBuffer doc) {
}

private String encodeAttr(String attr) {
String result = replaceAmpersand(attr, ENTITY);
String result = replaceAmpersand(attr);
for (Map.Entry<String, Pattern> e : ATTR_ESCAPES.entrySet()) {
result = e.getValue().matcher(result).replaceAll(e.getKey());
}

return result;
}

private String replaceAmpersand(String str, Pattern pattern) {
private String replaceAmpersand(String str) {
int start = 0;
int idx = str.indexOf('&', start);
if (idx == -1) {
return str;
}
StringBuilder result = new StringBuilder();
while (idx != -1) {
result.append(str.substring(start, idx));
if (pattern.matcher(str.substring(idx)).matches()) {
result.append(str, start, idx);
if (JUnitXMLReporter.ENTITY.matcher(str.substring(idx)).matches()) {
// do nothing it is an entity;
result.append("&");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void generateReport(
// Generate the various reports
//
XmlSuite xmlSuite = suite.getXmlSuite();
if (xmlSuite.getTests().size() == 0) {
if (xmlSuite.getTests().isEmpty()) {
continue;
}
generateTableOfContents(xmlSuite, suite);
Expand Down Expand Up @@ -133,7 +133,7 @@ private void generateIndex(List<ISuite> suites) {

StringBuilder suiteBuf = new StringBuilder();
for (ISuite suite : suites) {
if (suite.getResults().size() == 0) {
if (suite.getResults().isEmpty()) {
continue;
}

Expand Down Expand Up @@ -493,7 +493,7 @@ private void generateMethodsAndGroups(XmlSuite xmlSuite, ISuite suite) {
Map<String, Collection<ITestNGMethod>> groups = suite.getMethodsByGroups();

sb.append("<h2>Groups used for this test run</h2>");
if (groups.size() > 0) {
if (!groups.isEmpty()) {
sb.append("<table border=\"1\">\n")
.append("<tr> <td align=\"center\"><b>Group name</b></td>")
.append("<td align=\"center\"><b>Methods</b></td></tr>");
Expand Down Expand Up @@ -607,7 +607,7 @@ private void generateTableOfContents(XmlSuite xmlSuite, ISuite suite) {
.append("chronological</a><br/>\n")
.append("&nbsp;&nbsp;<a target='mainFrame' href='")
.append(METHODS_ALPHABETICAL)
.append("\'>")
.append("'>")
.append("alphabetical</a><br/>\n")
.append("&nbsp;&nbsp;<a target='mainFrame' href='")
.append(METHODS_NOT_RUN)
Expand Down Expand Up @@ -689,7 +689,7 @@ private String getOutputDirectory(XmlSuite xmlSuite) {
File fileResult =
new File(m_outputDirectory + File.separatorChar + xmlSuite.getName()).getAbsoluteFile();
if (!fileResult.exists()) {
fileResult.mkdirs();
boolean ignored = fileResult.mkdirs();
if (!fileResult.exists()) {
Utils.log(
"Reports", 2, "Problem creating output directory " + fileResult.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private void addTestResultOutput(XMLStringBuffer xmlBuffer, ITestResult testResu
}

private void addTestResultAttributes(XMLStringBuffer xmlBuffer, ITestResult testResult) {
if (testResult.getAttributeNames() != null && testResult.getAttributeNames().size() > 0) {
if (testResult.getAttributeNames() != null && !testResult.getAttributeNames().isEmpty()) {
xmlBuffer.push(XMLReporterConfig.TAG_ATTRIBUTES);
for (String attrName : testResult.getAttributeNames()) {
if (attrName == null) {
Expand Down