From 6f6d435949e41f7abf8c09a513381f0e4c8d6189 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 30 Jun 2016 15:05:16 +0200 Subject: [PATCH] SONAR-7850 Improve SensorContextTester to differentiate no symbol from no references --- .../batch/sensor/internal/SensorContextTester.java | 7 ++++--- .../sensor/internal/SensorContextTesterTest.java | 11 +++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index 6906d3ca850..71a35021a28 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -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 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> symbol : symbolTable.getReferencesBySymbol().entrySet()) { @@ -284,7 +285,7 @@ public class SensorContextTester implements SensorContext { return symbol.getValue(); } } - return Collections.emptyList(); + return null; } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java index c8c983b87be..75d49b3a7b3 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java @@ -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)); } -- 2.39.5