Skip to content

Commit

Permalink
Irina/sonarjava 4396 (#4317)
Browse files Browse the repository at this point in the history
  • Loading branch information
irina-batinic-sonarsource authored Mar 1, 2023
1 parent 3edf049 commit e985f2c
Show file tree
Hide file tree
Showing 12 changed files with 706 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
*/
package org.sonar.java.checks.verifier.internal;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -33,6 +36,7 @@
import org.sonar.check.Rule;
import org.sonar.java.AnalysisException;
import org.sonar.java.caching.DummyCache;
import org.sonar.java.caching.FileHashingUtils;
import org.sonar.java.caching.JavaReadCacheImpl;
import org.sonar.java.caching.JavaWriteCacheImpl;
import org.sonar.java.reporting.AnalyzerMessage;
Expand Down Expand Up @@ -950,9 +954,10 @@ void addFiles_throws_an_IllegalArgumentException_if_file_added_before() {
}

@Test
void withCache_effectively_sets_the_caches_for_scanWithoutParsing() {
ReadCache readCache = new InternalReadCache();
WriteCache writeCache = new InternalWriteCache();
void withCache_effectively_sets_the_caches_for_scanWithoutParsing() throws IOException, NoSuchAlgorithmException {
InputFile inputFile = InternalInputFile.inputFile("", new File(TEST_FILE), InputFile.Status.SAME);
ReadCache readCache = new InternalReadCache().put("java:contentHash:MD5::" + TEST_FILE, FileHashingUtils.inputFileContentHash(inputFile));
WriteCache writeCache = new InternalWriteCache().bind(readCache);
CacheContext cacheContext = new InternalCacheContext(
true,
new JavaReadCacheImpl(readCache),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/
package org.sonar.java.checks;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -30,15 +27,20 @@
import org.sonar.api.utils.log.LogTesterJUnit5;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.java.AnalysisException;
import org.sonar.java.caching.FileHashingUtils;
import org.sonar.java.checks.helpers.HashCacheTestHelper;
import org.sonar.java.checks.verifier.CheckVerifier;
import org.sonar.java.checks.verifier.internal.InternalReadCache;
import org.sonar.java.checks.verifier.internal.InternalWriteCache;
import org.sonar.plugins.java.api.InputFileScannerContext;
import org.sonar.plugins.java.api.JavaFileScannerContext;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.in;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -98,6 +100,7 @@ void caching() {
mainCodeSourcesPath("checks/packageInfo/nopackageinfo/nopackageinfo.java")
)
.withCheck(new MissingPackageInfoCheck())
.withCache(readCache, writeCache)
.verifyIssueOnProject(EXPECTED_MESSAGE);

var check = spy(new MissingPackageInfoCheck());
Expand All @@ -119,26 +122,30 @@ void caching() {
verify(check, times(0)).scanFile(any());
verify(check, times(5)).scanWithoutParsing(any());
assertThat(writeCache2.getData())
.hasSize(5)
.hasSizeGreaterThanOrEqualTo(5)
.containsExactlyInAnyOrderEntriesOf(writeCache.getData());
}

@Test
void cache_deserialization_throws_IOException() throws IOException {
void cache_deserialization_throws_IOException() throws IOException, NoSuchAlgorithmException {
String filePath = mainCodeSourcesPath("checks/packageInfo/HelloWorld.java");
InputFile cachedFile = HashCacheTestHelper.inputFileFromPath(filePath);
byte[] cachedHash = FileHashingUtils.inputFileContentHash(cachedFile);
var inputStream = mock(InputStream.class);
doThrow(new IOException()).when(inputStream).readAllBytes();
var readCache = mock(ReadCache.class);
doReturn(inputStream).when(readCache).read(any());
doReturn(true).when(readCache).contains(any());

var verifier = CheckVerifier.newVerifier()
.withCache(readCache, writeCache)
.addFiles(InputFile.Status.SAME,
mainCodeSourcesPath("checks/packageInfo/HelloWorld.java")
)
var localReadCache = mock(ReadCache.class);
InternalWriteCache localWriteCache = new InternalWriteCache().bind(localReadCache);
doReturn(inputStream).when(localReadCache).read("java:S1228;S4032:package:" + cachedFile.key());
doReturn(true).when(localReadCache).contains(any());
doReturn(new ByteArrayInputStream(cachedHash))
.when(localReadCache).read("java:contentHash:MD5:" + cachedFile.key());

var localVerifier = CheckVerifier.newVerifier()
.withCache(localReadCache, localWriteCache)
.addFiles(InputFile.Status.SAME, filePath)
.withCheck(new MissingPackageInfoCheck());

assertThatThrownBy(verifier::verifyNoIssues)
assertThatThrownBy(localVerifier::verifyNoIssues)
.isInstanceOf(AnalysisException.class)
.hasRootCauseInstanceOf(IOException.class);
}
Expand All @@ -159,17 +166,19 @@ void write_cache_multiple_writes() {
}

@Test
void emptyCache() {
void emptyCache() throws NoSuchAlgorithmException, IOException {
logTester.setLevel(LoggerLevel.TRACE);
verifier
.addFiles(InputFile.Status.SAME,
mainCodeSourcesPath("checks/packageInfo/HelloWorld.java")
)
String filePath = mainCodeSourcesPath("checks/packageInfo/HelloWorld.java");
ReadCache populatedReadCache = HashCacheTestHelper.internalReadCacheFromFile(filePath);
CheckVerifier.newVerifier()
.addFiles(InputFile.Status.SAME, filePath)
.withCache(populatedReadCache, new InternalWriteCache().bind(populatedReadCache))
.withCheck(new MissingPackageInfoCheck())
.verifyNoIssues();

assertThat(logTester.logs(LoggerLevel.TRACE).stream()
.filter(msg -> msg.matches("Cache miss for key '[^']+'")))
.hasSize(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
*/
package org.sonar.java.checks;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -29,7 +31,8 @@
import org.sonar.api.utils.log.LogTesterJUnit5;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.java.AnalysisException;
import org.sonar.java.caching.CacheReadException;
import org.sonar.java.caching.FileHashingUtils;
import org.sonar.java.checks.helpers.HashCacheTestHelper;
import org.sonar.java.checks.verifier.CheckVerifier;
import org.sonar.java.checks.verifier.internal.InternalReadCache;
import org.sonar.java.checks.verifier.internal.InternalWriteCache;
Expand Down Expand Up @@ -106,19 +109,24 @@ void defaultPackage() {

@Test
void caching() throws IOException, ClassNotFoundException {
String changedFilePath1 = mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/package-info.java");
String changedFilePath2 = mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFiles/package-info.java");
verifier
.onFiles(
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java"),
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld2.java"),
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/package-info.java"),
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFiles/package-info.java")
changedFilePath1,
changedFilePath2
)
.withCheck(new UselessPackageInfoCheck())
.withCache(readCache, writeCache)
.verifyIssueOnFile("Remove this package.");

var check = spy(new UselessPackageInfoCheck());

var populatedReadCache = new InternalReadCache().putAll(writeCache);
populatedReadCache.put(HashCacheTestHelper.contentHashKey(changedFilePath1), new byte[0]);
populatedReadCache.put(HashCacheTestHelper.contentHashKey(changedFilePath2), new byte[0]);
var writeCache2 = new InternalWriteCache().bind(populatedReadCache);
CheckVerifier.newVerifier()
.withCache(populatedReadCache, writeCache2)
Expand All @@ -127,32 +135,37 @@ void caching() throws IOException, ClassNotFoundException {
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld2.java")
)
.addFiles(InputFile.Status.CHANGED,
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/package-info.java"),
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFiles/package-info.java")
changedFilePath1,
changedFilePath2
)
.withCheck(check)
.verifyIssueOnFile("Remove this package.");

verify(check, times(2)).scanFile(any());
verify(check, times(2)).scanWithoutParsing(any());
assertThat(writeCache2.getData())
.hasSize(4)
.hasSizeGreaterThanOrEqualTo(4)
.containsExactlyInAnyOrderEntriesOf(writeCache.getData());
}

@Test
void cache_deserialization_throws_IOException() throws IOException {
void cache_deserialization_throws_IOException() throws IOException, NoSuchAlgorithmException {
var inputStream = mock(InputStream.class);
doThrow(new IOException()).when(inputStream).readAllBytes();
var readCache = mock(ReadCache.class);
doReturn(inputStream).when(readCache).read(any());
doReturn(true).when(readCache).contains(any());
var localReadCache = mock(ReadCache.class);

String filePath = mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java");
InputFile cachedFile = HashCacheTestHelper.inputFileFromPath(filePath);
byte[] cachedHash = FileHashingUtils.inputFileContentHash(cachedFile);

doReturn(inputStream).when(localReadCache).read("java:S1228;S4032:package:"+cachedFile.key());
doReturn(true).when(localReadCache).contains(any());
doReturn(new ByteArrayInputStream(cachedHash))
.when(localReadCache).read("java:contentHash:MD5:"+cachedFile.key());

var verifier = CheckVerifier.newVerifier()
.withCache(readCache, writeCache)
.addFiles(InputFile.Status.SAME,
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java")
)
.withCache(localReadCache, new InternalWriteCache().bind(localReadCache))
.addFiles(InputFile.Status.SAME, filePath)
.withCheck(new UselessPackageInfoCheck());

assertThatThrownBy(verifier::verifyNoIssues)
Expand All @@ -176,13 +189,14 @@ void write_cache_multiple_writes() {
}

@Test
void emptyCache() {
void emptyCache() throws NoSuchAlgorithmException, IOException {
logTester.setLevel(LoggerLevel.TRACE);
String filePath = mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java");
ReadCache populatedReadCache = HashCacheTestHelper.internalReadCacheFromFile(filePath);
verifier
.addFiles(InputFile.Status.SAME,
mainCodeSourcesPath("checks/UselessPackageInfoCheck/packageWithNoOtherFilesButNotPackageInfo/HelloWorld1.java")
)
.addFiles(InputFile.Status.SAME, filePath)
.withCheck(new UselessPackageInfoCheck())
.withCache(populatedReadCache, new InternalWriteCache().bind(populatedReadCache))
.verifyNoIssues();

assertThat(logTester.logs(LoggerLevel.TRACE).stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* SonarQube Java
* Copyright (C) 2012-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.java.checks.helpers;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.cache.ReadCache;
import org.sonar.java.caching.FileHashingUtils;
import org.sonar.java.checks.verifier.internal.InternalInputFile;
import org.sonar.java.checks.verifier.internal.InternalReadCache;

public class HashCacheTestHelper {

public static InputFile inputFileFromPath(String path) {
return InternalInputFile
.inputFile("", new File(path), InputFile.Status.SAME);
}

public static String contentHashKey(String path) {
return contentHashKey(inputFileFromPath(path));
}

public static String contentHashKey(InputFile inputFile) {
return "java:contentHash:MD5:" + inputFile.key();
}

public static ReadCache internalReadCacheFromFile(String path) throws NoSuchAlgorithmException, IOException {
InputFile cachedFile = inputFileFromPath(path);
byte[] cachedHash = FileHashingUtils.inputFileContentHash(cachedFile);
InternalReadCache localReadCache = new InternalReadCache().put(contentHashKey(cachedFile), cachedHash);
return localReadCache;
}

public static ReadCache internalReadCacheFromFiles(Collection<String> paths) throws NoSuchAlgorithmException, IOException {
InternalReadCache localReadCache = new InternalReadCache();
for (String path : paths) {
InputFile cachedFile = inputFileFromPath(path);
byte[] cachedHash = FileHashingUtils.inputFileContentHash(cachedFile);
localReadCache.put(contentHashKey(cachedFile), cachedHash);
}
return localReadCache;
}

public static byte[] getSlightlyDifferentContentHash(String path) throws NoSuchAlgorithmException, IOException {
InputFile cachedFile = inputFileFromPath(path);
byte[] cachedHash = FileHashingUtils.inputFileContentHash(cachedFile);
byte[] copy = Arrays.copyOf(cachedHash, cachedHash.length+1);
copy[cachedHash.length] = 10;
return copy;
}

}
Loading

0 comments on commit e985f2c

Please sign in to comment.