Пример #1
0
        public void Test02()
        {
            string msg1, msg2;
            string fileName = string.Format("{0}_4.txt", this.GetType().Name);

            CommonUtilities.DeleteFile(fileName);

            try
            {
                FileStream   stream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                StreamWriter writer = new StreamWriter(stream);

                _delimitedListener = new DelimitedListTraceListener(writer);
                Assert.True(_delimitedListener.Delimiter == ";");
                Assert.True(_delimitedListener.Name == "");
                Assert.True(_delimitedListener.Writer == writer);

                msg1 = "Msg1";
                _delimitedListener.WriteLine(msg1);
                _delimitedListener.Delimiter = ",";
                msg2 = "Msg2";
                _delimitedListener.WriteLine(msg2);
                _delimitedListener.Flush();
                _delimitedListener.Dispose();

                Assert.True(_delimitedListener.Delimiter == ",");
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg1));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg2));
            }
            finally
            {
                CommonUtilities.DeleteFile(fileName);
            }
        }
Пример #2
0
        public void Test04()
        {
            string msg1, name;

            name      = null;
            _fileName = string.Format("{0}_4.xml", this.GetType().Name);

            //Scenario 4: name is null -its set as the empty string
            try
            {
                CommonUtilities.DeleteFile(_fileName);

                _stream     = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write);
                _textWriter = new TextWriterTraceListener(_stream, name);
                Assert.True(_textWriter.Name == string.Empty);

                msg1 = "HelloWorld";
                _textWriter.WriteLine(msg1);
                _textWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(TextWriterTraceListener) }, new string[] { _fileName }, msg1));
            }
            finally
            {
                CommonUtilities.DeleteFile(_fileName);
            }
        }
Пример #3
0
        public void Test03()
        {
            string msg1, name;

            name = "MyXMLTraceWriter";
            string fileName = string.Format("{0}_3.xml", this.GetType().Name);

            //Testing ctor(Stream, string)
            //Scenario 3: Vanilla - Pass in a valid name and check with the Name property
            try
            {
                CommonUtilities.DeleteFile(fileName);
                _stream     = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                _textWriter = new TextWriterTraceListener(_stream, name);
                Assert.True(_textWriter.Name == name);
                msg1 = "HelloWorld";
                _textWriter.WriteLine(msg1);
                _textWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(TextWriterTraceListener) }, new string[] { fileName }, msg1));
            }
            finally
            {
                CommonUtilities.DeleteFile(fileName);
            }
        }
Пример #4
0
        public void Test05()
        {
            string msg1;

            string[] names    = { string.Empty, new string('a', 100000), "hell0<", "><&" };
            string   fileName = string.Format("{0}_5.xml", this.GetType().Name);

            //Scenario 6: Other interesting string values - empty, very long, interesting characters (ones that will clash with xml like <, >, etc)
            try
            {
                foreach (string name in names)
                {
                    CommonUtilities.DeleteFile(fileName);
                    _stream     = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                    _textWriter = new TextWriterTraceListener(_stream, name);

                    Assert.True(_textWriter.Name == name);
                    msg1 = "HelloWorld";
                    _textWriter.WriteLine(msg1);
                    _textWriter.Dispose();

                    Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(TextWriterTraceListener) }, new string[] { fileName }, msg1));
                }
            }
            finally
            {
                CommonUtilities.DeleteFile(fileName);
            }
        }
Пример #5
0
        public void Test02()
        {
            try
            {
                //Testing ctor(Stream)
                //[] Vanilla - pass a valid stream. Also check via FileName return the expected value
                string msg1;
                _fileName = string.Format("{0}_2.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(_fileName);

                _stream     = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write);
                _textWriter = new TextWriterTraceListener(_stream);
                msg1        = "HelloWorld";
                _textWriter.WriteLine(msg1);
                _textWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(TextWriterTraceListener) }, new string[] { _fileName }, msg1));
            }
            finally
            {
                CommonUtilities.DeleteFile(_fileName);
            }
        }
Пример #6
0
        public void RunTest()
        {
            DelimitedListTraceListener delimitedWriter;
            string          fileName, msg1, msg2, msg3, format, source;
            TraceEventCache eventCache;
            TraceEventType  severity;
            int             id;

            object[]     args, data;
            FileStream   stream;
            StreamWriter writer;

            try
            {
                //Scenario 1: TraceEvent (TraceEventCache eventCache, string source, TraceEventType severity, int id, string format, params object[] args)
                fileName = string.Format("{0}_1.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(fileName);
                stream          = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                writer          = new StreamWriter(stream);
                delimitedWriter = new DelimitedListTraceListener(writer);

                eventCache = null;
                source     = "Co1971";
                severity   = TraceEventType.Information;
                id         = 1000;
                format     = "This is an object of type string:{0} and this is of type int: {1}";
                args       = new Object[] { "Hello", 6 };
                msg1       = string.Format(format, args);
                delimitedWriter.TraceEvent(eventCache, source, severity, id, format, args);
                delimitedWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, source));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, severity.ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, id.ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg1));

                CommonUtilities.DeleteFile(fileName);

                //Scenario 2: TraceEvent(TraceEventCache eventCache, string source, TraceEventType severity, int id, string message)

                fileName = string.Format("{0}_2.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(fileName);
                stream          = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                writer          = new StreamWriter(stream);
                delimitedWriter = new DelimitedListTraceListener(writer);

                eventCache = null;
                source     = "Co1971";
                severity   = TraceEventType.Information;
                id         = 1000;
                msg1       = "Hello World";
                delimitedWriter.TraceEvent(eventCache, source, severity, id, msg1);
                delimitedWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, source));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, severity.ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, id.ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg1));
                CommonUtilities.DeleteFile(fileName);

                //Scenario 3: TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, object data)

                fileName = string.Format("{0}_3.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(fileName);
                stream          = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                writer          = new StreamWriter(stream);
                delimitedWriter = new DelimitedListTraceListener(writer);
                object data1;
                eventCache = null;
                source     = "Co1971";
                severity   = TraceEventType.Information;
                id         = 1000;
                data1      = Decimal.MaxValue;
                delimitedWriter.TraceData(eventCache, source, severity, id, data1);
                delimitedWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, source));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, severity.ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, id.ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, data1.ToString()));
                CommonUtilities.DeleteFile(fileName);

                //Scenario 4: TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, params object[] data)

                fileName = string.Format("{0}_4.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(fileName);
                stream          = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                writer          = new StreamWriter(stream);
                delimitedWriter = new DelimitedListTraceListener(writer);

                eventCache = null;
                source     = "Co1971";
                severity   = TraceEventType.Information;
                id         = 1000;
                data       = new Object[] { Decimal.MaxValue, Int64.MinValue, Int32.MaxValue, "Hello" };
                msg1       = string.Format("{0}{1}{2}{3}", data);
                delimitedWriter.TraceData(eventCache, source, severity, id, data);
                delimitedWriter.Dispose();


                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, source));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, severity.ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, id.ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, data[0].ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, data[1].ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, data[2].ToString()));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, data[3].ToString()));
                CommonUtilities.DeleteFile(fileName);

                //Scenario 5: Write(string message)

                fileName = string.Format("{0}_5.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(fileName);
                stream          = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                writer          = new StreamWriter(stream);
                delimitedWriter = new DelimitedListTraceListener(writer);

                msg1 = "Hello World";
                delimitedWriter.Write(msg1);
                delimitedWriter.Dispose();
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg1));
                CommonUtilities.DeleteFile(fileName);

                //Scenario 6: WriteLine(string message)
                fileName = string.Format("{0}_6.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(fileName);
                stream          = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                writer          = new StreamWriter(stream);
                delimitedWriter = new DelimitedListTraceListener(writer);

                msg1 = "Hello World";
                delimitedWriter.WriteLine(msg1);
                delimitedWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg1));
                CommonUtilities.DeleteFile(fileName);

                //Scenario 7: some TraceListener methods that are not overridden
                fileName = string.Format("{0}_7.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(fileName);
                stream          = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                writer          = new StreamWriter(stream);
                delimitedWriter = new DelimitedListTraceListener(writer);
                msg1            = "Msg1";
                msg2            = "Msg2";
                delimitedWriter.Fail(msg1, msg2);
                msg3 = "Hello World";
                delimitedWriter.Fail(msg3);
                delimitedWriter.Dispose();

                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg1));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg2));
                Assert.True(CommonUtilities.TestListenerContent(new Type[] { typeof(DelimitedListTraceListener) }, new string[] { fileName }, msg1));
                CommonUtilities.DeleteFile(fileName);

                //Scenario 8: some TraceListener properties that are not overridden
                fileName = string.Format("{0}_8.xml", this.GetType().Name);
                CommonUtilities.DeleteFile(fileName);
                stream          = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                writer          = new StreamWriter(stream);
                delimitedWriter = new DelimitedListTraceListener(writer);

                Assert.True(delimitedWriter.Filter == null);
                Assert.True(delimitedWriter.IndentLevel == 0);
                Assert.True(delimitedWriter.IndentSize == 4);
                Assert.True(delimitedWriter.Name == string.Empty);
                Assert.True(delimitedWriter.TraceOutputOptions == TraceOptions.None);
                delimitedWriter.Dispose();
                CommonUtilities.DeleteFile(fileName);
            }
            finally
            {
                for (int i = 1; i <= 8; i++)
                {
                    fileName = string.Format("{0}_{1}.xml", this.GetType().Name, i);
                    CommonUtilities.DeleteFile(fileName);
                }
            }
        }