Skip to content

Commit

Permalink
Fix errorprone issues
Browse files Browse the repository at this point in the history
  • Loading branch information
juherr committed Aug 17, 2023
1 parent 254ad45 commit 14cb1ad
Show file tree
Hide file tree
Showing 51 changed files with 254 additions and 170 deletions.
25 changes: 12 additions & 13 deletions testng-asserts/src/test/java/org/testng/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,8 @@ public void compareUnEqualDoubleArraysWithDelta() {
@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 +643,22 @@ static class BrokenEqualsTrue {
public boolean equals(Object o) {
return true; // broken implementation
}

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

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

@Override
public int hashCode() {
return 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.testng.annotations.Test;

/** Tests different equality cases for nested collections and arrays. */
@SuppressWarnings("ArraysAsListPrimitiveArray")
public class ArrayEqualityAssertTest {

@Test
Expand Down
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 @@ -139,7 +139,7 @@ public boolean hasMoreInvocation() {

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

@Override
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");
}
}
13 changes: 9 additions & 4 deletions testng-core/src/test/java/test/BaseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertSame;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -158,8 +162,8 @@ public void setFailedConfigs(Map<String, List<ITestResult>> m) {
}

protected void run() {
assert null != getTest()
: "Test wasn't set, maybe @Configuration methodSetUp() was never called";
assertNotNull(
getTest(), "Test wasn't set, maybe @Configuration methodSetUp() was never called");
setPassedTests(Maps.newHashMap());
setFailedTests(Maps.newHashMap());
setSkippedTests(Maps.newHashMap());
Expand Down Expand Up @@ -314,9 +318,10 @@ protected void verifyResults(Map<String, List<ITestResult>> tests, int expected,
Object firstKey = keys.iterator().next();
List<ITestResult> passedResult = tests.get(firstKey);
int n = passedResult.size();
assert n == expected : "Expected " + expected + " " + message + " but found " + n;
assertSame(n, expected, "Expected " + expected + " " + message + " but found " + n);
} else {
assert expected == 0 : "Expected " + expected + " " + message + " but found " + tests.size();
assertEquals(
0, expected, "Expected " + expected + " " + message + " but found " + tests.size());
}
}

Expand Down
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);
}
}
3 changes: 2 additions & 1 deletion testng-core/src/test/java/test/JUnit4Test.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertTrue;

import org.testng.TestNG;
import org.testng.annotations.BeforeMethod;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void testTests(
String[] expectedFailedTests,
String[] expectedSkippedTests) {
addClasses(classes);
assert getTest().isJUnit();
assertTrue(getTest().isJUnit());

run();

Expand Down
4 changes: 3 additions & 1 deletion testng-core/src/test/java/test/JUnitTest1.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.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -21,7 +23,7 @@ public void initJUnitFlag() {
@Test
public void methodsThatStartWithTest() {
addClass("test.sample.JUnitSample1");
assert getTest().isJUnit();
assertTrue(getTest().isJUnit());

run();
String[] passed = {JUnitSample1.EXPECTED1, JUnitSample1.EXPECTED2};
Expand Down
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/ant/NoPackageTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package test.ant;

import static org.testng.Assert.assertTrue;

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

Expand All @@ -14,6 +16,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");
}
}
5 changes: 3 additions & 2 deletions testng-core/src/test/java/test/ant/TestCommandLineArgs.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test.ant;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.AssertJUnit.assertEquals;

import java.io.File;
Expand Down Expand Up @@ -31,10 +32,10 @@ public void testDOSPathResolution() {
public void testPathResolution() {
File file = new File("../CHANGES.txt");

assert file.exists();
assertThat(file.exists()).isTrue();

String path = file.getAbsolutePath();

assert path.split("[/\\\\]", -1).length > 1;
assertThat(path.split("[/\\\\]", -1).length).isGreaterThan(1);
}
}
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.");
}
}
Loading

0 comments on commit 14cb1ad

Please sign in to comment.