Added RedirectUsingNamedRoute
[castle.git] / Core / Castle.Core.Tests / StreamLoggerTests.cs
blob2f899b4de0248fbf420d4026ae1da8cbbba10291
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.IO;
19 using System.Text.RegularExpressions;
21 using NUnit.Framework;
23 /// <summary>
24 /// Summary description for StreamLoggerTests.
25 /// </summary>
26 [TestFixture]
27 public class StreamLoggerTests
29 private const string Name = "Test";
31 private StreamLogger logger;
32 private MemoryStream stream;
34 [SetUp]
35 public void SetUp()
37 stream = new MemoryStream();
39 logger = new StreamLogger(Name, stream);
40 logger.Level = LoggerLevel.Debug;
44 [Test]
45 public void Debug()
47 string message = "Debug message";
48 LoggerLevel level = LoggerLevel.Debug;
49 Exception exception = null;
51 logger.Debug(message);
53 ValidateCall(level, message, exception);
56 [Test]
57 public void DebugWithException()
59 string message = "Debug message 2";
60 LoggerLevel level = LoggerLevel.Debug;
61 Exception exception = new Exception();
63 logger.Debug(message, exception);
65 ValidateCall(level, message, exception);
68 [Test]
69 public void Info()
71 string message = "Info message";
72 LoggerLevel level = LoggerLevel.Info;
73 Exception exception = null;
75 logger.Info(message);
77 ValidateCall(level, message, exception);
80 [Test]
81 public void InfoWithException()
83 string message = "Info message 2";
84 LoggerLevel level = LoggerLevel.Info;
85 Exception exception = new Exception();
87 logger.Info(message, exception);
89 ValidateCall(level, message, exception);
92 [Test]
93 public void Warn()
95 string message = "Warn message";
96 LoggerLevel level = LoggerLevel.Warn;
97 Exception exception = null;
99 logger.Warn(message);
101 ValidateCall(level, message, exception);
104 [Test]
105 public void WarnWithException()
107 string message = "Warn message 2";
108 LoggerLevel level = LoggerLevel.Warn;
109 Exception exception = new Exception();
111 logger.Warn(message, exception);
113 ValidateCall(level, message, exception);
116 [Test]
117 public void Error()
119 string message = "Error message";
120 LoggerLevel level = LoggerLevel.Error;
121 Exception exception = null;
123 logger.Error(message);
125 ValidateCall(level, message, exception);
128 [Test]
129 public void ErrorWithException()
131 string message = "Error message 2";
132 LoggerLevel level = LoggerLevel.Error;
133 Exception exception = new Exception();
135 logger.Error(message, exception);
137 ValidateCall(level, message, exception);
140 [Test]
141 public void FatalError()
143 string message = "FatalError message";
144 LoggerLevel level = LoggerLevel.Fatal;
145 Exception exception = null;
147 logger.Fatal(message);
149 ValidateCall(level, message, exception);
152 [Test]
153 public void FatalErrorWithException()
155 string message = "FatalError message 2";
156 LoggerLevel level = LoggerLevel.Fatal;
157 Exception exception = new Exception();
159 logger.Fatal(message, exception);
161 ValidateCall(level, message, exception);
164 private void ValidateCall(LoggerLevel level, String expectedMessage, Exception expectedException)
166 stream.Seek(0, SeekOrigin.Begin);
168 StreamReader reader = new StreamReader(stream);
169 String line = reader.ReadLine();
171 Match match = Regex.Match(line, @"^\[(?<level>[^]]+)\] '(?<name>[^']+)' (?<message>.*)$");
173 Assert.IsTrue(match.Success, "StreamLogger.Log did not match the format");
174 Assert.AreEqual(Name, match.Groups["name"].Value, "StreamLogger.Log did not write the correct Name");
175 Assert.AreEqual(level.ToString(), match.Groups["level"].Value, "StreamLogger.Log did not write the correct Level");
176 Assert.AreEqual(expectedMessage, match.Groups["message"].Value, "StreamLogger.Log did not write the correct Message");
178 line = reader.ReadLine();
180 if (expectedException == null)
182 Assert.IsNull(line);
184 else
186 match = Regex.Match(line, @"^\[(?<level>[^]]+)\] '(?<name>[^']+)' (?<type>[^:]+): (?<message>.*)$");
188 Assert.IsTrue(match.Success, "StreamLogger.Log did not match the format");
189 Assert.AreEqual(Name, match.Groups["name"].Value, "StreamLogger.Log did not write the correct Name");
190 Assert.AreEqual(level.ToString(), match.Groups["level"].Value, "StreamLogger.Log did not write the correct Level");
191 Assert.AreEqual(expectedException.GetType().FullName, match.Groups["type"].Value, "StreamLogger.Log did not write the correct Exception Type");
192 // Assert.AreEqual(expectedException.Message, match.Groups["message"].Value, "StreamLogger.Log did not write the correct Exception Message");