Skip to content

Commit 94b2285

Browse files
committed
hashcode and equals should always inspect the same members. issue testng-team#772
1 parent 2301f92 commit 94b2285

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

src/main/java/org/testng/internal/BaseTestMethod.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -396,40 +396,44 @@ public boolean canRunFromClass(IClass testClass) {
396396
return m_methodClass.isAssignableFrom(testClass.getRealClass());
397397
}
398398

399-
/**
400-
* {@inheritDoc} Compares two BaseTestMethod using the test class then the associated
401-
* Java Method.
402-
*/
403399
@Override
404-
public boolean equals(Object obj) {
405-
if (this == obj) {
406-
return true;
407-
}
408-
if (obj == null) {
409-
return false;
410-
}
411-
if (getClass() != obj.getClass()) {
412-
return false;
413-
}
414-
415-
BaseTestMethod other = (BaseTestMethod) obj;
416-
417-
boolean isEqual = m_testClass == null ? other.m_testClass == null
418-
: other.m_testClass != null &&
419-
m_testClass.getRealClass().equals(other.m_testClass.getRealClass())
420-
&& m_instance == other.getInstance();
421-
422-
return isEqual && getConstructorOrMethod().equals(other.getConstructorOrMethod());
423-
}
400+
public boolean equals(Object obj) {
401+
if (this == obj)
402+
return true;
403+
if (obj == null)
404+
return false;
405+
if (getClass() != obj.getClass())
406+
return false;
407+
BaseTestMethod other = (BaseTestMethod) obj;
408+
if (m_instance == null) {
409+
if (other.m_instance != null)
410+
return false;
411+
} else if (!m_instance.equals(other.m_instance))
412+
return false;
413+
if (m_method == null) {
414+
if (other.m_method != null)
415+
return false;
416+
} else if (!m_method.equals(other.m_method))
417+
return false;
418+
if (m_testClass == null) {
419+
if (other.m_testClass != null)
420+
return false;
421+
} else if (!m_testClass.equals(other.m_testClass))
422+
return false;
423+
return true;
424+
}
424425

425-
/**
426-
* {@inheritDoc} This implementation returns the associated Java Method's hash code.
427-
* @return the associated Java Method's hash code.
428-
*/
429426
@Override
430-
public int hashCode() {
431-
return m_method.hashCode();
432-
}
427+
public int hashCode() {
428+
final int prime = 31;
429+
int result = 1;
430+
result = prime * result
431+
+ ((m_instance == null) ? 0 : m_instance.hashCode());
432+
result = prime * result + ((m_method == null) ? 0 : m_method.hashCode());
433+
result = prime * result
434+
+ ((m_testClass == null) ? 0 : m_testClass.hashCode());
435+
return result;
436+
}
433437

434438
protected void initGroups(Class<? extends ITestOrConfiguration> annotationClass) {
435439
//

0 commit comments

Comments
 (0)