This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 411bfbdc87 Unit tests
411bfbdc87 is described below
commit 411bfbdc878e83c5160c5d33b4bad332e41ea128
Author: James Bognar <[email protected]>
AuthorDate: Wed Dec 3 16:09:56 2025 -0800
Unit tests
---
.../org/apache/juneau/commons/utils/FileUtils.java | 25 +-
.../juneau/commons/utils/FileUtils_Test.java | 157 ++---
.../apache/juneau/commons/utils/IOUtils_Test.java | 776 ++++++++++++++++++++-
3 files changed, 842 insertions(+), 116 deletions(-)
diff --git
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/FileUtils.java
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/FileUtils.java
index bd2fb6b985..cbdc772547 100644
---
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/FileUtils.java
+++
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/FileUtils.java
@@ -42,12 +42,9 @@ public class FileUtils {
public static void create(File f) {
if (f.exists())
return;
- try {
- if (! f.createNewFile())
- throw rex("Could not create file ''{0}''",
f.getAbsolutePath());
- } catch (IOException e) {
- throw toRex(e);
- }
+ safe(() -> {
+ opt(f.createNewFile()).filter(x -> x).orElseThrow(() ->
rex("Could not create file ''{0}''", f.getAbsolutePath()));
+ });
}
/**
@@ -201,14 +198,12 @@ public class FileUtils {
assertArgNotNull("f", f);
if (f.exists()) {
if (clean) {
- if (! deleteFile(f))
- throw rex("Could not clean directory
''{0}''", f.getAbsolutePath());
+ opt(deleteFile(f)).filter(x ->
x).orElseThrow(() -> rex("Could not clean directory ''{0}''",
f.getAbsolutePath()));
} else {
return f;
}
}
- if (! f.mkdirs())
- throw rex("Could not create directory ''{0}''",
f.getAbsolutePath());
+ opt(f.mkdirs()).filter(x -> x).orElseThrow(() -> rex("Could not
create directory ''{0}''", f.getAbsolutePath()));
return f;
}
@@ -237,14 +232,10 @@ public class FileUtils {
var l = System.currentTimeMillis();
if (lm == l)
l++;
- if (! f.setLastModified(l))
- throw rex("Could not modify timestamp on file ''{0}''",
f.getAbsolutePath());
+ opt(f.setLastModified(l)).filter(x -> x).orElseThrow(() ->
rex("Could not modify timestamp on file ''{0}''", f.getAbsolutePath()));
// Linux only gives 1s precision, so set the date 1s into the
future.
- if (lm == f.lastModified()) {
- l += 1000;
- if (! f.setLastModified(l))
- throw rex("Could not modify timestamp on file
''{0}''", f.getAbsolutePath());
- }
+ if (lm == f.lastModified())
+ opt(f.setLastModified(l + 1000)).filter(x ->
x).orElseThrow(() -> rex("Could not modify timestamp on file ''{0}''",
f.getAbsolutePath()));
}
}
\ No newline at end of file
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/FileUtils_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/FileUtils_Test.java
index d2b23d018e..162d9cf183 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/FileUtils_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/FileUtils_Test.java
@@ -46,11 +46,10 @@ class FileUtils_Test extends TestBase {
}
//====================================================================================================
- // create(File) tests
+ // create(File)
//====================================================================================================
-
@Test
- void a01_create_newFile() {
+ void a001_create() {
var f = new File(tempDir.toFile(), "test.txt");
assertFalse(f.exists());
FileUtils.create(f);
@@ -59,7 +58,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void a02_create_existingFile() throws IOException {
+ void a002_create_existingFile() throws IOException {
var f = new File(tempDir.toFile(), "test.txt");
f.createNewFile();
assertTrue(f.exists());
@@ -71,7 +70,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void a03_create_parentDirectoryDoesNotExist() {
+ void a003_create_parentDirectoryDoesNotExist() {
var f = new File(tempDir.toFile(), "nonexistent/test.txt");
var e = assertThrows(RuntimeException.class, () -> {
FileUtils.create(f);
@@ -83,11 +82,10 @@ class FileUtils_Test extends TestBase {
}
//====================================================================================================
- // createTempFile(String) tests
+ // createTempFile(String)
//====================================================================================================
-
@Test
- void b01_createTempFile_basic() throws IOException {
+ void a004_createTempFile() throws IOException {
var f = FileUtils.createTempFile("test.txt");
assertNotNull(f);
assertTrue(f.exists());
@@ -97,7 +95,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void b02_createTempFile_multipleDots() throws IOException {
+ void a005_createTempFile_multipleDots() throws IOException {
var f = FileUtils.createTempFile("test.backup.txt");
assertNotNull(f);
assertTrue(f.exists());
@@ -109,7 +107,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void b03_createTempFile_noExtension() {
+ void a006_createTempFile_noExtension() {
// When there's no dot, split("\\.") returns array with one
element
// This will cause ArrayIndexOutOfBoundsException when
accessing parts[1]
// Note: This tests the current behavior, which may be a bug in
the implementation
@@ -119,11 +117,10 @@ class FileUtils_Test extends TestBase {
}
//====================================================================================================
- // createTempFile(String, String) tests
+ // createTempFile(String, String)
//====================================================================================================
-
@Test
- void c01_createTempFile_withContents() throws IOException {
+ void a007_createTempFile_withContents() throws IOException {
var contents = "test content\nline 2";
var f = FileUtils.createTempFile("test.txt", contents);
assertNotNull(f);
@@ -143,7 +140,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void c02_createTempFile_withEmptyContents() throws IOException {
+ void a008_createTempFile_withEmptyContents() throws IOException {
var f = FileUtils.createTempFile("test.txt", "");
assertNotNull(f);
assertTrue(f.exists());
@@ -152,20 +149,20 @@ class FileUtils_Test extends TestBase {
}
@Test
- void c03_createTempFile_withNullContents() throws IOException {
+ void a009_createTempFile_withNullContents() throws IOException {
var f = FileUtils.createTempFile("test.txt", null);
assertNotNull(f);
assertTrue(f.exists());
- // null contents should result in empty file or "null" string
+ // null contents should result in empty file
+ assertEquals(0, f.length());
f.delete();
}
//====================================================================================================
- // deleteFile(File) tests
+ // deleteFile(File)
//====================================================================================================
-
@Test
- void d01_deleteFile_singleFile() throws IOException {
+ void a010_deleteFile() throws IOException {
var f = new File(tempDir.toFile(), "test.txt");
f.createNewFile();
assertTrue(f.exists());
@@ -174,19 +171,19 @@ class FileUtils_Test extends TestBase {
}
@Test
- void d02_deleteFile_null() {
+ void a011_deleteFile_null() {
assertTrue(FileUtils.deleteFile(null));
}
@Test
- void d03_deleteFile_nonexistent() {
+ void a012_deleteFile_nonexistent() {
var f = new File(tempDir.toFile(), "nonexistent.txt");
assertFalse(f.exists());
assertFalse(FileUtils.deleteFile(f));
}
@Test
- void d04_deleteFile_directory() throws IOException {
+ void a013_deleteFile_directory() throws IOException {
var dir = new File(tempDir.toFile(), "testdir");
dir.mkdirs();
var f1 = new File(dir, "file1.txt");
@@ -203,7 +200,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void d05_deleteFile_nestedDirectories() throws IOException {
+ void a014_deleteFile_nestedDirectories() throws IOException {
var dir1 = new File(tempDir.toFile(), "dir1");
var dir2 = new File(dir1, "dir2");
var f = new File(dir2, "file.txt");
@@ -219,7 +216,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void d06_deleteFile_emptyDirectory() {
+ void a015_deleteFile_emptyDirectory() {
var dir = new File(tempDir.toFile(), "emptydir");
dir.mkdirs();
assertTrue(dir.exists());
@@ -228,11 +225,10 @@ class FileUtils_Test extends TestBase {
}
//====================================================================================================
- // fileExists(File, String) tests
+ // fileExists(File, String)
//====================================================================================================
-
@Test
- void e01_fileExists_existingFile() throws IOException {
+ void a016_fileExists() throws IOException {
var dir = tempDir.toFile();
var f = new File(dir, "test.txt");
f.createNewFile();
@@ -240,29 +236,29 @@ class FileUtils_Test extends TestBase {
}
@Test
- void e02_fileExists_nonexistentFile() {
+ void a017_fileExists_nonexistentFile() {
var dir = tempDir.toFile();
assertFalse(FileUtils.fileExists(dir, "nonexistent.txt"));
}
@Test
- void e03_fileExists_nullDir() {
+ void a018_fileExists_nullDir() {
assertFalse(FileUtils.fileExists(null, "test.txt"));
}
@Test
- void e04_fileExists_nullFileName() {
+ void a019_fileExists_nullFileName() {
var dir = tempDir.toFile();
assertFalse(FileUtils.fileExists(dir, null));
}
@Test
- void e05_fileExists_bothNull() {
+ void a020_fileExists_bothNull() {
assertFalse(FileUtils.fileExists(null, null));
}
@Test
- void e06_fileExists_subdirectory() throws IOException {
+ void a021_fileExists_subdirectory() throws IOException {
var dir = tempDir.toFile();
var subdir = new File(dir, "subdir");
subdir.mkdirs();
@@ -272,168 +268,163 @@ class FileUtils_Test extends TestBase {
}
//====================================================================================================
- // getBaseName(String) tests
+ // getBaseName(String)
//====================================================================================================
-
@Test
- void f01_getBaseName_withExtension() {
+ void a022_getBaseName() {
assertEquals("test", FileUtils.getBaseName("test.txt"));
assertEquals("file.backup",
FileUtils.getBaseName("file.backup.txt"));
}
@Test
- void f02_getBaseName_noExtension() {
+ void a023_getBaseName_noExtension() {
assertEquals("test", FileUtils.getBaseName("test"));
assertEquals("file", FileUtils.getBaseName("file"));
}
@Test
- void f03_getBaseName_null() {
+ void a024_getBaseName_null() {
assertNull(FileUtils.getBaseName(null));
}
@Test
- void f04_getBaseName_onlyExtension() {
+ void a025_getBaseName_onlyExtension() {
assertEquals("", FileUtils.getBaseName(".txt"));
}
@Test
- void f05_getBaseName_multipleDots() {
+ void a026_getBaseName_multipleDots() {
assertEquals("test.backup",
FileUtils.getBaseName("test.backup.txt"));
}
//====================================================================================================
- // getFileExtension(String) tests
+ // getFileExtension(String)
//====================================================================================================
-
@Test
- void g01_getFileExtension_withExtension() {
+ void a027_getFileExtension() {
assertEquals("txt", FileUtils.getFileExtension("test.txt"));
assertEquals("java",
FileUtils.getFileExtension("FileUtils.java"));
}
@Test
- void g02_getFileExtension_noExtension() {
+ void a028_getFileExtension_noExtension() {
assertEquals("", FileUtils.getFileExtension("test"));
assertEquals("", FileUtils.getFileExtension("file"));
}
@Test
- void g03_getFileExtension_null() {
+ void a029_getFileExtension_null() {
assertNull(FileUtils.getFileExtension(null));
}
@Test
- void g04_getFileExtension_onlyExtension() {
+ void a030_getFileExtension_onlyExtension() {
assertEquals("txt", FileUtils.getFileExtension(".txt"));
}
@Test
- void g05_getFileExtension_multipleDots() {
+ void a031_getFileExtension_multipleDots() {
assertEquals("txt",
FileUtils.getFileExtension("test.backup.txt"));
}
@Test
- void g06_getFileExtension_emptyString() {
+ void a032_getFileExtension_emptyString() {
assertEquals("", FileUtils.getFileExtension(""));
}
//====================================================================================================
- // getFileName(String) tests
+ // getFileName(String)
//====================================================================================================
-
@Test
- void h01_getFileName_simplePath() {
+ void a033_getFileName() {
assertEquals("test.txt", FileUtils.getFileName("test.txt"));
}
@Test
- void h02_getFileName_withPath() {
+ void a034_getFileName_withPath() {
assertEquals("test.txt",
FileUtils.getFileName("/path/to/test.txt"));
assertEquals("file.java",
FileUtils.getFileName("dir/subdir/file.java"));
}
@Test
- void h03_getFileName_withTrailingSlash() {
+ void a035_getFileName_withTrailingSlash() {
assertEquals("test.txt",
FileUtils.getFileName("/path/to/test.txt/"));
assertEquals("dir", FileUtils.getFileName("/path/to/dir/"));
}
@Test
- void h04_getFileName_null() {
+ void a036_getFileName_null() {
assertNull(FileUtils.getFileName(null));
}
@Test
- void h05_getFileName_emptyString() {
+ void a037_getFileName_emptyString() {
assertNull(FileUtils.getFileName(""));
}
@Test
- void h06_getFileName_onlySlashes() {
+ void a038_getFileName_onlySlashes() {
assertNull(FileUtils.getFileName("/"));
assertNull(FileUtils.getFileName("//"));
}
@Test
- void h07_getFileName_windowsPath() {
+ void a039_getFileName_windowsPath() {
assertEquals("test.txt",
FileUtils.getFileName("C:\\path\\to\\test.txt"));
}
//====================================================================================================
- // hasExtension(String, String) tests
+ // hasExtension(String, String)
//====================================================================================================
-
@Test
- void i01_hasExtension_matching() {
+ void a040_hasExtension() {
assertTrue(FileUtils.hasExtension("test.txt", "txt"));
assertTrue(FileUtils.hasExtension("file.java", "java"));
}
@Test
- void i02_hasExtension_notMatching() {
+ void a041_hasExtension_notMatching() {
assertFalse(FileUtils.hasExtension("test.txt", "java"));
assertFalse(FileUtils.hasExtension("file.java", "txt"));
}
@Test
- void i03_hasExtension_noExtension() {
+ void a042_hasExtension_noExtension() {
assertFalse(FileUtils.hasExtension("test", "txt"));
}
@Test
- void i04_hasExtension_nullName() {
+ void a043_hasExtension_nullName() {
assertFalse(FileUtils.hasExtension(null, "txt"));
}
@Test
- void i05_hasExtension_nullExt() {
+ void a044_hasExtension_nullExt() {
assertFalse(FileUtils.hasExtension("test.txt", null));
}
@Test
- void i06_hasExtension_bothNull() {
+ void a045_hasExtension_bothNull() {
assertFalse(FileUtils.hasExtension(null, null));
}
@Test
- void i07_hasExtension_caseSensitive() {
+ void a046_hasExtension_caseSensitive() {
assertFalse(FileUtils.hasExtension("test.TXT", "txt"));
assertTrue(FileUtils.hasExtension("test.TXT", "TXT"));
}
@Test
- void i08_hasExtension_multipleDots() {
+ void a047_hasExtension_multipleDots() {
assertTrue(FileUtils.hasExtension("test.backup.txt", "txt"));
assertFalse(FileUtils.hasExtension("test.backup.txt",
"backup"));
}
//====================================================================================================
- // mkdirs(File, boolean) tests
+ // mkdirs(File, boolean)
//====================================================================================================
-
@Test
- void j01_mkdirs_newDirectory() {
+ void a048_mkdirs() {
var dir = new File(tempDir.toFile(), "newdir");
assertFalse(dir.exists());
var result = FileUtils.mkdirs(dir, false);
@@ -443,7 +434,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void j02_mkdirs_nestedDirectories() {
+ void a049_mkdirs_nestedDirectories() {
var dir = new File(tempDir.toFile(), "dir1/dir2/dir3");
assertFalse(dir.exists());
var result = FileUtils.mkdirs(dir, false);
@@ -453,7 +444,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void j03_mkdirs_existingDirectory_cleanFalse() {
+ void a050_mkdirs_existingDirectory_cleanFalse() {
var dir = new File(tempDir.toFile(), "existingdir");
dir.mkdirs();
assertTrue(dir.exists());
@@ -463,7 +454,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void j04_mkdirs_existingDirectory_cleanTrue() throws IOException {
+ void a051_mkdirs_existingDirectory_cleanTrue() throws IOException {
var dir = new File(tempDir.toFile(), "cleandir");
dir.mkdirs();
var f = new File(dir, "test.txt");
@@ -477,18 +468,17 @@ class FileUtils_Test extends TestBase {
}
@Test
- void j05_mkdirs_null() {
+ void a052_mkdirs_null() {
assertThrows(IllegalArgumentException.class, () -> {
FileUtils.mkdirs((File)null, false);
});
}
//====================================================================================================
- // mkdirs(String, boolean) tests
+ // mkdirs(String, boolean)
//====================================================================================================
-
@Test
- void k01_mkdirs_string_newDirectory() {
+ void a053_mkdirs_string() {
var dir = new File(tempDir.toFile(), "newdir");
var path = dir.getAbsolutePath();
var result = FileUtils.mkdirs(path, false);
@@ -498,7 +488,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void k02_mkdirs_string_nestedDirectories() {
+ void a054_mkdirs_string_nestedDirectories() {
var dir = new File(tempDir.toFile(), "dir1/dir2/dir3");
var path = dir.getAbsolutePath();
var result = FileUtils.mkdirs(path, false);
@@ -508,7 +498,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void k03_mkdirs_string_existingDirectory_cleanTrue() throws IOException
{
+ void a055_mkdirs_string_existingDirectory_cleanTrue() throws
IOException {
var dir = new File(tempDir.toFile(), "cleandir");
dir.mkdirs();
var f = new File(dir, "test.txt");
@@ -521,18 +511,17 @@ class FileUtils_Test extends TestBase {
}
@Test
- void k04_mkdirs_string_null() {
+ void a056_mkdirs_string_null() {
assertThrows(IllegalArgumentException.class, () -> {
FileUtils.mkdirs((String)null, false);
});
}
//====================================================================================================
- // modifyTimestamp(File) tests
+ // modifyTimestamp(File)
//====================================================================================================
-
@Test
- void l01_modifyTimestamp_existingFile() throws IOException,
InterruptedException {
+ void a057_modifyTimestamp() throws IOException, InterruptedException {
var f = new File(tempDir.toFile(), "test.txt");
f.createNewFile();
var originalTime = f.lastModified();
@@ -547,7 +536,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void l02_modifyTimestamp_nonexistentFile() {
+ void a058_modifyTimestamp_nonexistentFile() {
var f = new File(tempDir.toFile(), "nonexistent.txt");
assertFalse(f.exists());
assertThrowsWithMessage(RuntimeException.class, "Could not
modify timestamp", () -> {
@@ -556,7 +545,7 @@ class FileUtils_Test extends TestBase {
}
@Test
- void l03_modifyTimestamp_directory() throws InterruptedException {
+ void a059_modifyTimestamp_directory() throws InterruptedException {
var dir = new File(tempDir.toFile(), "testdir");
dir.mkdirs();
var originalTime = dir.lastModified();
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/IOUtils_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/IOUtils_Test.java
index 8b7afc2183..ac3017a9f0 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/IOUtils_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/IOUtils_Test.java
@@ -21,7 +21,9 @@ import static org.apache.juneau.commons.utils.IOUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import java.io.*;
+import java.nio.charset.*;
import java.nio.file.*;
+import java.util.concurrent.atomic.*;
import java.util.*;
import org.apache.juneau.*;
@@ -44,31 +46,166 @@ class IOUtils_Test extends TestBase {
}
//====================================================================================================
- // read(Path)
+ // close(Object...)
//====================================================================================================
- @Test void a01_readPath() throws IOException {
- var p = new Properties();
- p.load(new
StringReader(read(Paths.get("src/test/resources/files/Test3.properties"))));
- assertEquals("files/Test3.properties", p.get("file"));
+ @Test
+ void a001_close() throws IOException {
+ var is = new TestInputStream("test");
+ var os = new TestOutputStream();
+ var r = new TestReader("test");
+ var w = new TestWriter();
+
+ close(is, os, r, w);
+
+ assertTrue(is.closed);
+ assertTrue(os.closed);
+ assertTrue(r.closed);
+ assertTrue(w.closed);
+
+ // Test with null entries
+ close((Object)null, is, null, os);
+ // Should not throw
+
+ // Test with empty array
+ close();
}
//====================================================================================================
- // pipe(Reader, Writer)
+ // closeQuietly(InputStream)
//====================================================================================================
- @Test void b01_pipe() throws Exception {
- var out = new TestWriter();
- var in = new TestReader("foobar");
+ @Test
+ void a002_closeQuietly_InputStream() {
+ var is = new TestInputStream("test");
+ closeQuietly(is);
+ assertTrue(is.closed);
- pipe(in, out);
- assertTrue(in.closed);
- assertFalse(out.closed);
- assertEquals("foobar", out.toString());
+ // Test with null
+ closeQuietly((InputStream)null);
+ // Should not throw
+ }
+
+
//====================================================================================================
+ // closeQuietly(Object...)
+
//====================================================================================================
+ @Test
+ void a003_closeQuietly_Object() {
+ var is = new TestInputStream("test");
+ var os = new TestOutputStream();
+ var r = new TestReader("test");
+ var w = new TestWriter();
+
+ closeQuietly(is, os, r, w);
+
+ assertTrue(is.closed);
+ assertTrue(os.closed);
+ assertTrue(r.closed);
+ assertTrue(w.closed);
+
+ // Test with null entries
+ closeQuietly((Object)null, is, null, os);
+ // Should not throw
+
+ // Test with empty array
+ closeQuietly();
+ }
+
+
//====================================================================================================
+ // closeQuietly(OutputStream)
+
//====================================================================================================
+ @Test
+ void a004_closeQuietly_OutputStream() {
+ var os = new TestOutputStream();
+ closeQuietly(os);
+ assertTrue(os.closed);
+
+ // Test with null
+ closeQuietly((OutputStream)null);
+ // Should not throw
+ }
+
+
//====================================================================================================
+ // closeQuietly(Reader)
+
//====================================================================================================
+ @Test
+ void a005_closeQuietly_Reader() {
+ var r = new TestReader("test");
+ closeQuietly(r);
+ assertTrue(r.closed);
+
+ // Test with null
+ closeQuietly((Reader)null);
+ // Should not throw
+ }
+
+
//====================================================================================================
+ // closeQuietly(Writer)
+
//====================================================================================================
+ @Test
+ void a006_closeQuietly_Writer() {
+ var w = new TestWriter();
+ closeQuietly(w);
+ assertTrue(w.closed);
+
+ // Test with null
+ closeQuietly((Writer)null);
+ // Should not throw
+ }
+
+
//====================================================================================================
+ // count(InputStream)
+
//====================================================================================================
+ @Test
+ void a007_count_InputStream() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var count = count(is);
+ assertEquals(data.length(), count);
+ assertTrue(is.available() == 0 || is.read() == -1); // Stream
should be closed/consumed
+
+ // Test with null
+ assertEquals(0, count((InputStream)null));
+ }
+
+
//====================================================================================================
+ // count(Reader)
+
//====================================================================================================
+ @Test
+ void a008_count_Reader() throws IOException {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var count = count(r);
+ assertEquals(data.length(), count);
+
+ // Test with null
+ assertEquals(0, count((Reader)null));
+ }
+
+
//====================================================================================================
+ // flush(Object...)
+
//====================================================================================================
+ @Test
+ void a009_flush() throws IOException {
+ var os = new ByteArrayOutputStream();
+ var w = new StringWriter();
+
+ os.write("test".getBytes());
+ w.write("test");
+
+ flush(os, w);
+
+ // Test with null entries
+ flush((Object)null, os, null, w);
+ // Should not throw
+
+ // Test with empty array
+ flush();
}
//====================================================================================================
// loadSystemResourceAsString(String, String...)
//====================================================================================================
- @Test void c01_loadSystemResourceAsString() throws Exception {
+ @Test
+ void a010_loadSystemResourceAsString() throws Exception {
assertNotNull(loadSystemResourceAsString("test1.txt", "."));
assertNull(loadSystemResourceAsString("test2.txt", "."));
assertNull(loadSystemResourceAsString("test3.txt", "sub"));
@@ -78,6 +215,614 @@ class IOUtils_Test extends TestBase {
assertNotNull(loadSystemResourceAsString("test4.txt", "sub"));
}
+
//====================================================================================================
+ // pipe(byte[], OutputStream, int)
+
//====================================================================================================
+ @Test
+ void a011_pipe_byteArray_OutputStream_int() throws IOException {
+ var data = "Hello World".getBytes();
+ var os = new ByteArrayOutputStream();
+ var count = pipe(data, os, -1);
+ assertEquals(data.length, count);
+ assertArrayEquals(data, os.toByteArray());
+
+ // Test with maxBytes
+ os = new ByteArrayOutputStream();
+ count = pipe(data, os, 5);
+ assertEquals(5, count);
+ assertEquals(5, os.toByteArray().length);
+
+ // Test with null
+ assertEquals(0, pipe((byte[])null, os, -1));
+ assertEquals(0, pipe(data, null, -1));
+ }
+
+
//====================================================================================================
+ // pipe(InputStream, OutputStream)
+
//====================================================================================================
+ @Test
+ void a012_pipe_InputStream_OutputStream() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var os = new ByteArrayOutputStream();
+ var count = pipe(is, os);
+ assertEquals(data.length(), count);
+ assertEquals(data, os.toString());
+ assertTrue(is.available() == 0 || is.read() == -1); // Stream
should be closed
+
+ // Test with null
+ assertEquals(0, pipe((InputStream)null, os));
+ assertEquals(0, pipe(is, (OutputStream)null));
+ }
+
+
//====================================================================================================
+ // pipe(InputStream, OutputStream, Consumer<IOException>)
+
//====================================================================================================
+ @Test
+ void a013_pipe_InputStream_OutputStream_Consumer() {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var os = new ByteArrayOutputStream();
+ var exceptionCaught = new AtomicBoolean(false);
+ var count = pipe(is, os, e -> exceptionCaught.set(true));
+ assertEquals(data.length(), count);
+ assertEquals(data, os.toString());
+ assertFalse(exceptionCaught.get());
+ }
+
+
//====================================================================================================
+ // pipe(InputStream, OutputStream, long)
+
//====================================================================================================
+ @Test
+ void a014_pipe_InputStream_OutputStream_long() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var os = new ByteArrayOutputStream();
+ var count = pipe(is, os, -1);
+ assertEquals(data.length(), count);
+ assertEquals(data, os.toString());
+
+ // Test with maxBytes
+ is = new ByteArrayInputStream(data.getBytes());
+ os = new ByteArrayOutputStream();
+ count = pipe(is, os, 5);
+ assertEquals(5, count);
+ assertEquals(5, os.toString().length());
+
+ // Test with null
+ assertEquals(0, pipe((InputStream)null, os, -1));
+ assertEquals(0, pipe(is, null, -1));
+ }
+
+
//====================================================================================================
+ // pipe(InputStream, Writer)
+
//====================================================================================================
+ @Test
+ void a015_pipe_InputStream_Writer() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var w = new StringWriter();
+ var count = pipe(is, w);
+ assertEquals(data.length(), count);
+ assertEquals(data, w.toString());
+
+ // Test with null
+ assertEquals(0, pipe((InputStream)null, w));
+ assertEquals(0, pipe(is, (Writer)null));
+ }
+
+
//====================================================================================================
+ // pipe(InputStream, Writer, Consumer<IOException>)
+
//====================================================================================================
+ @Test
+ void a016_pipe_InputStream_Writer_Consumer() {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var w = new StringWriter();
+ var exceptionCaught = new AtomicBoolean(false);
+ var count = pipe(is, w, e -> exceptionCaught.set(true));
+ assertEquals(data.length(), count);
+ assertEquals(data, w.toString());
+ assertFalse(exceptionCaught.get());
+ }
+
+
//====================================================================================================
+ // pipe(Reader, File)
+
//====================================================================================================
+ @Test
+ void a017_pipe_Reader_File() throws IOException {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var file = File.createTempFile("test", ".txt");
+ try {
+ var count = pipe(r, file);
+ assertEquals(data.length(), count);
+ var content = read(file);
+ assertEquals(data, content);
+ } finally {
+ file.delete();
+ }
+
+ // Test with null
+ assertEquals(0, pipe((Reader)null, file));
+ assertEquals(0, pipe(r, (File)null));
+ }
+
+
//====================================================================================================
+ // pipe(Reader, OutputStream)
+
//====================================================================================================
+ @Test
+ void a018_pipe_Reader_OutputStream() throws IOException {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var os = new ByteArrayOutputStream();
+ var count = pipe(r, os);
+ assertEquals(data.length(), count);
+ assertEquals(data, os.toString());
+
+ // Test with null
+ assertEquals(0, pipe((Reader)null, os));
+ assertEquals(0, pipe(r, (OutputStream)null));
+ }
+
+
//====================================================================================================
+ // pipe(Reader, OutputStream, Consumer<IOException>)
+
//====================================================================================================
+ @Test
+ void a019_pipe_Reader_OutputStream_Consumer() {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var os = new ByteArrayOutputStream();
+ var exceptionCaught = new AtomicBoolean(false);
+ var count = pipe(r, os, e -> exceptionCaught.set(true));
+ assertEquals(data.length(), count);
+ assertEquals(data, os.toString());
+ assertFalse(exceptionCaught.get());
+ }
+
+
//====================================================================================================
+ // pipe(Reader, Writer)
+
//====================================================================================================
+ @Test
+ void a020_pipe_Reader_Writer() throws IOException {
+ var data = "foobar";
+ var in = new TestReader(data);
+ var out = new TestWriter();
+
+ var count = pipe(in, out);
+ assertTrue(in.closed);
+ assertFalse(out.closed);
+ assertEquals(data, out.toString());
+ assertEquals(data.length(), count);
+
+ // Test with null
+ assertEquals(0, pipe((Reader)null, out));
+ assertEquals(0, pipe(in, (Writer)null));
+ }
+
+
//====================================================================================================
+ // pipe(Reader, Writer, Consumer<IOException>)
+
//====================================================================================================
+ @Test
+ void a021_pipe_Reader_Writer_Consumer() {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var w = new StringWriter();
+ var exceptionCaught = new AtomicBoolean(false);
+ var count = pipe(r, w, e -> exceptionCaught.set(true));
+ assertEquals(data.length(), count);
+ assertEquals(data, w.toString());
+ assertFalse(exceptionCaught.get());
+ }
+
+
//====================================================================================================
+ // pipeLines(Reader, Writer)
+
//====================================================================================================
+ @Test
+ void a022_pipeLines() throws IOException {
+ var data = "Line 1\nLine 2\nLine 3";
+ var r = new StringReader(data);
+ var w = new StringWriter();
+ var count = pipeLines(r, w);
+ assertEquals(data.length() + 1, count); // +1 for final newline
+ assertTrue(w.toString().contains("Line 1"));
+ assertTrue(w.toString().contains("Line 2"));
+ assertTrue(w.toString().contains("Line 3"));
+
+ // Test with null
+ assertEquals(0, pipeLines((Reader)null, w));
+ assertEquals(0, pipeLines(r, null));
+ }
+
+
//====================================================================================================
+ // read(byte[])
+
//====================================================================================================
+ @Test
+ void a023_read_byteArray() {
+ var data = "Hello World";
+ var bytes = data.getBytes();
+ var result = read(bytes);
+ assertEquals(data, result);
+
+ // Test with null
+ assertNull(read((byte[])null));
+ }
+
+
//====================================================================================================
+ // read(byte[], Charset)
+
//====================================================================================================
+ @Test
+ void a024_read_byteArray_Charset() {
+ var data = "Hello World";
+ var bytes = data.getBytes(StandardCharsets.UTF_8);
+ var result = read(bytes, StandardCharsets.UTF_8);
+ assertEquals(data, result);
+
+ // Test with different charset
+ bytes = data.getBytes(StandardCharsets.ISO_8859_1);
+ result = read(bytes, StandardCharsets.ISO_8859_1);
+ assertEquals(data, result);
+
+ // Test with null
+ assertNull(read((byte[])null, StandardCharsets.UTF_8));
+ }
+
+
//====================================================================================================
+ // read(File)
+
//====================================================================================================
+ @Test
+ void a025_read_File() throws IOException {
+ var p = new Properties();
+ p.load(new
StringReader(read(Paths.get("src/test/resources/files/Test3.properties").toFile())));
+ assertEquals("files/Test3.properties", p.get("file"));
+
+ // Test with null
+ assertNull(read((File)null));
+
+ // Test with non-existent file
+ var nonExistent = new File("nonexistent.txt");
+ assertNull(read(nonExistent));
+ }
+
+
//====================================================================================================
+ // read(InputStream)
+
//====================================================================================================
+ @Test
+ void a026_read_InputStream() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var result = read(is);
+ assertEquals(data, result);
+
+ // Test with null
+ assertNull(read((InputStream)null));
+ }
+
+
//====================================================================================================
+ // read(InputStream, Charset)
+
//====================================================================================================
+ @Test
+ void a027_read_InputStream_Charset() throws IOException {
+ var data = "Hello World";
+ var is = new
ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
+ var result = read(is, StandardCharsets.UTF_8);
+ assertEquals(data, result);
+
+ // Test with null
+ assertNull(read((InputStream)null, StandardCharsets.UTF_8));
+ }
+
+
//====================================================================================================
+ // read(InputStream, Charset, Consumer<IOException>)
+
//====================================================================================================
+ @Test
+ void a028_read_InputStream_Charset_Consumer() {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var exceptionCaught = new AtomicBoolean(false);
+ var result = read(is, StandardCharsets.UTF_8, e ->
exceptionCaught.set(true));
+ assertEquals(data, result);
+ assertFalse(exceptionCaught.get());
+
+ // Test with null
+ assertNull(read((InputStream)null, StandardCharsets.UTF_8, e ->
exceptionCaught.set(true)));
+ }
+
+
//====================================================================================================
+ // read(InputStream, Consumer<IOException>)
+
//====================================================================================================
+ @Test
+ void a029_read_InputStream_Consumer() {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var exceptionCaught = new AtomicBoolean(false);
+ var result = read(is, e -> exceptionCaught.set(true));
+ assertEquals(data, result);
+ assertFalse(exceptionCaught.get());
+
+ // Test with null
+ assertNull(read((InputStream)null, e ->
exceptionCaught.set(true)));
+ }
+
+
//====================================================================================================
+ // read(InputStream, int)
+
//====================================================================================================
+ @Test
+ void a030_read_InputStream_int() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ // Note: maxBytes is used for buffer sizing, not limiting the
read
+ var result = read(is, 5);
+ assertEquals(data, result); // Reads all bytes
+
+ // Test with -1 (read all)
+ is = new ByteArrayInputStream(data.getBytes());
+ result = read(is, -1);
+ assertEquals(data, result);
+
+ // Test with null
+ assertNull(read((InputStream)null, 10));
+ }
+
+
//====================================================================================================
+ // read(InputStream, int, Charset)
+
//====================================================================================================
+ @Test
+ void a031_read_InputStream_int_Charset() throws IOException {
+ var data = "Hello World";
+ var is = new
ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
+ // Note: maxBytes is used for buffer sizing, not limiting the
read
+ var result = read(is, 5, StandardCharsets.UTF_8);
+ assertEquals(data, result); // Reads all bytes
+
+ // Test with null
+ assertNull(read((InputStream)null, 10, StandardCharsets.UTF_8));
+ }
+
+
//====================================================================================================
+ // read(InputStream, long)
+
//====================================================================================================
+ @Test
+ void a032_read_InputStream_long() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ // Note: maxBytes is used for buffer sizing, not limiting the
read
+ var result = read(is, 5L);
+ assertEquals(data, result); // Reads all bytes
+
+ // Test with null
+ assertNull(read((InputStream)null, 10L));
+ }
+
+
//====================================================================================================
+ // read(Object)
+
//====================================================================================================
+ @Test
+ void a033_read_Object() throws IOException {
+ var data = "Hello World";
+
+ // Test with Reader
+ var r = new StringReader(data);
+ assertEquals(data, read(r));
+
+ // Test with InputStream
+ var is = new ByteArrayInputStream(data.getBytes());
+ assertEquals(data, read(is));
+
+ // Test with File
+ var file = File.createTempFile("test", ".txt");
+ try {
+ try (var w = new FileWriter(file)) {
+ w.write(data);
+ }
+ assertEquals(data, read(file));
+ } finally {
+ file.delete();
+ }
+
+ // Test with byte[]
+ var bytes = data.getBytes();
+ assertEquals(data, read(bytes));
+
+ // Test with null
+ assertNull(read((Object)null));
+
+ // Test with invalid type
+ assertThrows(IllegalArgumentException.class, () -> read(new
Object()));
+ }
+
+
//====================================================================================================
+ // read(Path)
+
//====================================================================================================
+ @Test
+ void a034_read_Path() throws IOException {
+ var p = new Properties();
+ p.load(new
StringReader(read(Paths.get("src/test/resources/files/Test3.properties"))));
+ assertEquals("files/Test3.properties", p.get("file"));
+
+ // Test with null
+ assertNull(read((Path)null));
+
+ // Test with non-existent path
+ var nonExistent = Paths.get("nonexistent.txt");
+ assertNull(read(nonExistent));
+ }
+
+
//====================================================================================================
+ // read(Reader)
+
//====================================================================================================
+ @Test
+ void a035_read_Reader() throws IOException {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var result = read(r);
+ assertEquals(data, result);
+
+ // Test with null
+ assertNull(read((Reader)null));
+ }
+
+
//====================================================================================================
+ // read(Reader, Consumer<IOException>)
+
//====================================================================================================
+ @Test
+ void a036_read_Reader_Consumer() {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var exceptionCaught = new AtomicBoolean(false);
+ var result = read(r, e -> exceptionCaught.set(true));
+ assertEquals(data, result);
+ assertFalse(exceptionCaught.get());
+
+ // Test with null
+ assertNull(read((Reader)null, e -> exceptionCaught.set(true)));
+ }
+
+
//====================================================================================================
+ // read(Reader, long)
+
//====================================================================================================
+ @Test
+ void a037_read_Reader_long() throws IOException {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var result = read(r, data.length());
+ assertEquals(data, result);
+
+ // Test with -1 (unknown length)
+ r = new StringReader(data);
+ result = read(r, -1);
+ assertEquals(data, result);
+
+ // Test with null
+ assertNull(read((Reader)null, 10L));
+ }
+
+
//====================================================================================================
+ // readBytes(File)
+
//====================================================================================================
+ @Test
+ void a038_readBytes_File() throws IOException {
+ var data = "Hello World";
+ var file = File.createTempFile("test", ".txt");
+ try {
+ try (var w = new FileWriter(file)) {
+ w.write(data);
+ }
+ var bytes = readBytes(file);
+ assertArrayEquals(data.getBytes(), bytes);
+ } finally {
+ file.delete();
+ }
+
+ // Test with null
+ var bytes = readBytes((File)null);
+ assertArrayEquals(new byte[0], bytes);
+
+ // Test with non-existent file
+ var nonExistent = new File("nonexistent.txt");
+ bytes = readBytes(nonExistent);
+ assertArrayEquals(new byte[0], bytes);
+ }
+
+
//====================================================================================================
+ // readBytes(File, int)
+
//====================================================================================================
+ @Test
+ void a039_readBytes_File_int() throws IOException {
+ var data = "Hello World";
+ var file = File.createTempFile("test", ".txt");
+ try {
+ try (var w = new FileWriter(file)) {
+ w.write(data);
+ }
+ // Note: maxBytes is used for buffer sizing, not
limiting the read
+ var bytes = readBytes(file, 5);
+ assertArrayEquals(data.getBytes(), bytes); // Reads all
bytes
+ } finally {
+ file.delete();
+ }
+
+ // Test with null
+ var bytes = readBytes((File)null, 10);
+ assertArrayEquals(new byte[0], bytes);
+ }
+
+
//====================================================================================================
+ // readBytes(InputStream)
+
//====================================================================================================
+ @Test
+ void a040_readBytes_InputStream() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ var bytes = readBytes(is);
+ assertArrayEquals(data.getBytes(), bytes);
+
+ // Test with null
+ var bytes2 = readBytes((InputStream)null);
+ assertArrayEquals(new byte[0], bytes2);
+ }
+
+
//====================================================================================================
+ // readBytes(InputStream, int)
+
//====================================================================================================
+ @Test
+ void a041_readBytes_InputStream_int() throws IOException {
+ var data = "Hello World";
+ var is = new ByteArrayInputStream(data.getBytes());
+ // Note: maxBytes is used for buffer sizing, not limiting the
read
+ var bytes = readBytes(is, 5);
+ assertArrayEquals(data.getBytes(), bytes); // Reads all bytes
+
+ // Test with -1 (read all)
+ is = new ByteArrayInputStream(data.getBytes());
+ bytes = readBytes(is, -1);
+ assertArrayEquals(data.getBytes(), bytes);
+
+ // Test with null
+ var bytes2 = readBytes((InputStream)null, 10);
+ assertArrayEquals(new byte[0], bytes2);
+ }
+
+
//====================================================================================================
+ // readBytes(Reader)
+
//====================================================================================================
+ @Test
+ void a042_readBytes_Reader() throws IOException {
+ var data = "Hello World";
+ var r = new StringReader(data);
+ var bytes = readBytes(r);
+ assertArrayEquals(data.getBytes(), bytes);
+
+ // Test with null
+ var bytes2 = readBytes((Reader)null);
+ assertArrayEquals(new byte[0], bytes2);
+ }
+
+
//====================================================================================================
+ // toBufferedReader(Reader)
+
//====================================================================================================
+ @Test
+ void a043_toBufferedReader() {
+ var r = new StringReader("test");
+ var br = toBufferedReader(r);
+ assertSame(r, br); // StringReader should be returned as-is
+
+ // Test with BufferedReader
+ var br2 = new BufferedReader(new StringReader("test"));
+ var br3 = toBufferedReader(br2);
+ assertSame(br2, br3);
+
+ // Test with other Reader
+ var fr = new StringReader("test");
+ var br4 = toBufferedReader(fr);
+ assertSame(fr, br4); // StringReader should be returned as-is
+
+ // Test with FileReader (not StringReader or BufferedReader)
+ // We can't easily test this without creating a file, but we
can test the logic
+ // Test with null
+ assertNull(toBufferedReader(null));
+ }
+
//====================================================================================================
// Test helper classes
//====================================================================================================
@@ -135,4 +880,5 @@ class IOUtils_Test extends TestBase {
return new String(this.toByteArray(), UTF8);
}
}
-}
\ No newline at end of file
+}
+