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 AbsoluteNameLocationStrategy}.
32   */
33  public class TestAbsoluteNameLocationStrategy {
34  
35      /** A mock for the file system. */
36      private FileSystem fileSystem;
37  
38      /** The strategy to be tested. */
39      private AbsoluteNameLocationStrategy strategy;
40  
41      @BeforeEach
42      public void setUp() throws Exception {
43          fileSystem = mock(FileSystem.class);
44          strategy = new AbsoluteNameLocationStrategy();
45      }
46  
47      /**
48       * Tests a successful locate() operation.
49       */
50      @Test
51      void testExistingAbsoluteFile() {
52          final File file = ConfigurationAssert.getTestFile("test.xml");
53          final FileLocator locator = FileLocatorUtils.fileLocator().fileName(file.getAbsolutePath()).create();
54          final URL url = strategy.locate(fileSystem, locator);
55          assertEquals(file.getAbsoluteFile(), FileLocatorUtils.fileFromURL(url).getAbsoluteFile());
56      }
57  
58      /**
59       * Tests a locate() operation if no absolute file name is provided.
60       */
61      @Test
62      void testNoAbsoluteFileName() {
63          final FileLocator locator = FileLocatorUtils.fileLocator().fileName("test.xml").create();
64          assertNull(strategy.locate(fileSystem, locator));
65      }
66  
67      /**
68       * Tests a locate() operation if no file name is provided.
69       */
70      @Test
71      void testNoFileName() {
72          final FileLocator locator = FileLocatorUtils.fileLocator().create();
73          assertNull(strategy.locate(fileSystem, locator));
74      }
75  
76      /**
77       * Tests a locate() operation if an absolute file name is provided, but this file does not exist.
78       */
79      @Test
80      void testNonExistingAbsoluteFile() {
81          final File file = ConfigurationAssert.getOutFile("NotExistingFile.tst");
82          final FileLocator locator = FileLocatorUtils.fileLocator().fileName(file.getAbsolutePath()).create();
83          assertNull(strategy.locate(fileSystem, locator));
84      }
85  }