From 23371c7491bdb7bda3eee7072b6fc64fa47ff2ca Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Mon, 19 Feb 2024 09:03:42 +0100 Subject: [PATCH] fix: Remove Error Prone compilation errors --- .../src/test/java/org/testng/AssertTest.java | 26 ++++++++--------- .../src/main/java/org/testng/SuiteResult.java | 4 +-- .../org/testng/internal/ClonedMethod.java | 2 +- .../internal/annotations/JDK15TagFactory.java | 2 +- testng-core/src/test/java/NoPackageTest.java | 4 ++- .../test/java/test/ClassConfigurations.java | 17 +++++------ .../src/test/java/test/CtorCalledOnce.java | 8 +++-- testng-core/src/test/java/test/Exclude.java | 9 ++++-- .../src/test/java/test/MethodTest.java | 4 ++- .../src/test/java/test/NestedStaticTest.java | 9 +----- .../src/test/java/test/SampleInheritance.java | 21 ++++++++------ .../src/test/java/test/classgroup/Second.java | 4 ++- .../issue2729/BeforeConfigTestSample.java | 1 + .../custom/CustomAttributesTransformer.java | 17 +++++++++++ .../issue2565/SampleTestUsingFunction.java | 2 +- .../issue2565/SampleTestUsingPredicate.java | 2 +- .../test/dependent/BaseOrderMethodTest.java | 9 ++++-- .../SampleDependentConfigurationMethods.java | 8 +++-- .../dependent/SampleDependentMethods.java | 29 ++++++++++--------- .../dependent/SampleDependentMethods2.java | 29 ++++++++++--------- .../dependent/SampleDependentMethods3.java | 22 ++++++++------ .../factory/FactoryInSeparateClassTest.java | 10 +++++-- .../test/factory/FactoryInterleavingTest.java | 11 ++++--- .../test/hook/issue2251/SampleTestCase.java | 2 +- .../test/junitreports/JUnitReportsTest.java | 10 +------ .../test/java/test/methods/SampleMethod1.java | 7 +++-- .../ContextAwareObjectFactoryFactory.java | 4 ++- ...EfficientPriorityParallelizationTest2.java | 2 -- .../test/sample/AfterClassCalledAtEnd.java | 20 ++++++++----- .../sample/BaseAfterClassCalledAtEnd.java | 4 ++- .../test/sample/BaseSampleInheritance.java | 6 ++-- .../src/test/java/test/sample/Basic1.java | 4 ++- .../src/test/java/test/sample/Basic2.java | 11 ++++--- .../java/test/sample/InvocationCountTest.java | 8 +++-- .../test/java/test/sample/JUnitSample2.java | 2 +- .../test/sample/PartialGroupVerification.java | 7 +++-- .../src/test/java/test/sample/Scope.java | 6 ++-- .../java/test/superclass/BaseSampleTest3.java | 4 ++- .../BaseParallelizationTest.java | 2 +- testng-core/src/test/java/test/tmp/Tmp.java | 2 +- .../src/test/java/test/triangle/Child1.java | 6 ++-- .../src/test/java/test/triangle/Child2.java | 4 ++- 42 files changed, 212 insertions(+), 149 deletions(-) diff --git a/testng-asserts/src/test/java/org/testng/AssertTest.java b/testng-asserts/src/test/java/org/testng/AssertTest.java index dff81b4b91..9034a5a504 100644 --- a/testng-asserts/src/test/java/org/testng/AssertTest.java +++ b/testng-asserts/src/test/java/org/testng/AssertTest.java @@ -305,22 +305,10 @@ public void compareUnEqualDoubleArraysWithDelta() { Assert.assertEquals(actual, expected, 0.1d); } - @SuppressWarnings("serial") @Test(expectedExceptions = AssertionError.class) public void assertEqualsMapShouldFail() { - Map mapActual = - new HashMap() { - { - put("a", "1"); - } - }; - Map mapExpected = - new HashMap() { - { - put("a", "1"); - put("b", "2"); - } - }; + Map mapActual = Map.of("a", "1"); + Map mapExpected = Map.of("a", "1", "b", "2"); Assert.assertEquals(mapActual, mapExpected); } @@ -654,6 +642,11 @@ static class BrokenEqualsTrue { public boolean equals(Object o) { return true; // broken implementation } + + @Override + public int hashCode() { + return 0; + } } static class BrokenEqualsFalse { @@ -661,5 +654,10 @@ static class BrokenEqualsFalse { public boolean equals(Object o) { return false; // broken implementation } + + @Override + public int hashCode() { + return 0; + } } } diff --git a/testng-core/src/main/java/org/testng/SuiteResult.java b/testng-core/src/main/java/org/testng/SuiteResult.java index 982978e077..d10a300262 100644 --- a/testng-core/src/main/java/org/testng/SuiteResult.java +++ b/testng-core/src/main/java/org/testng/SuiteResult.java @@ -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 { +class SuiteResult implements ISuiteResult, Comparable { private final XmlSuite m_suite; private final ITestContext m_testContext; @@ -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(); diff --git a/testng-core/src/main/java/org/testng/internal/ClonedMethod.java b/testng-core/src/main/java/org/testng/internal/ClonedMethod.java index a72c48e665..55830f3d54 100644 --- a/testng-core/src/main/java/org/testng/internal/ClonedMethod.java +++ b/testng-core/src/main/java/org/testng/internal/ClonedMethod.java @@ -138,7 +138,7 @@ public boolean hasMoreInvocation() { @Override public Class getRealClass() { - return m_javaMethod.getClass(); + return m_javaMethod.getDeclaringClass(); } @Override diff --git a/testng-core/src/main/java/org/testng/internal/annotations/JDK15TagFactory.java b/testng-core/src/main/java/org/testng/internal/annotations/JDK15TagFactory.java index 37d603e291..000180101d 100644 --- a/testng-core/src/main/java/org/testng/internal/annotations/JDK15TagFactory.java +++ b/testng-core/src/main/java/org/testng/internal/annotations/JDK15TagFactory.java @@ -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); diff --git a/testng-core/src/test/java/NoPackageTest.java b/testng-core/src/test/java/NoPackageTest.java index d5185ce5a9..f7f8cf2455 100644 --- a/testng-core/src/test/java/NoPackageTest.java +++ b/testng-core/src/test/java/NoPackageTest.java @@ -1,3 +1,5 @@ +import static org.testng.Assert.assertTrue; + import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; @@ -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"); } } diff --git a/testng-core/src/test/java/test/ClassConfigurations.java b/testng-core/src/test/java/test/ClassConfigurations.java index 144710f14a..bfe477267c 100644 --- a/testng-core/src/test/java/test/ClassConfigurations.java +++ b/testng-core/src/test/java/test/ClassConfigurations.java @@ -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; @@ -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); } } diff --git a/testng-core/src/test/java/test/CtorCalledOnce.java b/testng-core/src/test/java/test/CtorCalledOnce.java index eeb4aa381d..2df5bd6f07 100644 --- a/testng-core/src/test/java/test/CtorCalledOnce.java +++ b/testng-core/src/test/java/test/CtorCalledOnce.java @@ -1,5 +1,7 @@ package test; +import static org.testng.Assert.assertEquals; + import org.testng.annotations.AfterTest; import org.testng.annotations.Test; @@ -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 diff --git a/testng-core/src/test/java/test/Exclude.java b/testng-core/src/test/java/test/Exclude.java index cbbe0f18a4..23fafd1364 100644 --- a/testng-core/src/test/java/test/Exclude.java +++ b/testng-core/src/test/java/test/Exclude.java @@ -1,5 +1,7 @@ package test; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; public class Exclude { @@ -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); } } diff --git a/testng-core/src/test/java/test/MethodTest.java b/testng-core/src/test/java/test/MethodTest.java index 10effb2a40..dbd261068e 100644 --- a/testng-core/src/test/java/test/MethodTest.java +++ b/testng-core/src/test/java/test/MethodTest.java @@ -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; @@ -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 = {}; diff --git a/testng-core/src/test/java/test/NestedStaticTest.java b/testng-core/src/test/java/test/NestedStaticTest.java index 02ef058501..1d2870bdc0 100644 --- a/testng-core/src/test/java/test/NestedStaticTest.java +++ b/testng-core/src/test/java/test/NestedStaticTest.java @@ -1,6 +1,5 @@ package test; -import java.util.HashSet; import java.util.List; import java.util.Set; import org.testng.Assert; @@ -19,13 +18,7 @@ public void nestedClassShouldBeIncluded() { tng.addListener(tla); tng.run(); - Set expected = - new HashSet() { - { - add("nested"); - add("f"); - } - }; + Set expected = Set.of("nested", "f"); Set actual = Sets.newHashSet(); List passedTests = tla.getPassedTests(); for (ITestResult t : passedTests) { diff --git a/testng-core/src/test/java/test/SampleInheritance.java b/testng-core/src/test/java/test/SampleInheritance.java index 31e2ba6581..139980f69f 100644 --- a/testng-core/src/test/java/test/SampleInheritance.java +++ b/testng-core/src/test/java/test/SampleInheritance.java @@ -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; @@ -17,9 +20,9 @@ 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"); } @@ -27,15 +30,15 @@ public void configuration2() { 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"); } } diff --git a/testng-core/src/test/java/test/classgroup/Second.java b/testng-core/src/test/java/test/classgroup/Second.java index 78f11af8ab..a6ffa31f58 100644 --- a/testng-core/src/test/java/test/classgroup/Second.java +++ b/testng-core/src/test/java/test/classgroup/Second.java @@ -1,5 +1,7 @@ package test.classgroup; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; @Test(dependsOnGroups = {"first"}) @@ -7,6 +9,6 @@ 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."); } } diff --git a/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigTestSample.java b/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigTestSample.java index ad73df8199..ee232b6554 100644 --- a/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigTestSample.java +++ b/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigTestSample.java @@ -9,6 +9,7 @@ public class BeforeConfigTestSample { @BeforeClass public void beforeClass() { + @SuppressWarnings("ConstantOverflow") int i = 5 / 0; } diff --git a/testng-core/src/test/java/test/custom/CustomAttributesTransformer.java b/testng-core/src/test/java/test/custom/CustomAttributesTransformer.java index 831ba5d875..8f5e45071c 100644 --- a/testng-core/src/test/java/test/custom/CustomAttributesTransformer.java +++ b/testng-core/src/test/java/test/custom/CustomAttributesTransformer.java @@ -3,6 +3,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Objects; import org.testng.IAnnotationTransformer; import org.testng.annotations.CustomAttribute; import org.testng.annotations.ITestAnnotation; @@ -41,5 +43,20 @@ public String[] values() { public Class annotationType() { return CustomAttribute.class; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MoreAttribute that = (MoreAttribute) o; + return Objects.equals(key, that.key) && Arrays.equals(values, that.values); + } + + @Override + public int hashCode() { + int result = Objects.hash(key); + result = 31 * result + Arrays.hashCode(values); + return result; + } } } diff --git a/testng-core/src/test/java/test/dataprovider/issue2565/SampleTestUsingFunction.java b/testng-core/src/test/java/test/dataprovider/issue2565/SampleTestUsingFunction.java index 4da351119c..834a4384f2 100644 --- a/testng-core/src/test/java/test/dataprovider/issue2565/SampleTestUsingFunction.java +++ b/testng-core/src/test/java/test/dataprovider/issue2565/SampleTestUsingFunction.java @@ -12,7 +12,7 @@ public class SampleTestUsingFunction { @Test(dataProvider = "dp") public void testMethod(Function data) { - data.apply("Bumble_Bee"); + String ignored = data.apply("Bumble_Bee"); } @DataProvider(name = "dp") diff --git a/testng-core/src/test/java/test/dataprovider/issue2565/SampleTestUsingPredicate.java b/testng-core/src/test/java/test/dataprovider/issue2565/SampleTestUsingPredicate.java index 2311414d87..7315b10fe1 100644 --- a/testng-core/src/test/java/test/dataprovider/issue2565/SampleTestUsingPredicate.java +++ b/testng-core/src/test/java/test/dataprovider/issue2565/SampleTestUsingPredicate.java @@ -11,7 +11,7 @@ public class SampleTestUsingPredicate { @Test(dataProvider = "dp") public void testMethod(Predicate data) { - data.test("IronHide"); + boolean ignored = data.test("IronHide"); } @DataProvider(name = "dp") diff --git a/testng-core/src/test/java/test/dependent/BaseOrderMethodTest.java b/testng-core/src/test/java/test/dependent/BaseOrderMethodTest.java index 235f41ec93..d3b86b5b37 100644 --- a/testng-core/src/test/java/test/dependent/BaseOrderMethodTest.java +++ b/testng-core/src/test/java/test/dependent/BaseOrderMethodTest.java @@ -1,5 +1,7 @@ package test.dependent; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; /** @@ -30,13 +32,14 @@ public void third0() { protected void verifyGroup(int groupNumber, boolean[] group) { for (int i = 0; i < group.length; i++) { - assert group[i] - : "Error while running group " + assertTrue( + group[i], + "Error while running group " + groupNumber + ": " + " index " + i - + " of previous group should have been run before."; + + " of previous group should have been run before."); } } } diff --git a/testng-core/src/test/java/test/dependent/SampleDependentConfigurationMethods.java b/testng-core/src/test/java/test/dependent/SampleDependentConfigurationMethods.java index a90c7e5d53..2855f952d7 100644 --- a/testng-core/src/test/java/test/dependent/SampleDependentConfigurationMethods.java +++ b/testng-core/src/test/java/test/dependent/SampleDependentConfigurationMethods.java @@ -1,5 +1,7 @@ package test.dependent; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -14,13 +16,13 @@ public void createInstance() { @BeforeMethod(dependsOnMethods = {"createInstance"}) public void firstInvocation() { - assert m_create : "createInstance() was never called"; + assertTrue(m_create, "createInstance() was never called"); m_first = true; } @Test public void verifyDependents() { - assert m_create : "createInstance() was never called"; - assert m_first : "firstInvocation() was never called"; + assertTrue(m_create, "createInstance() was never called"); + assertTrue(m_first, "firstInvocation() was never called"); } } diff --git a/testng-core/src/test/java/test/dependent/SampleDependentMethods.java b/testng-core/src/test/java/test/dependent/SampleDependentMethods.java index 24fb3c3bab..f0754a020a 100644 --- a/testng-core/src/test/java/test/dependent/SampleDependentMethods.java +++ b/testng-core/src/test/java/test/dependent/SampleDependentMethods.java @@ -1,5 +1,8 @@ package test.dependent; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.AfterClass; import org.testng.annotations.Test; @@ -16,7 +19,7 @@ public class SampleDependentMethods { @Test public void oneA() { - assert !m_secondA : "secondA shouldn't have been run yet"; + assertFalse(m_secondA, "secondA shouldn't have been run yet"); m_oneA = true; } @@ -25,32 +28,32 @@ public void canBeRunAnytime() {} @Test(dependsOnMethods = {"oneA", "oneB"}) public void secondA() { - assert m_oneA : "oneA wasn't run"; - assert m_oneB : "oneB wasn't run"; - assert !m_secondA : "secondA shouldn't have been run yet"; + assertTrue(m_oneA, "oneA wasn't run"); + assertTrue(m_oneB, "oneB wasn't run"); + assertFalse(m_secondA, "secondA shouldn't have been run yet"); m_secondA = true; } @Test(dependsOnMethods = {"secondA"}) public void thirdA() { - assert m_oneA : "oneA wasn't run"; - assert m_oneB : "oneB wasn't run"; - assert m_secondA : "secondA wasn't run"; - assert !m_thirdA : "thirdA shouldn't have been run yet"; + assertTrue(m_oneA, "oneA wasn't run"); + assertTrue(m_oneB, "oneB wasn't run"); + assertTrue(m_secondA, "secondA wasn't run"); + assertFalse(m_thirdA, "thirdA shouldn't have been run yet"); m_thirdA = true; } @Test public void oneB() { - assert !m_secondA : "secondA shouldn't have been run yet"; + assertFalse(m_secondA, "secondA shouldn't have been run yet"); m_oneB = true; } @AfterClass public void tearDown() { - assert m_oneA : "oneA wasn't run"; - assert m_oneB : "oneB wasn't run"; - assert m_secondA : "secondA wasn't run"; - assert m_thirdA : "thirdA wasn't run"; + assertTrue(m_oneA, "oneA wasn't run"); + assertTrue(m_oneB, "oneB wasn't run"); + assertTrue(m_secondA, "secondA wasn't run"); + assertTrue(m_thirdA, "thirdA wasn't run"); } } diff --git a/testng-core/src/test/java/test/dependent/SampleDependentMethods2.java b/testng-core/src/test/java/test/dependent/SampleDependentMethods2.java index 709e356fbc..5cfaafc215 100644 --- a/testng-core/src/test/java/test/dependent/SampleDependentMethods2.java +++ b/testng-core/src/test/java/test/dependent/SampleDependentMethods2.java @@ -1,5 +1,8 @@ package test.dependent; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.AfterClass; import org.testng.annotations.Test; @@ -16,7 +19,7 @@ public class SampleDependentMethods2 { @Test(groups = {"one"}) public void oneA() { - assert !m_secondA : "secondA shouldn't have been run yet"; + assertFalse(m_secondA, "secondA shouldn't have been run yet"); m_oneA = true; } @@ -25,32 +28,32 @@ public void canBeRunAnytime() {} @Test(dependsOnGroups = {"one"}) public void secondA() { - assert m_oneA : "oneA wasn't run"; - assert m_oneB : "oneB wasn't run"; - assert !m_secondA : "secondA shouldn't have been run yet"; + assertTrue(m_oneA, "oneA wasn't run"); + assertTrue(m_oneB, "oneB wasn't run"); + assertFalse(m_secondA, "secondA shouldn't have been run yet"); m_secondA = true; } @Test(dependsOnMethods = {"secondA"}) public void thirdA() { - assert m_oneA : "oneA wasn't run"; - assert m_oneB : "oneB wasn't run"; - assert m_secondA : "secondA wasn't run"; - assert !m_thirdA : "thirdA shouldn't have been run yet"; + assertTrue(m_oneA, "oneA wasn't run"); + assertTrue(m_oneB, "oneB wasn't run"); + assertTrue(m_secondA, "secondA wasn't run"); + assertFalse(m_thirdA, "thirdA shouldn't have been run yet"); m_thirdA = true; } @Test(groups = {"one"}) public void oneB() { - assert !m_secondA : "secondA shouldn't have been run yet"; + assertFalse(m_secondA, "secondA shouldn't have been run yet"); m_oneB = true; } @AfterClass public void tearDown() { - assert m_oneA : "oneA wasn't run"; - assert m_oneB : "oneB wasn't run"; - assert m_secondA : "secondA wasn't run"; - assert m_thirdA : "thirdA wasn't run"; + assertTrue(m_oneA, "oneA wasn't run"); + assertTrue(m_oneB, "oneB wasn't run"); + assertTrue(m_secondA, "secondA wasn't run"); + assertTrue(m_thirdA, "thirdA wasn't run"); } } diff --git a/testng-core/src/test/java/test/dependent/SampleDependentMethods3.java b/testng-core/src/test/java/test/dependent/SampleDependentMethods3.java index 5c023827cd..3f141a1e08 100644 --- a/testng-core/src/test/java/test/dependent/SampleDependentMethods3.java +++ b/testng-core/src/test/java/test/dependent/SampleDependentMethods3.java @@ -1,5 +1,9 @@ package test.dependent; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.AfterClass; import org.testng.annotations.Parameters; import org.testng.annotations.Test; @@ -16,30 +20,30 @@ public class SampleDependentMethods3 { @Test public void one() { - assert !m_secondA : "secondA shouldn't have been run yet"; + assertFalse(m_secondA, "secondA shouldn't have been run yet"); m_oneA = true; } @Parameters({"foo"}) @Test public void one(String s) { - assert !m_secondA : "secondA shouldn't have been run yet"; - assert "Cedric".equals(s) : "Expected parameter value Cedric but got " + s; + assertFalse(m_secondA, "secondA shouldn't have been run yet"); + assertEquals(s, "Cedric", "Expected parameter value Cedric but got " + s); m_oneB = true; } @Test(dependsOnMethods = {"one"}) public void secondA() { - assert m_oneA : "oneA wasn't run"; - assert m_oneB : "oneB wasn't run"; - assert !m_secondA : "secondA shouldn't have been run yet"; + assertTrue(m_oneA, "oneA wasn't run"); + assertTrue(m_oneB, "oneB wasn't run"); + assertFalse(m_secondA, "secondA shouldn't have been run yet"); m_secondA = true; } @AfterClass public void tearDown() { - assert m_oneA : "oneA wasn't run"; - assert m_oneB : "oneB wasn't run"; - assert m_secondA : "secondA wasn't run"; + assertTrue(m_oneA, "oneA wasn't run"); + assertTrue(m_oneB, "oneB wasn't run"); + assertTrue(m_secondA, "secondA wasn't run"); } } diff --git a/testng-core/src/test/java/test/factory/FactoryInSeparateClassTest.java b/testng-core/src/test/java/test/factory/FactoryInSeparateClassTest.java index d85ce198a3..7876b84713 100644 --- a/testng-core/src/test/java/test/factory/FactoryInSeparateClassTest.java +++ b/testng-core/src/test/java/test/factory/FactoryInSeparateClassTest.java @@ -1,5 +1,7 @@ package test.factory; +import static org.testng.Assert.assertEquals; + import org.testng.annotations.BeforeTest; import org.testng.annotations.Factory; import org.testng.annotations.Test; @@ -34,9 +36,11 @@ public Object[] createObjects() { dependsOnGroups = {"MySample"}) public void checkSum() { m_wasRun = true; - assert (m_checkSum == 6) - : "Test instances made by factory did not invoke their test methods correctly. expected 6 but got " - + m_checkSum; + assertEquals( + m_checkSum, + 6, + "Test instances made by factory did not invoke their test methods correctly. expected 6 but got " + + m_checkSum); } public static boolean wasRun() { diff --git a/testng-core/src/test/java/test/factory/FactoryInterleavingTest.java b/testng-core/src/test/java/test/factory/FactoryInterleavingTest.java index 3dd9211ec1..f66c0c45e9 100644 --- a/testng-core/src/test/java/test/factory/FactoryInterleavingTest.java +++ b/testng-core/src/test/java/test/factory/FactoryInterleavingTest.java @@ -1,7 +1,8 @@ package test.factory; +import static org.assertj.core.api.Assertions.assertThat; + import java.util.List; -import org.testng.Assert; import org.testng.TestNG; import org.testng.annotations.Test; import org.testng.collections.Lists; @@ -26,10 +27,8 @@ public void methodsShouldBeInterleaved() { 10, 11, 12, 13, }; Integer[] logArray = LOG.toArray(new Integer[0]); - if (!logArray.equals(valid1)) { - Assert.assertEquals(logArray, valid1); - } else if (!logArray.equals(valid2)) { - Assert.assertEquals(logArray, valid2); - } + assertThat(logArray) + .satisfiesAnyOf( + it -> assertThat(it).isEqualTo(valid1), it -> assertThat(it).isEqualTo(valid2)); } } diff --git a/testng-core/src/test/java/test/hook/issue2251/SampleTestCase.java b/testng-core/src/test/java/test/hook/issue2251/SampleTestCase.java index f32e3e7872..3a84984eb7 100644 --- a/testng-core/src/test/java/test/hook/issue2251/SampleTestCase.java +++ b/testng-core/src/test/java/test/hook/issue2251/SampleTestCase.java @@ -14,6 +14,6 @@ public String toString() { @Test(timeOut = 1000000) // removing timeout fixes error output public void testError() throws Exception { - new NullExObj().toString(); + String unused = new NullExObj().toString(); } } diff --git a/testng-core/src/test/java/test/junitreports/JUnitReportsTest.java b/testng-core/src/test/java/test/junitreports/JUnitReportsTest.java index bd3c0deff6..d56e2ba25e 100644 --- a/testng-core/src/test/java/test/junitreports/JUnitReportsTest.java +++ b/testng-core/src/test/java/test/junitreports/JUnitReportsTest.java @@ -10,7 +10,6 @@ import java.io.IOException; import java.nio.file.Path; import java.util.Arrays; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.stream.Stream; @@ -106,14 +105,7 @@ public void testTestCaseOrderingInJUnitReportReporterWhenPrioritiesDefined() thr tng.run(); Testsuite suite = reportReporter.getTestsuite(Issue1262TestSample.class.getName()); List expected = - new LinkedList() { - { - add("testRoles001_Post"); - add("testRoles002_Post"); - add("testRoles003_Post"); - add("testRoles004_Post"); - } - }; + List.of("testRoles001_Post", "testRoles002_Post", "testRoles003_Post", "testRoles004_Post"); List actual = Lists.newLinkedList(); for (Testcase testcase : suite.getTestcase()) { actual.add(testcase.getName().trim()); diff --git a/testng-core/src/test/java/test/methods/SampleMethod1.java b/testng-core/src/test/java/test/methods/SampleMethod1.java index 2f59a4a105..e7e020902c 100644 --- a/testng-core/src/test/java/test/methods/SampleMethod1.java +++ b/testng-core/src/test/java/test/methods/SampleMethod1.java @@ -1,5 +1,7 @@ package test.methods; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; /** @@ -41,7 +43,8 @@ public void shouldNotRun2() { } public static void verify() { - assert m_ok1 && m_ok2 && m_ok3 && m_ok4 - : "All booleans should be true: " + m_ok1 + " " + m_ok2 + " " + m_ok3 + " " + m_ok4; + assertTrue( + m_ok1 && m_ok2 && m_ok3 && m_ok4, + "All booleans should be true: " + m_ok1 + " " + m_ok2 + " " + m_ok3 + " " + m_ok4); } } diff --git a/testng-core/src/test/java/test/objectfactory/ContextAwareObjectFactoryFactory.java b/testng-core/src/test/java/test/objectfactory/ContextAwareObjectFactoryFactory.java index f55775cbec..4e4136b860 100644 --- a/testng-core/src/test/java/test/objectfactory/ContextAwareObjectFactoryFactory.java +++ b/testng-core/src/test/java/test/objectfactory/ContextAwareObjectFactoryFactory.java @@ -1,5 +1,7 @@ package test.objectfactory; +import static org.testng.Assert.assertNotNull; + import org.testng.ITestContext; import org.testng.ITestObjectFactory; import org.testng.annotations.ObjectFactory; @@ -11,7 +13,7 @@ public class ContextAwareObjectFactoryFactory { @ObjectFactory public ITestObjectFactory create(ITestContext context) { - assert context != null; + assertNotNull(context); invoked++; return new ObjectFactoryImpl(); } diff --git a/testng-core/src/test/java/test/priority/parallel/EfficientPriorityParallelizationTest2.java b/testng-core/src/test/java/test/priority/parallel/EfficientPriorityParallelizationTest2.java index fa3931ce17..249b7a64a8 100644 --- a/testng-core/src/test/java/test/priority/parallel/EfficientPriorityParallelizationTest2.java +++ b/testng-core/src/test/java/test/priority/parallel/EfficientPriorityParallelizationTest2.java @@ -100,8 +100,6 @@ public void setUp() { suiteOneTestOneTestMethodLevelEventLogs = getTestMethodLevelEventLogsForTest(SUITE_A, SUITE_A_TEST_A); - suiteOneTestOneTestMethodLevelEventLogs.stream() - .filter(e -> e.getEvent() == TestNgRunEvent.LISTENER_TEST_METHOD_START); testEventLogsMap.put(SUITE_A_TEST_A, getTestLevelEventLogsForTest(SUITE_A, SUITE_A_TEST_A)); diff --git a/testng-core/src/test/java/test/sample/AfterClassCalledAtEnd.java b/testng-core/src/test/java/test/sample/AfterClassCalledAtEnd.java index af4862b878..e42c170936 100644 --- a/testng-core/src/test/java/test/sample/AfterClassCalledAtEnd.java +++ b/testng-core/src/test/java/test/sample/AfterClassCalledAtEnd.java @@ -1,5 +1,8 @@ package test.sample; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -22,28 +25,29 @@ public void before1Class() { @AfterClass(groups = {"someGroup"}) public void afterClass() { m_afterClass = true; - assert m_test1 && m_test2 && m_test3 - : "One of the test methods was not invoked: " + m_test1 + " " + m_test2 + " " + m_test3; + assertTrue( + m_test1 && m_test2 && m_test3, + "One of the test methods was not invoked: " + m_test1 + " " + m_test2 + " " + m_test3); } @Test(description = "Verify that beforeClass and afterClass are called correctly") public void test1() { m_test1 = true; - assert m_before1Class : "beforeClass configuration must be called before method"; - assert !m_afterClass : "afterClass configuration must not be called before test method"; + assertTrue(m_before1Class, "beforeClass configuration must be called before method"); + assertFalse(m_afterClass, "afterClass configuration must not be called before test method"); } @Test public void test2() { m_test2 = true; - assert m_before1Class : "beforeClass configuration must be called before method"; - assert !m_afterClass : "afterClass configuration must not be called before test method"; + assertTrue(m_before1Class, "beforeClass configuration must be called before method"); + assertFalse(m_afterClass, "afterClass configuration must not be called before test method"); } @Test public void test3() { m_test3 = true; - assert m_before1Class : "beforeClass configuration must be called before method"; - assert !m_afterClass : "afterClass configuration must not be called before test method"; + assertTrue(m_before1Class, "beforeClass configuration must be called before method"); + assertFalse(m_afterClass, "afterClass configuration must not be called before test method"); } } diff --git a/testng-core/src/test/java/test/sample/BaseAfterClassCalledAtEnd.java b/testng-core/src/test/java/test/sample/BaseAfterClassCalledAtEnd.java index de9d8241c5..17dbe70dc0 100644 --- a/testng-core/src/test/java/test/sample/BaseAfterClassCalledAtEnd.java +++ b/testng-core/src/test/java/test/sample/BaseAfterClassCalledAtEnd.java @@ -1,5 +1,7 @@ package test.sample; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.AfterClass; public class BaseAfterClassCalledAtEnd { @@ -7,6 +9,6 @@ public class BaseAfterClassCalledAtEnd { @AfterClass(dependsOnGroups = {".*"}) public void baseAfterClass() { - assert m_afterClass : "This afterClass method should have been called last"; + assertTrue(m_afterClass, "This afterClass method should have been called last"); } } diff --git a/testng-core/src/test/java/test/sample/BaseSampleInheritance.java b/testng-core/src/test/java/test/sample/BaseSampleInheritance.java index d83a60b431..420b4adfea 100644 --- a/testng-core/src/test/java/test/sample/BaseSampleInheritance.java +++ b/testng-core/src/test/java/test/sample/BaseSampleInheritance.java @@ -1,5 +1,7 @@ package test.sample; +import static org.testng.Assert.assertTrue; + import java.util.ArrayList; import java.util.List; import org.testng.annotations.BeforeClass; @@ -38,7 +40,7 @@ public void configuration1() { @Test(dependsOnGroups = {"inheritedTestMethod"}) public void testBooleans() { - 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"); } } diff --git a/testng-core/src/test/java/test/sample/Basic1.java b/testng-core/src/test/java/test/sample/Basic1.java index f9a224520e..3834974ef4 100644 --- a/testng-core/src/test/java/test/sample/Basic1.java +++ b/testng-core/src/test/java/test/sample/Basic1.java @@ -1,5 +1,7 @@ package test.sample; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -21,6 +23,6 @@ public void beforeTestMethod() { @Test(groups = {"basic1"}) public void basic1() { - assert getCount() > 0 : "COUNT WAS NOT INCREMENTED"; + assertTrue(getCount() > 0, "COUNT WAS NOT INCREMENTED"); } } diff --git a/testng-core/src/test/java/test/sample/Basic2.java b/testng-core/src/test/java/test/sample/Basic2.java index d30b407339..b2bb688906 100644 --- a/testng-core/src/test/java/test/sample/Basic2.java +++ b/testng-core/src/test/java/test/sample/Basic2.java @@ -1,5 +1,8 @@ package test.sample; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.AfterClass; import org.testng.annotations.AfterTest; import org.testng.annotations.Test; @@ -11,7 +14,7 @@ public class Basic2 { @Test(dependsOnGroups = {"basic1"}) public void basic2() { m_basic2WasRun = true; - assert Basic1.getCount() > 0 : "COUNT WAS NOT INCREMENTED"; + assertTrue(Basic1.getCount() > 0, "COUNT WAS NOT INCREMENTED"); } @AfterTest @@ -23,8 +26,8 @@ public void cleanUp() { @AfterClass public void checkTestAtClassLevelWasRun() { m_afterClass++; - assert m_basic2WasRun : "Class annotated with @Test didn't have its methods run."; - assert 1 == m_afterClass - : "After class should have been called exactly once, not " + m_afterClass; + assertTrue(m_basic2WasRun, "Class annotated with @Test didn't have its methods run."); + assertEquals( + m_afterClass, 1, "After class should have been called exactly once, not " + m_afterClass); } } diff --git a/testng-core/src/test/java/test/sample/InvocationCountTest.java b/testng-core/src/test/java/test/sample/InvocationCountTest.java index cde4fd19ce..c61609a57d 100644 --- a/testng-core/src/test/java/test/sample/InvocationCountTest.java +++ b/testng-core/src/test/java/test/sample/InvocationCountTest.java @@ -1,5 +1,7 @@ package test.sample; +import static org.testng.Assert.assertEquals; + import org.testng.annotations.AfterClass; import org.testng.annotations.AfterSuite; import org.testng.annotations.Test; @@ -69,7 +71,9 @@ public void successPercentageShouldFail() { @AfterClass(groups = {"invocationOnly"}) public void verify() { - assert 10 == m_count - : "Method should have been invoked 10 times but was invoked " + m_count + " times"; + assertEquals( + m_count, + 10, + "Method should have been invoked 10 times but was invoked " + m_count + " times"); } } diff --git a/testng-core/src/test/java/test/sample/JUnitSample2.java b/testng-core/src/test/java/test/sample/JUnitSample2.java index 34d26ef871..c6e1a35700 100644 --- a/testng-core/src/test/java/test/sample/JUnitSample2.java +++ b/testng-core/src/test/java/test/sample/JUnitSample2.java @@ -25,6 +25,6 @@ public void setUp() { } public void testSample2ThatSetUpWasRun() { - assert null != m_field : "setUp() wasn't run"; + assertNotNull("setUp() wasn't run", m_field); } } diff --git a/testng-core/src/test/java/test/sample/PartialGroupVerification.java b/testng-core/src/test/java/test/sample/PartialGroupVerification.java index 3399abea72..0aeb6cfdf7 100644 --- a/testng-core/src/test/java/test/sample/PartialGroupVerification.java +++ b/testng-core/src/test/java/test/sample/PartialGroupVerification.java @@ -1,5 +1,7 @@ package test.sample; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; /** @@ -10,7 +12,8 @@ public class PartialGroupVerification { @Test public void verify() { - assert PartialGroupTest.m_successMethod && PartialGroupTest.m_successClass - : "test1 and test2 should have been invoked both"; + assertTrue( + PartialGroupTest.m_successMethod && PartialGroupTest.m_successClass, + "test1 and test2 should have been invoked both"); } } diff --git a/testng-core/src/test/java/test/sample/Scope.java b/testng-core/src/test/java/test/sample/Scope.java index 04b63fa29c..e725f2947d 100644 --- a/testng-core/src/test/java/test/sample/Scope.java +++ b/testng-core/src/test/java/test/sample/Scope.java @@ -1,5 +1,7 @@ package test.sample; +import static org.testng.Assert.assertEquals; + import org.testng.annotations.Parameters; import org.testng.annotations.Test; @@ -9,12 +11,12 @@ public class Scope { @Parameters({"parameter"}) @Test(groups = {"outer-group"}) public void outerDeprecated(String s) { - assert "out".equals(s) : "Expected out got " + s; + assertEquals(s, "out", "Expected out got " + s); } @Parameters({"parameter"}) @Test(groups = {"inner-group"}) public void innerDeprecated(String s) { - assert "in".equals(s) : "Expected in got " + s; + assertEquals(s, "in", "Expected in got " + s); } } diff --git a/testng-core/src/test/java/test/superclass/BaseSampleTest3.java b/testng-core/src/test/java/test/superclass/BaseSampleTest3.java index f3dc96cd57..febc1954ff 100644 --- a/testng-core/src/test/java/test/superclass/BaseSampleTest3.java +++ b/testng-core/src/test/java/test/superclass/BaseSampleTest3.java @@ -1,10 +1,12 @@ package test.superclass; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; public class BaseSampleTest3 { @Test public void base() { - assert true; + assertTrue(true); } } diff --git a/testng-core/src/test/java/test/thread/parallelization/BaseParallelizationTest.java b/testng-core/src/test/java/test/thread/parallelization/BaseParallelizationTest.java index 0d50aa2097..bb89aa905d 100644 --- a/testng-core/src/test/java/test/thread/parallelization/BaseParallelizationTest.java +++ b/testng-core/src/test/java/test/thread/parallelization/BaseParallelizationTest.java @@ -778,7 +778,7 @@ public static void verifyEventsForTestMethodsRunInTheSameThread( Annotation[] annotations = method.getDeclaredAnnotations(); for (Annotation a : annotations) { - if (Test.class.isAssignableFrom(a.getClass())) { + if (Test.class.isAssignableFrom(a.annotationType())) { isTestMethod = true; } } diff --git a/testng-core/src/test/java/test/tmp/Tmp.java b/testng-core/src/test/java/test/tmp/Tmp.java index e2c828c329..b742ec527d 100644 --- a/testng-core/src/test/java/test/tmp/Tmp.java +++ b/testng-core/src/test/java/test/tmp/Tmp.java @@ -11,7 +11,7 @@ public void f() { debug("START " + Thread.currentThread().getId()); try { Random r = new Random(); - Thread.sleep(Math.abs(r.nextInt() % 300)); + Thread.sleep(Math.abs(r.nextInt(300))); } catch (InterruptedException handled) { Thread.currentThread().interrupt(); handled.printStackTrace(); diff --git a/testng-core/src/test/java/test/triangle/Child1.java b/testng-core/src/test/java/test/triangle/Child1.java index 9e37cab1fe..83caae2e43 100644 --- a/testng-core/src/test/java/test/triangle/Child1.java +++ b/testng-core/src/test/java/test/triangle/Child1.java @@ -1,5 +1,7 @@ package test.triangle; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; /** @@ -10,11 +12,11 @@ public class Child1 extends Base { @Test public void child1() { - assert m_isInitialized : "Wasn't initialized correctly " + hashCode() + " " + getClass(); + assertTrue(m_isInitialized, "Wasn't initialized correctly " + hashCode() + " " + getClass()); } @Test public void child1a() { - assert m_isInitialized : "Wasn't initialized correctly " + hashCode() + " " + getClass(); + assertTrue(m_isInitialized, "Wasn't initialized correctly " + hashCode() + " " + getClass()); } } diff --git a/testng-core/src/test/java/test/triangle/Child2.java b/testng-core/src/test/java/test/triangle/Child2.java index 73eb1bce7b..3a9ba9b066 100644 --- a/testng-core/src/test/java/test/triangle/Child2.java +++ b/testng-core/src/test/java/test/triangle/Child2.java @@ -1,5 +1,7 @@ package test.triangle; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; /** @@ -11,6 +13,6 @@ public class Child2 extends Base { @Test public void child2() { - assert m_isInitialized : "Wasn't initialized correctly"; + assertTrue(m_isInitialized, "Wasn't initialized correctly"); } }