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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* 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.batch.index;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.ResourceFilter;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilters;
import org.sonar.api.measures.MetricFinder;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.File;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.JavaPackage;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Library;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
import org.sonar.api.violations.ViolationQuery;
import org.sonar.batch.DefaultResourceCreationLock;
import org.sonar.batch.ProjectTree;
import org.sonar.batch.ResourceFilters;
import org.sonar.batch.ViolationFilters;
import org.sonar.core.component.ScanGraph;
public class DefaultIndexTest {
private DefaultIndex index = null;
private DefaultResourceCreationLock lock;
private Rule rule;
@Before
public void createIndex() {
lock = new DefaultResourceCreationLock(new Settings());
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
index = new DefaultIndex(mock(PersistenceManager.class), lock, mock(ProjectTree.class), metricFinder, 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");
rulesProfile.activateRule(rule, null);
index.setCurrentProject(project, new ResourceFilters(new ResourceFilter[] { filter }), new ViolationFilters(), rulesProfile);
index.doStart(project);
}
@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"));
}
@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));
}
@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));
}
@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));
}
@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());
}
/**
* 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));
}
@Test(expected = SonarException.class)
public void shouldFailIfIndexingAndLocked() {
lock.setFailWhenLocked(true);
lock.lock();
Directory dir = new Directory("org/foo");
index.index(dir);
}
@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));
}
@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));
}
/**
* 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));
}
@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));
}
@Test
public void testGetViolationsWithQuery() {
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(ViolationQuery.create().forResource(file).setSwitchedOff(true)).size(), is(2));
}
@Test(expected = IllegalArgumentException.class)
public void testGetViolationsWithQueryWithNoResource() {
index.getViolations(ViolationQuery.create());
}
}
|