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