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