@CheckForNull private SourceHashHolder loadSourceHashes(InputComponent component) { SourceHashHolder sourceHashHolder = null; if (component.isFile()) { DefaultInputFile file = (DefaultInputFile) component; sourceHashHolder = new SourceHashHolder(project, file, lastLineHashes); } return sourceHashHolder; }
private static Input<ServerIssueFromWs> createBaseInput(Collection<ServerIssueFromWs> serverIssues, @Nullable SourceHashHolder sourceHashHolder) { List<String> refHashes; if (sourceHashHolder != null && sourceHashHolder.getHashedReference() != null) { refHashes = Arrays.asList(sourceHashHolder.getHashedReference().hashes()); } else { refHashes = new ArrayList<>(0); } return new IssueTrackingInput<>(serverIssues, refHashes); }
private static Input<TrackedIssue> createRawInput(Collection<TrackedIssue> rIssues, @Nullable SourceHashHolder sourceHashHolder) { List<String> baseHashes; if (sourceHashHolder != null && sourceHashHolder.getHashedSource() != null) { baseHashes = Arrays.asList(sourceHashHolder.getHashedSource().hashes()); } else { baseHashes = new ArrayList<>(0); } return new IssueTrackingInput<>(rIssues, baseHashes); }
public FileHashes getHashedSource() { initHashes(); return hashedSource; } }
@CheckForNull public FileHashes getHashedReference() { initHashes(); return hashedReference; }
@Test public void should_not_load_reference_hashes_when_status_same() throws Exception { final String source = "source"; String key = "foo:src/Foo.java"; FileUtils.write(ioFile, source, StandardCharsets.UTF_8); when(file.key()).thenReturn(key); when(file.status()).thenReturn(InputFile.Status.SAME); assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); Mockito.verifyNoMoreInteractions(lastSnapshots); }
@Test public void should_lazy_load_line_hashes() throws Exception { final String source = "source"; FileUtils.write(ioFile, source + "\n", StandardCharsets.UTF_8); when(file.lines()).thenReturn(2); assertThat(sourceHashHolder.getHashedSource().getHash(1)).isEqualTo(md5Hex(source)); assertThat(sourceHashHolder.getHashedSource().getHash(2)).isEqualTo(""); assertThat(sourceHashHolder.getHashedSource().getHash(1)).isEqualTo(md5Hex(source)); }
@Before public void setUp() throws Exception { lastSnapshots = mock(ServerLineHashesLoader.class); file = mock(DefaultInputFile.class); ioFile = temp.newFile(); when(file.file()).thenReturn(ioFile); when(file.path()).thenReturn(ioFile.toPath()); when(file.inputStream()).thenAnswer(i -> Files.newInputStream(ioFile.toPath())); when(file.lines()).thenReturn(1); when(file.charset()).thenReturn(StandardCharsets.UTF_8); project = mock(DefaultInputProject.class); sourceHashHolder = new SourceHashHolder(project, file, lastSnapshots); }
public FileHashes getHashedSource() { initHashes(); return hashedSource; } }
@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); }
public static TrackedIssue toTrackedIssue(InputComponent component, ScannerReport.Issue rawIssue, @Nullable SourceHashHolder hashes) { RuleKey ruleKey = RuleKey.of(rawIssue.getRuleRepository(), rawIssue.getRuleKey()); Preconditions.checkNotNull(component.key(), "Component key must be set"); Preconditions.checkNotNull(ruleKey, "Rule key must be set"); TrackedIssue issue = new TrackedIssue(hashes != null ? hashes.getHashedSource() : null); issue.setKey(Uuids.createFast()); issue.setComponentKey(component.key()); issue.setRuleKey(ruleKey); issue.setGap(rawIssue.getGap() != 0 ? rawIssue.getGap() : null); issue.setSeverity(rawIssue.getSeverity().name()); issue.setMessage(StringUtils.trimToNull(rawIssue.getMsg())); issue.setResolution(null); issue.setStatus(Issue.STATUS_OPEN); issue.setNew(true); if (rawIssue.hasTextRange()) { TextRange r = rawIssue.getTextRange(); issue.setStartLine(r.getStartLine()); issue.setStartLineOffset(r.getStartOffset()); issue.setEndLine(r.getEndLine()); issue.setEndLineOffset(r.getEndOffset()); } return issue; }
@CheckForNull private SourceHashHolder loadSourceHashes(InputComponent component) { SourceHashHolder sourceHashHolder = null; if (component.isFile()) { DefaultInputFile file = (DefaultInputFile) component; sourceHashHolder = new SourceHashHolder(project, file, lastLineHashes); } return sourceHashHolder; }
@CheckForNull public FileHashes getHashedReference() { initHashes(); return hashedReference; }
@Test public void no_reference_hashes_when_status_added() throws Exception { final String source = "source"; String key = "foo:src/Foo.java"; FileUtils.write(ioFile, source, StandardCharsets.UTF_8); when(file.key()).thenReturn(key); when(file.status()).thenReturn(InputFile.Status.ADDED); assertThat(sourceHashHolder.getHashedReference()).isNull(); Mockito.verifyNoMoreInteractions(lastSnapshots); }
private static Input<TrackedIssue> createRawInput(Collection<TrackedIssue> rIssues, @Nullable SourceHashHolder sourceHashHolder) { List<String> baseHashes; if (sourceHashHolder != null && sourceHashHolder.getHashedSource() != null) { baseHashes = Arrays.asList(sourceHashHolder.getHashedSource().hashes()); } else { baseHashes = new ArrayList<>(0); } return new IssueTrackingInput<>(rIssues, baseHashes); }
@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); }
public static TrackedIssue toTrackedIssue(InputComponent component, ScannerReport.Issue rawIssue, @Nullable SourceHashHolder hashes) { RuleKey ruleKey = RuleKey.of(rawIssue.getRuleRepository(), rawIssue.getRuleKey()); Preconditions.checkNotNull(component.key(), "Component key must be set"); Preconditions.checkNotNull(ruleKey, "Rule key must be set"); TrackedIssue issue = new TrackedIssue(hashes != null ? hashes.getHashedSource() : null); issue.setKey(Uuids.createFast()); issue.setComponentKey(component.key()); issue.setRuleKey(ruleKey); issue.setGap(rawIssue.getGap() != 0 ? rawIssue.getGap() : null); issue.setSeverity(rawIssue.getSeverity().name()); issue.setMessage(StringUtils.trimToNull(rawIssue.getMsg())); issue.setResolution(null); issue.setStatus(Issue.STATUS_OPEN); issue.setNew(true); if (rawIssue.hasTextRange()) { TextRange r = rawIssue.getTextRange(); issue.setStartLine(r.getStartLine()); issue.setStartLineOffset(r.getStartOffset()); issue.setEndLine(r.getEndLine()); issue.setEndLineOffset(r.getEndOffset()); } return issue; }
private static Input<ServerIssueFromWs> createBaseInput(Collection<ServerIssueFromWs> serverIssues, @Nullable SourceHashHolder sourceHashHolder) { List<String> refHashes; if (sourceHashHolder != null && sourceHashHolder.getHashedReference() != null) { refHashes = Arrays.asList(sourceHashHolder.getHashedReference().hashes()); } else { refHashes = new ArrayList<>(0); } return new IssueTrackingInput<>(serverIssues, refHashes); }