diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-11-26 15:40:01 +0100 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-11-26 15:40:01 +0100 |
commit | 302171599cc644b772495276dc86f62ccc6b9655 (patch) | |
tree | 2c856a3d5769ab6f4a0fa29c7285606a8e7e934d /server | |
parent | 074b44ed7d64a306826fd76add9d48fdcbb9ae59 (diff) | |
download | sonarqube-302171599cc644b772495276dc86f62ccc6b9655.tar.gz sonarqube-302171599cc644b772495276dc86f62ccc6b9655.zip |
SONAR-5868 Protect against NPE when there is no source for a file (e.g directory)
Diffstat (limited to 'server')
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/source/ws/HashAction.java | 10 | ||||
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java | 10 |
2 files changed, 17 insertions, 3 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/HashAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/HashAction.java index c78e8ed7ca5..7a780ecbad6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/HashAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/HashAction.java @@ -68,9 +68,13 @@ public class HashAction implements RequestHandler { throw new NotFoundException("Unable to find component with key " + componentKey); } String lineHashes = fileSourceDao.selectLineHashes(component.uuid(), session); - OutputStream output = response.stream().setMediaType("text/plain").output(); - output.write(lineHashes.getBytes(Charsets.UTF_8)); - output.close(); + if (lineHashes == null) { + response.noContent(); + } else { + OutputStream output = response.stream().setMediaType("text/plain").output(); + output.write(lineHashes.getBytes(Charsets.UTF_8)); + output.close(); + } } finally { session.close(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java index 879893d3706..20be6fbedda 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java @@ -83,6 +83,16 @@ public class HashActionTest { } @Test + public void hashes_empty_if_no_source() throws Exception { + String componentKey = "project:src/File.xoo"; + String uuid = "polop"; + ComponentDto component = new ComponentDto().setUuid(uuid); + when(componentDao.getByKey(any(DbSession.class), eq(componentKey))).thenReturn(component); + WsTester.TestRequest request = tester.newGetRequest("api/sources", "hash").setParam("key", componentKey); + request.execute().assertNoContent(); + } + + @Test public void fail_to_show_hashes_if_file_does_not_exist() throws Exception { String componentKey = "project:src/File.xoo"; try { |