Skip to content

Commit 9d090ea

Browse files
committed
style(testng): stop spelling out imports the compiler can resolve
SuiteResult compared two possibly-absent <test> names by writing out both java.util.Objects.compare and java.util.Comparator.nullsFirst on one line, where neither name clashes with anything the file imports. The comparator is now a named constant that says what the order is -- an unnamed <test> sorts first -- and the call site is one short expression. ClassImpl and TestResult keep java.util.Objects out of their bodies with a static import of requireNonNull. Both files import org.testng.collections.Objects for toStringHelper, which is what forced the qualified form; the repo already static imports JDK members this way (StandardCharsets.UTF_8) and its own helpers (Utils.isStringNotEmpty, ListenerComparator.sort). MethodInstance keeps the one qualified java.util.Objects.equals it has: org.testng.collections.Objects offers no equals, and a bare static-imported equals(a, b) inside a class reads like this.equals. testng-core's dependency block also loses the comment about annotations needing to be on the compile classpath. It explained the spotbugs compileOnly that went with the last javax.annotation uses, and described nothing once that left.
1 parent fe259fb commit 9d090ea

4 files changed

Lines changed: 13 additions & 7 deletions

File tree

testng-core/src/main/java/org/testng/SuiteResult.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package org.testng;
22

3+
import java.util.Comparator;
34
import org.testng.collections.Objects;
45
import org.testng.log4testng.Logger;
56
import org.testng.xml.XmlSuite;
67

78
/** This class logs the result of an entire Test Suite (defined by a property file). */
89
class SuiteResult implements ISuiteResult, Comparable<SuiteResult> {
10+
11+
/** A &lt;test&gt; that carries no name sorts ahead of the ones that do. */
12+
private static final Comparator<String> NAME_ORDER = Comparator.nullsFirst(String::compareTo);
13+
914
private final XmlSuite m_suite;
1015
private final ITestContext m_testContext;
1116

@@ -30,8 +35,7 @@ public int compareTo(SuiteResult other) {
3035
try {
3136
String n1 = getTestContext().getName();
3237
String n2 = other.getTestContext().getName();
33-
result =
34-
java.util.Objects.compare(n1, n2, java.util.Comparator.nullsFirst(String::compareTo));
38+
result = NAME_ORDER.compare(n1, n2);
3539
} catch (Exception ex) {
3640
Logger.getLogger(SuiteResult.class).error(ex.getMessage(), ex);
3741
}

testng-core/src/main/java/org/testng/internal/ClassImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.testng.internal;
22

3+
import static java.util.Objects.requireNonNull;
4+
35
import java.util.ArrayList;
46
import java.util.Arrays;
57
import java.util.List;
@@ -104,8 +106,7 @@ public XmlTest getXmlTest() {
104106
factory = m_testContext.getSuite().getObjectFactory();
105107
}
106108
IObjectDispenser dispenser =
107-
Dispenser.newInstance(
108-
java.util.Objects.requireNonNull(factory, "a suite carries an object factory"));
109+
Dispenser.newInstance(requireNonNull(factory, "a suite carries an object factory"));
109110
BasicAttributes basic = new BasicAttributes(this, null);
110111
DetailedAttributes detailed = newDetailedAttributes(create, errMsgPrefix);
111112
CreationAttributes attributes = new CreationAttributes(m_testContext, basic, detailed);
@@ -174,7 +175,7 @@ private static int computeHashCode(IdentifiableObject identifiable) {
174175
// derive a stable one from its unique instance id instead.
175176
return identifiable.getInstanceId().hashCode();
176177
}
177-
return java.util.Objects.requireNonNull(
178+
return requireNonNull(
178179
IParameterInfo.embeddedInstance(instance), "the factory instance is not available")
179180
.hashCode();
180181
}

testng-core/testng-core-build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ tasks.withType<GroovyCompile>().configureEach {
2121

2222
dependencies {
2323
api(projects.testngCoreApi)
24-
// Annotations have to be available on the compile classpath for the proper compilation
2524

2625
"guiceApi"(platform("com.google.inject:guice-bom:6.0.0"))
2726
"guiceApi"("com.google.inject:guice")

testng-runner-api/src/main/java/org/testng/internal/TestResult.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.testng.internal;
22

3+
import static java.util.Objects.requireNonNull;
4+
35
import java.lang.reflect.InvocationTargetException;
46
import java.lang.reflect.Method;
57
import java.util.ArrayList;
@@ -195,7 +197,7 @@ public void setEndMillis(long millis) {
195197
* builds a carrier that has no method, and those members are not reachable on it.
196198
*/
197199
private ITestNGMethod requireMethod() {
198-
return java.util.Objects.requireNonNull(
200+
return requireNonNull(
199201
m_method, "This TestResult carries parameters only; it has no test method");
200202
}
201203

0 commit comments

Comments
 (0)