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