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  
18  package org.apache.commons.configuration2.io;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.mockito.Mockito.mock;
25  
26  import java.net.URL;
27  import java.util.HashSet;
28  import java.util.Set;
29  
30  import org.apache.commons.configuration2.ConfigurationAssert;
31  import org.apache.commons.configuration2.ex.ConfigurationDeniedException;
32  import org.apache.commons.configuration2.io.AbstractFileLocationStrategy.StrategyBuilder;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Test class for {@code ProvidedURLLocationStrategy}.
38   */
39  public class TestProvidedURLLocationStrategy {
40  
41      /** The strategy to be tested. */
42      private ProvidedURLLocationStrategy strategy;
43  
44      @BeforeEach
45      public void setUp() throws Exception {
46          strategy = new ProvidedURLLocationStrategy();
47      }
48  
49      /**
50       * Tests a failed locate() operation.
51       */
52      @Test
53      void testLocateFail() {
54          final FileSystem fs = mock(FileSystem.class);
55          final FileLocator locator = FileLocatorUtils.fileLocator().basePath("somePath").fileName("someFile.xml").create();
56          assertNull(strategy.locate(fs, locator));
57      }
58  
59      /**
60       * Tests a failed locate() operation.
61       */
62      @Test
63      void testLocateFailDefaultBuilder() {
64          final FileSystem fs = mock(FileSystem.class);
65          final FileLocator locator = FileLocatorUtils.fileLocator().basePath("somePath").fileName("someFile.xml").create();
66          assertNull(ProvidedURLLocationStrategy.builder().get().locate(fs, locator));
67      }
68  
69      @Test
70      void testLocateSchemes() {
71          final FileSystem fs = mock(FileSystem.class);
72          final URL url = ConfigurationAssert.getTestURL("test.xml");
73          final FileLocator locator = FileLocatorUtils.fileLocator().sourceURL(url).create();
74          final Set<String> schemes = new HashSet<>();
75          final StrategyBuilder<ProvidedURLLocationStrategy> builder = ProvidedURLLocationStrategy.builder();
76          assertEquals("file", builder.setSchemes(schemes).get().locate(fs, locator).getProtocol());
77          schemes.add("foo");
78          assertThrows(ConfigurationDeniedException.class, () -> builder.setSchemes(schemes).get().locate(fs, locator));
79          schemes.add("file");
80          assertSame(url, builder.setSchemes(schemes).get().locate(fs, locator));
81      }
82  
83      /**
84       * Tests a successful locate() operation.
85       */
86      @Test
87      void testLocateSuccess() {
88          final FileSystem fs = mock(FileSystem.class);
89          final URL url = ConfigurationAssert.getTestURL("test.xml");
90          final FileLocator locator = FileLocatorUtils.fileLocator().sourceURL(url).create();
91          assertSame(url, strategy.locate(fs, locator));
92      }
93  
94      /**
95       * Tests a successful locate() operation.
96       */
97      @Test
98      void testLocateSuccessDefaultBuilder() {
99          final FileSystem fs = mock(FileSystem.class);
100         final URL url = ConfigurationAssert.getTestURL("test.xml");
101         final FileLocator locator = FileLocatorUtils.fileLocator().sourceURL(url).create();
102         assertSame(url, ProvidedURLLocationStrategy.builder().get().locate(fs, locator));
103     }
104 }