@Override public void store(Measure measure) { // Emulate duplicate measure check String componentKey = measure.inputComponent().key(); String metricKey = measure.metric().key(); if (measuresByComponentAndMetric.contains(componentKey, metricKey)) { throw new SonarException("Can not add the same measure twice"); } measuresByComponentAndMetric.row(componentKey).put(metricKey, measure); }
// SONAR-6000 @Test public void truncateDuplication() throws IOException { File srcDir = new File(baseDir, "src"); srcDir.mkdir(); String duplicatedStuff = "Sample xoo\n"; int blockCount = 10000; File xooFile1 = new File(srcDir, "sample.xoo"); for (int i = 0; i < blockCount; i++) { FileUtils.write(xooFile1, duplicatedStuff, true); FileUtils.write(xooFile1, "" + i + "\n", true); } TaskResult result = tester .newTask() .properties( builder .put("sonar.sources", "src") .put("sonar.cpd.xoo.minimumTokens", "1") .put("sonar.cpd.xoo.minimumLines", "1") .build()) .start(); Measure duplicatedBlocks = null; for (Measure m : result.allMeasures()) { if (m.metric().key().equals("duplicated_blocks")) { duplicatedBlocks = m; } } assertThat(duplicatedBlocks.value()).isEqualTo(blockCount); List<Duplication> duplicationGroups = result.duplicationsFor(result.inputFile("src/sample.xoo")); assertThat(duplicationGroups).hasSize(1); Duplication cloneGroup = duplicationGroups.get(0); assertThat(cloneGroup.duplicates()).hasSize(100); }