Example #1
0
        public void SupersededWriterTest()
        {
            var logWriter   = new TestLogWriter();
            var accumulator = new LogAccumulator(logWriter, null);

            Assert.AreSame(logWriter, accumulator.SupersededWriter);
        }
Example #2
0
        public void FileLogWriterConstructorTest()
        {
            FileLogWriter target;
            ILogWriter    fallbackWriter = new TestLogWriter();

            // Test empty/null file name -- [MH] This can't be tested because of the 'Debugger.IsAttached' condition in FileLogWriter.
            //target = new FileLogWriter("", LogSyncMode.Message, null);
            //accessor = new FileLogWriter_Accessor(new PrivateObject(target));
            //string autoGeneratedPath = accessor.logTarget;
            //Assert.IsFalse(!string.IsNullOrEmpty(autoGeneratedPath));
            //Assert.AreEqual(System.Environment.SpecialFolder.DesktopDirectory, Path.GetDirectoryName(autoGeneratedPath));

            string fileName = "TestFile.log";

            target = new FileLogWriter(fileName, LogSyncMode.Message, fallbackWriter);
            PrivateObject privateObj = new PrivateObject(target);

            Assert.AreEqual(privateObj.GetFieldOrProperty("fallbackWriter"), fallbackWriter);
            Assert.AreEqual(fileName, privateObj.GetFieldOrProperty("logTarget"));
            Assert.IsTrue((bool)privateObj.GetFieldOrProperty("closeAfterWrite"));
            Assert.IsFalse((bool)privateObj.GetFieldOrProperty("autoFlush"));

            target     = new FileLogWriter(fileName, LogSyncMode.Flush, fallbackWriter);
            privateObj = new PrivateObject(target);
            Assert.AreEqual(fileName, privateObj.GetFieldOrProperty("logTarget"));
            Assert.IsFalse((bool)privateObj.GetFieldOrProperty("closeAfterWrite"));
            Assert.IsTrue((bool)privateObj.GetFieldOrProperty("autoFlush"));

            target     = new FileLogWriter(fileName, LogSyncMode.Session, fallbackWriter);
            privateObj = new PrivateObject(target);
            Assert.AreEqual(fileName, privateObj.GetFieldOrProperty("logTarget"));
            Assert.IsFalse((bool)privateObj.GetFieldOrProperty("closeAfterWrite"));
            Assert.IsFalse((bool)privateObj.GetFieldOrProperty("autoFlush"));
        }
Example #3
0
        public void DisposeTest()
        {
            var  supersededWriter = new TestLogWriter();
            bool callbackCalled   = false;

            LogAccumulator.DisposalCallback disposalCallback = () => { callbackCalled = true; };
            LogAccumulator target = new LogAccumulator(supersededWriter, disposalCallback);

            target.Dispose();

            Assert.IsTrue(callbackCalled);

            callbackCalled = false;

            target.Dispose();
            Assert.IsFalse(callbackCalled);
        }
Example #4
0
        public void WriteLineTest()
        {
            ILogWriter supersededWriter = new TestLogWriter();

            LogAccumulator.DisposalCallback disposalCallback = null;
            LogAccumulator target  = new LogAccumulator(supersededWriter, disposalCallback);
            string         message = "Hello";

            target.WriteLine(message);

            PrivateObject privateObj = new PrivateObject(target);

            Assert.AreEqual(message + Environment.NewLine, privateObj.GetFieldOrProperty("accumulatedMessages").ToString());

            target.WriteLine(message);
            Assert.AreEqual(message + Environment.NewLine + message + Environment.NewLine, privateObj.GetFieldOrProperty("accumulatedMessages").ToString());
        }
Example #5
0
        public void DiscardTest()
        {
            var supersededWriter = new TestLogWriter();

            LogAccumulator.DisposalCallback disposalCallback = null;
            LogAccumulator target = new LogAccumulator(supersededWriter, disposalCallback);

            string message = "Hello";

            target.WriteLine(message);

            target.Discard();

            var actual = supersededWriter.Output.ToString().Replace(Environment.NewLine, "$n");

            Assert.AreEqual("", actual, "(Note: $n replaces new line characters)");

            PrivateObject privateObj = new PrivateObject(target);

            Assert.AreEqual("", privateObj.GetFieldOrProperty("accumulatedMessages").ToString());
        }
Example #6
0
        public void FallbackWriteTest()
        {
            FileLogWriter target;
            //FileLogWriter_Accessor accessor;
            TestLogWriter fallbackWriter = new TestLogWriter();

            target = new FileLogWriter(Path.Combine(TestContext.DeploymentDirectory, "testlog.txt"), LogSyncMode.Message, fallbackWriter);

            // Writing should be successful, fallback write should not be called.
            target.WriteLine("Hello");
            Assert.IsFalse(fallbackWriter.FlushWasInvoked);
            Assert.IsFalse(fallbackWriter.WriteLineWasInvoked);
            Assert.IsFalse(fallbackWriter.WriteWasInvoked);

            target = new FileLogWriter("c:\\this_dir_does_not_exist\\testlog.txt", LogSyncMode.Message, fallbackWriter);
            // First time printing will cause an exception and will print to the fallback writer.
            target.WriteLine("Hello");
            Assert.IsTrue(fallbackWriter.WriteWasInvoked);

            // At this point the fallback writer has an exception message from the Open operation.
            fallbackWriter.ResetInvocationIndicators();
            fallbackWriter.ClearOutput();

            target.WriteLine("Hello");
            string expected = "Hello" + "$n";
            string actual   = fallbackWriter.Output.ToString().Replace(Environment.NewLine, "$n");

            Assert.AreEqual(expected, actual);

            fallbackWriter.ResetInvocationIndicators();
            fallbackWriter.ClearOutput();

            target.Write("Hello");
            expected = "Hello";
            actual   = fallbackWriter.Output.ToString().Replace(Environment.NewLine, "$n");
            Assert.AreEqual(expected, actual);
        }