@Before
 public void prepare() throws IOException {
   tester =
       BatchMediumTester.builder()
           .bootstrapProperties(
               ImmutableMap.of(
                   CoreProperties.ANALYSIS_MODE,
                   CoreProperties.ANALYSIS_MODE_ISSUES,
                   CoreProperties.GLOBAL_WORKING_DIRECTORY,
                   temp.newFolder().getAbsolutePath()))
           .registerPlugin("xoo", new XooPlugin())
           .addQProfile("xoo", "Sonar Way")
           .addRules(new XooRulesDefinition())
           .addRule("manual:MyManualIssue", "manual", "MyManualIssue", "My manual issue")
           .addRule("manual:MyManualIssueDup", "manual", "MyManualIssue", "My manual issue")
           .addActiveRule(
               "xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", null, "xoo")
           .addActiveRule(
               "xoo", "OneIssueOnDirPerFile", null, "OneIssueOnDirPerFile", "MAJOR", null, "xoo")
           .addActiveRule(
               "xoo", "OneIssuePerModule", null, "OneIssuePerModule", "MAJOR", null, "xoo")
           .addActiveRule("manual", "MyManualIssue", null, "My manual issue", "MAJOR", null, null)
           .setAssociated(false)
           .build();
   tester.start();
 }
  @Test
  public void testNonAssociated() throws Exception {
    File projectDir = copyProject("/mediumtest/xoo/multi-modules-sample-not-associated");

    TaskResult result =
        tester.newScanTask(new File(projectDir, "sonar-project.properties")).start();
  }
  @Test
  public void computeSyntaxHighlightingOnTempProject() throws IOException {

    File baseDir = temp.newFolder();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();

    File xooFile = new File(srcDir, "sample.xoo");
    File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting");
    FileUtils.write(xooFile, "Sample xoo\ncontent plop");
    FileUtils.write(xoohighlightingFile, "0:10:s\n11:18:k");

    TaskResult result =
        tester
            .newTask()
            .properties(
                ImmutableMap.<String, String>builder()
                    .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
                    .put("sonar.projectKey", "com.foo.project")
                    .put("sonar.projectName", "Foo Project")
                    .put("sonar.projectVersion", "1.0-SNAPSHOT")
                    .put("sonar.projectDescription", "Description of Foo Project")
                    .put("sonar.sources", "src")
                    .build())
            .start();

    InputFile file = result.inputFile("src/sample.xoo");
    assertThat(result.highlightingTypeFor(file, 1, 0)).containsExactly(TypeOfText.STRING);
    assertThat(result.highlightingTypeFor(file, 1, 9)).containsExactly(TypeOfText.STRING);
    assertThat(result.highlightingTypeFor(file, 2, 0)).containsExactly(TypeOfText.KEYWORD);
    assertThat(result.highlightingTypeFor(file, 2, 8)).isEmpty();
  }
  @Before
  public void prepare() throws IOException {
    tester.start();

    baseDir = temp.newFolder();

    builder =
        ImmutableMap.<String, String>builder()
            .put("sonar.task", "scan")
            .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
            .put("sonar.projectKey", "com.foo.project")
            .put("sonar.projectName", "Foo Project")
            .put("sonar.projectVersion", "1.0-SNAPSHOT")
            .put("sonar.projectDescription", "Description of Foo Project");
  }
  @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);
  }
  @Test
  public void computeInvalidOffsets() throws IOException {

    File baseDir = temp.newFolder();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();

    File xooFile = new File(srcDir, "sample.xoo");
    File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting");
    FileUtils.write(xooFile, "Sample xoo\ncontent plop");
    FileUtils.write(xoohighlightingFile, "0:10:s\n18:18:k");

    exception.expect(IllegalStateException.class);
    exception.expectMessage("Error processing line 2");
    exception.expectCause(
        new TypeSafeMatcher<IllegalArgumentException>() {
          @Override
          public void describeTo(Description description) {
            description.appendText("Invalid cause");
          }

          @Override
          protected boolean matchesSafely(IllegalArgumentException e) {
            return e.getMessage().contains("Unable to highlight file");
          }
        });

    TaskResult result =
        tester
            .newTask()
            .properties(
                ImmutableMap.<String, String>builder()
                    .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
                    .put("sonar.projectKey", "com.foo.project")
                    .put("sonar.projectName", "Foo Project")
                    .put("sonar.projectVersion", "1.0-SNAPSHOT")
                    .put("sonar.projectDescription", "Description of Foo Project")
                    .put("sonar.sources", "src")
                    .build())
            .start();
  }
  // 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);
  }
 @After
 public void stop() {
   tester.stop();
 }
 @Before
 public void prepare() {
   tester.start();
 }
public class HighlightingMediumTest {

  @Rule public TemporaryFolder temp = new TemporaryFolder();

  @Rule public ExpectedException exception = ExpectedException.none();

  public BatchMediumTester tester =
      BatchMediumTester.builder()
          .registerPlugin("xoo", new XooPlugin())
          .addDefaultQProfile("xoo", "Sonar Way")
          .build();

  @Before
  public void prepare() {
    tester.start();
  }

  @After
  public void stop() {
    tester.stop();
  }

  @Test
  public void computeSyntaxHighlightingOnTempProject() throws IOException {

    File baseDir = temp.newFolder();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();

    File xooFile = new File(srcDir, "sample.xoo");
    File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting");
    FileUtils.write(xooFile, "Sample xoo\ncontent plop");
    FileUtils.write(xoohighlightingFile, "0:10:s\n11:18:k");

    TaskResult result =
        tester
            .newTask()
            .properties(
                ImmutableMap.<String, String>builder()
                    .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
                    .put("sonar.projectKey", "com.foo.project")
                    .put("sonar.projectName", "Foo Project")
                    .put("sonar.projectVersion", "1.0-SNAPSHOT")
                    .put("sonar.projectDescription", "Description of Foo Project")
                    .put("sonar.sources", "src")
                    .build())
            .start();

    InputFile file = result.inputFile("src/sample.xoo");
    assertThat(result.highlightingTypeFor(file, 1, 0)).containsExactly(TypeOfText.STRING);
    assertThat(result.highlightingTypeFor(file, 1, 9)).containsExactly(TypeOfText.STRING);
    assertThat(result.highlightingTypeFor(file, 2, 0)).containsExactly(TypeOfText.KEYWORD);
    assertThat(result.highlightingTypeFor(file, 2, 8)).isEmpty();
  }

  @Test
  public void computeInvalidOffsets() throws IOException {

    File baseDir = temp.newFolder();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();

    File xooFile = new File(srcDir, "sample.xoo");
    File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting");
    FileUtils.write(xooFile, "Sample xoo\ncontent plop");
    FileUtils.write(xoohighlightingFile, "0:10:s\n18:18:k");

    exception.expect(IllegalStateException.class);
    exception.expectMessage("Error processing line 2");
    exception.expectCause(
        new TypeSafeMatcher<IllegalArgumentException>() {
          @Override
          public void describeTo(Description description) {
            description.appendText("Invalid cause");
          }

          @Override
          protected boolean matchesSafely(IllegalArgumentException e) {
            return e.getMessage().contains("Unable to highlight file");
          }
        });

    TaskResult result =
        tester
            .newTask()
            .properties(
                ImmutableMap.<String, String>builder()
                    .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
                    .put("sonar.projectKey", "com.foo.project")
                    .put("sonar.projectName", "Foo Project")
                    .put("sonar.projectVersion", "1.0-SNAPSHOT")
                    .put("sonar.projectDescription", "Description of Foo Project")
                    .put("sonar.sources", "src")
                    .build())
            .start();
  }
}
  @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());
  }
public class CpdMediumTest {

  @Rule public TemporaryFolder temp = new TemporaryFolder();

  @Rule public ExpectedException thrown = ExpectedException.none();

  public BatchMediumTester tester =
      BatchMediumTester.builder()
          .registerPlugin("xoo", new XooPlugin())
          .addDefaultQProfile("xoo", "Sonar Way")
          .build();

  private File baseDir;

  private ImmutableMap.Builder<String, String> builder;

  @Before
  public void prepare() throws IOException {
    tester.start();

    baseDir = temp.newFolder();

    builder =
        ImmutableMap.<String, String>builder()
            .put("sonar.task", "scan")
            .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
            .put("sonar.projectKey", "com.foo.project")
            .put("sonar.projectName", "Foo Project")
            .put("sonar.projectVersion", "1.0-SNAPSHOT")
            .put("sonar.projectDescription", "Description of Foo Project");
  }

  @After
  public void stop() {
    tester.stop();
  }

  @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());
  }

  // 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 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);
  }
}