Skip to content

Commit

Permalink
Fix WrapperValidatorTest.testGradleWrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed May 20, 2020
1 parent 9d8498d commit 31c55fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.gradle.api.UncheckedIOException;

import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
Expand Down Expand Up @@ -69,8 +68,8 @@ protected IStatus run(IProgressMonitor monitor) {
final HttpURLConnection connection;
try {
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
} catch (IOException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
continue;
Expand All @@ -82,7 +81,8 @@ protected IStatus run(IProgressMonitor monitor) {
WrapperValidator.allow(sha256);
subMonitor.worked(2);
} catch (Exception e) {
throw new UncheckedIOException("Cannot download Gradle sha256 checksum: " + url.toString(), e);
JavaLanguageServerPlugin.logException("Cannot download Gradle sha256 checksum: " + url.toString(), e);
continue;
}
}
subMonitor.done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,23 @@
*/
public class WrapperValidator {

private static final int QUEUE_LENGTH = 20;
private static final String WRAPPER_CHECKSUM_URL = "wrapperChecksumUrl";
private static final String GRADLE_WRAPPER_JAR = "gradle/wrapper/gradle-wrapper.jar";

private static Set<String> allowed = new HashSet<>();
private static Set<String> disallowed = new HashSet<>();
private static AtomicBoolean downloaded = new AtomicBoolean(false);
private HashProvider hashProvider;
private int queueLength;

public WrapperValidator() {
public WrapperValidator(int queueLength) {
this.hashProvider = new HashProvider();
queueLength = queueLength;
}

public WrapperValidator() {
this(QUEUE_LENGTH);
}

public ValidationResult checkWrapper(String baseDir) throws CoreException {
Expand Down Expand Up @@ -118,7 +125,7 @@ public String apply(Map<String, String> input) {
File sha256File = new File(cacheDir, fileName);
if (!sha256File.exists() || sha256File.lastModified() < versionFile.lastModified()) {
count++;
if (count > 20) {
if (count > queueLength) {
downloadJob.schedule();
downloadJob = new DownloadChecksumJob();
count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
Expand All @@ -44,46 +46,53 @@ public class WrapperValidatorTest extends AbstractGradleBasedTest{
@Before
public void setProperty() throws Exception {
System.setProperty("gradle.checksum.cacheDir", "target/gradle/checksums");
WrapperValidator.clear();
}

@After
public void clearProperty() {
public void clearProperty() throws IOException {
System.clearProperty("gradle.checksum.cacheDir");
}

@Test
public void testGradleWrapper() throws Exception {
File file = new File(getSourceProjectDirectory(), "gradle/simple-gradle");
assertTrue(file.isDirectory());
ValidationResult result = new WrapperValidator().checkWrapper(file.getAbsolutePath());
assertTrue(result.isValid());
File sha256Directory = WrapperValidator.getSha256CacheFile();
assertTrue(sha256Directory.isDirectory());
ValidationResult result = new WrapperValidator(100).checkWrapper(file.getAbsolutePath());
assertTrue(result.isValid());
// test cache
file = new File(sha256Directory, "gradle-6.4-wrapper.jar.sha256");
assertTrue(sha256Directory.isDirectory());
String message = Files.list(Paths.get(sha256Directory.getAbsolutePath())).collect(Collectors.toList()).toString();
file = new File(sha256Directory, "gradle-6.3-wrapper.jar.sha256");
assertTrue(message, file.isFile());
String sha256 = Files.lines(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8).findFirst().get();
assertEquals("70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857", sha256);
assertEquals("1cef53de8dc192036e7b0cc47584449b0cf570a00d560bfaa6c9eabe06e1fc06", sha256);
}

@Test
public void testMissingSha256() throws Exception {
Preferences prefs = JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
List<String> allowed = prefs.getSha256Allowed();
int size = WrapperValidator.size();
WrapperValidator wrapperValidator = new WrapperValidator();
WrapperValidator wrapperValidator = new WrapperValidator(100);
File file = new File(getSourceProjectDirectory(), "gradle/gradle-4.0");
List<String> sha256 = new ArrayList<>();
try {
List<String> sha256 = new ArrayList<>();
sha256.add("41c8aa7a337a44af18d8cda0d632ebba469aef34f3041827624ef5c1a4e4419d");
prefs.putSha256(null, sha256);
assertTrue(file.isDirectory());
ValidationResult result = wrapperValidator.checkWrapper(file.getAbsolutePath());
assertFalse(result.isValid());
size = WrapperValidator.size();
assertNotNull(result.getChecksum());
prefs.putSha256(sha256, null);
result = wrapperValidator.checkWrapper(file.getAbsolutePath());
assertTrue(result.isValid());
} finally {
prefs.putSha256(allowed, null);
prefs.putSha256(sha256, null);
wrapperValidator.checkWrapper(file.getAbsolutePath());
assertEquals(size, WrapperValidator.size());
}
Expand Down

0 comments on commit 31c55fb

Please sign in to comment.