Skip to content

Commit

Permalink
Test for checksum file format, covering the leading zero case
Browse files Browse the repository at this point in the history
  • Loading branch information
dmivankov committed Oct 2, 2020
1 parent 95380cf commit 971f85e
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions tests/integration/java_export/PublishShapeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -18,6 +20,12 @@
import static org.junit.Assume.assumeFalse;

public class PublishShapeTest {
// Jar content is fake, but chosen to produce leading zero in the checksum
// to be sure leading zeroes aren't omitted
private static final String JAR_CONTENTS = "magic!";
private static final String JAR_MD5 = "05427eba78c92912c86d004b9857d6a0";
private static final String JAR_SHA1 = "cbb0126a346a4dd6694fc48e3a94174fd1c7fa93";

@Rule
public TemporaryFolder temp = new TemporaryFolder();

Expand All @@ -34,17 +42,25 @@ public void publishingToAFileEndPointLooksCorrect() throws IOException, Interrup
Path repoRoot = publish("com.example:my-lib:1.0.0");

// Check that the files are where we expect them to be
assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0.jar")));
assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0.jar.md5")));
assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0.jar.sha1")));
checkJarShape(repoRoot, "com/example/my-lib/1.0.0/my-lib-1.0.0.jar");
checkJarShape(repoRoot, "com/example/my-lib/1.0.0/my-lib-1.0.0-sources.jar");
checkJarShape(repoRoot, "com/example/my-lib/1.0.0/my-lib-1.0.0-javadoc.jar");
}

private void checkJarShape(Path repoRoot, String path) throws IOException {
assertTrue(Files.exists(repoRoot.resolve(path)));
assertTrue(Files.exists(repoRoot.resolve(path + ".md5")));
assertTrue(Files.exists(repoRoot.resolve(path + ".sha1")));

assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-sources.jar")));
assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-sources.jar.md5")));
assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-sources.jar.sha1")));
// Basic checksum format check
String md5 = new String(Files.readAllBytes(repoRoot.resolve(path + ".md5")));
assertEquals(32, md5.length());
String sha1 = new String(Files.readAllBytes(repoRoot.resolve(path + ".sha1")));
assertEquals(40, sha1.length());

assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-javadoc.jar")));
assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-javadoc.jar.md5")));
assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-javadoc.jar.sha1")));
// Check checksum values
assertEquals(JAR_MD5, md5);
assertEquals(JAR_SHA1, sha1);
}

private Path publish(String coordinates) throws IOException, InterruptedException {
Expand All @@ -56,7 +72,7 @@ private Path publish(String coordinates) throws IOException, InterruptedExceptio

// The publisher doesn't validate inputs (though remote maven repos do)
// so we'll stub out the bits we need.
File stubJar = temp.newFile("dummy.jar");
File stubJar = writeFile("dummy.jar", JAR_CONTENTS);
File pomXml = temp.newFile("pom.xml");

// We'd prefer to use `bazel run`, but this is a reasonable proxy for
Expand Down Expand Up @@ -87,4 +103,13 @@ private Path publish(String coordinates) throws IOException, InterruptedExceptio

return repoRoot.toPath();
}

private File writeFile(String name, String contents) throws IOException {
File file = temp.newFile(name);

try (PrintStream out = new PrintStream(new FileOutputStream(file))) {
out.print(contents);
}
return file;
}
}

0 comments on commit 971f85e

Please sign in to comment.