-
Notifications
You must be signed in to change notification settings - Fork 687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SONARJAVA-4446 Check if the parent class or implemented interface symbols of the test class are known. #4956
Changes from 5 commits
696dc58
5272e3e
1d1b1c7
49d8434
f5d1b45
95e559d
221771e
b1d8595
c0588fe
7899e97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S2187", | ||
"hasTruePositives": true, | ||
"falseNegatives": 12, | ||
"falsePositives": 1 | ||
"falseNegatives": 13, | ||
"falsePositives": 0 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,18 @@ | |
import com.googlecode.zohhak.api.TestWith; | ||
import com.googlecode.zohhak.api.runners.ZohhakRunner; | ||
|
||
class SubclassTest extends ParentTestClass { // Compliant, we cannot know what is in ParentTestClass so we don't raise issues | ||
} | ||
|
||
class Subclass2Test extends myPackage.ParentTestClass { | ||
} | ||
|
||
class Subclass3Test implements MyInterface { | ||
|
||
} | ||
|
||
abstract class ResolvedParent {} | ||
class Subclass4Test extends ResolvedParent {} // Noncompliant | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a good reason to put this new block at the beginning of the file. Could we move it towards the end? |
||
|
||
class A extends junit.framework.TestCase { | ||
void testFoo() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Stream; | ||
|
@@ -78,7 +79,16 @@ private void resetAnnotationCache() { | |
} | ||
|
||
private void checkClass(ClassTree classTree) { | ||
if (!ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT)) { | ||
boolean knownParent = Optional.ofNullable(classTree.superClass()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a more typical way of doing this would be to define |
||
.map(parent -> !parent.symbolType().isUnknown()) | ||
.orElse(true); | ||
boolean knownImplementedInterfaces = classTree.superInterfaces().stream() | ||
.noneMatch(i -> i.symbolType().isUnknown()); | ||
|
||
if (!ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT) | ||
&& knownParent | ||
&& knownImplementedInterfaces | ||
) { | ||
Symbol.TypeSymbol classSymbol = classTree.symbol(); | ||
Stream<Symbol> members = getAllMembers(classSymbol, checkRunWith(classSymbol, "Enclosed")); | ||
IdentifierTree simpleName = classTree.simpleName(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we change the name so it suggests that the class is not defined, for example,
UndefinedParentTestClass
?