aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-squid/src/test/java/org/sonar/squid/indexer/SquidIndexTest.java
blob: c8fe1b30d861dbf936fbe90723f2dff5c64a2bac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2009 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */

package org.sonar.squid.indexer;

import java.util.Collection;

import org.junit.Before;
import org.junit.Test;
import org.sonar.squid.api.SourceClass;
import org.sonar.squid.api.SourceCode;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.api.SourcePackage;
import org.sonar.squid.api.SourceProject;
import org.sonar.squid.indexer.QueryByMeasure.Operator;
import org.sonar.squid.measures.Metric;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class SquidIndexTest {

  private SquidIndex indexer;
  private SourceProject project;
  private SourcePackage packSquid;
  private SourceFile fileSquid;
  private SourceFile file2Squid;
  private SourceCode classSquid;

  @Before
  public void setup() {
    indexer = new SquidIndex();
    project = new SourceProject("Squid Project");
    indexer.index(project);
    packSquid = new SourcePackage("org.sonar.squid");
    project.addChild(packSquid);
    fileSquid = new SourceFile("org.sonar.squid.Squid.java", "Squid.java");
    packSquid.addChild(fileSquid);
    file2Squid = new SourceFile("org.sonar.squid.SquidConfiguration.java", "SquidConfiguration.java");
    packSquid.addChild(file2Squid);
    classSquid = new SourceClass("org.sonar.squid.Squid", "Squid");
    fileSquid.addChild(classSquid);
  }

  @Test
  public void searchSingleResource() {
    SourceCode squidClass = indexer.search("org.sonar.squid.Squid");
    assertEquals(new SourceClass("org.sonar.squid.Squid", "Squid"), squidClass);
    SourceCode javaNCSSClass = indexer.search("org.sonar.squid.JavaNCSS");
    assertNull(javaNCSSClass);
  }

  @Test
  public void searchByType() {
    Collection<SourceCode> resources = indexer.search(new QueryByType(SourceFile.class));
    assertEquals(2, resources.size());
    resources = indexer.search(new QueryByType(SourceClass.class));
    assertEquals(1, resources.size());
    assertTrue(resources.contains(classSquid));
  }
  
  @Test
  public void searchByName() {
    Collection<SourceCode> resources = indexer.search(new QueryByName("Squid.java"));
    assertEquals(1, resources.size());
    assertTrue(resources.contains(fileSquid));
  }

  @Test
  public void searchByParent() {
    Collection<SourceCode> resources = indexer.search(new QueryByParent(packSquid));
    assertEquals(3, resources.size());
  }

  @Test
  public void searchByParentAndByType() {
    Collection<SourceCode> resources = indexer.search(new QueryByParent(packSquid), new QueryByType(SourceClass.class));
    assertEquals(1, resources.size());
    assertTrue(resources.contains(classSquid));
  }

  @Test
  public void searchByMeasure() {
    fileSquid.add(Metric.COMPLEXITY, 2);
    assertEquals(1, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.GREATER_THAN, 1)).size());
    assertEquals(1, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.GREATER_THAN_EQUALS, 2)).size());
    assertEquals(0, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.GREATER_THAN, 3)).size());
    assertEquals(4, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.LESS_THAN, 1)).size());
    assertEquals(5, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.LESS_THAN, 3)).size());
    assertEquals(5, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.LESS_THAN_EQUALS, 2)).size());
    assertEquals(0, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.EQUALS, 6)).size());
    assertEquals(1, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.EQUALS, 2)).size());
  }
}