001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.configuration.reloading; 019 020 import static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.assertFalse; 022 import static org.junit.Assert.assertNull; 023 import static org.junit.Assert.assertTrue; 024 025 import java.io.File; 026 import java.io.FileWriter; 027 028 import org.apache.commons.configuration.ConfigurationException; 029 import org.apache.commons.configuration.FileSystem; 030 import org.apache.commons.configuration.PropertiesConfiguration; 031 import org.apache.commons.configuration.VFSFileSystem; 032 import org.junit.After; 033 import org.junit.Before; 034 import org.junit.Test; 035 036 /** 037 * Test case for the VFSFileMonitorReloadingStrategy class. 038 * 039 * @author Ralph Goers 040 * @version $Id: TestVFSFileChangedReloadingStrategy.java 1225909 2011-12-30 20:09:00Z oheger $ 041 */ 042 public class TestVFSFileChangedReloadingStrategy 043 { 044 /** Constant for the name of a test properties file.*/ 045 private static final String TEST_FILE = "test.properties"; 046 047 @Before 048 public void setUp() throws Exception 049 { 050 FileSystem.setDefaultFileSystem(new VFSFileSystem()); 051 } 052 053 @After 054 public void tearDown() throws Exception 055 { 056 FileSystem.resetDefaultFileSystem(); 057 } 058 059 @Test 060 public void testAutomaticReloading() throws Exception 061 { 062 // create a new configuration 063 File file = new File("target/testReload.properties"); 064 065 if (file.exists()) 066 { 067 file.delete(); 068 } 069 070 // create the configuration file 071 FileWriter out = new FileWriter(file); 072 out.write("string=value1"); 073 out.flush(); 074 out.close(); 075 076 // load the configuration 077 PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties"); 078 VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy(); 079 strategy.setRefreshDelay(500); 080 config.setReloadingStrategy(strategy); 081 assertEquals("Initial value", "value1", config.getString("string")); 082 083 Thread.sleep(2000); 084 085 // change the file 086 out = new FileWriter(file); 087 out.write("string=value2"); 088 out.flush(); 089 out.close(); 090 091 // test the automatic reloading 092 assertEquals("Modified value with enabled reloading", "value2", config.getString("string")); 093 } 094 095 @Test 096 public void testNewFileReloading() throws Exception 097 { 098 // create a new configuration 099 File file = new File("target/testReload.properties"); 100 101 if (file.exists()) 102 { 103 file.delete(); 104 } 105 106 PropertiesConfiguration config = new PropertiesConfiguration(); 107 config.setFile(file); 108 VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy(); 109 strategy.setRefreshDelay(500); 110 config.setReloadingStrategy(strategy); 111 112 assertNull("Initial value", config.getString("string")); 113 114 // change the file 115 FileWriter out = new FileWriter(file); 116 out.write("string=value1"); 117 out.flush(); 118 out.close(); 119 120 Thread.sleep(2000); 121 122 // test the automatic reloading 123 assertEquals("Modified value with enabled reloading", "value1", config.getString("string")); 124 } 125 126 @Test 127 public void testGetRefreshDelay() throws Exception 128 { 129 VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy(); 130 strategy.setRefreshDelay(500); 131 assertEquals("refresh delay", 500, strategy.getRefreshDelay()); 132 } 133 134 /** 135 * Tests calling reloadingRequired() multiple times before a reload actually 136 * happens. This test is related to CONFIGURATION-302. 137 */ 138 @Test 139 public void testReloadingRequiredMultipleTimes() 140 throws ConfigurationException 141 { 142 VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy() 143 { 144 @Override 145 protected boolean hasChanged() 146 { 147 // signal always a change 148 return true; 149 } 150 }; 151 strategy.setRefreshDelay(100000); 152 PropertiesConfiguration config = new PropertiesConfiguration(TEST_FILE); 153 config.setReloadingStrategy(strategy); 154 assertTrue("Reloading not required", strategy.reloadingRequired()); 155 assertTrue("Reloading no more required", strategy.reloadingRequired()); 156 strategy.reloadingPerformed(); 157 assertFalse("Reloading still required", strategy.reloadingRequired()); 158 } 159 }