Skip to content

Commit

Permalink
Merge pull request #2958 from testng-team/feature/errorprone
Browse files Browse the repository at this point in the history
Add errorprone
  • Loading branch information
juherr authored Feb 21, 2024
2 parents 49ddafb + 6c97402 commit 38910de
Show file tree
Hide file tree
Showing 52 changed files with 255 additions and 165 deletions.
4 changes: 4 additions & 0 deletions build-logic/build-parameters/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ buildParameters {
defaultValue.set(true)
description.set("Fail build on javadoc warnings")
}
bool("skipErrorProne") {
defaultValue.set(false)
description.set("Skip Error Prone verifications")
}
}
1 change: 1 addition & 0 deletions build-logic/code-quality/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dependencies {
api(projects.basics)
api("org.sonarqube:org.sonarqube.gradle.plugin:4.4.1.3373")
api("com.github.autostyle:autostyle-plugin-gradle:4.0")
api("net.ltgt.gradle:gradle-errorprone-plugin:3.1.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import net.ltgt.gradle.errorprone.errorprone

plugins {
id("net.ltgt.errorprone")
id("build-logic.build-params")
}

dependencies {
errorprone("com.google.errorprone:error_prone_core:2.20.0")
}

tasks.withType<JavaCompile>().configureEach {
options.errorprone.disableWarningsInGeneratedCode.set(true)
}
4 changes: 4 additions & 0 deletions build-logic/jvm/src/main/kotlin/testng.java.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ tasks.configureEach<Test> {
inputs.property("java.vm.version", System.getProperty("java.vm.version"))
inputs.property("java.vm.vendor", System.getProperty("java.vm.vendor"))
}

if (!buildParameters.skipErrorProne) {
apply(plugin = "testng.errorprone")
}
26 changes: 12 additions & 14 deletions testng-asserts/src/test/java/org/testng/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,22 +305,10 @@ public void compareUnEqualDoubleArraysWithDelta() {
Assert.assertEquals(actual, expected, 0.1d);
}

@SuppressWarnings("serial")
@Test(expectedExceptions = AssertionError.class)
public void assertEqualsMapShouldFail() {
Map<String, String> mapActual =
new HashMap<String, String>() {
{
put("a", "1");
}
};
Map<String, String> mapExpected =
new HashMap<String, String>() {
{
put("a", "1");
put("b", "2");
}
};
Map<String, String> mapActual = Map.of("a", "1");
Map<String, String> mapExpected = Map.of("a", "1", "b", "2");

Assert.assertEquals(mapActual, mapExpected);
}
Expand Down Expand Up @@ -654,12 +642,22 @@ static class BrokenEqualsTrue {
public boolean equals(Object o) {
return true; // broken implementation
}

@Override
public int hashCode() {
return 0;
}
}

static class BrokenEqualsFalse {
@Override
public boolean equals(Object o) {
return false; // broken implementation
}

@Override
public int hashCode() {
return 0;
}
}
}
4 changes: 2 additions & 2 deletions testng-core/src/main/java/org/testng/SuiteResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.testng.xml.XmlSuite;

/** This class logs the result of an entire Test Suite (defined by a property file). */
class SuiteResult implements ISuiteResult, Comparable<ISuiteResult> {
class SuiteResult implements ISuiteResult, Comparable<SuiteResult> {
private final XmlSuite m_suite;
private final ITestContext m_testContext;

Expand All @@ -26,7 +26,7 @@ public XmlSuite getSuite() {
}

@Override
public int compareTo(@Nonnull ISuiteResult other) {
public int compareTo(@Nonnull SuiteResult other) {
int result = 0;
try {
String n1 = getTestContext().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public boolean hasMoreInvocation() {

@Override
public Class<?> getRealClass() {
return m_javaMethod.getClass();
return m_javaMethod.getDeclaringClass();
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions testng-core/src/main/java/org/testng/internal/Graph.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.testng.internal;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -190,9 +192,9 @@ public List<T> findPredecessors(T o) {
// already encountered. "queue" is the queue of items whose
// predecessors we haven't yet explored.

LinkedList<T> result = new LinkedList<>();
Deque<T> result = new ArrayDeque<>();
Set<T> visited = new HashSet<>();
LinkedList<T> queue = new LinkedList<>();
Deque<T> queue = new ArrayDeque<>();
visited.add(o);
queue.addLast(o);

Expand All @@ -206,7 +208,7 @@ public List<T> findPredecessors(T o) {
}
}

return result;
return new ArrayList<>(result);
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions testng-core/src/main/java/org/testng/internal/Tarjan.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.testng.internal;

import java.util.ArrayDeque;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Stack;
import org.testng.collections.Lists;
import org.testng.collections.Maps;

Expand All @@ -14,13 +14,13 @@
*/
public class Tarjan<T> {
int m_index = 0;
private final Stack<T> stack;
private final ArrayDeque<T> stack;
Map<T, Integer> visitedNodes = Maps.newHashMap();
Map<T, Integer> m_lowlinks = Maps.newHashMap();
private List<T> m_cycle;

public Tarjan(Graph<T> graph, T start) {
stack = new Stack<>();
stack = new ArrayDeque<>();
run(graph, start);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ private Object invokeMethod(Annotation test, String methodName) {
Object result = null;
try {
// Note: we should cache methods already looked up
Method m = test.getClass().getMethod(methodName);
Method m = test.annotationType().getMethod(methodName);
result = m.invoke(test);
} catch (Exception e) {
Logger.getLogger(JDK15TagFactory.class).error(e.getMessage(), e);
Expand Down
4 changes: 3 additions & 1 deletion testng-core/src/test/java/NoPackageTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import static org.testng.Assert.assertTrue;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

Expand All @@ -12,6 +14,6 @@ public void test() {

@AfterMethod(groups = {"nopackage"})
public void after() {
assert m_run : "test method was not run";
assertTrue(m_run, "test method was not run");
}
}
17 changes: 8 additions & 9 deletions testng-core/src/test/java/test/ClassConfigurations.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package test;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -29,22 +31,19 @@ public void afterTestClass() {

@Test
public void testOne() {
// System.out.println("testOne");
assert beforeCount == 1;
assert afterCount == 0;
assertEquals(beforeCount, 1);
assertEquals(afterCount, 0);
}

@Test
public void testTwo() {
// System.out.println("testTwo");
assert beforeCount == 1;
assert afterCount == 0;
assertEquals(beforeCount, 1);
assertEquals(afterCount, 0);
}

@Test
public void testThree() {
// System.out.println("testThree");
assert beforeCount == 1;
assert afterCount == 0;
assertEquals(beforeCount, 1);
assertEquals(afterCount, 0);
}
}
8 changes: 5 additions & 3 deletions testng-core/src/test/java/test/CtorCalledOnce.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package test;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

Expand All @@ -16,17 +18,17 @@ public CtorCalledOnce() {

@Test
public void testMethod1() {
assert instantiated == 1 : "Expected 1, was invoked " + instantiated + " times";
assertEquals(instantiated, 1, "Expected 1, was invoked " + instantiated + " times");
}

@Test
public void testMethod2() {
assert instantiated == 1 : "Expected 1, was invoked " + instantiated + " times";
assertEquals(instantiated, 1, "Expected 1, was invoked " + instantiated + " times");
}

@Test
public void testMethod3() {
assert instantiated == 1 : "Expected 1, was invoked " + instantiated + " times";
assertEquals(instantiated, 1, "Expected 1, was invoked " + instantiated + " times");
}

@AfterTest
Expand Down
9 changes: 6 additions & 3 deletions testng-core/src/test/java/test/Exclude.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package test;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

public class Exclude {
Expand Down Expand Up @@ -32,14 +34,15 @@ public void excluded2() {
dependsOnGroups = {"group1"},
groups = {"group2"})
public void verify() {
assert m_included1 && m_included2 && m_excluded1 && m_excluded2
: "Should all be true: "
assertTrue(
m_included1 && m_included2 && m_excluded1 && m_excluded2,
"Should all be true: "
+ m_included1
+ " "
+ m_included2
+ " "
+ m_excluded1
+ " "
+ m_excluded2;
+ m_excluded2);
}
}
4 changes: 3 additions & 1 deletion testng-core/src/test/java/test/MethodTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package test;

import static org.testng.AssertJUnit.assertEquals;

import org.testng.Assert;
import org.testng.annotations.Test;
import test.sample.Sample2;
Expand Down Expand Up @@ -36,7 +38,7 @@ public void excludeMethodsOnly() {
@Test
public void excludePackage() {
addClass(CLASS_NAME);
assert 1 == getTest().getXmlClasses().size();
assertEquals(getTest().getXmlClasses().size(), 1);
addExcludedMethod(CLASS_NAME, ".*");
run();
String[] passed = {};
Expand Down
9 changes: 1 addition & 8 deletions testng-core/src/test/java/test/NestedStaticTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package test;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.testng.Assert;
Expand All @@ -19,13 +18,7 @@ public void nestedClassShouldBeIncluded() {
tng.addListener(tla);
tng.run();

Set<String> expected =
new HashSet<String>() {
{
add("nested");
add("f");
}
};
Set<String> expected = Set.of("nested", "f");
Set<String> actual = Sets.newHashSet();
List<ITestResult> passedTests = tla.getPassedTests();
for (ITestResult t : passedTests) {
Expand Down
21 changes: 12 additions & 9 deletions testng-core/src/test/java/test/SampleInheritance.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import test.sample.BaseSampleInheritance;
Expand All @@ -17,25 +20,25 @@ public void configuration0() {
groups = "final",
dependsOnGroups = {"configuration1"})
public void configuration2() {
assert m_configurations.size() == 2 : "Expected size 2 found " + m_configurations.size();
assert "configuration0".equals(m_configurations.get(0)) : "Expected configuration0 to be run";
assert "configuration1".equals(m_configurations.get(1)) : "Expected configuration1 to be run";
assertEquals(m_configurations.size(), 2, "Expected size 2 found " + m_configurations.size());
assertEquals(m_configurations.get(0), "configuration0", "Expected configuration0 to be run");
assertEquals(m_configurations.get(1), "configuration1", "Expected configuration1 to be run");
addConfiguration("configuration2");
}

@Test(
groups = "final",
dependsOnGroups = {"inheritedTestMethod"})
public void inheritedMethodsWereCalledInOrder() {
assert m_invokedBaseMethod : "Didn't invoke test method in base class";
assert m_invokedBaseConfiguration : "Didn't invoke configuration method in base class";
assertTrue(m_invokedBaseMethod, "Didn't invoke test method in base class");
assertTrue(m_invokedBaseConfiguration, "Didn't invoke configuration method in base class");
}

@Test(groups = "final2", dependsOnGroups = "final")
public void configurationsWereCalledInOrder() {
assert m_configurations.size() == 3;
assert "configuration0".equals(m_configurations.get(0)) : "Expected configuration0 to be run";
assert "configuration1".equals(m_configurations.get(1)) : "Expected configuration1 to be run";
assert "configuration2".equals(m_configurations.get(2)) : "Expected configuration1 to be run";
assertEquals(m_configurations.size(), 3);
assertEquals(m_configurations.get(0), "configuration0", "Expected configuration0 to be run");
assertEquals(m_configurations.get(1), "configuration1", "Expected configuration1 to be run");
assertEquals(m_configurations.get(2), "configuration2", "Expected configuration1 to be run");
}
}
4 changes: 3 additions & 1 deletion testng-core/src/test/java/test/classgroup/Second.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package test.classgroup;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

@Test(dependsOnGroups = {"first"})
public class Second {

@Test
public void verify() {
assert First.allRun() : "Methods for class First should have been invoked first.";
assertTrue(First.allRun(), "Methods for class First should have been invoked first.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class BeforeConfigTestSample {
@BeforeClass
public void beforeClass() {
@SuppressWarnings("ConstantOverflow")
int i = 5 / 0;
}

Expand Down
Loading

0 comments on commit 38910de

Please sign in to comment.