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