Merge pull request #4212 from BrzVlad/fix-ephemeron-leak
[mono.git] / mcs / class / System / Test / System.Diagnostics / SwitchesTest.cs
1 //
2 // SwitchesTest.cs:
3 //              NUnit Test Cases for System.Diagnostics.BooleanSwitch and
4 //              System.Diagnostics.TraceSwitch
5 //
6 // Authors:
7 //   Jonathan Pryor (jonpryor@vt.edu)
8 //   Martin Willemoes Hansen (mwh@sysrq.dk)
9 //
10 // (C) 2002 Jonathan Pryor
11 // (C) 2003 Martin Willemoes Hansen
12 //
13
14 #if !MOBILE && !MONOMAC
15
16 using NUnit.Framework;
17 using System;
18 using System.Text;
19 using System.Collections;
20 using System.Configuration;
21 using System.Diagnostics;
22
23 namespace MonoTests.System.Diagnostics {
24
25         class TestNewSwitch : Switch {
26                 private string v;
27                 private StringBuilder ops = new StringBuilder ();
28                 private const string expected = 
29                         ".ctor\n" +
30                         "get_TestValue\n" +
31                         "OnSwitchSettingChanged\n" +
32                         "GetSetting\n";
33
34                 public TestNewSwitch (string name, string desc)
35                         : base (name, desc)
36                 {
37                         ops.Append (".ctor\n");
38                 }
39
40                 public string TestValue {
41                         get {
42                                 ops.Append ("get_TestValue\n");
43                                 // ensure that the .config file is read in
44                                 int n = base.SwitchSetting;
45                                 // remove warning about unused variable
46                                 n = 5;
47                                 return v;
48                         }
49                 }
50
51                 public string [] ExposeSupportedAttributes ()
52                 {
53                         return GetSupportedAttributes ();
54                 }
55
56                 public bool Validate ()
57                 {
58                         return expected == ops.ToString();
59                 }
60
61                 private void GetSetting ()
62                 {
63                         ops.Append ("GetSetting\n");
64                         IDictionary d = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
65                         if (d != null) {
66                                 d = (IDictionary) d ["switches"];
67                                 if (d != null) {
68                                         v = d [DisplayName].ToString();
69                                 }
70                         }
71                 }
72
73                 protected override void OnSwitchSettingChanged ()
74                 {
75                         ops.Append ("OnSwitchSettingChanged\n");
76
77                         GetSetting ();
78                 }
79         }
80
81         class TestNullSwitch : Switch {
82                 public TestNullSwitch () : base (null, null)
83                 {
84                 }
85         }
86
87         [TestFixture]
88         public class SwitchesTest {
89     
90                 private static BooleanSwitch bon = new BooleanSwitch ("bool-true", "");
91                 private static BooleanSwitch bon2 = new BooleanSwitch ("bool-true-2", "");
92                 private static BooleanSwitch bon3 = new BooleanSwitch ("bool-true-3", "");
93                 private static BooleanSwitch boff = new BooleanSwitch ("bool-false", "");
94                 private static BooleanSwitch boff2 = new BooleanSwitch ("bool-default", "");
95
96                 private static TraceSwitch toff = new TraceSwitch ("trace-off", "");
97                 private static TraceSwitch terror = new TraceSwitch ("trace-error", "");
98                 private static TraceSwitch twarning = new TraceSwitch ("trace-warning", "");
99                 private static TraceSwitch tinfo = new TraceSwitch ("trace-info", "");
100                 private static TraceSwitch tverbose = new TraceSwitch ("trace-verbose", "");
101                 private static TraceSwitch tdefault = new TraceSwitch ("no-value", "");
102                 private static TraceSwitch tsv = new TraceSwitch ("string-value", "");
103                 private static TraceSwitch tnegative = new TraceSwitch ("trace-negative", "");
104
105                 private static TestNewSwitch tns = new TestNewSwitch ("custom-switch", "");
106
107                 [Test]
108                 public void BooleanSwitches ()
109                 {
110                         Assert.IsTrue (bon.Enabled, "#BS:T:1");
111                         Assert.IsTrue (bon2.Enabled, "#BS:T:2");
112                         Assert.IsTrue (bon3.Enabled, "#BS:T:3");
113                         Assert.IsTrue (!boff.Enabled, "#BS:F:1");
114                         Assert.IsTrue (!boff2.Enabled, "#BS:F:2");
115                 }
116
117                 [Test]
118                 public void TraceSwitches ()
119                 {
120                         // The levels 0..4:
121                         CheckTraceSwitch (toff,      false, false, false, false);
122                         CheckTraceSwitch (terror,    true,  false, false, false);
123                         CheckTraceSwitch (twarning,  true,  true,  false, false);
124                         CheckTraceSwitch (tinfo,     true,  true,  true,  false);
125                         CheckTraceSwitch (tverbose,  true,  true,  true,  true);
126
127                         // Default value is 0
128                         CheckTraceSwitch (tdefault,  false, false, false, false);
129
130                         // string value can't be converted to int, so default is 0
131                         CheckTraceSwitch (tsv,       false, false, false, false);
132
133                         // negative number is < 0, so all off
134                         CheckTraceSwitch (tnegative, false, false, false, false);
135                 }
136
137                 private void CheckTraceSwitch (TraceSwitch ts, bool te, bool tw, bool ti, bool tv)
138                 {
139                         string desc = string.Format ("#TS:{0}", ts.DisplayName);
140                         Assert.AreEqual (te, ts.TraceError, desc + ":TraceError");
141                         Assert.AreEqual (tw, ts.TraceWarning, desc + ":TraceWarning");
142                         Assert.AreEqual (ti, ts.TraceInfo, desc + ":TraceInfo");
143                         Assert.AreEqual (tv, ts.TraceVerbose, desc + ":TraceVerbose");
144                 }
145
146                 [Test]
147                 [Ignore ("this test depends on 1.x configuration type")]
148                 public void NewSwitch ()
149                 {
150                         Assert.AreEqual ("42", tns.TestValue, "#NS:TestValue");
151                         Assert.IsTrue (tns.Validate(), "#NS:Validate");
152                 }
153
154                 [Test]
155                 public void GetSupportedAttributes ()
156                 {
157                         Assert.IsNull (tns.ExposeSupportedAttributes ());
158                 }
159
160                 [Test] // no ArgumentNullException happens...
161                 public void BooleanSwitchNullDefaultValue ()
162                 {
163                         new BooleanSwitch ("test", "", null);
164                 }
165
166                 [Test]
167                 public void BooleanSwitchValidDefaultValue ()
168                 {
169                         BooleanSwitch s = new BooleanSwitch ("test", "", "2");
170                         Assert.IsTrue (s.Enabled, "#1");
171                         s = new BooleanSwitch ("test", "", "0");
172                         Assert.IsTrue (!s.Enabled, "#2");
173                         s = new BooleanSwitch ("test", "", "true");
174                         Assert.IsTrue (s.Enabled, "#3");
175                         s = new BooleanSwitch ("test", "", "True");
176                         Assert.IsTrue (s.Enabled, "#4");
177                         s = new BooleanSwitch ("test", "", "truE");
178                         Assert.IsTrue (s.Enabled, "#5");
179                 }
180
181                 [Test]
182                 [ExpectedException (typeof (FormatException))]
183                 public void BooleanSwitchInvalidDefaultValue ()
184                 {
185                         BooleanSwitch s = new BooleanSwitch ("test", "", "hoge");
186                         Assert.IsTrue (!s.Enabled);
187                 }
188
189                 [Test]
190                 public void NullSwitchHasEmptyDisplayNameAndDescription ()
191                 {
192                         var s = new TestNullSwitch ();
193                         AssertHelper.IsEmpty (s.DisplayName);
194                         AssertHelper.IsEmpty (s.Description);
195                 }
196         }
197 }
198
199 #endif