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.assertSame;
20  import static org.mockito.Mockito.mock;
21  import static org.mockito.Mockito.verify;
22  import static org.mockito.Mockito.verifyNoMoreInteractions;
23  import static org.mockito.Mockito.when;
24  
25  import java.net.URL;
26  
27  import org.apache.commons.configuration2.ConfigurationAssert;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test class for {@code FileSystemLocationStrategy}.
33   */
34  public class TestFileSystemLocationStrategy {
35  
36      /** The strategy to be tested. */
37      private FileSystemLocationStrategy strategy;
38  
39      @BeforeEach
40      public void setUp() throws Exception {
41          strategy = new FileSystemLocationStrategy();
42      }
43  
44      /**
45       * Tests a locate() operation.
46       */
47      @Test
48      void testLocate() {
49          final FileSystem fs = mock(FileSystem.class);
50          final URL url = ConfigurationAssert.getTestURL("test.xml");
51          final String basePath = "testBasePath";
52          final String fileName = "testFileName.txt";
53  
54          when(fs.locateFromURL(basePath, fileName)).thenReturn(url);
55  
56          final FileLocator locator = FileLocatorUtils.fileLocator().basePath(basePath).fileName(fileName).fileSystem(FileLocatorUtils.DEFAULT_FILE_SYSTEM)
57              .sourceURL(ConfigurationAssert.getTestURL("test.properties")).create();
58  
59          assertSame(url, strategy.locate(fs, locator));
60  
61          verify(fs).locateFromURL(basePath, fileName);
62          verifyNoMoreInteractions(fs);
63      }
64  }