Skip to content

Commit 8347ba4

Browse files
committed
Fix quality flaw : add coverage on parent link
1 parent 4c94a7d commit 8347ba4

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012 SonarSource
4+
* sonarqube@googlegroups.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonar.java.resolve;
21+
22+
import com.google.common.base.Charsets;
23+
import com.sonar.sslr.api.typed.ActionParser;
24+
import org.junit.Test;
25+
import org.sonar.java.ast.parser.JavaParser;
26+
import org.sonar.plugins.java.api.tree.ClassTree;
27+
import org.sonar.plugins.java.api.tree.CompilationUnitTree;
28+
import org.sonar.plugins.java.api.tree.MethodTree;
29+
import org.sonar.plugins.java.api.tree.Tree;
30+
import org.sonar.plugins.java.api.tree.VariableTree;
31+
32+
import java.io.File;
33+
import java.util.Collections;
34+
35+
import static org.fest.assertions.Assertions.assertThat;
36+
37+
public class SemanticModelTest {
38+
39+
private static final ActionParser<Tree> PARSER = JavaParser.createParser(Charsets.UTF_8);
40+
41+
@Test
42+
public void parent_link_should_be_computed() {
43+
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse("class A { int field; void foo() {} }");
44+
SemanticModel semanticModel = SemanticModel.createFor(cut, Collections.<File>emptyList());
45+
ClassTree classTree = (ClassTree) cut.types().get(0);
46+
VariableTree field = (VariableTree) classTree.members().get(0);
47+
MethodTree method = (MethodTree) classTree.members().get(1);
48+
49+
assertThat(semanticModel.getParent(method)).isSameAs(classTree);
50+
assertThat(semanticModel.getParent(field)).isSameAs(classTree);
51+
assertThat(semanticModel.getParent(classTree)).isSameAs(cut);
52+
53+
}
54+
}

0 commit comments

Comments
 (0)