import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.sonar.api.utils.MessageException;
import org.sonar.scanner.bootstrap.ScannerWsClient;
import org.sonar.scanner.util.ScannerUtils;
import org.sonarqube.ws.WsBatch;
private static boolean shouldThrow(Exception e) {
for (Throwable t : Throwables.getCausalChain(e)) {
- if (t instanceof HttpException) {
- HttpException http = (HttpException) t;
- return http.code() != HttpURLConnection.HTTP_NOT_FOUND;
- }
- if (t instanceof MessageException) {
- return true;
+ if (t instanceof HttpException && ((HttpException) t).code() == HttpURLConnection.HTTP_NOT_FOUND) {
+ return false;
}
}
- return false;
+ return true;
}
private static ProjectRepositories processStream(InputStream is, String projectKey) {
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.HttpURLConnection;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
wsClient = mock(ScannerWsClient.class);
InputStream is = mockData();
WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
+ InputStream isIssueMode = mockData();
+ WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F&issues_mode=true", isIssueMode);
loader = new DefaultProjectRepositoriesLoader(wsClient);
}
@Test
- public void continueOnError() {
+ public void continueOnHttp404Exception() {
+ when(wsClient.call(any(WsRequest.class))).thenThrow(new HttpException("/batch/project.protobuf?key=foo%3F", HttpURLConnection.HTTP_NOT_FOUND, ""));
+ ProjectRepositories proj = loader.load(PROJECT_KEY, false, null);
+ assertThat(proj.exists()).isEqualTo(false);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void failOnNonHttp404Exception() {
when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class);
ProjectRepositories proj = loader.load(PROJECT_KEY, false, null);
assertThat(proj.exists()).isEqualTo(false);
}
- @Test
+ @Test(expected = IllegalStateException.class)
public void parsingError() throws IOException {
InputStream is = mock(InputStream.class);
- when(is.read()).thenThrow(IOException.class);
+ when(is.read(any(byte[].class), anyInt(), anyInt())).thenThrow(IOException.class);
WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
loader.load(PROJECT_KEY, false, null);
}
}
@Test
- public void deserializeResponse() throws IOException {
+ public void deserializeResponse() {
loader.load(PROJECT_KEY, false, null);
}