Skip to content

Commit e0fd42b

Browse files
committed
Fix quality flaws: add unit tests for java-squid
1 parent 8347ba4 commit e0fd42b

4 files changed

Lines changed: 129 additions & 7 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012 SonarSource
4+
* sonarqube@googlegroups.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonar.java;
21+
22+
import org.junit.Test;
23+
import org.sonar.plugins.java.api.JavaCheck;
24+
25+
import java.io.File;
26+
27+
import static org.fest.assertions.Assertions.assertThat;
28+
import static org.mockito.Mockito.mock;
29+
30+
public class JavaCheckMessageTest {
31+
32+
@Test
33+
public void testAnalyzerMessage() {
34+
JavaCheck check = mock(JavaCheck.class);
35+
String messageString = "message";
36+
JavaCheckMessage javaCheckMessage = new JavaCheckMessage(check, messageString);
37+
AnalyzerMessage analyzerMessage = new AnalyzerMessage(check, new File("a"), 1, messageString, 0);
38+
javaCheckMessage.setAnalyzerMessage(analyzerMessage);
39+
assertThat(javaCheckMessage.getAnalyzerMessage()).isEqualTo(analyzerMessage);
40+
}
41+
}

java-squid/src/test/java/org/sonar/java/SonarComponentsTest.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
*/
2020
package org.sonar.java;
2121

22+
import com.google.common.collect.ImmutableList;
2223
import com.google.common.collect.Lists;
23-
import java.util.ArrayList;
24-
import java.util.Collection;
2524
import org.junit.After;
2625
import org.junit.Before;
2726
import org.junit.Test;
2827
import org.junit.runner.RunWith;
2928
import org.mockito.Mock;
3029
import org.mockito.runners.MockitoJUnitRunner;
30+
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
3131
import org.sonar.api.batch.rule.CheckFactory;
3232
import org.sonar.api.batch.rule.Checks;
3333
import org.sonar.api.component.ResourcePerspectives;
@@ -36,9 +36,14 @@
3636
import org.sonar.plugins.java.api.JavaCheck;
3737
import org.sonar.squidbridge.api.CodeVisitor;
3838

39+
import java.io.File;
40+
import java.util.ArrayList;
41+
import java.util.Collection;
42+
3943
import static org.fest.assertions.Assertions.assertThat;
4044
import static org.mockito.Matchers.anyCollectionOf;
4145
import static org.mockito.Matchers.anyString;
46+
import static org.mockito.Mockito.mock;
4247
import static org.mockito.Mockito.times;
4348
import static org.mockito.Mockito.verify;
4449
import static org.mockito.Mockito.when;
@@ -76,6 +81,40 @@ public void postTestExecutionChecks() {
7681
verify(this.checks, times(2)).all();
7782
}
7883

84+
@Test
85+
public void test_sonar_components() {
86+
DefaultFileSystem fs = new DefaultFileSystem(new File(""));
87+
JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
88+
ImmutableList<File> javaTestClasspathList = ImmutableList.of();
89+
when(javaTestClasspath.getElements()).thenReturn(javaTestClasspathList);
90+
91+
JavaCheck expectedCheck = new CustomCheck();
92+
CheckRegistrar expectedRegistrar = new CheckRegistrar() {
93+
@Override
94+
public void register(RegistrarContext registrarContext) {
95+
registrarContext.registerClassesForRepository(
96+
REPOSITORY_NAME,
97+
Lists.<Class<? extends JavaCheck>>newArrayList(CustomTestCheck.class),
98+
null);
99+
}
100+
};
101+
102+
when(this.checks.all()).thenReturn(Lists.newArrayList(expectedCheck)).thenReturn(new ArrayList<JavaCheck>());
103+
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, resourcePerspectives, fs, null, javaTestClasspath, checkFactory, new CheckRegistrar[] {
104+
expectedRegistrar
105+
});
106+
107+
CodeVisitor[] visitors = sonarComponents.checkClasses();
108+
assertThat(visitors).hasSize(1);
109+
assertThat(visitors[0]).isEqualTo(expectedCheck);
110+
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
111+
assertThat(testChecks).hasSize(0);
112+
assertThat(sonarComponents.getFileSystem()).isEqualTo(fs);
113+
assertThat(sonarComponents.getResourcePerspectives()).isEqualTo(resourcePerspectives);
114+
assertThat(sonarComponents.getJavaClasspath()).isEmpty();
115+
assertThat(sonarComponents.getJavaTestClasspath()).isEqualTo(javaTestClasspathList);
116+
}
117+
79118
@Test
80119
public void creation_of_custom_checks() {
81120
JavaCheck expectedCheck = new CustomCheck();
@@ -89,7 +128,7 @@ public void register(RegistrarContext registrarContext) {
89128
}
90129
};
91130

92-
when(this.checks.all()).thenReturn(Lists.<JavaCheck>newArrayList(expectedCheck)).thenReturn(new ArrayList<JavaCheck>());
131+
when(this.checks.all()).thenReturn(Lists.newArrayList(expectedCheck)).thenReturn(new ArrayList<JavaCheck>());
93132
SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, this.resourcePerspectives, null, null, null, this.checkFactory, new CheckRegistrar[] {
94133
expectedRegistrar
95134
});
@@ -114,7 +153,7 @@ public void register(RegistrarContext registrarContext) {
114153
}
115154
};
116155

117-
when(this.checks.all()).thenReturn(new ArrayList<JavaCheck>()).thenReturn(Lists.<JavaCheck>newArrayList(expectedCheck));
156+
when(this.checks.all()).thenReturn(new ArrayList<JavaCheck>()).thenReturn(Lists.newArrayList(expectedCheck));
118157
SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, this.resourcePerspectives, null, null, null, this.checkFactory, new CheckRegistrar[] {
119158
expectedRegistrar
120159
});
@@ -140,7 +179,7 @@ public void register(RegistrarContext registrarContext) {
140179
}
141180
};
142181

143-
when(this.checks.all()).thenReturn(Lists.<JavaCheck>newArrayList(expectedCheck)).thenReturn(Lists.<JavaCheck>newArrayList(expectedTestCheck));
182+
when(this.checks.all()).thenReturn(Lists.newArrayList(expectedCheck)).thenReturn(Lists.newArrayList(expectedTestCheck));
144183
SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, this.resourcePerspectives, null, null, null, this.checkFactory, new CheckRegistrar[] {
145184
expectedRegistrar
146185
});
@@ -151,6 +190,7 @@ public void register(RegistrarContext registrarContext) {
151190
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
152191
assertThat(testChecks).hasSize(1);
153192
assertThat(testChecks.iterator().next()).isEqualTo(expectedTestCheck);
193+
assertThat(sonarComponents.checks()).hasSize(2);
154194
}
155195

156196
private static class CustomCheck implements JavaCheck {

java-squid/src/test/java/org/sonar/plugins/java/api/IssuableSubscriptionVisitorTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void test_custom_rules_report_issues() throws Exception {
4848
Collection<SourceCode> sourceCodes = javaSquid.search(new QueryByType(SourceFile.class));
4949
assertThat(sourceCodes).hasSize(1);
5050
SourceCode sourceCode = sourceCodes.iterator().next();
51-
assertThat(sourceCode.getCheckMessages()).hasSize(3);
51+
assertThat(sourceCode.getCheckMessages()).hasSize(6);
5252
}
5353

5454
private static class CustomRule extends IssuableSubscriptionVisitor {
@@ -62,7 +62,10 @@ public List<Tree.Kind> nodesToVisit() {
6262
public void visitNode(Tree tree) {
6363
addIssue(tree, "issue on tree");
6464
addIssue(1, "issue on 1st line");
65+
addIssue(tree, "message", 0d);
6566
addIssueOnFile("issue on file");
67+
reportIssue(tree, "issue on tree");
68+
reportIssue(tree, "issue on tree", ImmutableList.<JavaFileScannerContext.Location>of(), null);
6669
}
6770
}
68-
}
71+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012 SonarSource
4+
* sonarqube@googlegroups.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonar.plugins.java.api;
21+
22+
import org.junit.Test;
23+
import org.sonar.plugins.java.api.tree.Tree;
24+
25+
import static org.fest.assertions.Assertions.assertThat;
26+
import static org.mockito.Mockito.mock;
27+
28+
public class LocationTest {
29+
30+
@Test
31+
public void testLocation() throws Exception {
32+
String message = "message";
33+
Tree node = mock(Tree.class);
34+
JavaFileScannerContext.Location location = new JavaFileScannerContext.Location(message, node);
35+
assertThat(location.msg).isEqualTo(message);
36+
assertThat(location.syntaxNode).isEqualTo(node);
37+
}
38+
}

0 commit comments

Comments
 (0)