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    *     https://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.configuration2.io;
18  
19  import static org.apache.commons.configuration2.TempDirUtils.newFile;
20  import static org.apache.commons.configuration2.TempDirUtils.newFolder;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  import static org.mockito.Mockito.mock;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.net.URL;
30  
31  import org.apache.commons.lang3.SystemProperties;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.io.TempDir;
35  
36  /**
37   * Test class for {@code HomeDirectoryLocationStrategy}.
38   */
39  public class TestHomeDirectoryLocationStrategy {
40  
41      /** Constant for a test file name. */
42      private static final String FILE_NAME = "test.tst";
43  
44      /** Constant for a base path to be used. */
45      private static final String BASE_PATH = "sub";
46  
47      /** A folder for temporary files. */
48      @TempDir
49      public File tempFolder;
50  
51      /** A mock for the file system. */
52      private FileSystem fileSystem;
53  
54      @BeforeEach
55      public void setUp() throws Exception {
56          fileSystem = mock(FileSystem.class);
57      }
58  
59      /**
60       * Creates a strategy test object which uses the temporary root directory as its home directory.
61       *
62       * @param withBasePath the base path flag
63       * @return the test strategy
64       */
65      private HomeDirectoryLocationStrategy setUpStrategy(final boolean withBasePath) {
66          return new HomeDirectoryLocationStrategy(tempFolder.getAbsolutePath(), withBasePath);
67      }
68  
69      /**
70       * Tests whether default values are correctly set by the constructor.
71       */
72      @Test
73      void testInitDefaults() {
74          final HomeDirectoryLocationStrategy strategy = new HomeDirectoryLocationStrategy();
75          assertEquals(SystemProperties.getUserHome(), strategy.getHomeDirectory());
76          assertFalse(strategy.isEvaluateBasePath());
77      }
78  
79      /**
80       * Tests whether the base is actually evaluated if the flag is set.
81       */
82      @Test
83      void testLocateFailedWithBasePath() throws IOException {
84          newFile(FILE_NAME, tempFolder);
85          final FileLocator locator = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName(FILE_NAME).create();
86          final HomeDirectoryLocationStrategy strategy = setUpStrategy(true);
87          assertNull(strategy.locate(fileSystem, locator));
88      }
89  
90      /**
91       * Tests whether a file can be located if the base path is ignored.
92       */
93      @Test
94      void testLocateSuccessIgnoreBasePath() throws IOException {
95          final File file = newFile(FILE_NAME, tempFolder);
96          final FileLocator locator = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName(FILE_NAME).create();
97          final HomeDirectoryLocationStrategy strategy = setUpStrategy(false);
98          final URL url = strategy.locate(fileSystem, locator);
99          assertEquals(file.getAbsoluteFile(), FileLocatorUtils.fileFromURL(url).getAbsoluteFile());
100     }
101 
102     /**
103      * Tests whether a file in a sub folder can be located.
104      */
105     @Test
106     void testLocateSuccessInSubFolder() throws IOException {
107         final File sub = newFolder(BASE_PATH, tempFolder);
108         final File file = new File(sub, FILE_NAME);
109         assertTrue(file.createNewFile());
110         final FileLocator locator = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName(FILE_NAME).create();
111         final HomeDirectoryLocationStrategy strategy = setUpStrategy(true);
112         final URL url = strategy.locate(fileSystem, locator);
113         assertEquals(file.getAbsoluteFile(), FileLocatorUtils.fileFromURL(url).getAbsoluteFile());
114     }
115 
116     /**
117      * Tests a locate() operation which evaluates the base path if no base path is set.
118      */
119     @Test
120     void testLocateSuccessNoBasePath() throws IOException {
121         final File file = newFile(FILE_NAME, tempFolder);
122         final FileLocator locator = FileLocatorUtils.fileLocator().fileName(FILE_NAME).create();
123         final HomeDirectoryLocationStrategy strategy = setUpStrategy(true);
124         final URL url = strategy.locate(fileSystem, locator);
125         assertEquals(file.getAbsoluteFile(), FileLocatorUtils.fileFromURL(url).getAbsoluteFile());
126     }
127 
128     /**
129      * Tests a locate() operation if no file name is specified.
130      */
131     @Test
132     void testNoFileName() {
133         final FileLocator locator = FileLocatorUtils.fileLocator().basePath(BASE_PATH).create();
134         final HomeDirectoryLocationStrategy strategy = setUpStrategy(true);
135         assertNull(strategy.locate(fileSystem, locator));
136     }
137 }