View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link FileObjectUtils}.
34   *
35   * @since 2.4
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 }