1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.util;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.nio.charset.Charset;
27 import java.nio.charset.StandardCharsets;
28 import java.util.Properties;
29
30 import org.apache.commons.lang3.SystemUtils;
31 import org.apache.commons.vfs2.FileSystemException;
32 import org.apache.commons.vfs2.VFS;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 public class FileObjectUtilsTest {
39
40 private void assertProperties(final Properties p) {
41 assertNotNull(p);
42 assertEquals("1", p.getProperty("one"));
43 assertEquals("2", p.getProperty("two"));
44 }
45
46 @Test
47 public void testExistsNotNull() throws FileSystemException {
48 assertTrue(FileObjectUtils.exists(VFS.getManager().toFileObject(SystemUtils.getJavaIoTmpDir())));
49 }
50
51 @Test
52 public void testgetContentAsStringCharset() throws IOException {
53 assertEquals("This is a test file.",
54 FileObjectUtils.getContentAsString(
55 VFS.getManager().toFileObject(new File("src/test/resources/test-data/read-tests/file1.txt")),
56 StandardCharsets.UTF_8));
57 }
58
59 @Test
60 public void testgetContentAsStringCharsetNull() throws IOException {
61 assertEquals("This is a test file.",
62 FileObjectUtils.getContentAsString(
63 VFS.getManager().toFileObject(new File("src/test/resources/test-data/read-tests/file1.txt")),
64 (Charset) null));
65 }
66
67 @Test
68 public void testgetContentAsStringString() throws IOException {
69 assertEquals("This is a test file.", FileObjectUtils.getContentAsString(
70 VFS.getManager().toFileObject(new File("src/test/resources/test-data/read-tests/file1.txt")), StandardCharsets.UTF_8.name()));
71 }
72
73 @Test
74 public void testgetContentAsStringStringNull() throws IOException {
75 assertEquals("This is a test file.",
76 FileObjectUtils.getContentAsString(
77 VFS.getManager().toFileObject(new File("src/test/resources/test-data/read-tests/file1.txt")),
78 (String) null));
79 }
80
81 @Test
82 public void testNotExistsNotNull() throws FileSystemException {
83 assertFalse(
84 FileObjectUtils.exists(VFS.getManager().toFileObject(new File("This file can't possibly exist, right?"))));
85 }
86
87 @Test
88 public void testNotExistsNull() throws FileSystemException {
89 assertFalse(FileObjectUtils.exists(null));
90 }
91
92 @Test
93 public void testReadProperties() throws FileSystemException, IOException {
94 assertProperties(FileObjectUtils
95 .readProperties(VFS.getManager().toFileObject(new File("src/test/resources/test.properties"))));
96 }
97
98 @Test
99 public void testReadPropertiesInto() throws IOException {
100 final Properties p = new Properties();
101 p.setProperty("extraKey", "extraValue");
102 assertProperties(FileObjectUtils
103 .readProperties(VFS.getManager().toFileObject(new File("src/test/resources/test.properties")), p));
104 assertEquals("extraValue", p.getProperty("extraKey"));
105 }
106
107 }