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