Skip to content

Create TestFileUtils.getMD5Hash to compare binary files #2547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/org/labkey/test/TestFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -105,6 +107,9 @@ public static String getFileContents(final File file)
return getFileContents(path);
}

/**
* Get text content of a file. Will throw an error for non-text files (e.g. PDF).
*/
public static String getFileContents(Path path)
{
try
Expand All @@ -117,6 +122,21 @@ public static String getFileContents(Path path)
}
}

/**
* Compute MD5 hash for the given file. Useful checking file equivalence.
*/
public static String getMD5Hash(Path path)
{
try
{
return new String(MessageDigest.getInstance("MD5").digest(Files.readAllBytes(path)), StandardCharsets.UTF_8);
}
catch (IOException | NoSuchAlgorithmException fail)
{
throw new RuntimeException(fail);
}
}

public static String getStreamContentsAsString(InputStream is) throws IOException
{
return StringUtils.join(IOUtils.readLines(is, Charset.defaultCharset()).toArray(), System.lineSeparator());
Expand Down
12 changes: 6 additions & 6 deletions src/org/labkey/test/tests/FileAttachmentColumnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,9 @@ private void validateSampleData(String sampleType, String folderPath, List<File>
{
// verify fie download behavior
File downloadedFile = doAndWaitForDownload(() -> optionalFileLink.get().click());
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getFileContents(downloadedFile))
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath()))
.as("expect the downloaded file to be the expected file")
.isEqualTo(TestFileUtils.getFileContents(file))); // guard against renames like file2.xyz
.isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz
}
}
}
Expand All @@ -576,9 +576,9 @@ private void validateAssayRun(String assayName, String folderPath, String runNam
if (optionalFileLink.isPresent())
{
var file = doAndWaitForDownload(()-> optionalFileLink.get().click());
checker().wrapAssertion(()-> Assertions.assertThat(TestFileUtils.getFileContents(file))
checker().wrapAssertion(()-> Assertions.assertThat(TestFileUtils.getMD5Hash(file.toPath()))
.as("expect the downloaded file to have equivalent content")
.isEqualTo(TestFileUtils.getFileContents(runFile)));
.isEqualTo(TestFileUtils.getMD5Hash(runFile.toPath())));
}

var resultsPage = runsPage.clickAssayIdLink(runName);
Expand Down Expand Up @@ -644,9 +644,9 @@ private void validateDatasetData(String datasetName, String folderPath, List<Fil
{
// verify fie download behavior
File downloadedFile = doAndWaitForDownload(() -> optionalFileLink.get().click());
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getFileContents(downloadedFile))
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath()))
.as("expect the downloaded file to be the expected file")
.isEqualTo(TestFileUtils.getFileContents(file))); // guard against renames like file2.xyz
.isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz
}
}
}
Expand Down