Skip to content

Commit a93c294

Browse files
committed
Fix quality flaw: Add unit tests for SonarComponents
1 parent 58e14b8 commit a93c294

1 file changed

Lines changed: 30 additions & 21 deletions

File tree

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@
2121

2222
import com.google.common.collect.ImmutableList;
2323
import com.google.common.collect.Lists;
24-
import org.junit.After;
2524
import org.junit.Before;
2625
import org.junit.Test;
2726
import org.junit.runner.RunWith;
2827
import org.mockito.Mock;
2928
import org.mockito.runners.MockitoJUnitRunner;
29+
import org.sonar.api.batch.fs.InputFile;
3030
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;
34+
import org.sonar.api.issue.Issuable;
35+
import org.sonar.api.measures.FileLinesContext;
3436
import org.sonar.api.measures.FileLinesContextFactory;
37+
import org.sonar.api.source.Highlightable;
38+
import org.sonar.api.source.Symbolizable;
3539
import org.sonar.plugins.java.api.CheckRegistrar;
3640
import org.sonar.plugins.java.api.JavaCheck;
3741
import org.sonar.squidbridge.api.CodeVisitor;
@@ -41,8 +45,10 @@
4145
import java.util.Collection;
4246

4347
import static org.fest.assertions.Assertions.assertThat;
48+
import static org.mockito.Matchers.any;
4449
import static org.mockito.Matchers.anyCollectionOf;
4550
import static org.mockito.Matchers.anyString;
51+
import static org.mockito.Matchers.eq;
4652
import static org.mockito.Mockito.mock;
4753
import static org.mockito.Mockito.times;
4854
import static org.mockito.Mockito.verify;
@@ -72,9 +78,8 @@ public void setUp() {
7278
when(this.checks.addAnnotatedChecks(anyCollectionOf(Class.class))).thenReturn(this.checks);
7379
}
7480

75-
@After
7681
public void postTestExecutionChecks() {
77-
// each time a SonarComponent is instanciated the following methods must be called twice
82+
// each time a SonarComponent is instantiated the following methods must be called twice
7883
// once for custom checks, once for custom java checks
7984
verify(this.checkFactory, times(2)).create(REPOSITORY_NAME);
8085
verify(this.checks, times(2)).addAnnotatedChecks(anyCollectionOf(Class.class));
@@ -87,32 +92,30 @@ public void test_sonar_components() {
8792
JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
8893
ImmutableList<File> javaTestClasspathList = ImmutableList.of();
8994
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-
});
95+
File file = new File("");
96+
Issuable issuable = mock(Issuable.class);
97+
when(resourcePerspectives.as(eq(Issuable.class), any(InputFile.class))).thenReturn(issuable);
98+
Highlightable highlightable = mock(Highlightable.class);
99+
when(resourcePerspectives.as(eq(Highlightable.class), any(InputFile.class))).thenReturn(highlightable);
100+
Symbolizable symbolizable = mock(Symbolizable.class);
101+
when(resourcePerspectives.as(eq(Symbolizable.class), any(InputFile.class))).thenReturn(symbolizable);
102+
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
103+
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
104+
105+
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, resourcePerspectives, fs, null, javaTestClasspath, checkFactory);
106106

107107
CodeVisitor[] visitors = sonarComponents.checkClasses();
108-
assertThat(visitors).hasSize(1);
109-
assertThat(visitors[0]).isEqualTo(expectedCheck);
108+
assertThat(visitors).hasSize(0);
110109
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
111110
assertThat(testChecks).hasSize(0);
112111
assertThat(sonarComponents.getFileSystem()).isEqualTo(fs);
113112
assertThat(sonarComponents.getResourcePerspectives()).isEqualTo(resourcePerspectives);
114113
assertThat(sonarComponents.getJavaClasspath()).isEmpty();
115114
assertThat(sonarComponents.getJavaTestClasspath()).isEqualTo(javaTestClasspathList);
115+
assertThat(sonarComponents.issuableFor(file)).isEqualTo(issuable);
116+
assertThat(sonarComponents.highlightableFor(file)).isEqualTo(highlightable);
117+
assertThat(sonarComponents.symbolizableFor(file)).isEqualTo(symbolizable);
118+
assertThat(sonarComponents.fileLinesContextFor(file)).isEqualTo(fileLinesContext);
116119
}
117120

118121
@Test
@@ -138,6 +141,8 @@ public void register(RegistrarContext registrarContext) {
138141
assertThat(visitors[0]).isEqualTo(expectedCheck);
139142
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
140143
assertThat(testChecks).hasSize(0);
144+
145+
postTestExecutionChecks();
141146
}
142147

143148
@Test
@@ -163,6 +168,8 @@ public void register(RegistrarContext registrarContext) {
163168
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
164169
assertThat(testChecks).hasSize(1);
165170
assertThat(testChecks.iterator().next()).isEqualTo(expectedCheck);
171+
172+
postTestExecutionChecks();
166173
}
167174

168175
@Test
@@ -191,6 +198,8 @@ public void register(RegistrarContext registrarContext) {
191198
assertThat(testChecks).hasSize(1);
192199
assertThat(testChecks.iterator().next()).isEqualTo(expectedTestCheck);
193200
assertThat(sonarComponents.checks()).hasSize(2);
201+
202+
postTestExecutionChecks();
194203
}
195204

196205
private static class CustomCheck implements JavaCheck {

0 commit comments

Comments
 (0)