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