use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method report_issue_on_tree_with_secondary.
@Test
public void report_issue_on_tree_with_secondary() {
ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
Tree firstMember = tree.members().get(0);
Tree secondMember = tree.members().get(1);
ArrayList<Location> secondary = new ArrayList<>();
secondary.add(new JavaFileScannerContext.Location("secondary", firstMember));
secondary.add(new JavaFileScannerContext.Location("secondary", secondMember));
context.reportIssue(CHECK, tree.simpleName(), "msg", secondary, null);
assertThat(reportedMessage.getMessage()).isEqualTo("msg");
assertThat(reportedMessage.getCost()).isNull();
assertThat(reportedMessage.flows).hasSize(2);
assertMessagePosition(reportedMessage, 1, 6, 1, 7);
List<AnalyzerMessage> secondaries = reportedMessage.flows.stream().map(flow -> flow.get(0)).collect(Collectors.toList());
assertThat(secondaries).hasSize(2);
assertMessagePosition(secondaries.get(0), 2, 2, 2, 13);
assertMessagePosition(secondaries.get(1), 3, 2, 3, 15);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method report_issue_on_tree_with_cost.
@Test
public void report_issue_on_tree_with_cost() {
ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
context.reportIssue(CHECK, tree.simpleName(), "msg", new ArrayList<>(), COST);
assertThat(reportedMessage.getMessage()).isEqualTo("msg");
assertThat(reportedMessage.getCost()).isEqualTo(COST);
assertThat(reportedMessage.flows).isEmpty();
assertMessagePosition(reportedMessage, 1, 6, 1, 7);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method annotation_declaration.
/*
* 9.6. Annotation Types
*/
@Test
public void annotation_declaration() {
ClassTree tree = firstType("public @interface T { }");
assertThat(tree.is(Tree.Kind.ANNOTATION_TYPE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).keyword().text()).isEqualTo("public");
assertThat(tree.simpleName().name()).isEqualTo("T");
assertThat(tree.superClass()).isNull();
assertThat(tree.openBraceToken().text()).isEqualTo("{");
assertThat(tree.closeBraceToken().text()).isEqualTo("}");
assertThat(tree.superInterfaces()).isEmpty();
assertThat(tree.declarationKeyword().text()).isEqualTo("interface");
assertThatChildrenIteratorHasSize(tree, 8);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method interface_declaration.
/*
* 9. Interfaces
*/
@Test
public void interface_declaration() {
ClassTree tree = firstType("public interface T<U> extends I1, I2 { }");
assertThat(tree.is(Tree.Kind.INTERFACE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.simpleName().name()).isEqualTo("T");
TypeParameters typeParameters = tree.typeParameters();
assertThatChildrenIteratorHasSize(typeParameters, 3);
assertThat(typeParameters).isNotEmpty();
assertThat(tree.superClass()).isNull();
assertThat(tree.superInterfaces()).hasSize(2);
assertThat(tree.superInterfaces().separators()).hasSize(1);
assertThat(tree.declarationKeyword().text()).isEqualTo("interface");
assertThatChildrenIteratorHasSize(tree, 8);
tree = firstType("public interface T { }");
assertThat(tree.is(Tree.Kind.INTERFACE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.simpleName().name()).isEqualTo("T");
assertThat(tree.typeParameters()).isEmpty();
assertThat(tree.superClass()).isNull();
assertThat(tree.openBraceToken().text()).isEqualTo("{");
assertThat(tree.closeBraceToken().text()).isEqualTo("}");
assertThat(tree.superInterfaces()).isEmpty();
assertThat(tree.declarationKeyword().text()).isEqualTo("interface");
assertThatChildrenIteratorHasSize(tree, 7);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method class_declaration.
/*
* 8. Classes
*/
@Test
public void class_declaration() {
ClassTree tree = firstType("public class T<U> extends C implements I1, I2 { }");
assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
List<ModifierKeywordTree> modifiers = tree.modifiers().modifiers();
assertThat(modifiers).hasSize(1);
assertThat(modifiers.get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(modifiers.get(0).keyword().text()).isEqualTo("public");
assertThat(tree.simpleName().name()).isEqualTo("T");
TypeParameters typeParameters = tree.typeParameters();
assertThat(typeParameters).isNotEmpty();
assertThat(typeParameters.separators()).isEmpty();
assertThatChildrenIteratorHasSize(typeParameters, 3);
assertThat(tree.openBraceToken().text()).isEqualTo("{");
assertThat(tree.superClass()).isNotNull();
assertThat(tree.superInterfaces()).hasSize(2);
assertThat(tree.superInterfaces().separators()).hasSize(1);
assertThat(tree.superInterfaces().separators().get(0).text()).isEqualTo(",");
assertThat(tree.closeBraceToken().text()).isEqualTo("}");
assertThat(tree.declarationKeyword().text()).isEqualTo("class");
tree = firstType("public class T { }");
modifiers = tree.modifiers().modifiers();
assertThat(modifiers).hasSize(1);
assertThat(modifiers.get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(modifiers.get(0).keyword().text()).isEqualTo("public");
assertThat(tree.simpleName().name()).isEqualTo("T");
assertThat(tree.typeParameters()).isEmpty();
assertThat(tree.superClass()).isNull();
assertThat(tree.superInterfaces()).isEmpty();
assertThat(tree.declarationKeyword().text()).isEqualTo("class");
tree = firstType("class T<U,V> { }");
assertThat(tree.modifiers()).isEmpty();
assertThat(tree.simpleName().name()).isEqualTo("T");
typeParameters = tree.typeParameters();
assertThat(typeParameters).hasSize(2);
assertThat(typeParameters.separators()).hasSize(1);
assertThatChildrenIteratorHasSize(typeParameters, 5);
assertThat(tree.superClass()).isNull();
assertThat(tree.superInterfaces()).isEmpty();
assertThat(tree.declarationKeyword().text()).isEqualTo("class");
tree = firstType("@Deprecated class T { }");
assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
assertThat(tree.modifiers().annotations()).hasSize(1);
assertThat(tree.declarationKeyword().text()).isEqualTo("class");
}
Aggregations