Skip to content

Commit f2845c2

Browse files
committed
Create TestFileUtils.getMD5Hash to compare binary files
Don't use `Files.readString` to read non-text files
1 parent 0afb6ad commit f2845c2

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/org/labkey/test/TestFileUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
import java.nio.file.Paths;
6565
import java.nio.file.SimpleFileVisitor;
6666
import java.nio.file.attribute.BasicFileAttributes;
67+
import java.security.MessageDigest;
68+
import java.security.NoSuchAlgorithmException;
6769
import java.security.Security;
6870
import java.util.ArrayList;
6971
import java.util.Arrays;
@@ -105,6 +107,9 @@ public static String getFileContents(final File file)
105107
return getFileContents(path);
106108
}
107109

110+
/**
111+
* Get text content of a file. Will throw an error for non-text files (e.g. PDF).
112+
*/
108113
public static String getFileContents(Path path)
109114
{
110115
try
@@ -117,6 +122,21 @@ public static String getFileContents(Path path)
117122
}
118123
}
119124

125+
/**
126+
* Compute MD5 hash for the given file. Useful checking file equivalence.
127+
*/
128+
public static String getMD5Hash(Path path)
129+
{
130+
try
131+
{
132+
return new String(MessageDigest.getInstance("MD5").digest(Files.readAllBytes(path)), StandardCharsets.UTF_8);
133+
}
134+
catch (IOException | NoSuchAlgorithmException fail)
135+
{
136+
throw new RuntimeException(fail);
137+
}
138+
}
139+
120140
public static String getStreamContentsAsString(InputStream is) throws IOException
121141
{
122142
return StringUtils.join(IOUtils.readLines(is, Charset.defaultCharset()).toArray(), System.lineSeparator());

src/org/labkey/test/tests/FileAttachmentColumnTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,9 @@ private void validateSampleData(String sampleType, String folderPath, List<File>
550550
{
551551
// verify fie download behavior
552552
File downloadedFile = doAndWaitForDownload(() -> optionalFileLink.get().click());
553-
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getFileContents(downloadedFile))
553+
checker().wrapAssertion(() -> Assertions.assertThat(TestFileUtils.getMD5Hash(downloadedFile.toPath()))
554554
.as("expect the downloaded file to be the expected file")
555-
.isEqualTo(TestFileUtils.getFileContents(file))); // guard against renames like file2.xyz
555+
.isEqualTo(TestFileUtils.getMD5Hash(file.toPath()))); // guard against renames like file2.xyz
556556
}
557557
}
558558
}
@@ -576,9 +576,9 @@ private void validateAssayRun(String assayName, String folderPath, String runNam
576576
if (optionalFileLink.isPresent())
577577
{
578578
var file = doAndWaitForDownload(()-> optionalFileLink.get().click());
579-
checker().wrapAssertion(()-> Assertions.assertThat(TestFileUtils.getFileContents(file))
579+
checker().wrapAssertion(()-> Assertions.assertThat(TestFileUtils.getMD5Hash(file.toPath()))
580580
.as("expect the downloaded file to have equivalent content")
581-
.isEqualTo(TestFileUtils.getFileContents(runFile)));
581+
.isEqualTo(TestFileUtils.getMD5Hash(runFile.toPath())));
582582
}
583583

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

0 commit comments

Comments
 (0)