Search in sources :

Example 21 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class JavaFilesCacheTest method resource_file_mapping.

@Test
public void resource_file_mapping() {
    JavaFilesCache javaFilesCache = new JavaFilesCache();
    JavaAstScanner.scanSingleFileForTests(new File("src/test/resources/JavaFilesCacheTestFile.java"), new VisitorsBridge(javaFilesCache));
    assertThat(javaFilesCache.resourcesCache.keySet()).hasSize(8);
    assertThat(javaFilesCache.resourcesCache.keySet()).contains("org/sonar/java/JavaFilesCacheTestFile");
    assertThat(javaFilesCache.resourcesCache.keySet()).contains("org/sonar/java/JavaFilesCacheTestFile$A");
    assertThat(javaFilesCache.resourcesCache.keySet()).contains("org/sonar/java/JavaFilesCacheTestFile$plop");
    assertThat(javaFilesCache.resourcesCache.keySet()).contains("org/sonar/java/JavaFilesCacheTestFile$A$I");
    assertThat(javaFilesCache.resourcesCache.keySet()).contains("org/sonar/java/JavaFilesCacheTestFile$A$1B");
    assertThat(javaFilesCache.resourcesCache.keySet()).contains("org/sonar/java/JavaFilesCacheTestFile$A$1B$1");
    assertThat(javaFilesCache.resourcesCache.keySet()).contains("org/sonar/java/JavaFilesCacheTestFile$A$2");
    assertThat(javaFilesCache.resourcesCache.keySet()).contains("org/sonar/java/JavaFilesCacheTestFile$A$3");
}
Also used : VisitorsBridge(org.sonar.java.model.VisitorsBridge) File(java.io.File) Test(org.junit.Test)

Example 22 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class MethodMatcherTest method detected.

@Test
public void detected() {
    MethodMatcher objectToString = MethodMatcher.create().typeDefinition(TypeCriteria.subtypeOf("java.lang.Object")).name("toString").withoutParameter();
    MethodMatcher objectToStringWithIntParam = MethodMatcher.create().name("toString").parameters("int");
    MethodMatcher objectToStringWithStringParam = MethodMatcher.create().typeDefinition(TypeCriteria.anyType()).name(NameCriteria.is("toString")).parameters("java.lang.String");
    MethodMatcher objectToStringWithAnyParam = MethodMatcher.create().typeDefinition(TypeCriteria.is("Test")).name("toString").withAnyParameters();
    MethodMatcher integerToString = MethodMatcher.create().typeDefinition("java.lang.Integer").name("toString").withoutParameter();
    MethodMatcher foo = MethodMatcher.create().typeDefinition(TypeCriteria.anyType()).name("foo").withoutParameter();
    MethodMatcher callSiteIsTest = MethodMatcher.create().typeDefinition(TypeCriteria.anyType()).name(NameCriteria.any()).withAnyParameters().callSite(TypeCriteria.is("Test"));
    Map<MethodMatcher, List<Integer>> matches = new HashMap<>();
    matches.put(objectToString, new ArrayList<>());
    matches.put(objectToStringWithIntParam, new ArrayList<>());
    matches.put(objectToStringWithStringParam, new ArrayList<>());
    matches.put(objectToStringWithAnyParam, new ArrayList<>());
    matches.put(integerToString, new ArrayList<>());
    matches.put(foo, new ArrayList<>());
    matches.put(callSiteIsTest, new ArrayList<>());
    JavaAstScanner.scanSingleFileForTests(new File("src/test/files/matcher/Test.java"), new VisitorsBridge(Collections.singletonList(new Visitor(matches)), new ArrayList<>(), null));
    assertThat(matches.get(objectToString)).containsExactly(6, 19, 27);
    assertThat(matches.get(objectToStringWithIntParam)).containsExactly(10);
    assertThat(matches.get(objectToStringWithStringParam)).containsExactly(11, 14);
    assertThat(matches.get(objectToStringWithAnyParam)).containsExactly(6, 10, 11, 14);
    assertThat(matches.get(integerToString)).containsExactly(19);
    assertThat(matches.get(foo)).containsExactly(35, 36);
    assertThat(matches.get(callSiteIsTest)).containsExactly(6, 10, 11, 14, 18, 22);
}
Also used : SubscriptionVisitor(org.sonar.java.ast.visitors.SubscriptionVisitor) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VisitorsBridge(org.sonar.java.model.VisitorsBridge) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 23 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class AbstractMethodDetectionTest method withAnyParameters.

@Test
public void withAnyParameters() throws Exception {
    Visitor visitor = new Visitor(ImmutableList.of(MethodMatcher.create().typeDefinition("A").name("method").withAnyParameters()));
    JavaAstScanner.scanSingleFileForTests(new File("src/test/files/checks/AbstractMethodDetection.java"), new VisitorsBridge(visitor));
    assertThat(visitor.lines).containsExactly(14, 15, 16, 17);
}
Also used : VisitorsBridge(org.sonar.java.model.VisitorsBridge) File(java.io.File) Test(org.junit.Test)

Example 24 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class JavaSquid method createVisitorBridge.

private static VisitorsBridge createVisitorBridge(Iterable<JavaCheck> codeVisitors, List<File> classpath, JavaVersion javaVersion, @Nullable SonarComponents sonarComponents, SymbolicExecutionMode symbolicExecutionMode) {
    VisitorsBridge visitorsBridge = new VisitorsBridge(codeVisitors, classpath, sonarComponents, symbolicExecutionMode);
    visitorsBridge.setJavaVersion(javaVersion);
    return visitorsBridge;
}
Also used : VisitorsBridge(org.sonar.java.model.VisitorsBridge)

Example 25 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class MethodJavaSymbolTest method test_unknowns.

@Test
public void test_unknowns() {
    MethodVisitor methodVisitor = new MethodVisitor(Sets.newHashSet(16, 21), Sets.newHashSet(7, 15, 17, 31));
    JavaAstScanner.scanSingleFileForTests(new File("src/test/files/resolve/MethodSymbols.java"), new VisitorsBridge(methodVisitor));
}
Also used : VisitorsBridge(org.sonar.java.model.VisitorsBridge) File(java.io.File) Test(org.junit.Test)

Aggregations

VisitorsBridge (org.sonar.java.model.VisitorsBridge)26 File (java.io.File)25 Test (org.junit.Test)24 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)11 RecognitionException (com.sonar.sslr.api.RecognitionException)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)4 SonarComponents (org.sonar.java.SonarComponents)4 ArrayList (java.util.ArrayList)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 Measurer (org.sonar.java.Measurer)2 SubscriptionVisitor (org.sonar.java.ast.visitors.SubscriptionVisitor)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 InterruptedIOException (java.io.InterruptedIOException)1 HashMap (java.util.HashMap)1 BeforeClass (org.junit.BeforeClass)1 SensorContext (org.sonar.api.batch.SensorContext)1 InputPath (org.sonar.api.batch.fs.InputPath)1 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)1