From 3f6dfe9e36eeedbf6c9bcd0be07e36755e62238f Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 8 Jan 2025 20:22:18 +0300 Subject: [PATCH] more polished --- .../java/EOorg/EOeolang/EOfs/FilesTest.java | 48 +++++++------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java index 882025d3b5..8d4af1b462 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java @@ -48,17 +48,12 @@ */ @ExtendWith(MktmpResolver.class) final class FilesTest { - /** - * Files instance. - */ - private static final Files INSTANCE = Files.INSTANCE; - @Test void throwsOnReadingWithoutOpening(@Mktmp final Path dir) { Assertions.assertThrows( ExFailure.class, - () -> FilesTest.INSTANCE.read( - FilesTest.tempFile(dir), 10 + () -> Files.INSTANCE.read( + dir.resolve("c.txt").toFile().getAbsolutePath(), 10 ), "File should not allow to read before opening" ); @@ -68,8 +63,9 @@ void throwsOnReadingWithoutOpening(@Mktmp final Path dir) { void throwsOnWritingWithoutOpening(@Mktmp final Path dir) { Assertions.assertThrows( ExFailure.class, - () -> FilesTest.INSTANCE.write( - FilesTest.tempFile(dir), new byte[]{0x01} + () -> Files.INSTANCE.write( + dir.resolve("b.txt").toFile().getAbsolutePath(), + new byte[]{0x01} ), "File should not allow to write before opening" ); @@ -79,8 +75,8 @@ void throwsOnWritingWithoutOpening(@Mktmp final Path dir) { void throwsOnClosingWithoutOpening(@Mktmp final Path dir) { Assertions.assertThrows( ExFailure.class, - () -> FilesTest.INSTANCE.close( - FilesTest.tempFile(dir) + () -> Files.INSTANCE.close( + dir.resolve("a.txt").toFile().getAbsolutePath() ), "File should not allow to close before opening" ); @@ -88,46 +84,34 @@ void throwsOnClosingWithoutOpening(@Mktmp final Path dir) { @Test void readsFromFile(@Mktmp final Path dir) throws IOException { - final String file = FilesTest.tempFile(dir); + final String file = dir.resolve("bar.txt").toFile().getAbsolutePath(); try (BufferedWriter writer = java.nio.file.Files.newBufferedWriter(Paths.get(file))) { writer.write("Hello, world"); } - FilesTest.INSTANCE.open(file); + Files.INSTANCE.open(file); MatcherAssert.assertThat( "The string should have been read from file", - FilesTest.INSTANCE.read(file, 12), + Files.INSTANCE.read(file, 12), Matchers.equalTo("Hello, world".getBytes(StandardCharsets.UTF_8)) ); - FilesTest.INSTANCE.close(file); + Files.INSTANCE.close(file); } @Test void writesToFile(@Mktmp final Path dir) throws IOException { - final String file = FilesTest.tempFile(dir); + final String file = dir.resolve("foo.txt").toFile().getAbsolutePath(); try (BufferedWriter writer = java.nio.file.Files.newBufferedWriter(Paths.get(file))) { writer.write("Hello, world"); } - FilesTest.INSTANCE.open(file); - FilesTest.INSTANCE.write(file, "!".getBytes(StandardCharsets.UTF_8)); + Files.INSTANCE.open(file); + Files.INSTANCE.write(file, "!".getBytes(StandardCharsets.UTF_8)); MatcherAssert.assertThat( "The string should have been read from file", - FilesTest.INSTANCE.read(file, 13), + Files.INSTANCE.read(file, 13), Matchers.equalTo("Hello, world!".getBytes(StandardCharsets.UTF_8)) ); - FilesTest.INSTANCE.close(file); - } - - /** - * Creates temporary file in directory. - * @param dir Directory - * @return Absolute path to temp file - * @throws IOException If fails to create temp file - */ - private static String tempFile(final Path dir) throws IOException { - final Path file = java.nio.file.Files.createTempFile(dir, null, null); - file.toFile().deleteOnExit(); - return file.toAbsolutePath().toString(); + Files.INSTANCE.close(file); } }