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.reloading;
19  
20  import static org.apache.commons.configuration2.TempDirUtils.newFile;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.verify;
26  import static org.mockito.Mockito.verifyNoMoreInteractions;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.File;
30  import java.io.FileWriter;
31  import java.io.IOException;
32  import java.nio.file.Files;
33  
34  import org.apache.commons.configuration2.ConfigurationAssert;
35  import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
36  import org.apache.commons.configuration2.io.FileHandler;
37  import org.apache.commons.configuration2.io.VFSFileSystem;
38  import org.apache.commons.vfs2.FileName;
39  import org.apache.commons.vfs2.FileObject;
40  import org.apache.commons.vfs2.FileSystemException;
41  import org.junit.jupiter.api.Test;
42  import org.junit.jupiter.api.io.TempDir;
43  
44  /**
45   * Test case for the VFSFileHandlerReloadingDetector class.
46   */
47  public class TestVFSFileHandlerReloadingDetector {
48  
49      /** Constant for the name of the test property. */
50      private static final String PROPERTY = "string";
51  
52      /** Constant for the XML fragment to be written. */
53      private static final String FMT_XML = "<configuration><" + PROPERTY + ">%s</" + PROPERTY + "></configuration>";
54  
55      /** A folder for temporary files. */
56      @TempDir
57      public File tempFolder;
58  
59      /**
60       * Tests whether the refresh delay is correctly passed to the base class.
61       */
62      @Test
63      void testGetRefreshDelay() throws Exception {
64          final long delay = 20130325L;
65          final VFSFileHandlerReloadingDetector strategy = new VFSFileHandlerReloadingDetector(null, delay);
66          assertNotNull(strategy.getFileHandler());
67          assertEquals(delay, strategy.getRefreshDelay());
68      }
69  
70      /**
71       * Tests whether the last modification date of an existing file can be obtained.
72       */
73      @Test
74      void testLastModificationDateExisting() throws IOException {
75          final File file = newFile(tempFolder);
76          writeTestFile(file, "value1");
77          final VFSFileHandlerReloadingDetector strategy = new VFSFileHandlerReloadingDetector();
78          strategy.getFileHandler().setFile(file);
79          strategy.getFileHandler().setFileSystem(new VFSFileSystem());
80          final long modificationDate = strategy.getLastModificationDate();
81          // Workaround OpenJDK 8 and 9 bug JDK-8177809
82          // https://bugs.openjdk.java.net/browse/JDK-8177809
83          final long expectedMillis = Files.getLastModifiedTime(file.toPath()).toMillis();
84          assertEquals(expectedMillis, modificationDate);
85      }
86  
87      /**
88       * Tests whether a file system exception is handled when accessing the file object.
89       */
90      @Test
91      void testLastModificationDateFileSystemEx() throws FileSystemException {
92          final FileObject fo = mock(FileObject.class);
93          final FileName name = mock(FileName.class);
94  
95          when(fo.exists()).thenReturn(Boolean.TRUE);
96          when(fo.getContent()).thenThrow(new FileSystemException("error"));
97          when(fo.getName()).thenReturn(name);
98          when(name.getURI()).thenReturn("someURI");
99  
100         final VFSFileHandlerReloadingDetector strategy = new VFSFileHandlerReloadingDetector() {
101             @Override
102             protected FileObject getFileObject() {
103                 return fo;
104             }
105         };
106         assertEquals(0, strategy.getLastModificationDate());
107 
108         verify(fo).exists();
109         verify(fo).getContent();
110         verify(fo).getName();
111         verify(name).getURI();
112         verifyNoMoreInteractions(fo, name);
113     }
114 
115     /**
116      * Tests whether a non existing file is handled correctly.
117      */
118     @Test
119     void testLastModificationDateNonExisting() {
120         final File file = ConfigurationAssert.getOutFile("NonExistingFile.xml");
121         final FileHandler handler = new FileHandler();
122         handler.setFileSystem(new VFSFileSystem());
123         handler.setFile(file);
124         final VFSFileHandlerReloadingDetector strategy = new VFSFileHandlerReloadingDetector(handler);
125         assertEquals(0, strategy.getLastModificationDate());
126     }
127 
128     /**
129      * Tests whether an undefined file handler is handler correctly.
130      */
131     @Test
132     void testLastModificationDateUndefinedHandler() {
133         final VFSFileHandlerReloadingDetector strategy = new VFSFileHandlerReloadingDetector();
134         assertEquals(0, strategy.getLastModificationDate());
135     }
136 
137     /**
138      * Tests a URI which cannot be resolved.
139      */
140     @Test
141     void testLastModificationDateUnresolvableURI() {
142         final VFSFileHandlerReloadingDetector strategy = new VFSFileHandlerReloadingDetector() {
143             @Override
144             protected String resolveFileURI() {
145                 return null;
146             }
147         };
148         strategy.getFileHandler().setFileSystem(new VFSFileSystem());
149         strategy.getFileHandler().setFileName("test.xml");
150         assertThrows(ConfigurationRuntimeException.class, strategy::getLastModificationDate);
151     }
152 
153     /**
154      * Writes a test configuration file containing a single property with the given value.
155      *
156      * @param file the file to be written
157      * @param value the value of the test property
158      * @throws IOException if an error occurs
159      */
160     private void writeTestFile(final File file, final String value) throws IOException {
161         try (FileWriter out = new FileWriter(file)) {
162             out.write(String.format(FMT_XML, value));
163         }
164     }
165 }