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