assertThat(cloneGroupFile1.getOriginPosition().getEndLine()).isEqualTo(17); assertThat(cloneGroupFile1.getDuplicateList()).hasSize(1); assertThat(cloneGroupFile1.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(inputFile2).getRef()); assertThat(cloneGroupFile2.getOriginPosition().getEndLine()).isEqualTo(17); assertThat(cloneGroupFile2.getDuplicateList()).hasSize(1); assertThat(cloneGroupFile2.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(inputFile1).getRef());
@Test public void scanMultiModuleWithBranch() throws IOException { Path srcDir = baseDir.toPath().resolve("moduleA").resolve("src"); Files.createDirectories(srcDir); File xooFile = new File(srcDir.toFile(), "sample.xoo"); FileUtils.write(xooFile, "Sample xoo\ncontent"); AnalysisResult result = tester.newAnalysis() .properties(ImmutableMap.<String, String>builder() .putAll(commonProps) .put("sonar.branch", "branch") .put("sonar.modules", "moduleA") .build()) .execute(); assertThat(result.inputFiles()).hasSize(1); assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA/src/sample.xoo"); // no branch in the report DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("moduleA/src/sample.xoo"); assertThat(result.getReportReader().readComponent(inputfile.scannerId()).getProjectRelativePath()).isEqualTo("moduleA/src/sample.xoo"); assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); result = tester.newAnalysis() .properties(ImmutableMap.<String, String>builder() .putAll(commonProps) .put("sonar.branch", "") .put("sonar.modules", "moduleA") .build()) .execute(); assertThat(result.inputFiles()).hasSize(1); assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA/src/sample.xoo"); }
private Node createProjectHierarchy(ScannerReport.Component rootComponent) { Preconditions.checkArgument(rootComponent.getType() == ScannerReport.Component.ComponentType.PROJECT, "Expected root component of type 'PROJECT'"); LinkedList<ScannerReport.Component> queue = new LinkedList<>(); rootComponent.getChildRefList() .stream() .map(scannerComponentSupplier) .forEach(queue::addLast); Node root = new Node(); root.reportComponent = rootComponent; while (!queue.isEmpty()) { ScannerReport.Component component = queue.removeFirst(); switch (component.getType()) { case FILE: addFile(root, component); break; case MODULE: case DIRECTORY: issueRelocationToRoot.relocate(rootComponent, component); component.getChildRefList().stream() .map(scannerComponentSupplier) .forEach(queue::addLast); break; default: throw new IllegalArgumentException(format("Unsupported component type '%s'", component.getType())); } } return root; }
private ComponentImpl buildComponent(ScannerReport.Component component, ScannerReport.Component closestModule, @Nullable String scmBasePath) { switch (component.getType()) { case PROJECT: String projectKey = keyGenerator.generateKey(component, null); .setKey(projectKey) .setPublicKey(projectPublicKey) .setStatus(convertStatus(component.getStatus())) .setReportAttributes(createAttributesBuilder(component, scmBasePath) .setVersion(createProjectVersion(component)) .setPublicKey(modulePublicKey) .setName(nameOfOthers(component, modulePublicKey)) .setStatus(convertStatus(component.getStatus())) .setDescription(trimToNull(component.getDescription())) .setReportAttributes(createAttributesBuilder(component, scmBasePath).build()) .addChildren(buildChildren(component, component, scmBasePath)) String key = keyGenerator.generateKey(closestModule, component); String publicKey = publicKeyGenerator.generateKey(closestModule, component); return ComponentImpl.builder(convertDirOrFileType(component.getType())) .setUuid(uuidSupplier.apply(key)) .setKey(key) .setPublicKey(publicKey) .setName(nameOfOthers(component, publicKey)) .setStatus(convertStatus(component.getStatus())) .setDescription(trimToNull(component.getDescription())) .setReportAttributes(createAttributesBuilder(component, scmBasePath).build()) .setFileAttributes(createFileAttributes(component))
private void updateSource(Component component) { File sourceFile = reader.getFileStructure().fileFor(Domain.SOURCE, component.getRef()); sourceEditor.setText(""); if (sourceFile.exists()) { try (Scanner s = new Scanner(sourceFile, StandardCharsets.UTF_8.name()).useDelimiter("\\Z")) { if (s.hasNext()) { sourceEditor.setText(s.next()); } } catch (IOException ex) { StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); sourceEditor.setText(errors.toString()); } } }
public List<ScannerReport.Issue> issuesFor(Component reportComponent) { int ref = reportComponent.getRef(); return issuesFor(ref); }
private static String nameOfOthers(ScannerReport.Component reportComponent, String defaultName) { String name = trimToNull(reportComponent.getName()); return name == null ? defaultName : name; }
private void updateDuplications(Component component) { duplicationEditor.setText(""); if (reader.hasCoverage(component.getRef())) { try (CloseableIterator<ScannerReport.Duplication> it = reader.readComponentDuplications(component.getRef())) { while (it.hasNext()) { ScannerReport.Duplication dup = it.next(); duplicationEditor.getDocument().insertString(duplicationEditor.getDocument().getEndPosition().getOffset(), dup + "\n", null); } } catch (Exception e) { throw new IllegalStateException("Can't read duplications for " + getNodeName(component), e); } } }
private void updateCpdTextBlocks(Component component) { cpdTextBlocksEditor.setText(""); if (reader.hasCoverage(component.getRef())) { try (CloseableIterator<ScannerReport.CpdTextBlock> it = reader.readCpdTextBlocks(component.getRef())) { while (it.hasNext()) { ScannerReport.CpdTextBlock textBlock = it.next(); cpdTextBlocksEditor.getDocument().insertString(cpdTextBlocksEditor.getDocument().getEndPosition().getOffset(), textBlock + "\n", null); } } catch (Exception e) { throw new IllegalStateException("Can't read CPD text blocks for " + getNodeName(component), e); } } }
@Override public String generateKey(Component module, @Nullable Component component) { String mainBranchKey = component == null ? module.getKey() : ComponentKeys.createEffectiveKey(module.getKey(), StringUtils.trimToNull(component.getPath())); return main ? mainBranchKey : mainBranchKey + ComponentDto.BRANCH_KEY_SEPARATOR + branchName; }
private void setNameAndDescription(ScannerReport.Component component, ComponentImpl.Builder builder) { if (branch.isMain()) { builder .setName(nameOfProject(component)) .setDescription(component.getDescription()); } else { builder .setName(project.getName()) .setDescription(project.getDescription()); } }
private void updateHighlighting(Component component) { highlightingEditor.setText(""); try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(component.getRef())) { while (it.hasNext()) { ScannerReport.SyntaxHighlightingRule rule = it.next(); highlightingEditor.getDocument().insertString(highlightingEditor.getDocument().getEndPosition().getOffset(), rule + "\n", null); } } catch (Exception e) { throw new IllegalStateException("Can't read syntax highlighting for " + getNodeName(component), e); } }
private String nameOfProject(ScannerReport.Component component) { String name = trimToNull(component.getName()); if (name != null) { return name; } return project.getName(); }
private List<Component> buildChildren(ScannerReport.Component component, ScannerReport.Component parentModule, String projectScmPath) { return component.getChildRefList() .stream() .map(scannerComponentSupplier::apply) .map(c -> buildComponent(c, parentModule, projectScmPath)) .collect(Collectors.toList()); }
private void validateBatchKey(Component rawComponent) { String batchKey = reportReader.readComponent(rawComponent.getReportAttributes().getRef()).getKey(); if (!ComponentKeys.isValidModuleKey(batchKey)) { validationMessages.add(format("\"%s\" is not a valid project or module key. " + "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", batchKey)); } }
private void loadChildren(Component parentComponent, DefaultMutableTreeNode parentNode) { for (int ref : parentComponent.getChildRefList()) { Component child = reader.readComponent(ref); DefaultMutableTreeNode childNode = createNode(child); parentNode.add(childNode); loadChildren(child, childNode); } }
private void updateSymbols(Component component) { symbolEditor.setText(""); try (CloseableIterator<ScannerReport.Symbol> it = reader.readComponentSymbols(component.getRef())) { while (it.hasNext()) { ScannerReport.Symbol symbol = it.next(); symbolEditor.getDocument().insertString(symbolEditor.getDocument().getEndPosition().getOffset(), symbol + "\n", null); } } catch (Exception e) { throw new IllegalStateException("Can't read symbol references for " + getNodeName(component), e); } }
private void updateMeasures(Component component) { measuresEditor.setText(""); try (CloseableIterator<ScannerReport.Measure> it = reader.readComponentMeasures(component.getRef())) { while (it.hasNext()) { ScannerReport.Measure measure = it.next(); measuresEditor.getDocument().insertString(measuresEditor.getDocument().getEndPosition().getOffset(), measure + "\n", null); } } catch (Exception e) { throw new IllegalStateException("Can't read measures for " + getNodeName(component), e); } }
private void updateCoverage(Component component) { coverageEditor.setText(""); try (CloseableIterator<ScannerReport.LineCoverage> it = reader.readComponentCoverage(component.getRef())) { while (it.hasNext()) { ScannerReport.LineCoverage coverage = it.next(); coverageEditor.getDocument().insertString(coverageEditor.getDocument().getEndPosition().getOffset(), coverage + "\n", null); } } catch (Exception e) { throw new IllegalStateException("Can't read code coverage for " + getNodeName(component), e); } }
public File writeComponent(ScannerReport.Component component) { File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, component.getRef()); Protobuf.write(component, file); return file; }