Skip to content
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

Merged
merged 10 commits into from
Dec 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void javaCheckTestSources() throws Exception {
SoftAssertions softly = new SoftAssertions();
softly.assertThat(newDiffs).containsExactlyInAnyOrderElementsOf(knownDiffs.values());
softly.assertThat(newTotal).isEqualTo(knownTotal);
softly.assertThat(rulesCausingFPs).hasSize(11);
softly.assertThat(rulesCausingFPs).hasSize(10);
softly.assertThat(rulesNotReporting).hasSize(10);

/**
Expand Down
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
Expand Up @@ -21,7 +21,6 @@
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;


class A extends junit.framework.TestCase {
void testFoo() {
}
Expand Down Expand Up @@ -313,3 +312,16 @@ public class MyZohhak4Test { // Compliant
public void testFoo4() {
}
}

class SubclassTest extends UndefinedParent { // Compliant, we cannot know what is in ParentTestClass so we don't raise issues
}

class Subclass2Test extends myPackage.UndefinedParent {
}

class Subclass3Test implements UndefinedInterface {

}

abstract class ResolvedParent {}
class Subclass4Test extends ResolvedParent {} // Noncompliant
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +79,17 @@ private void resetAnnotationCache() {
}

private void checkClass(ClassTree classTree) {
if (!ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT)) {
boolean hasUnknownParent = Optional.ofNullable(classTree.superClass())
.map(parent -> parent.symbolType().isUnknown())
// If the superClass is null, then the class has no parent, so has no unknownParent.
.orElse(false);
boolean knownImplementedInterfaces = classTree.superInterfaces().stream()
.noneMatch(i -> i.symbolType().isUnknown());

if (!ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT)
&& !hasUnknownParent
&& knownImplementedInterfaces
) {
Symbol.TypeSymbol classSymbol = classTree.symbol();
Stream<Symbol> members = getAllMembers(classSymbol, checkRunWith(classSymbol, "Enclosed"));
IdentifierTree simpleName = classTree.simpleName();
Expand Down
Loading