~ubuntu-branches/debian/sid/nunit/sid

« back to all changes in this revision

Viewing changes to src/ClientUtilities/tests/FileWatcherTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2014-09-16 13:43:36 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140916134336-kjxz48tty6lx2ja5
Tags: 2.6.3+dfsg-1
* [c7bd1b5] Imported Upstream version 2.6.3+dfsg
* [bcb4bf8] Move nunit-console-runner to GAC-installed libnunit2.6, 
  don't treat it as a private lib. This lib is signed, and treated 
  as a GAC lib by consumers such as MonoDevelop.
* [7f08e99] Bump version to 2.6.3 as required
* [84535eb] Refreshed patches
* [8479f61] Split package up into per-assembly packages. This makes 
  ABI tracking easier in the future, as we can meaningfully have GAC 
  policy for cases where ABI isn't truly bumped, and no policy for 
  cases where it is. For example, if nunit.framework bumps ABI but 
  nunit.core does not, previously we would need to rebuild everything 
  using NUnit, but under the new split packaging, that rebuild would 
  not be needed for apps only using nunit.core.
* [17a7dc7] Add missing nunit.mocks.dll to nunit.pc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ****************************************************************
2
 
// Copyright 2011, Charlie Poole
3
 
// This is free software licensed under the NUnit license. You may
4
 
// obtain a copy of the license at http://nunit.org
5
 
// ****************************************************************
6
 
 
7
 
using System;
8
 
using System.IO;
9
 
using NUnit.Framework;
10
 
using System.Text;
11
 
using System.Timers;
12
 
 
13
 
namespace NUnit.Util.Tests
14
 
{
15
 
    [TestFixture]
16
 
    [Platform(Exclude = "Win95,Win98,WinMe")]
17
 
    public class FileWatcherTests
18
 
    {
19
 
        private FileWatcher watcher;
20
 
        private CounterEventHandler handler;
21
 
        private static int watcherDelayMs = 100;
22
 
        private string fileName;
23
 
        private string tempFileName;
24
 
 
25
 
        [SetUp]
26
 
        public void CreateFile()
27
 
        {
28
 
            string tempDir = Path.GetTempPath();
29
 
            fileName = Path.Combine(tempDir, "temp.txt");
30
 
            tempFileName = Path.Combine(tempDir, "newTempFile.txt");
31
 
 
32
 
            StreamWriter writer = new StreamWriter(fileName);
33
 
            writer.Write("Hello");
34
 
            writer.Close();
35
 
 
36
 
            handler = new CounterEventHandler();
37
 
            watcher = new FileWatcher(fileName, watcherDelayMs);
38
 
            watcher.Changed += new FileChangedHandler(handler.OnChanged);
39
 
            watcher.Start();
40
 
        }
41
 
 
42
 
        [TearDown]
43
 
        public void DeleteFile()
44
 
        {
45
 
            watcher.Stop();
46
 
            FileInfo fileInfo = new FileInfo(fileName);
47
 
            fileInfo.Delete();
48
 
 
49
 
            FileInfo temp = new FileInfo(tempFileName);
50
 
            if (temp.Exists) temp.Delete();
51
 
        }
52
 
 
53
 
        [Test]
54
 
        [Platform("Linux, Net", Reason="Fails under Mono on Windows")]
55
 
        public void MultipleCloselySpacedChangesTriggerWatcherOnlyOnce()
56
 
        {
57
 
            for (int i = 0; i < 3; i++)
58
 
            {
59
 
                StreamWriter writer = new StreamWriter(fileName, true);
60
 
                writer.WriteLine("Data");
61
 
                writer.Close();
62
 
                System.Threading.Thread.Sleep(20);
63
 
            }
64
 
            WaitForTimerExpiration();
65
 
            Assert.AreEqual(1, handler.Counter);
66
 
            Assert.AreEqual(Path.GetFullPath(fileName), handler.FileName);
67
 
        }
68
 
 
69
 
        [Test]
70
 
        [Platform("Linux, Net", Reason="Fails under Mono on Windows")]
71
 
        public void ChangingFileTriggersWatcher()
72
 
        {
73
 
            StreamWriter writer = new StreamWriter(fileName);
74
 
            writer.Write("Goodbye");
75
 
            writer.Close();
76
 
 
77
 
            WaitForTimerExpiration();
78
 
            Assert.AreEqual(1, handler.Counter);
79
 
            Assert.AreEqual(Path.GetFullPath(fileName), handler.FileName);
80
 
        }
81
 
 
82
 
        [Test]
83
 
        [Platform(Exclude = "Linux", Reason = "Attribute change triggers watcher")]
84
 
        public void ChangingAttributesDoesNotTriggerWatcher()
85
 
        {
86
 
            FileInfo fi = new FileInfo(fileName);
87
 
            FileAttributes attr = fi.Attributes;
88
 
            fi.Attributes = FileAttributes.Hidden | attr;
89
 
 
90
 
            WaitForTimerExpiration();
91
 
            Assert.AreEqual(0, handler.Counter);
92
 
        }
93
 
 
94
 
        [Test]
95
 
        public void CopyingFileDoesNotTriggerWatcher()
96
 
        {
97
 
            FileInfo fi = new FileInfo(fileName);
98
 
            fi.CopyTo(tempFileName);
99
 
            fi.Delete();
100
 
 
101
 
            WaitForTimerExpiration();
102
 
            Assert.AreEqual(0, handler.Counter);
103
 
        }
104
 
 
105
 
        private static void WaitForTimerExpiration()
106
 
        {
107
 
            System.Threading.Thread.Sleep(watcherDelayMs * 2);
108
 
        }
109
 
 
110
 
        private class CounterEventHandler
111
 
        {
112
 
            int counter;
113
 
            String fileName;
114
 
            public int Counter
115
 
            {
116
 
                get { return counter; }
117
 
            }
118
 
            public String FileName
119
 
            {
120
 
                get { return fileName; }
121
 
            }
122
 
 
123
 
            public void OnChanged(String fullPath)
124
 
            {
125
 
                fileName = fullPath;
126
 
                counter++;
127
 
            }
128
 
        }
129
 
    }
130
 
}
 
1
// ****************************************************************
 
2
// Copyright 2011, Charlie Poole
 
3
// This is free software licensed under the NUnit license. You may
 
4
// obtain a copy of the license at http://nunit.org
 
5
// ****************************************************************
 
6
 
 
7
using System;
 
8
using System.IO;
 
9
using NUnit.Framework;
 
10
using System.Text;
 
11
using System.Timers;
 
12
 
 
13
namespace NUnit.Util.Tests
 
14
{
 
15
    [TestFixture]
 
16
    [Platform(Exclude = "Win95,Win98,WinMe")]
 
17
    public class FileWatcherTests
 
18
    {
 
19
        private FileWatcher watcher;
 
20
        private CounterEventHandler handler;
 
21
        private static int watcherDelayMs = 100;
 
22
        private string fileName;
 
23
        private string tempFileName;
 
24
 
 
25
        [SetUp]
 
26
        public void CreateFile()
 
27
        {
 
28
            string tempDir = Path.GetTempPath();
 
29
            fileName = Path.Combine(tempDir, "temp.txt");
 
30
            tempFileName = Path.Combine(tempDir, "newTempFile.txt");
 
31
 
 
32
            StreamWriter writer = new StreamWriter(fileName);
 
33
            writer.Write("Hello");
 
34
            writer.Close();
 
35
 
 
36
            handler = new CounterEventHandler();
 
37
            watcher = new FileWatcher(fileName, watcherDelayMs);
 
38
            watcher.Changed += new FileChangedHandler(handler.OnChanged);
 
39
            watcher.Start();
 
40
        }
 
41
 
 
42
        [TearDown]
 
43
        public void DeleteFile()
 
44
        {
 
45
            watcher.Stop();
 
46
            FileInfo fileInfo = new FileInfo(fileName);
 
47
            fileInfo.Delete();
 
48
 
 
49
            FileInfo temp = new FileInfo(tempFileName);
 
50
            if (temp.Exists) temp.Delete();
 
51
        }
 
52
 
 
53
        [Test]
 
54
        [Platform("Linux, Net", Reason="Fails under Mono on Windows")]
 
55
        public void MultipleCloselySpacedChangesTriggerWatcherOnlyOnce()
 
56
        {
 
57
            for (int i = 0; i < 3; i++)
 
58
            {
 
59
                StreamWriter writer = new StreamWriter(fileName, true);
 
60
                writer.WriteLine("Data");
 
61
                writer.Close();
 
62
                System.Threading.Thread.Sleep(20);
 
63
            }
 
64
            WaitForTimerExpiration();
 
65
            Assert.AreEqual(1, handler.Counter);
 
66
            Assert.AreEqual(Path.GetFullPath(fileName), handler.FileName);
 
67
        }
 
68
 
 
69
        [Test]
 
70
        [Platform("Linux, Net", Reason="Fails under Mono on Windows")]
 
71
        public void ChangingFileTriggersWatcher()
 
72
        {
 
73
            StreamWriter writer = new StreamWriter(fileName);
 
74
            writer.Write("Goodbye");
 
75
            writer.Close();
 
76
 
 
77
            WaitForTimerExpiration();
 
78
            Assert.AreEqual(1, handler.Counter);
 
79
            Assert.AreEqual(Path.GetFullPath(fileName), handler.FileName);
 
80
        }
 
81
 
 
82
        [Test]
 
83
        [Platform(Exclude = "Linux", Reason = "Attribute change triggers watcher")]
 
84
        public void ChangingAttributesDoesNotTriggerWatcher()
 
85
        {
 
86
            FileInfo fi = new FileInfo(fileName);
 
87
            FileAttributes attr = fi.Attributes;
 
88
            fi.Attributes = FileAttributes.Hidden | attr;
 
89
 
 
90
            WaitForTimerExpiration();
 
91
            Assert.AreEqual(0, handler.Counter);
 
92
        }
 
93
 
 
94
        [Test]
 
95
        public void CopyingFileDoesNotTriggerWatcher()
 
96
        {
 
97
            FileInfo fi = new FileInfo(fileName);
 
98
            fi.CopyTo(tempFileName);
 
99
            fi.Delete();
 
100
 
 
101
            WaitForTimerExpiration();
 
102
            Assert.AreEqual(0, handler.Counter);
 
103
        }
 
104
 
 
105
        private static void WaitForTimerExpiration()
 
106
        {
 
107
            System.Threading.Thread.Sleep(watcherDelayMs * 2);
 
108
        }
 
109
 
 
110
        private class CounterEventHandler
 
111
        {
 
112
            int counter;
 
113
            String fileName;
 
114
            public int Counter
 
115
            {
 
116
                get { return counter; }
 
117
            }
 
118
            public String FileName
 
119
            {
 
120
                get { return fileName; }
 
121
            }
 
122
 
 
123
            public void OnChanged(String fullPath)
 
124
            {
 
125
                fileName = fullPath;
 
126
                counter++;
 
127
            }
 
128
        }
 
129
    }
 
130
}