Skip to content

Commit

Permalink
fix: Remove Error Prone compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
juherr committed Feb 19, 2024
1 parent 30b4843 commit 23371c7
Show file tree
Hide file tree
Showing 42 changed files with 212 additions and 149 deletions.
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,5 +43,20 @@ public String[] values() {
public Class<? extends Annotation> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SampleTestUsingFunction {

@Test(dataProvider = "dp")
public void testMethod(Function<String, String> data) {
data.apply("Bumble_Bee");
String ignored = data.apply("Bumble_Bee");
}

@DataProvider(name = "dp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SampleTestUsingPredicate {

@Test(dataProvider = "dp")
public void testMethod(Predicate<String> data) {
data.test("IronHide");
boolean ignored = data.test("IronHide");
}

@DataProvider(name = "dp")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package test.dependent;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

/**
Expand Down Expand Up @@ -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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package test.dependent;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

Expand All @@ -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");
}
}
Loading

0 comments on commit 23371c7

Please sign in to comment.