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  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   * Tests {@link FileObjectUtils}.
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 }