using System; using System.IO; using System.Collections; using System.Reflection; using System.Threading; using IBatisNet.Common.Utilities; using IBatisNet.Common.Test.NUnit; using NUnit.Framework; namespace IBatisNet.Common.Test.NUnit.CommonTests.ConfigWatcher { /// /// Summary description for ConfigWatcherTest. /// [TestFixture] public class ConfigWatcherTest { private bool _hasChanged = false; #region SetUp & TearDown /// /// SetUp /// [SetUp] public void SetUp() { _hasChanged = false; } /// /// TearDown /// [TearDown] public void Dispose() { } #endregion #region Test ConfigurationWatcher /// /// ConfigurationWatcher Test /// [Test] public void ConfigurationWatcherTest() { Assembly a = Assembly.GetExecutingAssembly(); Uri uri = new System.Uri(a.CodeBase); string binDirectory = Path.GetDirectoryName(uri.LocalPath); string fileName = "test.config"; // Configure a watcher ConfigureHandler configureDelegate = new ConfigureHandler( Configure ); ConfigWatcherHandler.ClearFilesMonitored(); ConfigWatcherHandler.AddFileToWatch( Resources.GetFileInfo( fileName) ); TimerCallback callBakDelegate = new TimerCallback( ConfigWatcherTest.OnConfigFileChange ); StateConfig state = new StateConfig(); state.FileName = fileName; state.ConfigureHandler = configureDelegate; new ConfigWatcherHandler( callBakDelegate, state ); FileInfo fi = Resources.GetFileInfo( fileName); fi.LastWriteTime = DateTime.Now; fi.Refresh(); // Let's give a small bit of time for the change to propagate. // The ConfigWatcherHandler class has a timer which // waits for 500 Millis before delivering // the event notification. System.Threading.Thread.Sleep(600); Assert.IsTrue(_hasChanged); } protected void Configure(object obj) { _hasChanged = true; } /// /// Called when the configuration has been updated. /// /// The state config. public static void OnConfigFileChange(object obj) { StateConfig state = (StateConfig)obj; state.ConfigureHandler(null); } #endregion } }