Skip to content

Commit 35cb906

Browse files
committed
More FileUtils tests
1 parent 4dd4919 commit 35cb906

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

convex-core/src/main/java/convex/core/util/FileUtils.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ public static String loadFileAsString(InputStream inputStream) throws IOExceptio
6161
public static Blob loadFileAsBlob(Path file) throws IOException {
6262
return Blob.wrap(Files.readAllBytes(file));
6363
}
64-
65-
public static byte[] loadFileAsBytes(Path file) throws IOException {
66-
return Files.readAllBytes(file);
67-
}
6864

6965
public static <T extends ACell> T loadCAD3(Path file) throws IOException, BadFormatException {
7066
Blob b=loadFileAsBlob(file);
@@ -106,6 +102,20 @@ public static File ensureFilePath(File file) throws IOException {
106102
Files.createDirectories(Path.of(dirPath));
107103
return target;
108104
}
105+
106+
/**
107+
* Create a path if necessary to a File object. Interprets leading "~" as user home directory.
108+
*
109+
* @param file File object to see if the path part of the filename exists, if not then create it.
110+
* @return target File, as an absolute path, with parent directories created recursively if needed
111+
* @throws IOException In case of IO Error
112+
*/
113+
public static Path ensureFilePath(Path file) throws IOException {
114+
// Get path of parent directory, using absolute path (may be current working directory user.dir)
115+
Path parent=file.getParent();
116+
Files.createDirectories(parent);
117+
return file;
118+
}
109119

110120
/**
111121
* Gets the absolute path File for a given file name. Interprets leading "~" as user home directory.

convex-core/src/test/java/convex/core/util/FileUtilsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,32 @@
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

77
import java.io.File;
8+
import java.io.IOException;
89
import java.nio.file.Files;
910
import java.nio.file.Path;
1011

12+
import org.junit.jupiter.api.BeforeAll;
1113
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.TestInstance;
15+
import org.junit.jupiter.api.TestInstance.Lifecycle;
1216

17+
import convex.core.data.Blob;
18+
import convex.core.data.Strings;
19+
20+
@TestInstance(Lifecycle.PER_CLASS)
1321
public class FileUtilsTest {
1422

23+
Path TEMP;
24+
25+
@BeforeAll
26+
public void setup() throws IOException {
27+
TEMP=Files.createTempDirectory("fileTest");
28+
}
29+
30+
@BeforeAll
31+
public void cleanup() throws IOException {
32+
Files.deleteIfExists(TEMP);
33+
}
1534

1635
@Test public void testGetHomePath() {
1736
String home=System.getProperty("user.home");
@@ -27,4 +46,32 @@ public class FileUtilsTest {
2746
assertTrue(path.isAbsolute());
2847
assertTrue(Files.isDirectory(path));
2948
}
49+
50+
@Test public void testFileOps() throws IOException {
51+
Path DIR=FileUtils.ensureFilePath(TEMP.resolve("testOps/foo.bar")).getParent();
52+
assertTrue(Files.exists(DIR));
53+
assertTrue(Files.isDirectory(DIR));
54+
try {
55+
56+
57+
Path TEXT=DIR.resolve("hello.txt");
58+
try { // text file
59+
FileUtils.writeFileAsString(TEXT, "hello");
60+
String rs=FileUtils.loadFileAsString(DIR.toString()+"/hello.txt");
61+
assertEquals("hello",rs);
62+
63+
Blob b=FileUtils.loadFileAsBlob(TEXT);
64+
assertEquals("hello",Strings.create(b).toString());
65+
66+
67+
} finally {
68+
Files.delete(TEXT);
69+
}
70+
71+
72+
73+
} finally {
74+
Files.delete(DIR);
75+
}
76+
}
3077
}

convex-gui/src/main/java/convex/gui/tools/SystemInfoPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public SystemInfoPanel() {
4242
hashPanel.add(new ActionButton("Compute...,",0xe8b6, e->{
4343
try {
4444
String hash;
45-
hash = Hashing.sha256(FileUtils.loadFileAsBytes(jarFile)).toHexString();
45+
hash = Hashing.sha256(Files.readAllBytes(jarFile)).toHexString();
4646
hashPanel.removeAll();
4747
hashPanel.add(new JLabel(hash.toUpperCase()));
4848
} catch (IOException e1) {

0 commit comments

Comments
 (0)