@Test
  public void testIntraFileDuplications() throws IOException {
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();

    String content = "Sample xoo\ncontent\nfoo\nbar\nSample xoo\ncontent\n";

    File xooFile = new File(srcDir, "sample.xoo");
    FileUtils.write(xooFile, content);

    TaskResult result =
        tester
            .newTask()
            .properties(
                builder
                    .put("sonar.sources", "src")
                    .put("sonar.cpd.xoo.minimumTokens", "2")
                    .put("sonar.cpd.xoo.minimumLines", "2")
                    .put("sonar.verbose", "true")
                    .build())
            .start();

    assertThat(result.measures("com.foo.project:src/sample.xoo"))
        .extracting("metric.key", "value")
        .contains(
            tuple("duplicated_blocks", 2),
            tuple("duplicated_files", 1),
            tuple("duplicated_lines", 4));

    InputFile inputFile = result.inputFile("src/sample.xoo");
    // One clone group
    List<Duplication> duplicationGroups = result.duplicationsFor(inputFile);
    assertThat(duplicationGroups).hasSize(1);

    Duplication cloneGroup = duplicationGroups.get(0);
    assertThat(cloneGroup.duplicates()).hasSize(1);
    assertThat(cloneGroup.originBlock().startLine()).isEqualTo(1);
    assertThat(cloneGroup.originBlock().length()).isEqualTo(2);
    assertThat(cloneGroup.duplicates()).hasSize(1);
    assertThat(cloneGroup.duplicates().get(0).startLine()).isEqualTo(5);
    assertThat(cloneGroup.duplicates().get(0).length()).isEqualTo(2);
  }
  // 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);
  }
  @Test
  public void testCrossFileDuplications() throws IOException {
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();

    String duplicatedStuff =
        "Sample xoo\ncontent\nfoo\nbar\ntoto\ntiti\nfoo\nbar\ntoto\ntiti\nbar\ntoto\ntiti\nfoo\nbar\ntoto\ntiti";

    File xooFile1 = new File(srcDir, "sample1.xoo");
    FileUtils.write(xooFile1, duplicatedStuff);

    File xooFile2 = new File(srcDir, "sample2.xoo");
    FileUtils.write(xooFile2, duplicatedStuff);

    TaskResult result =
        tester
            .newTask()
            .properties(
                builder
                    .put("sonar.sources", "src")
                    .put("sonar.cpd.xoo.minimumTokens", "10")
                    .put("sonar.verbose", "true")
                    .build())
            .start();

    assertThat(result.inputFiles()).hasSize(2);

    assertThat(result.measures("com.foo.project:src/sample1.xoo"))
        .extracting("metric.key", "value")
        .contains(
            tuple("duplicated_blocks", 1),
            tuple("duplicated_files", 1),
            tuple("duplicated_lines", 17));

    InputFile inputFile1 = result.inputFile("src/sample1.xoo");
    InputFile inputFile2 = result.inputFile("src/sample2.xoo");
    // One clone group on each file
    List<Duplication> duplicationGroupsFile1 = result.duplicationsFor(inputFile1);
    assertThat(duplicationGroupsFile1).hasSize(1);

    Duplication cloneGroupFile1 = duplicationGroupsFile1.get(0);
    assertThat(cloneGroupFile1.duplicates()).hasSize(1);
    assertThat(cloneGroupFile1.originBlock().startLine()).isEqualTo(1);
    assertThat(cloneGroupFile1.originBlock().length()).isEqualTo(17);
    assertThat(cloneGroupFile1.originBlock().resourceKey())
        .isEqualTo(((DefaultInputFile) inputFile1).key());
    assertThat(cloneGroupFile1.duplicates()).hasSize(1);
    assertThat(cloneGroupFile1.duplicates().get(0).resourceKey())
        .isEqualTo(((DefaultInputFile) inputFile2).key());

    List<Duplication> duplicationGroupsFile2 = result.duplicationsFor(inputFile2);
    assertThat(duplicationGroupsFile2).hasSize(1);

    Duplication cloneGroupFile2 = duplicationGroupsFile2.get(0);
    assertThat(cloneGroupFile2.duplicates()).hasSize(1);
    assertThat(cloneGroupFile2.originBlock().startLine()).isEqualTo(1);
    assertThat(cloneGroupFile2.originBlock().length()).isEqualTo(17);
    assertThat(cloneGroupFile2.originBlock().resourceKey())
        .isEqualTo(((DefaultInputFile) inputFile2).key());
    assertThat(cloneGroupFile2.duplicates()).hasSize(1);
    assertThat(cloneGroupFile2.duplicates().get(0).resourceKey())
        .isEqualTo(((DefaultInputFile) inputFile1).key());
  }