Reverted accidental checkin
[castle.git] / Core / Castle.Core.Tests / TraceLoggerTests.cs
blob0c8066542250b441196995f50a7d36416681bfb5
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Core.Logging.Tests
17 using System;
18 using System.Collections.Generic;
19 using System.Text;
20 using System.Diagnostics;
22 using NUnit.Framework;
24 /// <summary>
25 /// Tests the TraceLogger and TraceLoggerFactory classes
26 /// </summary>
27 [TestFixture]
28 public class TraceLoggerTests
30 [SetUp]
31 public void Initialize()
33 Listener.ClearMessages();
36 [TearDown]
37 public void Cleanup()
39 Listener.ClearMessages();
42 [Test]
43 public void WritingToLoggerByType()
45 TraceLoggerFactory factory = new TraceLoggerFactory();
46 ILogger logger = factory.Create(typeof(TraceLoggerTests), LoggerLevel.Debug);
47 logger.Debug("this is a tracing message");
49 Listener.AssertContains("testsrule", "Castle.Core.Logging.Tests.TraceLoggerTests");
50 Listener.AssertContains("testsrule", "this is a tracing message");
53 [Test]
54 public void TracingErrorInformation()
56 TraceLoggerFactory factory = new TraceLoggerFactory();
57 ILogger logger = factory.Create(typeof(TraceLoggerTests), LoggerLevel.Debug);
58 try
60 try
62 string fakearg = "Thisisavalue";
63 throw new ArgumentOutOfRangeException("fakearg", fakearg, "Thisisamessage" );
65 catch (Exception ex)
67 throw new ApplicationException("Inner error is " + ex.Message, ex);
70 catch (Exception ex)
72 logger.Error("Problem handled", ex);
75 Listener.AssertContains("testsrule", "Castle.Core.Logging.Tests.TraceLoggerTests");
76 Listener.AssertContains("testsrule", "Problem handled");
77 Listener.AssertContains("testsrule", "ApplicationException");
78 Listener.AssertContains("testsrule", "Inner error is");
79 Listener.AssertContains("testsrule", "ArgumentOutOfRangeException");
80 Listener.AssertContains("testsrule", "fakearg");
81 Listener.AssertContains("testsrule", "Thisisavalue");
82 Listener.AssertContains("testsrule", "Thisisamessage");
85 [Test]
86 public void FallUpToShorterSourceName()
88 TraceLoggerFactory factory = new TraceLoggerFactory();
89 ILogger logger = factory.Create(typeof(Castle.Core.Configuration.Xml.XmlConfigurationDeserializer), LoggerLevel.Debug);
90 logger.Info("Logging to config namespace");
92 Listener.AssertContains("configrule", "Castle.Core.Configuration.Xml.XmlConfigurationDeserializer");
93 Listener.AssertContains("configrule", "Logging to config namespace");
96 [Test]
97 public void FallUpToDefaultSource()
99 TraceLoggerFactory factory = new TraceLoggerFactory();
100 ILogger logger = factory.Create("System.Xml.XmlDocument", LoggerLevel.Debug);
101 logger.Info("Logging to non-configured namespace namespace");
103 Listener.AssertContains("defaultrule", "System.Xml.XmlDocument");
104 Listener.AssertContains("defaultrule", "Logging to non-configured namespace namespace");
107 #region in-memory listener class
109 /// <summary>
110 /// This class captures trace text and records it to StringBuilders in a static dictionary.
111 /// Used for the sake of unit testing.
112 /// </summary>
113 public class Listener : TraceListener
115 public Listener()
119 public Listener(string initializationData)
121 traceName = initializationData;
124 static Dictionary<string, StringBuilder> traces = new Dictionary<string, StringBuilder>();
125 readonly string traceName;
127 StringBuilder GetStringBuilder()
129 lock (traces)
131 if (!traces.ContainsKey(traceName))
132 traces.Add(traceName, new StringBuilder());
134 return traces[traceName];
138 public override void Write(string message)
140 GetStringBuilder().Append(message);
143 public override void WriteLine(string message)
145 GetStringBuilder().AppendLine(message);
148 public static void AssertContains(string traceName, string expected)
150 Assert.IsTrue(traces.ContainsKey(traceName), "Trace named {0} not found", traceName);
151 Assert.IsTrue(traces[traceName].ToString().Contains(expected), string.Format("Trace text expected to contain '{0}'", expected));
154 public static void ClearMessages()
156 traces = new Dictionary<string, StringBuilder>();
159 #endregion