]> source.dussan.org Git - sonarqube.git/commitdiff
Fix NPE when no source on file
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 27 Jun 2014 16:07:38 +0000 (18:07 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 27 Jun 2014 16:07:38 +0000 (18:07 +0200)
sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java
sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java
sonar-server/src/main/java/org/sonar/server/source/SourceService.java
sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java
sonar-server/src/test/java/org/sonar/server/source/DeprecatedSourceDecoratorTest.java
sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java
sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java

index 907225ab4d102ce1fd9ce6dafdef7dab87a66d00..9ad21b6d5440f3cd76da100da99a6fac8c5bc59c 100644 (file)
@@ -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);
index 8df8c2e59bd4f9ffd714175cc4f0df9cb078cad4..cfde941a2195652992df9aa2d88717fe2c5acd9a 100644 (file)
@@ -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<String> 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);
     }
index f430842e36dad858a38c24bc4a8c00380d4928ab..8e2733046fb8350e431ba6d82b9337bbe51ae231 100644 (file)
@@ -51,15 +51,17 @@ public class SourceService implements ServerComponent {
     this.deprecatedSourceDecorator = deprecatedSourceDecorator;
   }
 
+  @CheckForNull
   public List<String> getLinesAsHtml(String fileKey) {
     return getLinesAsHtml(fileKey, null, null);
   }
 
+  @CheckForNull
   public List<String> getLinesAsHtml(String fileKey, @Nullable Integer from, @Nullable Integer to) {
     checkPermission(fileKey);
 
     List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml(fileKey, from, to);
-    if (!decoratedSource.isEmpty()) {
+    if (decoratedSource != null) {
       return decoratedSource;
     }
     return deprecatedSourceDecorator.getSourceAsHtml(fileKey, from, to);
index b3947c65ef53224647c51cef031d461b4702a65f..7243a42010a0f94a1ff0b831c4ff2da02897ba38 100644 (file)
@@ -76,7 +76,7 @@ public class ShowAction implements RequestHandler {
     int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
 
     List<String> sourceHtml = sourceService.getLinesAsHtml(fileKey, from, to);
-    if (sourceHtml.isEmpty()) {
+    if (sourceHtml == null) {
       throw new NotFoundException("File '" + fileKey + "' has no sources");
     }
 
index 9975e4738157ffe339ee71cf6620c0113aa075a7..fb87aace956dc98b6ef68bfca393f1c71c0526fa 100644 (file)
@@ -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
index 2860bef146f4a3ed9d5999c1bda52dfbb7122816..27131efe1411fbba6c463ebb8f695588ca23f960 100644 (file)
@@ -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.<String>emptyList());
+    when(sourceDecorator.getDecoratedSourceAsHtml(eq(COMPONENT_KEY), anyInt(), anyInt())).thenReturn(null);
 
     service.getLinesAsHtml(COMPONENT_KEY, 1, 2);
 
index 3418dba94efa2e34b241ece089a12d2ad84784dc..1f21f64ee9e9879d100c5010563396736d60d7f1 100644 (file)
@@ -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.<String>emptyList());
+    when(sourceService.getLinesAsHtml(anyString(), anyInt(), anyInt())).thenReturn(null);
 
     try {
       WsTester.TestRequest request = tester.newGetRequest("api/sources", "show").setParam("key", componentKey);