1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using System
.Text
.RegularExpressions
;
21 using NUnit
.Framework
;
24 /// Summary description for StreamLoggerTests.
27 public class StreamLoggerTests
29 private const string Name
= "Test";
31 private StreamLogger logger
;
32 private MemoryStream stream
;
37 stream
= new MemoryStream();
39 logger
= new StreamLogger(Name
, stream
);
40 logger
.Level
= LoggerLevel
.Debug
;
47 string message
= "Debug message";
48 LoggerLevel level
= LoggerLevel
.Debug
;
49 Exception exception
= null;
51 logger
.Debug(message
);
53 ValidateCall(level
, message
, exception
);
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
);
71 string message
= "Info message";
72 LoggerLevel level
= LoggerLevel
.Info
;
73 Exception exception
= null;
77 ValidateCall(level
, message
, exception
);
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
);
95 string message
= "Warn message";
96 LoggerLevel level
= LoggerLevel
.Warn
;
97 Exception exception
= null;
101 ValidateCall(level
, message
, exception
);
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
);
119 string message
= "Error message";
120 LoggerLevel level
= LoggerLevel
.Error
;
121 Exception exception
= null;
123 logger
.Error(message
);
125 ValidateCall(level
, message
, exception
);
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
);
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
);
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)
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");