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.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotEquals;
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.assertTrue;
24  import static org.mockito.Mockito.mock;
25  
26  import java.net.URL;
27  import java.nio.charset.StandardCharsets;
28  
29  import org.apache.commons.configuration2.ConfigurationAssert;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Test class for {@code FileLocator}.
35   */
36  public class TestFileLocator {
37      /** Constant for a file name. */
38      private static final String FILE_NAME = "test.xml";
39  
40      /** Constant for a base path. */
41      private static final String BASE_PATH = "/etc/test/path/";
42  
43      /** Constant for a test encoding. */
44      private static final String ENCODING = StandardCharsets.UTF_8.name();
45  
46      /** A test URL. */
47      private static URL sourceURL;
48  
49      /** A test file system. */
50      private static FileSystem fileSystem;
51  
52      /** A test location strategy. */
53      private static FileLocationStrategy locationStrategy;
54  
55      /**
56       * Tests whether a locator has the expected properties.
57       *
58       * @param locator the locator to check
59       */
60      private static void checkLocator(final FileLocator locator) {
61          assertEquals(BASE_PATH, locator.getBasePath());
62          assertEquals(FILE_NAME, locator.getFileName());
63          assertEquals(ENCODING, locator.getEncoding());
64          assertEquals(sourceURL.toExternalForm(), locator.getSourceURL().toExternalForm());
65          assertSame(fileSystem, locator.getFileSystem());
66          assertSame(locationStrategy, locator.getLocationStrategy());
67      }
68  
69      @BeforeAll
70      public static void setUpOnce() throws Exception {
71          sourceURL = ConfigurationAssert.getTestURL(FILE_NAME);
72          fileSystem = mock(FileSystem.class);
73          locationStrategy = mock(FileLocationStrategy.class);
74      }
75  
76      /**
77       * Tests the creation of a file locator.
78       */
79      @Test
80      public void testCreateFileLocator() {
81          final FileLocator locator = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName(FILE_NAME).encoding(ENCODING).fileSystem(fileSystem)
82              .sourceURL(sourceURL).locationStrategy(locationStrategy).create();
83          checkLocator(locator);
84      }
85  
86      /**
87       * Tests whether a file locator can be created from a source locator.
88       */
89      @Test
90      public void testCreateFileLocatorFromSource() {
91          final FileLocator locatorSrc = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName("someFile").encoding(ENCODING).fileSystem(fileSystem)
92              .sourceURL(sourceURL).locationStrategy(locationStrategy).create();
93          final FileLocator locator = FileLocatorUtils.fileLocator(locatorSrc).fileName(FILE_NAME).create();
94          checkLocator(locator);
95      }
96  
97      /**
98       * Tests whether an undefined file locator can be created.
99       */
100     @Test
101     public void testCreateFileLocatorUndefined() {
102         final FileLocator locator = FileLocatorUtils.fileLocator().create();
103         assertNull(locator.getBasePath());
104         assertNull(locator.getFileName());
105         assertNull(locator.getSourceURL());
106         assertNull(locator.getEncoding());
107         assertNull(locator.getFileSystem());
108         assertNull(locator.getLocationStrategy());
109     }
110 
111     /**
112      * Tests the equals() implementation of FileLocator if the expected result is false.
113      */
114     @Test
115     public void testFileLocatorEqualsFalse() {
116         final FileLocator loc1 = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName(FILE_NAME).encoding(ENCODING).fileSystem(fileSystem)
117             .sourceURL(sourceURL).locationStrategy(locationStrategy).create();
118         FileLocator loc2 = FileLocatorUtils.fileLocator(loc1).basePath(BASE_PATH + "_other").create();
119         ConfigurationAssert.checkEquals(loc1, loc2, false);
120         loc2 = FileLocatorUtils.fileLocator(loc1).fileName(FILE_NAME + "_other").create();
121         ConfigurationAssert.checkEquals(loc1, loc2, false);
122         loc2 = FileLocatorUtils.fileLocator(loc1).encoding(ENCODING + "_other").create();
123         ConfigurationAssert.checkEquals(loc1, loc2, false);
124         loc2 = FileLocatorUtils.fileLocator(loc1).fileSystem(mock(FileSystem.class)).create();
125         ConfigurationAssert.checkEquals(loc1, loc2, false);
126         loc2 = FileLocatorUtils.fileLocator(loc1).sourceURL(ConfigurationAssert.getTestURL("test.properties")).create();
127         ConfigurationAssert.checkEquals(loc1, loc2, false);
128         loc2 = FileLocatorUtils.fileLocator(loc1).locationStrategy(mock(FileLocationStrategy.class)).create();
129         ConfigurationAssert.checkEquals(loc1, loc2, false);
130     }
131 
132     /**
133      * Tests equals() with a null object.
134      */
135     @Test
136     public void testFileLocatorEqualsNull() {
137         final FileLocator loc = FileLocatorUtils.fileLocator().fileName(FILE_NAME).create();
138         assertNotEquals(null, loc);
139     }
140 
141     /**
142      * Tests equals() with an object from another class.
143      */
144     @Test
145     public void testFileLocatorEqualsOtherClass() {
146         final FileLocator loc = FileLocatorUtils.fileLocator().fileName(FILE_NAME).create();
147         assertNotEquals(loc, this);
148     }
149 
150     /**
151      * Tests the equals() implementation of FileLocator if the expected result is true.
152      */
153     @Test
154     public void testFileLocatorEqualsTrue() {
155         FileLocator loc1 = FileLocatorUtils.fileLocator().create();
156         ConfigurationAssert.checkEquals(loc1, loc1, true);
157         FileLocator loc2 = FileLocatorUtils.fileLocator().create();
158         ConfigurationAssert.checkEquals(loc1, loc2, true);
159         loc1 = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName(FILE_NAME).encoding(ENCODING).fileSystem(fileSystem).sourceURL(sourceURL)
160             .locationStrategy(locationStrategy).create();
161         loc2 = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName(FILE_NAME).encoding(ENCODING).fileSystem(fileSystem).sourceURL(sourceURL)
162             .locationStrategy(locationStrategy).create();
163         ConfigurationAssert.checkEquals(loc1, loc2, true);
164     }
165 
166     /**
167      * Tests the string representation of a locator.
168      */
169     @Test
170     public void testFileLocatorToString() {
171         final FileLocator loc = FileLocatorUtils.fileLocator().basePath(BASE_PATH).fileName(FILE_NAME).encoding(ENCODING).fileSystem(fileSystem)
172             .sourceURL(sourceURL).locationStrategy(locationStrategy).create();
173         final String s = loc.toString();
174         assertTrue(s.contains("fileName=" + FILE_NAME));
175         assertTrue(s.contains("basePath=" + BASE_PATH));
176         assertTrue(s.contains("sourceURL=" + sourceURL));
177         assertTrue(s.contains("encoding=" + ENCODING));
178         assertTrue(s.contains("fileSystem=" + fileSystem));
179         assertTrue(s.contains("locationStrategy=" + locationStrategy));
180     }
181 }