19
19
*/
20
20
package org .sonar .java .checks ;
21
21
22
+ import java .io .ByteArrayInputStream ;
22
23
import java .io .IOException ;
23
24
import java .io .InputStream ;
25
+ import java .security .NoSuchAlgorithmException ;
24
26
import org .junit .jupiter .api .BeforeEach ;
25
27
import org .junit .jupiter .api .Test ;
26
28
import org .junit .jupiter .api .extension .RegisterExtension ;
29
31
import org .sonar .api .utils .log .LogTesterJUnit5 ;
30
32
import org .sonar .api .utils .log .LoggerLevel ;
31
33
import org .sonar .java .AnalysisException ;
32
- import org .sonar .java .caching .CacheReadException ;
34
+ import org .sonar .java .caching .FileHashingUtils ;
35
+ import org .sonar .java .checks .helpers .HashCacheTestHelper ;
33
36
import org .sonar .java .checks .verifier .CheckVerifier ;
34
37
import org .sonar .java .checks .verifier .internal .InternalReadCache ;
35
38
import org .sonar .java .checks .verifier .internal .InternalWriteCache ;
@@ -106,19 +109,24 @@ void defaultPackage() {
106
109
107
110
@ Test
108
111
void caching () throws IOException , ClassNotFoundException {
112
+ String changedFilePath1 = mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/package-info.java" );
113
+ String changedFilePath2 = mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFiles/package-info.java" );
109
114
verifier
110
115
.onFiles (
111
116
mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java" ),
112
117
mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld2.java" ),
113
- mainCodeSourcesPath ( "checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/package-info.java" ) ,
114
- mainCodeSourcesPath ( "checks/UselessPackageInfoCheck/packageWithNoOtherFiles/package-info.java" )
118
+ changedFilePath1 ,
119
+ changedFilePath2
115
120
)
116
121
.withCheck (new UselessPackageInfoCheck ())
122
+ .withCache (readCache , writeCache )
117
123
.verifyIssueOnFile ("Remove this package." );
118
124
119
125
var check = spy (new UselessPackageInfoCheck ());
120
126
121
127
var populatedReadCache = new InternalReadCache ().putAll (writeCache );
128
+ populatedReadCache .put (HashCacheTestHelper .contentHashKey (changedFilePath1 ), new byte [0 ]);
129
+ populatedReadCache .put (HashCacheTestHelper .contentHashKey (changedFilePath2 ), new byte [0 ]);
122
130
var writeCache2 = new InternalWriteCache ().bind (populatedReadCache );
123
131
CheckVerifier .newVerifier ()
124
132
.withCache (populatedReadCache , writeCache2 )
@@ -127,32 +135,37 @@ void caching() throws IOException, ClassNotFoundException {
127
135
mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld2.java" )
128
136
)
129
137
.addFiles (InputFile .Status .CHANGED ,
130
- mainCodeSourcesPath ( "checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/package-info.java" ) ,
131
- mainCodeSourcesPath ( "checks/UselessPackageInfoCheck/packageWithNoOtherFiles/package-info.java" )
138
+ changedFilePath1 ,
139
+ changedFilePath2
132
140
)
133
141
.withCheck (check )
134
142
.verifyIssueOnFile ("Remove this package." );
135
143
136
144
verify (check , times (2 )).scanFile (any ());
137
145
verify (check , times (2 )).scanWithoutParsing (any ());
138
146
assertThat (writeCache2 .getData ())
139
- .hasSize (4 )
147
+ .hasSizeGreaterThanOrEqualTo (4 )
140
148
.containsExactlyInAnyOrderEntriesOf (writeCache .getData ());
141
149
}
142
150
143
151
@ Test
144
- void cache_deserialization_throws_IOException () throws IOException {
152
+ void cache_deserialization_throws_IOException () throws IOException , NoSuchAlgorithmException {
145
153
var inputStream = mock (InputStream .class );
146
154
doThrow (new IOException ()).when (inputStream ).readAllBytes ();
147
- var readCache = mock (ReadCache .class );
148
- doReturn (inputStream ).when (readCache ).read (any ());
149
- doReturn (true ).when (readCache ).contains (any ());
155
+ var localReadCache = mock (ReadCache .class );
156
+
157
+ String filePath = mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java" );
158
+ InputFile cachedFile = HashCacheTestHelper .inputFileFromPath (filePath );
159
+ byte [] cachedHash = FileHashingUtils .inputFileContentHash (cachedFile );
160
+
161
+ doReturn (inputStream ).when (localReadCache ).read ("java:S1228;S4032:package:" +cachedFile .key ());
162
+ doReturn (true ).when (localReadCache ).contains (any ());
163
+ doReturn (new ByteArrayInputStream (cachedHash ))
164
+ .when (localReadCache ).read ("java:contentHash:MD5:" +cachedFile .key ());
150
165
151
166
var verifier = CheckVerifier .newVerifier ()
152
- .withCache (readCache , writeCache )
153
- .addFiles (InputFile .Status .SAME ,
154
- mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java" )
155
- )
167
+ .withCache (localReadCache , new InternalWriteCache ().bind (localReadCache ))
168
+ .addFiles (InputFile .Status .SAME , filePath )
156
169
.withCheck (new UselessPackageInfoCheck ());
157
170
158
171
assertThatThrownBy (verifier ::verifyNoIssues )
@@ -176,13 +189,14 @@ void write_cache_multiple_writes() {
176
189
}
177
190
178
191
@ Test
179
- void emptyCache () {
192
+ void emptyCache () throws NoSuchAlgorithmException , IOException {
180
193
logTester .setLevel (LoggerLevel .TRACE );
194
+ String filePath = mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java" );
195
+ ReadCache populatedReadCache = HashCacheTestHelper .internalReadCacheFromFile (filePath );
181
196
verifier
182
- .addFiles (InputFile .Status .SAME ,
183
- mainCodeSourcesPath ("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java" )
184
- )
197
+ .addFiles (InputFile .Status .SAME , filePath )
185
198
.withCheck (new UselessPackageInfoCheck ())
199
+ .withCache (populatedReadCache , new InternalWriteCache ().bind (populatedReadCache ))
186
200
.verifyNoIssues ();
187
201
188
202
assertThat (logTester .logs (LoggerLevel .TRACE ).stream ()
0 commit comments