import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.util.Arrays;
import java.util.List;
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;
}
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.util.Arrays;
import java.util.Date;
import java.util.List;
}
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);
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
+import java.util.Collections;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
if (source != null) {
return splitSourceByLine(source, component.getLanguage(), from, to);
} else {
- return null;
+ return Collections.emptyList();
}
} finally {
MyBatis.closeQuietly(session);
if (decoratedSource != null) {
return decoratedSource;
} else {
- return deprecatedSourceDecorator.getSourceAsHtml(componentKey, from, to);
+ return deprecatedSourceDecorator.getSourceAsHtml(componentKey, from, to);
}
}
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.");
}
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;
}
}
- 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);
}
}
@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
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;
@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);
@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);