]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 3 Feb 2014 09:37:11 +0000 (10:37 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 3 Feb 2014 09:37:21 +0000 (10:37 +0100)
sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java
sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java
sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java
sonar-server/src/main/java/org/sonar/server/source/SourceService.java
sonar-server/src/main/java/org/sonar/server/source/ws/SourcesShowWsHandler.java
sonar-server/src/test/java/org/sonar/server/source/DeprecatedSourceDecoratorTest.java
sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java

index 424aa57408cc53c253e494cec1e711a00be8f451..4b6bcc485ba9ca8dcc9a3923c45ab9bec49c8e6e 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+
 import java.util.Arrays;
 import java.util.List;
 
@@ -41,7 +42,7 @@ public class BadRequestException extends ServerException {
     super(BAD_REQUEST, message);
   }
 
-  public BadRequestException(String message, List<Message> errors) {
+  public BadRequestException(@Nullable String message, List<Message> errors) {
     super(BAD_REQUEST, message);
     this.errors = errors;
   }
index 46e1c997b7049bf0f6addc9a3d6e89ee3ab795af..4bd8a1f79795356ac1fdebb10c7e50215c70fd2c 100644 (file)
@@ -47,6 +47,7 @@ import org.sonar.server.user.UserSession;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
@@ -239,8 +240,8 @@ public class IssueShowWsHandler implements RequestHandler {
   }
 
   private void addUserWithLabel(IssueQueryResult result, @Nullable String value, String field, JsonWriter json) {
-    User user = result.user(value);
     if (value != null) {
+      User user = result.user(value);
       json
         .prop(field, value)
         .prop(field + "Name", user != null ? user.name() : null);
index 78dd20a3773140fc2fe6c96efac99079b1b924e7..4878c158493bd3e10ece104b04f733d27dd693c1 100644 (file)
@@ -32,6 +32,8 @@ 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;
@@ -70,7 +72,7 @@ public class DeprecatedSourceDecorator implements ServerComponent {
       if (source != null) {
         return splitSourceByLine(source, component.getLanguage(), from, to);
       } else {
-        return null;
+        return Collections.emptyList();
       }
     } finally {
       MyBatis.closeQuietly(session);
index 3d04cb4fa99ff28392453647883c392e85d8cb65..8c742b0f28643c08f58dc82a9dbaaced0c8c22c6 100644 (file)
@@ -69,7 +69,7 @@ public class SourceService implements ServerComponent {
     if (decoratedSource != null) {
       return decoratedSource;
     } else {
-     return deprecatedSourceDecorator.getSourceAsHtml(componentKey, from, to);
+      return deprecatedSourceDecorator.getSourceAsHtml(componentKey, from, to);
     }
   }
 
index 7878a48bad67c1171ec9b32b6cfb754950f82ff2..96611c2e414d018d1949a8aef48e26da24fc0c30 100644 (file)
@@ -48,7 +48,7 @@ public class SourcesShowWsHandler implements RequestHandler {
     Integer toParam = request.intParam("to");
     int from = (fromParam != null && fromParam > 0) ? fromParam : 1;
     List<String> sourceHtml = sourceService.getSourcesByComponent(componentKey, from, toParam);
-    if (sourceHtml == null) {
+    if (sourceHtml.isEmpty()) {
       throw new NotFoundException("Component : " + componentKey + " has no source.");
     }
 
@@ -89,14 +89,12 @@ public class SourcesShowWsHandler implements RequestHandler {
         String[] dateWithLine = splitColumn(dates.get(i));
         String date = dateWithLine[1];
         String formattedDate = DateUtils.formatDate(DateUtils.parseDateTime(date));
-        if (line >= from && line <= to) {
-          if (!isSameCommit(date, previousDate, author, previousAuthor) || !scmDataAdded) {
-            json.name(Integer.toString(line)).beginArray();
-            json.value(author);
-            json.value(formattedDate);
-            json.endArray();
-            scmDataAdded = true;
-          }
+        if (line >= from && line <= to && (!isSameCommit(date, previousDate, author, previousAuthor) || !scmDataAdded)) {
+          json.name(Integer.toString(line)).beginArray();
+          json.value(author);
+          json.value(formattedDate);
+          json.endArray();
+          scmDataAdded = true;
         }
         previousAuthor = author;
         previousDate = date;
@@ -105,7 +103,7 @@ public class SourcesShowWsHandler implements RequestHandler {
     }
   }
 
-  private boolean isSameCommit(String date, String previousDate, String author, String previousAuthor){
+  private boolean isSameCommit(String date, String previousDate, String author, String previousAuthor) {
     return author.equals(previousAuthor) && date.equals(previousDate);
   }
 
index 9811614e280d9b628160b441ca0c9a2eee25cfff..0c26283d75a8690be978e871e3988ade028c2f73 100644 (file)
@@ -82,12 +82,12 @@ public class DeprecatedSourceDecoratorTest {
   }
 
   @Test
-  public void return_null_if_no_source_code_on_component() throws Exception {
+  public void return_empty_list_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)).isNull();
+    assertThat(sourceDecorator.getSourceAsHtml(componentKey)).isEmpty();
   }
 
   @Test
index 64ff710c0bf909bb675bc87df69e04e3ff6dcb14..5931679e35d1fbbe52bbb75649bb971936fc44a4 100644 (file)
@@ -29,6 +29,8 @@ import org.sonar.api.server.ws.WsTester;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.source.SourceService;
 
+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;
@@ -68,7 +70,7 @@ public class SourcesShowWsHandlerTest {
   @Test
   public void fail_to_show_source_if_no_source_found() throws Exception {
     String componentKey = "org.apache.struts:struts:Dispatcher";
-    when(sourceService.getSourcesByComponent(anyString(), anyInt(), anyInt())).thenReturn(null);
+    when(sourceService.getSourcesByComponent(anyString(), anyInt(), anyInt())).thenReturn(Collections.<String>emptyList());
 
     try {
       WsTester.TestRequest request = tester.newRequest("show").setParam("key", componentKey);
@@ -94,6 +96,11 @@ public class SourcesShowWsHandlerTest {
   @Test
   public void show_source_always_should_not_begin_with_from_0() throws Exception {
     String componentKey = "org.apache.struts:struts:Dispatcher";
+    when(sourceService.getSourcesByComponent(componentKey, 1, 5)).thenReturn(newArrayList(
+      " */",
+      "",
+      "public class <span class=\"sym-31 sym\">HelloWorld</span> {"
+    ));
     WsTester.TestRequest request = tester.newRequest("show").setParam("key", componentKey).setParam("from", "0").setParam("to", "5");
     request.execute();
     verify(sourceService).getSourcesByComponent(componentKey, 1, 5);