use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class JavaPropertiesHelperTest method null_if_not_get_property.
@Test
public void null_if_not_get_property() throws Exception {
ExpressionTree tree = firstExpression("void foo(java.util.Properties props){ props.setProperty(\"myKey\", \"myValue\"); }");
ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(tree);
assertThat(defaultValue).isNull();
tree = firstExpression("void foo(){ System.out.println(); }");
defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(tree);
assertThat(defaultValue).isNull();
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class JavaPropertiesHelperTest method null_if_variable_not_initialized.
@Test
public void null_if_variable_not_initialized() throws Exception {
ExpressionTree tree = firstExpression("void foo(String prop){ foo(myValue);} " + "String myValue;");
ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0));
assertThat(defaultValue).isNull();
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class JavaPropertiesHelperTest method null_if_variable_not_initilialized_method_invocation.
@Test
public void null_if_variable_not_initilialized_method_invocation() throws Exception {
ExpressionTree tree = firstExpression("void foo(String prop){ foo(myValue);} " + "java.util.Properties props = new java.util.Properties();" + "String myValue = \"hello\";");
ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0));
assertThat(defaultValue).isNull();
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method outside_method.
@Test
public void outside_method() throws Exception {
String code = newCode("int b;", "int foo() {", " return b;", "}");
ClassTree classTree = classTree(code);
List<StatementTree> statements = ((MethodTree) classTree.members().get(1)).block().body();
ExpressionTree variableDeclaration = ((VariableTree) (classTree.members().get(0))).initializer();
assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, variableDeclaration);
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method declaration.
@Test
public void declaration() throws Exception {
String code = newCode("int foo() {", " int a = 0;", " return a;", "}");
List<StatementTree> statements = methodBody(code);
ExpressionTree aDeclarationInitializer = initializerFromVariableDeclarationStatement(statements.get(0));
assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, aDeclarationInitializer);
}
Aggregations