@Test
  public void shouldReturnViolationsOfEachLevelForFile() {
    Review review = getReview(REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY);

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations())
        .hasSize(3)
        .containsOnly(
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY,
                5,
                "ForLoopShouldBeWhileLoop: The for loop can be simplified to a while loop",
                Severity.ERROR),
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY,
                9,
                "AssertWithinFinallyBlock: A finally block within class FileWithOneViolationPerEachLevel contains an assert statement, potentially hiding the original exception, if there is one",
                Severity.WARNING),
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY,
                13,
                "EmptyMethod: Violation in class FileWithOneViolationPerEachLevel. The method bar is both empty and not marked with @Override",
                Severity.INFO));
  }
  @Test
  public void shouldReturnViolationsUsingImportAndBasicRuleSets() {
    config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_BASIC_AND_IMPORT_RULE_SET);
    sut = new CodeNarcProcessor(config);
    Review review = getReview(REVIEW_FILE_WITH_ONE_VIOLATION, REVIEW_FILE_WITH_IMPORT_VIOLATION);

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations())
        .hasSize(4)
        .containsOnly(
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION,
                5,
                "EmptyTryBlock: The try block is empty",
                Severity.WARNING),
            new Violation(
                REVIEW_FILE_WITH_IMPORT_VIOLATION,
                2,
                "EmptyClass: Class 'FileWithoutViolations' is empty (has no methods, fields or properties). Why would you need a class like this?",
                Severity.WARNING),
            new Violation(
                REVIEW_FILE_WITH_IMPORT_VIOLATION, 1, "UnnecessaryGroovyImport", Severity.INFO),
            new Violation(
                REVIEW_FILE_WITH_IMPORT_VIOLATION,
                1,
                "UnusedImport: The [java.util.ArrayList] import is never referenced",
                Severity.INFO));
  }
  @Test
  public void shouldReturnViolationsUsingDefaultRuleSetFromResources() {
    config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITHOUT_RULE_SET);
    sut = new CodeNarcProcessor(config);
    Review review = getReview(REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY);

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations())
        .hasSize(3)
        .containsOnly(
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY,
                5,
                "ForLoopShouldBeWhileLoop: The for loop can be simplified to a while loop",
                Severity.INFO),
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY,
                9,
                "AssertWithinFinallyBlock: A finally block within class FileWithOneViolationPerEachLevel contains an assert statement, potentially hiding the original exception, if there is one",
                Severity.WARNING),
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY,
                13,
                "EmptyMethod: Violation in class FileWithOneViolationPerEachLevel. The method bar is both empty and not marked with @Override",
                Severity.WARNING));
  }
示例#4
0
  @Test
  public void shouldReturnNoViolationsWhenThereIsNoFileToReview() {
    // when
    ReviewResult reviewResult = fixture.process(nonexistantReview());

    // then
    assertThat(reviewResult).isNotNull();
    assertThat(reviewResult.getViolations()).isEmpty();
  }
  @Test
  public void shouldReturnNoViolationsForPerfectFile() {
    Review review = getReview(REVIEW_FILE_WITHOUT_VIOLATIONS);

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations()).isEmpty();
  }
  @Test
  public void shouldReturnNoViolationsForFileWithoutExtension() {
    Review review = getReview(REVIEW_FILE_WITH_NOT_GROOVY_EXTENSION);

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations()).isEmpty();
  }
  @Test
  public void shouldReturnNoViolationsWhenNoFiles() {
    Review review = getReview();

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations()).isEmpty();
  }
  @Test
  public void shouldReturnScalastyleViolations() {
    // when
    ReviewResult reviewResult = fixture.process(review("scala/Point.scala"));

    // then
    assertThat(reviewResult.getViolations())
        .isNotEmpty()
        .hasSize(1)
        .extracting("message")
        .contains("Header does not match expected text");
  }
  @Test
  public void shouldReturnSomeViolationsForFile() {
    Review review = getReview(REVIEW_FILE_WITH_ONE_VIOLATION);

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations())
        .hasSize(1)
        .contains(
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION,
                5,
                "EmptyTryBlock: The try block is empty",
                Severity.WARNING));
  }
  @Test
  public void shouldReturnViolationsUsingImportRuleSet() {
    config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_IMPORT_RULE_SET);
    sut = new CodeNarcProcessor(config);
    Review review = getReview(REVIEW_FILE_WITH_IMPORT_VIOLATION, REVIEW_FILE_WITH_ONE_VIOLATION);

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations())
        .hasSize(2)
        .containsOnly(
            new Violation(
                REVIEW_FILE_WITH_IMPORT_VIOLATION, 1, "UnnecessaryGroovyImport", Severity.INFO),
            new Violation(
                REVIEW_FILE_WITH_IMPORT_VIOLATION,
                1,
                "UnusedImport: The [java.util.ArrayList] import is never referenced",
                Severity.INFO));
  }
  @Test
  public void shouldNotReturnViolationsFromExcudedFiles() {
    config =
        ConfigurationBuilder.initFromResource(
            CONFIGURATION_WITH_BASIC_AND_IMPORT_RULE_SET_AND_EXCLUDE);
    sut = new CodeNarcProcessor(config);
    Review review =
        getReview(
            REVIEW_FILE_WITH_ONE_VIOLATION,
            REVIEW_FILE_WITHOUT_VIOLATIONS,
            REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY);

    ReviewResult result = sut.process(review);

    assertThat(result).isNotNull();
    assertThat(result.getViolations())
        .hasSize(1)
        .containsOnly(
            new Violation(
                REVIEW_FILE_WITH_ONE_VIOLATION,
                5,
                "EmptyTryBlock: The try block is empty",
                Severity.WARNING));
  }