]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7850 Improve SensorContextTester to differentiate no symbol from no 1072/head
authorJulien HENRY <henryju@yahoo.fr>
Thu, 30 Jun 2016 13:05:16 +0000 (15:05 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 1 Jul 2016 07:39:04 +0000 (09:39 +0200)
references

sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java

index 6906d3ca850f14f26e67756b287d4d022fb3661b..71a35021a28b3091f67d58cd96b682d193336e0f 100644 (file)
@@ -271,12 +271,13 @@ public class SensorContextTester implements SensorContext {
    * @param componentKey Key of the file like 'myProjectKey:src/foo.php'
    * @param line Line you want to query
    * @param lineOffset Offset you want to query.
-   * @return List of references for the symbol or empty list if there is no symbol at this position or if there is no reference for this symbol.
+   * @return List of references for the symbol (potentially empty) or null if there is no symbol at this position.
    */
+  @CheckForNull
   public Collection<TextRange> referencesForSymbolAt(String componentKey, int line, int lineOffset) {
     DefaultSymbolTable symbolTable = sensorStorage.symbolsPerComponent.get(componentKey);
     if (symbolTable == null) {
-      return Collections.emptyList();
+      return null;
     }
     DefaultTextPointer location = new DefaultTextPointer(line, lineOffset);
     for (Map.Entry<TextRange, Set<TextRange>> symbol : symbolTable.getReferencesBySymbol().entrySet()) {
@@ -284,7 +285,7 @@ public class SensorContextTester implements SensorContext {
         return symbol.getValue();
       }
     }
-    return Collections.emptyList();
+    return null;
   }
 
 }
index c8c983b87be9c424c102e338cd6005826d34b9fa..75d49b3a7b3ed9392ad8353dff25e210dc1dde4e 100644 (file)
@@ -142,15 +142,22 @@ public class SensorContextTesterTest {
 
   @Test
   public void testSymbolReferences() {
-    assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 3)).isEmpty();
+    assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 0)).isNull();
+
     NewSymbolTable symbolTable = tester.newSymbolTable()
       .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))));
     symbolTable
-      .newSymbol(1, 0, 1, 5)
+      .newSymbol(1, 8, 1, 10);
+
+    symbolTable
+      .newSymbol(1, 1, 1, 5)
       .newReference(6, 9)
       .newReference(1, 10, 1, 13);
 
     symbolTable.save();
+
+    assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 0)).isNull();
+    assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 8)).isEmpty();
     assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 3)).extracting("start.line", "start.lineOffset", "end.line", "end.lineOffset").containsExactly(tuple(1, 6, 1, 9),
       tuple(1, 10, 1, 13));
   }