Пример #1
0
  @Before
  public void createIndex() {
    lock = new DefaultResourceCreationLock(new Settings());
    MetricFinder metricFinder = mock(MetricFinder.class);
    when(metricFinder.findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
    ruleFinder = mock(RuleFinder.class);

    index =
        new DefaultIndex(
            mock(PersistenceManager.class),
            lock,
            mock(ProjectTree.class),
            metricFinder,
            ruleFinder,
            mock(ScanGraph.class));
    Project project = new Project("project");

    ResourceFilter filter =
        new ResourceFilter() {

          public boolean isIgnored(Resource resource) {
            return StringUtils.containsIgnoreCase(resource.getKey(), "excluded");
          }
        };
    RulesProfile rulesProfile = RulesProfile.create();
    rule = Rule.create("repoKey", "ruleKey", "Rule");
    rule.setId(1);
    rulesProfile.activateRule(rule, null);
    index.setCurrentProject(
        project,
        new ResourceFilters(new ResourceFilter[] {filter}),
        new ViolationFilters(),
        rulesProfile);
    index.doStart(project);
  }
Пример #2
0
  /** See http://jira.codehaus.org/browse/SONAR-2107 */
  @Test
  public void shouldNotFailWhenSavingViolationOnNullRule() {
    File file = new File("org/foo/Bar.java");
    Violation violation = Violation.create((Rule) null, file);
    index.addViolation(violation);

    assertThat(index.getViolations(file).size(), is(0));
  }
Пример #3
0
 @Test
 public void shouldBeExcluded() {
   File file = new File("org/foo/ExcludedBar.java");
   assertThat(index.index(file), is(false));
   assertThat(index.isIndexed(file, true), is(true));
   assertThat(index.isIndexed(file, false), is(false));
   assertThat(index.isExcluded(file), is(true));
 }
Пример #4
0
  @Test
  public void shouldIndexResourceWhenAddingMeasure() {
    Resource dir = new Directory("org/foo");
    index.addMeasure(dir, new Measure("ncloc").setValue(50.0));

    assertThat(index.isIndexed(dir, true), is(true));
    assertThat(index.getMeasures(dir, MeasuresFilters.metric("ncloc")).getIntValue(), is(50));
  }
Пример #5
0
  /** Only a warning is logged when index is locked. */
  @Test
  public void shouldIndexEvenIfLocked() {
    lock.lock();

    Directory dir = new Directory("org/foo");
    assertThat(index.index(dir), is(true));
    assertThat(index.isIndexed(dir, true), is(true));
  }
Пример #6
0
  /** See https://jira.codehaus.org/browse/SONAR-3583 */
  @Test
  public void should_ignore_violation_on_unknown_rules() {
    Rule ruleWithoutID = Rule.create("repoKey", "ruleKey", "Rule");

    File file = new File("org/foo/Bar.java");
    Violation violation = Violation.create(ruleWithoutID, file);
    index.addViolation(violation);

    assertThat(index.getViolations(file).size(), is(0));
  }
Пример #7
0
  @Test
  public void shouldIndexLibraryOutsideProjectTree() {
    Library lib = new Library("junit", "4.8");
    assertThat(index.index(lib), is(true));

    Library reference = new Library("junit", "4.8");
    assertThat(index.getResource(reference).getQualifier(), is(Qualifiers.LIBRARY));
    assertThat(index.isIndexed(reference, true), is(true));
    assertThat(index.isExcluded(reference), is(false));
  }
Пример #8
0
  @Test
  public void shouldCreateUID() {
    Project project = new Project("my_project");
    assertThat(DefaultIndex.createUID(project, project), is("my_project"));

    JavaPackage javaPackage = new JavaPackage("org.foo");
    assertThat(DefaultIndex.createUID(project, javaPackage), is("my_project:org.foo"));

    Library library = new Library("junit:junit", "4.7");
    assertThat(DefaultIndex.createUID(project, library), is("junit:junit"));
  }
Пример #9
0
  @Test
  public void testGetViolations() {
    File file = new File("org/foo/Bar.java");
    Violation violation1 = Violation.create(rule, file);
    index.addViolation(violation1);
    Violation violation2 = Violation.create(rule, file);
    violation2.setSwitchedOff(true);
    index.addViolation(violation2);
    Violation violation3 = Violation.create(rule, file);
    violation3.setSwitchedOff(true);
    index.addViolation(violation3);

    assertThat(index.getViolations(file).size(), is(1));
  }
Пример #10
0
  /** See https://jira.codehaus.org/browse/SONAR-3583 */
  @Test
  public void should_support_violations_with_missing_rule_id() {
    Rule ruleWithoutId = Rule.create("repoKey", "ruleKey", "Rule");
    Rule ruleWithId = Rule.create("repoKey", "ruleKey", "Rule");
    ruleWithId.setId(123);
    when(ruleFinder.findByKey("repoKey", "ruleKey")).thenReturn(ruleWithId);

    File file = new File("org/foo/Bar.java");
    Violation violation = Violation.create(ruleWithoutId, file);
    index.addViolation(violation);

    List<Violation> violations = index.getViolations(file);
    assertThat(violations.size(), is(1));
    assertThat(violations.get(0).getRule().getId(), Matchers.is(123));
  }
Пример #11
0
  @Test(expected = SonarException.class)
  public void shouldFailIfIndexingAndLocked() {
    lock.setFailWhenLocked(true);
    lock.lock();

    Directory dir = new Directory("org/foo");
    index.index(dir);
  }
Пример #12
0
  @Test
  public void shouldIndexParentOfDeprecatedFiles() {
    File file = new File("org/foo/Bar.java");
    assertThat(index.index(file), is(true));

    Directory reference = new Directory("org/foo");
    assertThat(index.getResource(reference).getName(), is("org/foo"));
    assertThat(index.isIndexed(reference, true), is(true));
    assertThat(index.isExcluded(reference), is(false));
    assertThat(index.getChildren(reference).size(), is(1));
    assertThat(index.getParent(reference), is(Project.class));
  }
Пример #13
0
  @Test
  public void shouldIndexTreeOfResources() {
    Directory directory = new Directory("org/foo");
    File file = new File("org/foo/Bar.java");
    file.setLanguage(Java.INSTANCE);

    assertThat(index.index(directory), is(true));
    assertThat(index.index(file, directory), is(true));

    File fileRef = new File("org/foo/Bar.java");
    assertThat(index.getResource(fileRef).getKey(), is("org/foo/Bar.java"));
    assertThat(index.getResource(fileRef).getLanguage(), is((Language) Java.INSTANCE));
    assertThat(index.isIndexed(fileRef, true), is(true));
    assertThat(index.isExcluded(fileRef), is(false));
    assertThat(index.getChildren(fileRef).size(), is(0));
    assertThat(index.getParent(fileRef), is(Directory.class));
  }
Пример #14
0
  @Test
  public void shouldNotIndexResourceIfParentNotIndexed() {
    Directory directory = new Directory("org/other");
    File file = new File("org/foo/Bar.java");

    assertThat(index.index(file, directory), is(false));

    File fileRef = new File("org/foo/Bar.java");
    assertThat(index.isIndexed(directory, true), is(false));
    assertThat(index.isIndexed(fileRef, true), is(false));
    assertThat(index.isExcluded(fileRef), is(false));
    assertThat(index.getChildren(fileRef).size(), is(0));
    assertThat(index.getParent(fileRef), nullValue());
  }
Пример #15
0
 @Test(expected = IllegalArgumentException.class)
 public void testGetViolationsWithQueryWithNoResource() {
   index.getViolations(ViolationQuery.create());
 }