3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.batch.issue.tracking;
22 import org.sonar.batch.cache.WSLoader.LoadStrategy;
23 import org.sonar.batch.cache.WSLoaderResult;
24 import org.sonar.batch.cache.WSLoader;
25 import org.apache.commons.lang.mutable.MutableBoolean;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.utils.HttpDownloader;
33 import java.net.URISyntaxException;
35 import static org.mockito.Matchers.any;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Matchers.anyString;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
43 public class DefaultServerLineHashesLoaderTest {
46 public ExpectedException thrown = ExpectedException.none();
49 public void before() {
53 public void should_download_source_from_ws_if_preview_mode() {
54 WSLoader wsLoader = mock(WSLoader.class);
55 when(wsLoader.loadString(anyString(), any(LoadStrategy.class))).thenReturn(new WSLoaderResult<>("ae12\n\n43fb", true));
57 ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsLoader);
59 String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Bar.c", null);
60 assertThat(hashes).containsOnly("ae12", "", "43fb");
61 verify(wsLoader).loadString("/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FBar.c", LoadStrategy.CACHE_FIRST);
65 public void should_download_source_with_space_from_ws_if_preview_mode() {
66 WSLoader server = mock(WSLoader.class);
67 when(server.loadString(anyString(), any(LoadStrategy.class))).thenReturn(new WSLoaderResult<>("ae12\n\n43fb", true));
69 ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(server);
71 MutableBoolean fromCache = new MutableBoolean();
72 String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Foo Bar.c", fromCache);
73 assertThat(fromCache.booleanValue()).isTrue();
74 assertThat(hashes).containsOnly("ae12", "", "43fb");
75 verify(server).loadString("/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FFoo+Bar.c", LoadStrategy.CACHE_FIRST);
79 public void should_fail_to_download_source_from_ws() throws URISyntaxException {
80 WSLoader server = mock(WSLoader.class);
81 when(server.loadString(anyString(), any(LoadStrategy.class))).thenThrow(new HttpDownloader.HttpException(new URI(""), 500));
83 ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(server);
85 thrown.expect(HttpDownloader.HttpException.class);
86 lastSnapshots.getLineHashes("foo", null);