Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / System / Test / System.Diagnostics / SourceSwitchTest.cs
1 //
2 // SourceSwitchTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31
32 #if !MOBILE
33
34 #define TRACE
35
36 using NUnit.Framework;
37 using System;
38 using System.Text;
39 using System.Collections;
40 using System.Configuration;
41 using System.Diagnostics;
42
43 namespace MonoTests.System.Diagnostics
44 {
45         [TestFixture]
46         public class SourceSwitchTest
47         {
48                 internal TraceSource traceSource { get; set; }
49                 internal TestTextWriterTraceListener txtTraceListener;
50
51
52                 [Test]
53                 public void ConstructorNullName ()
54                 {
55                         SourceSwitch s = new SourceSwitch (null);
56                         AssertHelper.IsEmpty (s.DisplayName);
57                 }
58
59                 [Test]
60                 public void ConstructorNullDefaultValue ()
61                 {
62                         SourceSwitch s = new SourceSwitch ("foo", null);
63                 }
64
65                 [Test]
66                 public void ConstructorDefault ()
67                 {
68                         SourceSwitch s = new SourceSwitch ("foo");
69                         Assert.AreEqual ("foo", s.DisplayName, "#1");
70                         Assert.AreEqual (SourceLevels.Off, s.Level, "#2");
71                         Assert.AreEqual (0, s.Attributes.Count, "#3");
72                 }
73
74                 [Test]
75                 public void ShouldTrace ()
76                 {
77                         SourceSwitch s = new SourceSwitch ("foo");
78                         s.Level = SourceLevels.Verbose;
79                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Critical), "#1");
80                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Error), "#2");
81                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Warning), "#3");
82                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Information), "#4");
83                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Verbose), "#5");
84                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Start), "#6");
85                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Stop), "#7");
86                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Suspend), "#8");
87                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Resume), "#9");
88                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Transfer), "#10");
89                 }
90
91                 [Test]
92                 public void ShouldTrace2 ()
93                 {
94                         SourceSwitch s = new SourceSwitch ("foo");
95                         s.Level = SourceLevels.ActivityTracing;
96                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Critical), "#1");
97                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Error), "#2");
98                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Warning), "#3");
99                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Information), "#4");
100                         Assert.IsFalse (s.ShouldTrace (TraceEventType.Verbose), "#5");
101                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Start), "#6");
102                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Stop), "#7");
103                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Suspend), "#8");
104                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Resume), "#9");
105                         Assert.IsTrue (s.ShouldTrace (TraceEventType.Transfer), "#10");
106                 }
107
108
109                 [SetUp]                                         
110                 public void InitalizeSourceSwitchTest()
111                 {
112                         // Initializing the TraceSource instance
113                         traceSource = new TraceSource ("LoggingTraceSource");
114                         traceSource.Listeners.Remove("Default");
115                         traceSource.Switch = new SourceSwitch ("MySwitch");
116
117                         // Initializing the TraceListener instance
118                         txtTraceListener = new TestTextWriterTraceListener (Console.Out);
119                         traceSource.Listeners.Add (txtTraceListener); 
120                 }
121
122                 [Test]                                          
123                 public void setSwitchToCritical()
124                 {
125                         traceSource.Switch.Level = SourceLevels.Critical;
126                         LogAllTraceLevels ();
127                         // Switch.Level is Critical so it should log Critical
128                         Assert.AreEqual (1, txtTraceListener.TotalMessageCount);
129                         Assert.AreEqual (1, txtTraceListener.CritialMessageCount);
130                 }
131                         
132                 [Test]                                          
133                 public void setSwitchToError()
134                 {
135                         traceSource.Switch.Level = SourceLevels.Error;
136                         LogAllTraceLevels ();
137                         // Switch.Level is Error so it should log Critical, Error
138                         Assert.AreEqual (2, txtTraceListener.TotalMessageCount);
139                         Assert.AreEqual (1, txtTraceListener.ErrorMessageCount);
140                 }
141
142                 [Test]                                          
143                 public void setSwitchToWarning()
144                 {
145                         traceSource.Switch.Level = SourceLevels.Warning;
146                         LogAllTraceLevels ();
147                         // Switch.Level is Warning so it should log Critical, Error, Warning
148                         Assert.AreEqual (3, txtTraceListener.TotalMessageCount);
149                         Assert.AreEqual (1, txtTraceListener.WarningMessageCount);
150                 }
151
152                 [Test]                                          
153                 public void setSwitchToInfo()
154                 {
155                         traceSource.Switch.Level = SourceLevels.Information;
156                         LogAllTraceLevels ();
157                         // Switch.Level is Information so it should log Critical, Error, Warning, Information
158                         Assert.AreEqual (4, txtTraceListener.TotalMessageCount);
159                         Assert.AreEqual (1, txtTraceListener.InfoMessageCount);
160                 }
161
162                 [Test]                                          
163                 public void setSwitchToVerbose()
164                 {
165                         traceSource.Switch.Level = SourceLevels.Verbose;
166                         LogAllTraceLevels ();
167                         // Switch.Level is Verbose so it should log Critical, Error, Warning, Information, Verbose
168                         Assert.AreEqual (5, txtTraceListener.TotalMessageCount);
169                         Assert.AreEqual (1, txtTraceListener.VerboseMessageCount);
170                 }
171
172                 void LogAllTraceLevels ()
173                 {
174                         traceSource.TraceEvent (TraceEventType.Critical, 123, "Critical Level message.");
175                         traceSource.TraceEvent (TraceEventType.Error, 123, "Error Level message.");
176                         traceSource.TraceEvent (TraceEventType.Warning, 123, "Warning Level message.");
177                         traceSource.TraceEvent (TraceEventType.Information, 123, "Information Level message.");
178                         traceSource.TraceEvent (TraceEventType.Verbose, 123, "Verbose Level message.");
179                         traceSource.Flush ();
180                 }
181         }
182 }
183
184 #endif