diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-11-26 15:09:22 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-12-02 09:11:06 +0100 |
commit | 0c8e5f2b0a7e36bdc7e0ee67b28982c0e7749995 (patch) | |
tree | 04491c803516c2a374cee23c18318cb2e28990b1 /sonar-batch | |
parent | 1c56d9b8abc3c2a6dc2fdb93221b0dbc36c2c850 (diff) | |
download | sonarqube-0c8e5f2b0a7e36bdc7e0ee67b28982c0e7749995.tar.gz sonarqube-0c8e5f2b0a7e36bdc7e0ee67b28982c0e7749995.zip |
SONAR-5868 Issue tracking now use hash WS instead of raw sources
Diffstat (limited to 'sonar-batch')
7 files changed, 136 insertions, 251 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LastLineHashes.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LastLineHashes.java new file mode 100644 index 00000000000..ed0e3adce3b --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/LastLineHashes.java @@ -0,0 +1,53 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.scan; + +import com.google.common.base.Splitter; +import com.google.common.collect.Iterators; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.BatchComponent; +import org.sonar.api.utils.TimeProfiler; +import org.sonar.batch.bootstrap.ServerClient; + +public class LastLineHashes implements BatchComponent { + + private static final Logger LOG = LoggerFactory.getLogger(LastLineHashes.class); + + private final ServerClient server; + + public LastLineHashes(ServerClient server) { + this.server = server; + } + + public String[] getLineHashes(String fileKey) { + String hashesFromWs = loadHashesFromWs(fileKey); + return hashesFromWs != null ? Iterators.toArray(Splitter.on('\n').split(hashesFromWs).iterator(), String.class) : null; + } + + private String loadHashesFromWs(String fileKey) { + TimeProfiler profiler = new TimeProfiler(LOG).start("Load previous line hashes of: " + fileKey).setLevelToDebug(); + try { + return server.request("/api/sources/hash?key=" + ServerClient.encodeForUrl(fileKey)); + } finally { + profiler.stop(); + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java deleted file mode 100644 index 8a5d2bf2ee7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.BatchComponent; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.ResourceUtils; -import org.sonar.api.utils.HttpDownloader; -import org.sonar.api.utils.TimeProfiler; -import org.sonar.batch.bootstrap.AnalysisMode; -import org.sonar.batch.bootstrap.ServerClient; -import org.sonar.core.source.db.SnapshotSourceDao; - -import javax.annotation.CheckForNull; - -public class LastSnapshots implements BatchComponent { - - private static final Logger LOG = LoggerFactory.getLogger(LastSnapshots.class); - - private final AnalysisMode analysisMode; - private final ServerClient server; - private final SnapshotSourceDao sourceDao; - - public LastSnapshots(AnalysisMode analysisMode, SnapshotSourceDao dao, ServerClient server) { - this.analysisMode = analysisMode; - this.sourceDao = dao; - this.server = server; - } - - public String getSource(Resource resource) { - String source = null; - if (ResourceUtils.isFile(resource)) { - if (analysisMode.isPreview()) { - source = loadSourceFromWs(resource); - } else { - source = loadSourceFromDb(resource); - } - } - return StringUtils.defaultString(source, ""); - } - - private String loadSourceFromWs(Resource resource) { - TimeProfiler profiler = new TimeProfiler(LOG).start("Load previous source code of: " + resource.getEffectiveKey()).setLevelToDebug(); - try { - return server - .request("/api/sources/raw?key=" + ServerClient.encodeForUrl(resource.getEffectiveKey()), "GET", false, analysisMode.getPreviewReadTimeoutSec() * 1000); - } catch (HttpDownloader.HttpException he) { - if (he.getResponseCode() == 404) { - return ""; - } - throw he; - } finally { - profiler.stop(); - } - } - - @CheckForNull - private String loadSourceFromDb(Resource resource) { - return sourceDao.selectSnapshotSourceByComponentKey(resource.getEffectiveKey()); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index e1a1467a95f..b22723814e0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -149,7 +149,7 @@ public class ProjectScanContainer extends ComponentContainer { ResourceKeyMigration.class, DefaultFileLinesContextFactory.class, ProjectLock.class, - LastSnapshots.class, + LastLineHashes.class, Caches.class, SnapshotCache.class, ResourceCache.class, diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/LastLineHashesTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/LastLineHashesTest.java new file mode 100644 index 00000000000..7490781fc2d --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/LastLineHashesTest.java @@ -0,0 +1,82 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.scan; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.HttpDownloader; +import org.sonar.batch.bootstrap.ServerClient; + +import java.net.URI; +import java.net.URISyntaxException; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class LastLineHashesTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void before() { + } + + @Test + public void should_download_source_from_ws_if_preview_mode() { + ServerClient server = mock(ServerClient.class); + when(server.request(anyString())).thenReturn("ae12\n\n43fb"); + + LastLineHashes lastSnapshots = new LastLineHashes(server); + + String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Bar.c"); + assertThat(hashes).containsOnly("ae12", "", "43fb"); + verify(server).request("/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FBar.c"); + } + + @Test + public void should_download_source_with_space_from_ws_if_preview_mode() { + ServerClient server = mock(ServerClient.class); + when(server.request(anyString())).thenReturn("ae12\n\n43fb"); + + LastLineHashes lastSnapshots = new LastLineHashes(server); + + String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Foo Bar.c"); + assertThat(hashes).containsOnly("ae12", "", "43fb"); + verify(server).request("/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FFoo+Bar.c"); + } + + @Test + public void should_fail_to_download_source_from_ws() throws URISyntaxException { + ServerClient server = mock(ServerClient.class); + when(server.request(anyString())).thenThrow(new HttpDownloader.HttpException(new URI(""), 500)); + + LastLineHashes lastSnapshots = new LastLineHashes(server); + + thrown.expect(HttpDownloader.HttpException.class); + lastSnapshots.getLineHashes("foo"); + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java deleted file mode 100644 index 719ae5c1775..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Project; -import org.sonar.api.utils.HttpDownloader; -import org.sonar.batch.bootstrap.AnalysisMode; -import org.sonar.batch.bootstrap.ServerClient; -import org.sonar.core.persistence.TestDatabase; -import org.sonar.core.source.db.SnapshotSourceDao; - -import java.net.URI; -import java.net.URISyntaxException; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; - -public class LastSnapshotsTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Rule - public TestDatabase db = new TestDatabase(); - - private AnalysisMode mode; - - @Before - public void before() { - mode = mock(AnalysisMode.class); - when(mode.getPreviewReadTimeoutSec()).thenReturn(30); - } - - @Test - public void should_get_source_of_last_snapshot() { - db.prepareDbUnit(getClass(), "last_snapshot.xml"); - - ServerClient server = mock(ServerClient.class); - LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); - - assertThat(lastSnapshots.getSource(newFile())).isEqualTo("this is bar"); - verifyZeroInteractions(server); - } - - @Test - public void should_return_empty_source_if_no_last_snapshot() { - db.prepareDbUnit(getClass(), "no_last_snapshot.xml"); - ServerClient server = mock(ServerClient.class); - - LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); - - assertThat(lastSnapshots.getSource(newFile())).isEqualTo(""); - verifyZeroInteractions(server); - } - - @Test - public void should_download_source_from_ws_if_preview_mode() { - db.prepareDbUnit(getClass(), "last_snapshot.xml"); - ServerClient server = mock(ServerClient.class); - when(server.request(anyString(), eq("GET"), eq(false), eq(30 * 1000))).thenReturn("downloaded source of Bar.c"); - - when(mode.isPreview()).thenReturn(true); - LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); - - String source = lastSnapshots.getSource(newFile()); - assertThat(source).isEqualTo("downloaded source of Bar.c"); - verify(server).request("/api/sources/raw?key=myproject%3Aorg%2Ffoo%2FBar.c", "GET", false, 30 * 1000); - } - - @Test - public void should_download_source_with_space_from_ws_if_preview_mode() { - db.prepareDbUnit(getClass(), "last_snapshot.xml"); - ServerClient server = mock(ServerClient.class); - when(server.request(anyString(), eq("GET"), eq(false), eq(30 * 1000))).thenReturn("downloaded source of Foo Bar.c"); - - when(mode.isPreview()).thenReturn(true); - LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); - - String source = lastSnapshots.getSource(newFile()); - assertThat(source).isEqualTo("downloaded source of Foo Bar.c"); - verify(server).request("/api/sources/raw?key=myproject%3Aorg%2Ffoo%2FBar.c", "GET", false, 30 * 1000); - } - - @Test - public void should_fail_to_download_source_from_ws() throws URISyntaxException { - db.prepareDbUnit(getClass(), "last_snapshot.xml"); - ServerClient server = mock(ServerClient.class); - when(server.request(anyString(), eq("GET"), eq(false), eq(30 * 1000))).thenThrow(new HttpDownloader.HttpException(new URI(""), 500)); - - when(mode.isPreview()).thenReturn(true); - LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); - - thrown.expect(HttpDownloader.HttpException.class); - lastSnapshots.getSource(newFile()); - } - - @Test - public void should_return_empty_source_if_preview_mode_and_no_last_snapshot() throws URISyntaxException { - db.prepareDbUnit(getClass(), "last_snapshot.xml"); - ServerClient server = mock(ServerClient.class); - when(server.request(anyString(), eq("GET"), eq(false), eq(30 * 1000))).thenThrow(new HttpDownloader.HttpException(new URI(""), 404)); - - when(mode.isPreview()).thenReturn(true); - LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); - - String source = lastSnapshots.getSource(newFileWithSpace()); - assertThat(source).isEqualTo(""); - verify(server).request("/api/sources/raw?key=myproject%3Aorg%2Ffoo%2FFoo+Bar.c", "GET", false, 30 * 1000); - } - - @Test - public void should_not_load_source_of_non_files() throws URISyntaxException { - db.prepareDbUnit(getClass(), "last_snapshot.xml"); - ServerClient server = mock(ServerClient.class); - - LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); - - String source = lastSnapshots.getSource(new Project("my-project")); - assertThat(source).isEqualTo(""); - } - - private File newFile() { - File file = new File("org/foo", "Bar.c"); - file.setEffectiveKey("myproject:org/foo/Bar.c"); - return file; - } - - private File newFileWithSpace() { - File file = new File("org/foo", "Foo Bar.c"); - file.setEffectiveKey("myproject:org/foo/Foo Bar.c"); - return file; - } -} diff --git a/sonar-batch/src/test/resources/org/sonar/batch/scan/LastSnapshotsTest/last_snapshot.xml b/sonar-batch/src/test/resources/org/sonar/batch/scan/LastSnapshotsTest/last_snapshot.xml deleted file mode 100644 index 8cc02f51187..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/scan/LastSnapshotsTest/last_snapshot.xml +++ /dev/null @@ -1,6 +0,0 @@ -<dataset> - <projects id="100" kee="myproject:org/foo/Bar.c" enabled="[true]" scope="FIL" qualifier="FIL" language="c"/> - <snapshots id="1000" project_id="100" status="P" islast="[false]" purge_status="[null]"/> - <snapshots id="1100" project_id="100" status="P" islast="[true]" purge_status="[null]"/> - <snapshot_sources ID="10000" SNAPSHOT_ID="1100" DATA="this is bar"/> -</dataset>
\ No newline at end of file diff --git a/sonar-batch/src/test/resources/org/sonar/batch/scan/LastSnapshotsTest/no_last_snapshot.xml b/sonar-batch/src/test/resources/org/sonar/batch/scan/LastSnapshotsTest/no_last_snapshot.xml deleted file mode 100644 index 84d67a04385..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/scan/LastSnapshotsTest/no_last_snapshot.xml +++ /dev/null @@ -1,3 +0,0 @@ -<dataset> - -</dataset>
\ No newline at end of file |