5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc.
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
36 using NUnit.Framework;
39 using System.Collections;
40 using System.Configuration;
41 using System.Diagnostics;
43 namespace MonoTests.System.Diagnostics
46 public class SourceSwitchTest
48 internal TraceSource traceSource { get; set; }
49 internal TestTextWriterTraceListener txtTraceListener;
53 public void ConstructorNullName ()
55 SourceSwitch s = new SourceSwitch (null);
56 Assert.IsEmpty (s.DisplayName);
60 public void ConstructorNullDefaultValue ()
62 SourceSwitch s = new SourceSwitch ("foo", null);
66 public void ConstructorDefault ()
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");
75 public void ShouldTrace ()
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");
92 public void ShouldTrace2 ()
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");
110 public void InitalizeSourceSwitchTest()
112 // Initializing the TraceSource instance
113 traceSource = new TraceSource ("LoggingTraceSource");
114 traceSource.Listeners.Remove("Default");
115 traceSource.Switch = new SourceSwitch ("MySwitch");
117 // Initializing the TraceListener instance
118 txtTraceListener = new TestTextWriterTraceListener (Console.Out);
119 traceSource.Listeners.Add (txtTraceListener);
123 public void setSwitchToCritical()
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);
133 public void setSwitchToError()
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);
143 public void setSwitchToWarning()
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);
153 public void setSwitchToInfo()
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);
163 public void setSwitchToVerbose()
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);
172 void LogAllTraceLevels ()
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 ();