From 0f2c9d91534967377bd4901320b8ef568fdbad73 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 27 Jun 2014 18:07:38 +0200 Subject: [PATCH] Fix NPE when no source on file --- .../org/sonar/server/source/DeprecatedSourceDecorator.java | 3 +-- .../java/org/sonar/server/source/HtmlSourceDecorator.java | 4 ++-- .../src/main/java/org/sonar/server/source/SourceService.java | 4 +++- .../src/main/java/org/sonar/server/source/ws/ShowAction.java | 2 +- .../sonar/server/source/DeprecatedSourceDecoratorTest.java | 4 ++-- .../test/java/org/sonar/server/source/SourceServiceTest.java | 4 +--- .../test/java/org/sonar/server/source/ws/ShowActionTest.java | 4 +--- 7 files changed, 11 insertions(+), 14 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java b/sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java index 907225ab4d1..9ad21b6d544 100644 --- a/sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java +++ b/sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java @@ -33,7 +33,6 @@ import org.sonar.server.exceptions.NotFoundException; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.util.Collections; import java.util.List; import static com.google.common.collect.Lists.newArrayList; @@ -72,7 +71,7 @@ public class DeprecatedSourceDecorator implements ServerComponent { if (source != null) { return splitSourceByLine(source, component.getLanguage(), from, to); } else { - return Collections.emptyList(); + return null; } } finally { MyBatis.closeQuietly(session); diff --git a/sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java b/sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java index 8df8c2e59bd..cfde941a219 100644 --- a/sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java +++ b/sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java @@ -33,7 +33,6 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.util.Collection; -import java.util.Collections; import java.util.List; public class HtmlSourceDecorator implements ServerComponent { @@ -49,6 +48,7 @@ public class HtmlSourceDecorator implements ServerComponent { this.snapshotDataDao = snapshotDataDao; } + @CheckForNull public List getDecoratedSourceAsHtml(String componentKey, @Nullable Integer from, @Nullable Integer to) { SqlSession session = mybatis.openSession(false); try { @@ -57,7 +57,7 @@ public class HtmlSourceDecorator implements ServerComponent { String snapshotSource = snapshotSourceDao.selectSnapshotSourceByComponentKey(componentKey, session); return decorate(snapshotSource, snapshotDataEntries, from, to); } - return Collections.emptyList(); + return null; } finally { MyBatis.closeQuietly(session); } diff --git a/sonar-server/src/main/java/org/sonar/server/source/SourceService.java b/sonar-server/src/main/java/org/sonar/server/source/SourceService.java index f430842e36d..8e2733046fb 100644 --- a/sonar-server/src/main/java/org/sonar/server/source/SourceService.java +++ b/sonar-server/src/main/java/org/sonar/server/source/SourceService.java @@ -51,15 +51,17 @@ public class SourceService implements ServerComponent { this.deprecatedSourceDecorator = deprecatedSourceDecorator; } + @CheckForNull public List getLinesAsHtml(String fileKey) { return getLinesAsHtml(fileKey, null, null); } + @CheckForNull public List getLinesAsHtml(String fileKey, @Nullable Integer from, @Nullable Integer to) { checkPermission(fileKey); List decoratedSource = sourceDecorator.getDecoratedSourceAsHtml(fileKey, from, to); - if (!decoratedSource.isEmpty()) { + if (decoratedSource != null) { return decoratedSource; } return deprecatedSourceDecorator.getSourceAsHtml(fileKey, from, to); diff --git a/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java b/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java index b3947c65ef5..7243a42010a 100644 --- a/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java +++ b/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java @@ -76,7 +76,7 @@ public class ShowAction implements RequestHandler { int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE); List sourceHtml = sourceService.getLinesAsHtml(fileKey, from, to); - if (sourceHtml.isEmpty()) { + if (sourceHtml == null) { throw new NotFoundException("File '" + fileKey + "' has no sources"); } diff --git a/sonar-server/src/test/java/org/sonar/server/source/DeprecatedSourceDecoratorTest.java b/sonar-server/src/test/java/org/sonar/server/source/DeprecatedSourceDecoratorTest.java index 9975e473815..fb87aace956 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/DeprecatedSourceDecoratorTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/DeprecatedSourceDecoratorTest.java @@ -82,12 +82,12 @@ public class DeprecatedSourceDecoratorTest { } @Test - public void return_empty_list_if_no_source_code_on_component() throws Exception { + public void return_null_if_no_source_code_on_component() throws Exception { String componentKey = "org.sonar.sample:Sample"; when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(new ResourceDto().setKey(componentKey).setLanguage("java")); when(snapshotSourceDao.selectSnapshotSourceByComponentKey(componentKey, session)).thenReturn(null); - assertThat(sourceDecorator.getSourceAsHtml(componentKey)).isEmpty(); + assertThat(sourceDecorator.getSourceAsHtml(componentKey)).isNull(); } @Test diff --git a/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java b/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java index 2860bef146f..27131efe141 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java @@ -34,8 +34,6 @@ import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.measure.persistence.MeasureDao; import org.sonar.server.user.MockUserSession; -import java.util.Collections; - import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; import static org.mockito.Mockito.*; @@ -89,7 +87,7 @@ public class SourceServiceTest { @Test public void get_lines_from_deprecated_source_decorator_when_no_data_from_new_decorator() throws Exception { MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, PROJECT_KEY, COMPONENT_KEY); - when(sourceDecorator.getDecoratedSourceAsHtml(eq(COMPONENT_KEY), anyInt(), anyInt())).thenReturn(Collections.emptyList()); + when(sourceDecorator.getDecoratedSourceAsHtml(eq(COMPONENT_KEY), anyInt(), anyInt())).thenReturn(null); service.getLinesAsHtml(COMPONENT_KEY, 1, 2); diff --git a/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java b/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java index 3418dba94ef..1f21f64ee9e 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java @@ -25,8 +25,6 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.source.SourceService; import org.sonar.server.ws.WsTester; -import java.util.Collections; - import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; @@ -65,7 +63,7 @@ public class ShowActionTest { @Test public void fail_to_show_source_if_no_source_found() throws Exception { String componentKey = "src/Foo.java"; - when(sourceService.getLinesAsHtml(anyString(), anyInt(), anyInt())).thenReturn(Collections.emptyList()); + when(sourceService.getLinesAsHtml(anyString(), anyInt(), anyInt())).thenReturn(null); try { WsTester.TestRequest request = tester.newGetRequest("api/sources", "show").setParam("key", componentKey); -- 2.39.5