1 16 17 package org.apache.commons.configuration.reloading; 18 19 import java.io.File ; 20 import java.io.FileWriter ; 21 22 import junit.framework.TestCase; 23 import org.apache.commons.configuration.PropertiesConfiguration; 24 25 31 public class TestFileChangedReloadingStrategy extends TestCase 32 { 33 public void testAutomaticReloading() throws Exception 34 { 35 File file = new File ("target/testReload.properties"); 37 38 if (file.exists()) 39 { 40 file.delete(); 41 } 42 43 FileWriter out = new FileWriter (file); 45 out.write("string=value1"); 46 out.flush(); 47 out.close(); 48 49 PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties"); 51 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy(); 52 strategy.setRefreshDelay(500); 53 config.setReloadingStrategy(strategy); 54 assertEquals("Initial value", "value1", config.getString("string")); 55 56 Thread.sleep(2000); 57 58 out = new FileWriter (file); 60 out.write("string=value2"); 61 out.flush(); 62 out.close(); 63 64 assertEquals("Modified value with enabled reloading", "value2", config.getString("string")); 66 } 67 68 public void testNewFileReloading() throws Exception 69 { 70 File file = new File ("target/testReload.properties"); 72 73 if (file.exists()) 74 { 75 file.delete(); 76 } 77 78 PropertiesConfiguration config = new PropertiesConfiguration(); 79 config.setFile(file); 80 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy(); 81 strategy.setRefreshDelay(500); 82 config.setReloadingStrategy(strategy); 83 84 assertNull("Initial value", config.getString("string")); 85 86 FileWriter out = new FileWriter (file); 88 out.write("string=value1"); 89 out.flush(); 90 out.close(); 91 92 Thread.sleep(2000); 93 94 assertEquals("Modified value with enabled reloading", "value1", config.getString("string")); 96 } 97 98 public void testGetRefreshDelay() 99 { 100 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy(); 101 strategy.setRefreshDelay(500); 102 assertEquals("refresh delay", 500, strategy.getRefreshDelay()); 103 } 104 105 } 106 | Popular Tags |