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.assertNull;
20  import static org.mockito.Mockito.mock;
21  
22  import java.net.URL;
23  
24  import org.apache.commons.configuration2.ConfigurationAssert;
25  import org.apache.commons.configuration2.XMLConfiguration;
26  import org.apache.commons.configuration2.builder.fluent.Configurations;
27  import org.apache.commons.configuration2.ex.ConfigurationException;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test class for {@code ClasspathLocationStrategy}.
33   */
34  public class TestClasspathLocationStrategy {
35      /** Constant for a test file name. */
36      private static final String FILE_NAME = "test.xml";
37  
38      /** A mock for the file system. */
39      private FileSystem fileSystem;
40  
41      /** The strategy to be tested. */
42      private ClasspathLocationStrategy strategy;
43  
44      @BeforeEach
45      public void setUp() throws Exception {
46          fileSystem = mock(FileSystem.class);
47          strategy = new ClasspathLocationStrategy();
48      }
49  
50      /**
51       * Tests a failed locate() operation.
52       */
53      @Test
54      public void testLocateFailed() {
55          final FileLocator locator = FileLocatorUtils.fileLocator().fileName("non existing resource name!").create();
56          assertNull(strategy.locate(fileSystem, locator));
57      }
58  
59      /**
60       * Tests a locate() operation if no file name is provided.
61       */
62      @Test
63      public void testLocateNoFileName() {
64          final FileLocator locator = FileLocatorUtils.fileLocator().fileName("").create();
65          assertNull(strategy.locate(fileSystem, locator));
66      }
67  
68      /**
69       * Tests a successful location of a provided resource name.
70       */
71      @Test
72      public void testLocateSuccess() throws ConfigurationException {
73          final FileLocator locator = FileLocatorUtils.fileLocator().fileName(FILE_NAME).basePath("somePath").create();
74          final URL url = strategy.locate(fileSystem, locator);
75          final Configurations configurations = new Configurations();
76          final XMLConfiguration config1 = configurations.xml(url);
77          final XMLConfiguration config2 = configurations.xml(ConfigurationAssert.getTestURL(FILE_NAME));
78          ConfigurationAssert.assertConfigurationEquals(config1, config2);
79      }
80  }