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.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.mockito.Mockito.mock;
22  
23  import java.io.File;
24  import java.net.URL;
25  
26  import org.apache.commons.configuration2.ConfigurationAssert;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class for {@code BasePathLocationStrategy}.
32   */
33  public class TestBasePathLocationStrategy {
34  
35      /** Constant for the name of the test file. */
36      private static final String TEST_FILE = "test.xml";
37  
38      /**
39       * Checks whether the passed in URL points to the expected test file.
40       *
41       * @param url the URL to be checked
42       */
43      private static void checkURL(final URL url) {
44          assertEquals(FileLocatorUtils.fileFromURL(url).getAbsoluteFile(), ConfigurationAssert.getTestFile(TEST_FILE).getAbsoluteFile());
45      }
46  
47      /** A mock for the file system. */
48      private FileSystem fileSystem;
49  
50      /** The strategy to be tested. */
51      private BasePathLocationStrategy strategy;
52  
53      @BeforeEach
54      public void setUp() throws Exception {
55          fileSystem = mock(FileSystem.class);
56          strategy = new BasePathLocationStrategy();
57      }
58  
59      /**
60       * Tests a successful locate() operation with a valid base path and file name.
61       */
62      @Test
63      void testLocateSuccess() {
64          final File path = ConfigurationAssert.TEST_DIR;
65          final FileLocator locator = FileLocatorUtils.fileLocator().basePath(path.getAbsolutePath()).fileName(TEST_FILE).create();
66          checkURL(strategy.locate(fileSystem, locator));
67      }
68  
69      /**
70       * Tests whether a prefix for relative file names is handled correctly.
71       */
72      @Test
73      void testLocateSuccessRelativePrefix() {
74          final File path = ConfigurationAssert.TEST_DIR;
75          final FileLocator locator = FileLocatorUtils.fileLocator().basePath(path.getAbsolutePath()).fileName("." + File.separator + TEST_FILE).create();
76          checkURL(strategy.locate(fileSystem, locator));
77      }
78  
79      /**
80       * Tests whether a null base path is handled correctly.
81       */
82      @Test
83      void testNullBasePath() {
84          final FileLocator locator = FileLocatorUtils.fileLocator().fileName(TEST_FILE).create();
85          assertNull(strategy.locate(fileSystem, locator));
86      }
87  
88      /**
89       * Tests a locate() operation if no file name is provided.
90       */
91      @Test
92      void testNullFileName() {
93          final FileLocator locator = FileLocatorUtils.fileLocator().basePath(ConfigurationAssert.getTestFile(TEST_FILE).getAbsolutePath()).create();
94          assertNull(strategy.locate(fileSystem, locator));
95      }
96  }