@Test public void should_lazy_load_reference_hashes_when_status_changed_on_branch() throws Exception { final String source = "source"; FileUtils.write(ioFile, source, StandardCharsets.UTF_8); when(project.getKeyWithBranch()).thenReturn("foo:myBranch"); when(file.getProjectRelativePath()).thenReturn("src/Foo.java"); String key = "foo:myBranch:src/Foo.java"; when(file.status()).thenReturn(InputFile.Status.CHANGED); when(lastSnapshots.getLineHashes(key)).thenReturn(new String[] {md5Hex(source)}); assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); verify(lastSnapshots).getLineHashes(key); assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); Mockito.verifyNoMoreInteractions(lastSnapshots); }
@Test public void should_lazy_load_reference_hashes_when_status_changed() throws Exception { final String source = "source"; FileUtils.write(ioFile, source, StandardCharsets.UTF_8); when(project.getKeyWithBranch()).thenReturn("foo"); when(file.getProjectRelativePath()).thenReturn("src/Foo.java"); String key = "foo:src/Foo.java"; when(file.status()).thenReturn(InputFile.Status.CHANGED); when(lastSnapshots.getLineHashes(key)).thenReturn(new String[] {md5Hex(source)}); assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); verify(lastSnapshots).getLineHashes(key); assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); Mockito.verifyNoMoreInteractions(lastSnapshots); }
private void initHashes() { if (hashedSource == null) { hashedSource = FileHashes.create(inputFile); Status status = inputFile.status(); if (status == Status.ADDED) { hashedReference = null; } else if (status == Status.SAME) { hashedReference = hashedSource; } else { // Need key with branch String serverSideKey = ComponentKeys.createEffectiveKey(project.getKeyWithBranch(), inputFile); String[] lineHashes = lastSnapshots.getLineHashes(serverSideKey); hashedReference = lineHashes != null ? FileHashes.create(lineHashes) : null; } } }
@Test public void should_fail_to_download_source_from_ws() throws URISyntaxException { WsTestUtil.mockException(wsClient, new HttpDownloader.HttpException(new URI(""), 500)); ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsClient); thrown.expect(HttpDownloader.HttpException.class); lastSnapshots.getLineHashes("foo"); }
@Test public void should_download_source_from_ws_if_preview_mode() { WsTestUtil.mockReader(wsClient, new StringReader("ae12\n\n43fb")); ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsClient); String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Bar.c"); assertThat(hashes).containsOnly("ae12", "", "43fb"); WsTestUtil.verifyCall(wsClient, "/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FBar.c"); }
@Test public void should_download_source_with_space_from_ws_if_preview_mode() { WsTestUtil.mockReader(wsClient, new StringReader("ae12\n\n43fb")); ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsClient); String[] hashes = lastSnapshots.getLineHashes("myproject:org/foo/Foo Bar.c"); assertThat(hashes).containsOnly("ae12", "", "43fb"); WsTestUtil.verifyCall(wsClient, "/api/sources/hash?key=myproject%3Aorg%2Ffoo%2FFoo+Bar.c"); }
private void initHashes() { if (hashedSource == null) { hashedSource = FileHashes.create(inputFile); Status status = inputFile.status(); if (status == Status.ADDED) { hashedReference = null; } else if (status == Status.SAME) { hashedReference = hashedSource; } else { // Need key with branch String serverSideKey = ComponentKeys.createEffectiveKey(project.getKeyWithBranch(), inputFile); String[] lineHashes = lastSnapshots.getLineHashes(serverSideKey); hashedReference = lineHashes != null ? FileHashes.create(lineHashes) : null; } } }