1919 */
2020package org .sonar .java ;
2121
22+ import com .google .common .collect .ImmutableList ;
2223import com .google .common .collect .Lists ;
23- import java .util .ArrayList ;
24- import java .util .Collection ;
2524import org .junit .After ;
2625import org .junit .Before ;
2726import org .junit .Test ;
2827import org .junit .runner .RunWith ;
2928import org .mockito .Mock ;
3029import org .mockito .runners .MockitoJUnitRunner ;
30+ import org .sonar .api .batch .fs .internal .DefaultFileSystem ;
3131import org .sonar .api .batch .rule .CheckFactory ;
3232import org .sonar .api .batch .rule .Checks ;
3333import org .sonar .api .component .ResourcePerspectives ;
3636import org .sonar .plugins .java .api .JavaCheck ;
3737import org .sonar .squidbridge .api .CodeVisitor ;
3838
39+ import java .io .File ;
40+ import java .util .ArrayList ;
41+ import java .util .Collection ;
42+
3943import static org .fest .assertions .Assertions .assertThat ;
4044import static org .mockito .Matchers .anyCollectionOf ;
4145import static org .mockito .Matchers .anyString ;
46+ import static org .mockito .Mockito .mock ;
4247import static org .mockito .Mockito .times ;
4348import static org .mockito .Mockito .verify ;
4449import 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 {
0 commit comments