nicko       2004/05/16 15:12:56

  Modified:    tests/src log4net.Tests.csproj
  Added:       tests/src/Appender RemotingAppenderTest.cs
               tests/src/Util PropertiesDictionaryTest.cs
  Log:
  Added tests for the RemotingAppender and the PropertiesDictionary.
  
  Revision  Changes    Path
  1.4       +15 -0     logging-log4net/tests/src/log4net.Tests.csproj
  
  Index: log4net.Tests.csproj
  ===================================================================
  RCS file: /home/cvs/logging-log4net/tests/src/log4net.Tests.csproj,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- log4net.Tests.csproj      26 Feb 2004 21:44:53 -0000      1.3
  +++ log4net.Tests.csproj      16 May 2004 22:12:56 -0000      1.4
  @@ -82,6 +82,11 @@
                       Project = "{F6A02431-167E-4347-BC43-65532C31CDB7}"
                       Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
                   />
  +                <Reference
  +                    Name = "System.Runtime.Remoting"
  +                    AssemblyName = "System.Runtime.Remoting"
  +                    HintPath = 
"..\..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.0.3705\System.Runtime.Remoting.dll"
  +                />
               </References>
           </Build>
           <Files>
  @@ -113,6 +118,11 @@
                       BuildAction = "Compile"
                   />
                   <File
  +                    RelPath = "Appender\RemotingAppenderTest.cs"
  +                    SubType = "Code"
  +                    BuildAction = "Compile"
  +                />
  +                <File
                       RelPath = "Appender\RollingFileAppenderTest.cs"
                       SubType = "Code"
                       BuildAction = "Compile"
  @@ -129,6 +139,11 @@
                   />
                   <File
                       RelPath = "Layout\PatternLayoutTest.cs"
  +                    SubType = "Code"
  +                    BuildAction = "Compile"
  +                />
  +                <File
  +                    RelPath = "Util\PropertiesDictionaryTest.cs"
                       SubType = "Code"
                       BuildAction = "Compile"
                   />
  
  
  
  1.1                  
logging-log4net/tests/src/Appender/RemotingAppenderTest.cs
  
  Index: RemotingAppenderTest.cs
  ===================================================================
  #region Copyright & License
  //
  // Copyright 2001-2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  // http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  //
  #endregion
  
  using System;
  using System.Diagnostics;
  using System.Globalization;
  using System.Runtime.Remoting;
  using System.Runtime.Remoting.Channels;
  using System.Runtime.Remoting.Channels.Tcp;
  
  using log4net.Util;
  using log4net.Layout;
  using log4net.Core;
  using log4net.Appender;
  using IRemoteLoggingSink = 
log4net.Appender.RemotingAppender.IRemoteLoggingSink;
  
  using NUnit.Framework;
  
  namespace log4net.Tests.Appender
  {
        /// <summary>
        /// Used for internal unit testing the <see cref="RemotingAppender"/> 
class.
        /// </summary>
        /// <remarks>
        /// Used for internal unit testing the <see cref="RemotingAppender"/> 
class.
        /// </remarks>
        [TestFixture] public class RemotingAppenderTest
        {
                private IChannel m_remotingChannel = null;
  
                /// <summary>
                /// Test that the Message property is correctly remoted
                /// </summary>
                [Test] public void TestRemotedMessage()
                {
                        // Setup the remoting appender
                        ConfigureRootAppender(FixFlags.Partial);
  
                        RemoteLoggingSinkImpl.Instance.Events = null;
  
                        log4net.Repository.Hierarchy.Logger root = null;
                        root = 
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;      
 
  
                        string testMessage = string.Format("test message [ {0} 
]", (new Random()).Next());
  
                        // Log a message that will be remoted
                        root.Log(Level.Debug, testMessage, null);
  
                        // Wait for the remoted object to be delivered
                        System.Threading.Thread.Sleep(1000);
  
                        LoggingEvent[] events = 
RemoteLoggingSinkImpl.Instance.Events;
                        Assertion.AssertEquals("Expect to receive 1 remoted 
event", 1, events.Length);
  
                        Assertion.AssertEquals("Expect Message match after 
remoting event", testMessage, events[0].RenderedMessage);
                }
  
                /// <summary>
                /// Test that the UserName property is not remoted when doing a 
Fix.Partial
                /// </summary>
                [Test] public void TestPartialFix()
                {
                        // Setup the remoting appender
                        ConfigureRootAppender(FixFlags.Partial);
  
                        RemoteLoggingSinkImpl.Instance.Events = null;
  
                        log4net.Repository.Hierarchy.Logger root = null;
                        root = 
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;      
 
  
                        // Log a message that will be remoted
                        root.Log(Level.Debug, "test message", null);
  
                        // Wait for the remoted object to be delivered
                        System.Threading.Thread.Sleep(1000);
  
                        LoggingEvent[] events = 
RemoteLoggingSinkImpl.Instance.Events;
                        Assertion.AssertEquals("Expect to receive 1 remoted 
event", 1, events.Length);
  
                        // Grab the event data
                        LoggingEventData eventData = 
GetLoggingEventData(events[0]);
  
                        Assertion.AssertNull("Expect username to be null 
because only doing a partial fix", eventData.UserName);
                }
  
                /// <summary>
                /// Test that the UserName property is remoted when doing a 
Fix.All
                /// </summary>
                [Test] public void TestFullFix()
                {
                        // Setup the remoting appender
                        ConfigureRootAppender(FixFlags.All);
  
                        RemoteLoggingSinkImpl.Instance.Events = null;
  
                        log4net.Repository.Hierarchy.Logger root = null;
                        root = 
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;      
 
  
                        // Log a message that will be remoted
                        root.Log(Level.Debug, "test message", null);
  
                        // Wait for the remoted object to be delivered
                        System.Threading.Thread.Sleep(1000);
  
                        LoggingEvent[] events = 
RemoteLoggingSinkImpl.Instance.Events;
                        Assertion.AssertEquals("Expect to receive 1 remoted 
event", 1, events.Length);
  
                        // Grab the event data
                        LoggingEventData eventData = 
GetLoggingEventData(events[0]);
  
                        Assertion.AssertNotNull("Expect username to not be null 
because doing a full fix", eventData.UserName);
                }
  
                private void RegisterRemotingServerChannel()
                {
                        if (m_remotingChannel == null)
                        {
                                m_remotingChannel = new TcpChannel(8085);
  
                                // Setup remoting server
                                try
                                {
                                        
ChannelServices.RegisterChannel(m_remotingChannel);
                                }
                                catch(Exception)
                                {
                                }
  
                                // Marshal the sink object
                                
RemotingServices.Marshal(RemoteLoggingSinkImpl.Instance, "LoggingSink", 
typeof(IRemoteLoggingSink));
                        }
                }
  
                /// <summary>
                /// Shuts down any loggers in the hierarchy, along
                /// with all appenders.
                /// </summary>
                private void ResetRepository()
                {
                        // Regular users should not use the clear method 
lightly!
                        LogManager.GetRepository().ResetConfiguration();
                        LogManager.GetRepository().Shutdown();
                        
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Clear();
                }
  
                /// <summary>
                /// Any initialization that happens before each test can
                /// go here
                /// </summary>
                [SetUp] public void SetUp() 
                {
                        ResetRepository();
                        RegisterRemotingServerChannel();
                }
  
                /// <summary>
                /// Any steps that happen after each test go here
                /// </summary>
                [TearDown] public void TearDown() 
                {
                        ResetRepository();
                }
  
                /// <summary>
                /// Configures the root appender for counting and rolling
                /// </summary>
                private void ConfigureRootAppender(FixFlags fixFlags)
                {
                        log4net.Repository.Hierarchy.Logger root = null;
                        root = 
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;      
 
                        root.Level = Level.Debug;
                        root.AddAppender(CreateAppender(fixFlags));
                        root.Repository.Configured = true;
                }
  
                private RemotingAppender CreateAppender(FixFlags fixFlags)
                {
                        RemotingAppender appender = new RemotingAppender();
                        appender.Sink = "tcp://localhost:8085/LoggingSink";
                        appender.Lossy = false;
                        appender.BufferSize = 1;
                        appender.Fix = fixFlags;
  
                        appender.ActivateOptions();
  
                        return appender;
                }
  
                public class RemoteLoggingSinkImpl : MarshalByRefObject, 
IRemoteLoggingSink
                {
                        public static readonly RemoteLoggingSinkImpl Instance = 
new RemoteLoggingSinkImpl();
  
                        public LoggingEvent[] Events = null;
  
                        #region Public Instance Constructors
  
                        private RemoteLoggingSinkImpl()
                        {
                        }
  
                        #endregion Public Instance Constructors
  
                        #region Implementation of IRemoteLoggingSink
  
                        /// <summary>
                        /// Logs the events to the repository.
                        /// </summary>
                        /// <param name="events">The events to log.</param>
                        /// <remarks>
                        /// The events passed are logged to the <see 
cref="LoggerRepository"/>
                        /// </remarks>
                        public void LogEvents(LoggingEvent[] events)
                        {
                                Events = events;
                        }
  
                        #endregion Implementation of IRemoteLoggingSink
  
                        #region Override implementation of MarshalByRefObject
  
                        /// <summary>
                        /// Obtains a lifetime service object to control the 
lifetime 
                        /// policy for this instance.
                        /// </summary>
                        /// <returns>
                        /// <c>null</c> to indicate that this instance should 
live
                        /// forever.
                        /// </returns>
                        public override object InitializeLifetimeService()
                        {
                                return null;
                        }
  
                        #endregion Override implementation of MarshalByRefObject
                }
  
                //
                // Helper functions to dig into the appender
                //
  
                private static LoggingEventData 
GetLoggingEventData(LoggingEvent loggingEvent)
                {
                        return (LoggingEventData)Utils.GetField(loggingEvent, 
"m_data");
                }
        }
  }
  
  
  
  1.1                  
logging-log4net/tests/src/Util/PropertiesDictionaryTest.cs
  
  Index: PropertiesDictionaryTest.cs
  ===================================================================
  #region Copyright & License
  //
  // Copyright 2001-2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  // http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  //
  #endregion
  
  using System;
  using System.IO;
  using System.Diagnostics;
  using System.Globalization;
  using System.Runtime.Serialization.Formatters.Binary;
  using System.Runtime.Serialization;
  
  using log4net.Config;
  using log4net.Util;
  using log4net.Layout;
  using log4net.Core;
  using log4net.Appender;
  using log4net.Repository;
  
  using log4net.Tests.Appender;
  
  using NUnit.Framework;
  
  namespace log4net.Tests.Util
  {
        /// <summary>
        /// Used for internal unit testing the <see 
cref="PropertiesDictionary"/> class.
        /// </summary>
        /// <remarks>
        /// Used for internal unit testing the <see 
cref="PropertiesDictionary"/> class.
        /// </remarks>
        [TestFixture] public class PropertiesDictionaryTest
        {
                [Test] public void TestSerialization()
                {
                        PropertiesDictionary pd = new PropertiesDictionary();
  
                        for(int i=0; i<10; i++)
                        {
                                pd[i.ToString()] = i;
                        }
  
                        Assertion.AssertEquals("Dictionary should have 10 
items", 10, pd.Count);
  
                        // Serialize the properties into a memory stream
                        BinaryFormatter formatter = new BinaryFormatter();
                        MemoryStream memory = new MemoryStream();
                        formatter.Serialize(memory, pd);
  
                        // Deserialize the stream into a new properties 
dictionary
                        memory.Position = 0;
                        PropertiesDictionary pd2 = 
(PropertiesDictionary)formatter.Deserialize(memory);
  
                        Assertion.AssertEquals("Deserialized Dictionary should 
have 10 items", 10, pd2.Count);
  
                        foreach(string key in pd.GetKeys())
                        {
                                Assertion.AssertEquals("Check Value Persisted 
for key ["+key+"]", pd[key], pd2[key]);
                        }
                }
  
        }
  }
  
  
  

Reply via email to