-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3edf049
commit e985f2c
Showing
12 changed files
with
706 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
java-checks/src/test/java/org/sonar/java/checks/helpers/HashCacheTestHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.