public void checkChars_7(XmlWriterUtils utils, string indentChars)
        {
            XmlWriterSettings wSettings = new XmlWriterSettings();

            wSettings.OmitXmlDeclaration = true;
            wSettings.CheckCharacters    = true;
            wSettings.CloseOutput        = true;
            wSettings.Indent             = true;
            wSettings.IndentChars        = indentChars;
            if (indentChars == "~surogate~")
            {
                wSettings.IndentChars = "\uDD12\uDD01";
            }

            XmlWriter w = null;

            try
            {
                w = WriterHelper.Create("writer.out", wSettings, overrideAsync: true, async: utils.Async);
            }
            catch (ArgumentException e)
            {
                CError.WriteLineIgnore(e.ToString());
                return;
            }
            finally
            {
                if (w != null)
                {
                    w.Dispose();
                }
            }

            Assert.True(false, "Did not throw exception");
        }
        public void checkChars_9(XmlWriterUtils utils)
        {
            XmlWriterSettings wSettings = new XmlWriterSettings();

            wSettings.OmitXmlDeclaration  = true;
            wSettings.CheckCharacters     = true;
            wSettings.CloseOutput         = true;
            wSettings.NewLineOnAttributes = true;
            wSettings.Indent      = true;
            wSettings.IndentChars = "foo";

            XmlWriter w = null;

            try
            {
                w = WriterHelper.Create("writer.out", wSettings, overrideAsync: true, async: utils.Async);
            }
            catch (ArgumentException e)
            {
                CError.WriteLineIgnore(e.ToString());
                return;
            }
            finally
            {
                if (w != null)
                {
                    w.Dispose();
                }
            }

            CError.WriteLine("Did not throw exception");
            Assert.True(false);
        }
Beispiel #3
0
        protected void CreateWriter(WriteThru writeThru)
        {
            // Assumption is that the Create method doesn't throw NullReferenceException and
            // it is not the goal of this framework to test if they are thrown anywhere.
            // but if they are thrown that's a problem and they shouldn't be caught but exposed.

            Log("Writing thru : " + writeThru);

            try
            {
                switch (writeThru)
                {
                case WriteThru.Stream:
                    _factoryWriter = WriterHelper.Create(_stream, _settings);
                    break;

                case WriteThru.StringBuilder:
                    _factoryWriter = WriterHelper.Create(_stringBuilder, _settings);
                    break;

                case WriteThru.TextWriter:
                    _factoryWriter = WriterHelper.Create(_textWriter, _settings);
                    break;

                case WriteThru.XmlWriter:
                    _factoryWriter = WriterHelper.Create(_underlyingWriter, _settings);
                    break;
                }

                pstate = TestState.CreateSuccess;
            }
            catch (Exception ane)
            {
                Log(ane.ToString());
                if (!IsVariationValid)
                {
                    if (!CheckException(ane))
                    {
                        pstate = TestState.Error;
                        DumpVariationInfo();
                        throw new CTestFailedException(
                                  "Exception Thrown in CreateMethod, is your variation data correct?");
                    }
                    else
                    {
                        //This means that the Exception was checked and everything is fine.
                        pstate = TestState.Pass;
                    }
                }//Else valid variation threw exception
                else
                {
                    pstate = TestState.Error;
                    DumpVariationInfo();
                    throw new CTestFailedException(
                              "Exception Thrown in CreateMethod, is your variation data correct?");
                }
            }
        }
Beispiel #4
0
        public void CloseOutput_4(XmlWriterUtils utils, string outputType)
        {
            Stream            writerStream = FilePathUtil.getStream("writer.out");
            XmlWriterSettings wSettings    = new XmlWriterSettings();

            wSettings.CloseOutput = true;
            XmlWriter w = null;

            switch (utils.WriterType)
            {
            case WriterType.UTF8Writer:
                wSettings.Encoding = Encoding.UTF8;
                break;

            case WriterType.UnicodeWriter:
                wSettings.Encoding = Encoding.Unicode;
                break;
            }

            switch (outputType)
            {
            case "Stream":
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case "Textwriter":
                StreamWriter tw = new StreamWriter(writerStream, wSettings.Encoding);
                w = WriterHelper.Create(tw, wSettings, overrideAsync: true, async: utils.Async);
                break;
            }

            bool bResult = false;

            try
            {
                w.WriteStartDocument();
                w.WriteStartDocument();
            }
            catch (Exception e)
            {
                CError.WriteLineIgnore("Exception: " + e.ToString());
                if (writerStream != null && writerStream.CanWrite)
                {
                    bResult = true;
                }
                else
                {
                    bResult = false;
                }
            }
            finally
            {
                writerStream.Dispose();
            }
            Assert.True(bResult);
        }
        public XmlWriter CreateMemWriter(XmlWriterUtils utils, Stream writerStream, XmlWriterSettings settings)
        {
            XmlWriterSettings wSettings = settings.Clone();

            wSettings.CloseOutput        = false;
            wSettings.OmitXmlDeclaration = true;
            wSettings.CheckCharacters    = false;
            XmlWriter w = null;

            switch (utils.WriterType)
            {
            case WriterType.UTF8Writer:
                wSettings.Encoding = Encoding.UTF8;
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.UnicodeWriter:
                wSettings.Encoding = Encoding.Unicode;
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.WrappedWriter:
                XmlWriter ww = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                w = WriterHelper.Create(ww, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.CharCheckingWriter:
                XmlWriter         cw  = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                XmlWriterSettings cws = settings.Clone();
                cws.CheckCharacters = true;
                w = WriterHelper.Create(cw, cws, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.CustomWriter:
                wSettings.Async = utils.Async;
                w = new CustomWriter(writerStream, wSettings);
                break;

            case WriterType.UTF8WriterIndent:
                wSettings.Encoding = Encoding.UTF8;
                wSettings.Indent   = true;
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.UnicodeWriterIndent:
                wSettings.Encoding = Encoding.Unicode;
                wSettings.Indent   = true;
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            default:
                throw new Exception("Unknown writer type");
            }
            return(w);
        }
        //[Variation(Desc = "Wrapped writer tests for various types of nodes with 'Entitize'", Param = NewLineHandling.Entitize, Priority = 2)]
        //[Variation(Desc = "Wrapped writer tests for various types of nodes with 'Replace'", Param = NewLineHandling.Replace, Priority = 2)]
        //[Variation(Desc = "Wrapped writer tests for various types of nodes with 'None'", Param = NewLineHandling.None, Priority = 2)]
        public int EOF_Handling_19()
        {
            if (WriterType == WriterType.UTF8WriterIndent || WriterType == WriterType.UnicodeWriterIndent)
            {
                CError.Skip("skipped");
            }
            XmlWriterSettings wSettings = new XmlWriterSettings();

            wSettings.NewLineHandling = (NewLineHandling)CurVariation.Param;
            wSettings.CheckCharacters = false;
            XmlWriter         ww = CreateMemWriter(wSettings);
            XmlWriterSettings ws = wSettings.Clone();

            ws.NewLineHandling = NewLineHandling.Replace;
            ws.CheckCharacters = true;
            XmlWriter w = WriterHelper.Create(ww, ws);

            string NewLines = "\r \n " + nl;

            w.WriteStartElement("root");
            w.WriteCData(NewLines);
            w.WriteChars(NewLines.ToCharArray(), 0, NewLines.Length);
            w.WriteEndElement();
            w.WriteProcessingInstruction("pi", NewLines);
            w.WriteWhitespace(NewLines);
            w.WriteComment(NewLines);
            w.Dispose();

            string expOut;

            if ((NewLineHandling)CurVariation.Param == NewLineHandling.Entitize)
            {
                expOut = "<root><![CDATA[" + NewLines + "]]>" + ExpectedOutput(NewLines, NewLineHandling.Entitize, false) + "</root>" + "<?pi " + NewLines + "?>" + ExpectedOutput(NewLines, NewLineHandling.Entitize, false) + "<!--" + NewLines + "-->";
            }
            else
            {
                expOut = ExpectedOutput("<root><![CDATA[" + NewLines + "]]>" + NewLines + "</root><?pi " + NewLines + "?>" + NewLines + "<!--" + NewLines + "-->", NewLineHandling.Replace, false);
            }

            VerifyOutput(expOut);
            return(TEST_PASS);
        }
Beispiel #7
0
        public void CloseOutput_3(XmlWriterUtils utils, string outputType)
        {
            XmlWriterSettings wSettings = new XmlWriterSettings();

            wSettings.CloseOutput = true;
            XmlWriter w = null;

            switch (utils.WriterType)
            {
            case WriterType.UTF8Writer:
                wSettings.Encoding = Encoding.UTF8;
                break;

            case WriterType.UnicodeWriter:
                wSettings.Encoding = Encoding.Unicode;
                break;
            }

            Stream writerStream = FilePathUtil.getStream("writer.out");

            switch (outputType)
            {
            case "Stream":
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case "Textwriter":
                StreamWriter tw = new StreamWriter(writerStream, wSettings.Encoding);
                w = WriterHelper.Create(tw, wSettings, overrideAsync: true, async: utils.Async);
                break;
            }

            w.WriteStartElement("root");
            w.WriteEndElement();
            w.Dispose();

            if (writerStream.CanWrite)
            {
                writerStream.Dispose();
                Assert.True(false);
            }
        }
Beispiel #8
0
        public void CloseOutput_2(XmlWriterUtils utils, bool closeOutput)
        {
            XmlWriterSettings wSettings = new XmlWriterSettings();

            switch (utils.WriterType)
            {
            case WriterType.UTF8Writer:
                wSettings.Encoding = Encoding.UTF8;
                break;

            case WriterType.UnicodeWriter:
                wSettings.Encoding = Encoding.Unicode;
                break;
            }
            wSettings.CloseOutput = closeOutput;

            XmlWriter w = WriterHelper.Create("writer.out", wSettings, overrideAsync: true, async: utils.Async);

            w.WriteStartElement("root");
            w.WriteEndElement();
            w.Dispose();

            // Check if you can open the file in ReadWrite mode
            Stream fs = null;

            try
            {
                fs = FilePathUtil.getStream("writer.out");/*new FileStream("writer.out", FileMode.Open, FileAccess.ReadWrite);*/
            }
            catch (Exception e)
            {
                CError.WriteLineIgnore("Exception: " + e.ToString());
                CError.WriteLine("Uri stream is not closed by writer");
                Assert.True(false);
            }
            finally
            {
                fs.Dispose();
            }
            return;
        }
Beispiel #9
0
        public void CloseOutput_1(XmlWriterUtils utils, string outputType)
        {
            XmlWriterSettings wSettings = new XmlWriterSettings();
            XmlWriter         w         = null;

            switch (utils.WriterType)
            {
            case WriterType.UTF8Writer:
                wSettings.Encoding = Encoding.UTF8;
                break;

            case WriterType.UnicodeWriter:
                wSettings.Encoding = Encoding.Unicode;
                break;
            }
            Stream writerStream = new MemoryStream();

            switch (outputType)
            {
            case "Stream":
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case "Textwriter":
                StreamWriter tw = new StreamWriter(writerStream, wSettings.Encoding);
                w = WriterHelper.Create(tw, wSettings, overrideAsync: true, async: utils.Async);
                break;
            }

            w.WriteStartElement("root");
            w.WriteEndElement();
            w.Dispose();

            if (writerStream.CanWrite)
            {
                writerStream.Dispose();
                return;
            }
            CError.WriteLine("Error: XmlWriter closed the stream when CloseOutput = false");
            Assert.True(false);
        }
        public void EOF_Handling_19(XmlWriterUtils utils, NewLineHandling nlHandling)
        {
            XmlWriterSettings wSettings = new XmlWriterSettings();

            wSettings.NewLineHandling = nlHandling;
            wSettings.CheckCharacters = false;
            XmlWriter         ww = CreateMemWriter(utils, wSettings);
            XmlWriterSettings ws = wSettings.Clone();

            ws.NewLineHandling = NewLineHandling.Replace;
            ws.CheckCharacters = true;
            XmlWriter w = WriterHelper.Create(ww, ws, overrideAsync: true, async: utils.Async);

            string NewLines = "\r \n " + Environment.NewLine;

            w.WriteStartElement("root");
            w.WriteCData(NewLines);
            w.WriteChars(NewLines.ToCharArray(), 0, NewLines.Length);
            w.WriteEndElement();
            w.WriteProcessingInstruction("pi", NewLines);
            w.WriteWhitespace(NewLines);
            w.WriteComment(NewLines);
            w.Dispose();

            string expOut;

            if (nlHandling == NewLineHandling.Entitize)
            {
                expOut = "<root><![CDATA[" + NewLines + "]]>" + ExpectedOutput(NewLines, NewLineHandling.Entitize, false) + "</root>" + "<?pi " + NewLines + "?>" + ExpectedOutput(NewLines, NewLineHandling.Entitize, false) + "<!--" + NewLines + "-->";
            }
            else
            {
                expOut = ExpectedOutput("<root><![CDATA[" + NewLines + "]]>" + NewLines + "</root><?pi " + NewLines + "?>" + NewLines + "<!--" + NewLines + "-->", NewLineHandling.Replace, false);
            }

            VerifyOutput(expOut);
            return;
        }
        private XmlWriter CreateMemWriter(XmlWriterSettings settings)
        {
            XmlWriterSettings wSettings = settings.Clone();

            wSettings.CloseOutput        = false;
            wSettings.OmitXmlDeclaration = true;
            wSettings.CheckCharacters    = false;
            XmlWriter w = null;

            switch (WriterType)
            {
            case WriterType.UTF8Writer:
                wSettings.Encoding = Encoding.UTF8;
                if (_strWriter != null)
                {
                    _strWriter.Dispose();
                }
                _strWriter = new StringWriter();
                w          = WriterHelper.Create(_strWriter, wSettings);
                break;

            case WriterType.UnicodeWriter:
                wSettings.Encoding = Encoding.Unicode;
                if (_strWriter != null)
                {
                    _strWriter.Dispose();
                }
                _strWriter = new StringWriter();
                w          = WriterHelper.Create(_strWriter, wSettings);
                break;

            case WriterType.WrappedWriter:
                if (_strWriter != null)
                {
                    _strWriter.Dispose();
                }
                _strWriter = new StringWriter();
                XmlWriter ww = WriterHelper.Create(_strWriter, wSettings);
                w = WriterHelper.Create(ww, wSettings);
                break;

            case WriterType.CharCheckingWriter:
                if (_strWriter != null)
                {
                    _strWriter.Dispose();
                }
                _strWriter = new StringWriter();
                XmlWriter         cw  = WriterHelper.Create(_strWriter, wSettings);
                XmlWriterSettings cws = settings.Clone();
                cws.CheckCharacters = true;
                w = WriterHelper.Create(cw, cws);
                break;

            case WriterType.UTF8WriterIndent:
                wSettings.Encoding = Encoding.UTF8;
                wSettings.Indent   = true;
                if (_strWriter != null)
                {
                    _strWriter.Dispose();
                }
                _strWriter = new StringWriter();
                w          = WriterHelper.Create(_strWriter, wSettings);
                break;

            case WriterType.UnicodeWriterIndent:
                wSettings.Encoding = Encoding.Unicode;
                wSettings.Indent   = true;
                if (_strWriter != null)
                {
                    _strWriter.Dispose();
                }
                _strWriter = new StringWriter();
                w          = WriterHelper.Create(_strWriter, wSettings);
                break;

            default:
                throw new Exception("Unknown writer type");
            }
            return(w);
        }
Beispiel #12
0
        protected override void Test()
        {
            Log("Testing : " + TestFileName);
            Log("Overload : " + _overload);
            switch (_overload)
            {
            case WriterOverload.MemoryStream:
                _stream = new MemoryStream();
                CreateWriter(WriteThru.Stream);
                break;

            case WriterOverload.StreamWriter:
                _textWriter = new StreamWriter(new MemoryStream());
                CreateWriter(WriteThru.TextWriter);
                break;

            case WriterOverload.StringBuilder:
                _stringBuilder = new StringBuilder();
                CreateWriter(WriteThru.StringBuilder);
                break;

            case WriterOverload.StringWriter:
                _textWriter = new StringWriter();
                CreateWriter(WriteThru.TextWriter);
                break;

            case WriterOverload.UnicodeWriter:
                _underlyingSettings          = new XmlWriterSettings();
                _underlyingSettings.Encoding = Encoding.Unicode;
                _underlyingWriter            = WriterHelper.Create(TestFileName, _underlyingSettings);
                CreateWriter(WriteThru.XmlWriter);
                break;

            case WriterOverload.UTF8Writer:
                _underlyingSettings          = new XmlWriterSettings();
                _underlyingSettings.Encoding = Encoding.UTF8;
                _underlyingWriter            = WriterHelper.Create(TestFileName, _underlyingSettings);
                CreateWriter(WriteThru.XmlWriter);
                break;

            default:
                throw new CTestFailedException("Unknown WriterOverload: " + _overload);
            }

            if (pstate == TestState.Pass)
            {
                return;
            }

            CError.Compare(pstate, TestState.CreateSuccess, "Invalid State after Create: " + pstate);

            //By this time the factory Reader is already set up correctly. So we must go Consume it now.
            CError.Compare(pstate != TestState.Pass && pstate == TestState.CreateSuccess, "Invalid state before Consuming Reader: " + pstate);

            //Call TestWriter to Consume Reader;
            TestWriter();
            if (pstate == TestState.Pass)
            {
                return;
            }
            CError.Compare(pstate != TestState.Pass && pstate == TestState.Consume, "Invalid state after Consuming Reader: " + pstate);
        }
Beispiel #13
0
        public void flush_1(XmlWriterUtils utils, bool closeOutput)
        {
            Stream            writerStream = new MemoryStream();
            XmlWriterSettings wSettings    = new XmlWriterSettings();

            wSettings.NewLineChars = "\r\n";
            XmlWriter w = null;
            long      expectedLength = 0;

            wSettings.CloseOutput = closeOutput;

            switch (utils.WriterType)
            {
            case WriterType.WrappedWriter:
                expectedLength = 113;
                XmlWriter ww = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                w = WriterHelper.Create(ww, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.CharCheckingWriter:
                expectedLength = 113;
                XmlWriter         w1  = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                XmlWriterSettings ws1 = new XmlWriterSettings();
                ws1.CheckCharacters = true;
                w = WriterHelper.Create(w1, ws1, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.UTF8Writer:
                wSettings.Encoding = Encoding.UTF8;
                expectedLength     = 113;
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.UnicodeWriter:
                wSettings.Encoding = Encoding.Unicode;
                expectedLength     = 224;
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.UTF8WriterIndent:
                wSettings.Encoding = Encoding.UTF8;
                expectedLength     = 125;
                wSettings.Indent   = true;
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            case WriterType.UnicodeWriterIndent:
                wSettings.Encoding = Encoding.Unicode;
                expectedLength     = 248;
                wSettings.Indent   = true;
                w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);
                break;

            default:
                Assert.True(false, "unknown writer");
                break;
            }

            wSettings.CloseOutput = closeOutput;

            var beginning = writerStream.Length;

            try
            {
                w.WriteStartElement("root");
                w.WriteStartElement("OneChar");
                w.WriteAttributeString("a", "myAttribute");
                w.WriteString("a");
                w.WriteEndElement();

                w.WriteStartElement("twoChars");
                w.WriteString("ab");
                w.WriteEndElement();
                w.WriteEndElement();

                Assert.Equal(writerStream.Length, beginning);

                w.Flush();

                Assert.Equal(expectedLength, writerStream.Length);
            }
            finally
            {
                w.Dispose();
                writerStream.Dispose();
            }
        }
Beispiel #14
0
        public void close_1(XmlWriterUtils utils)
        {
            Stream            writerStream = new MemoryStream();
            XmlWriterSettings wSettings    = new XmlWriterSettings();

            long expectedLength1 = 0;
            long expectedLength2 = 0;

            switch (utils.WriterType)
            {
            case WriterType.UTF8Writer:
                wSettings.Encoding = Encoding.UTF8;
                expectedLength1    = 83;
                expectedLength2    = 113;
                break;

            case WriterType.UnicodeWriter:
                wSettings.Encoding = Encoding.Unicode;
                expectedLength1    = 164;
                expectedLength2    = 224;
                break;
            }

            wSettings.CloseOutput = false;

            XmlWriter w = WriterHelper.Create(writerStream, wSettings, overrideAsync: true, async: utils.Async);

            try
            {
                var beginning = writerStream.Length;

                w.WriteStartElement("root");
                w.WriteStartElement("OneChar");
                w.WriteAttributeString("a", "myAttribute");
                w.WriteString("a");
                w.WriteEndElement();

                CError.WriteLine("File Size Before Flush: {0}", writerStream.Length);
                CError.Compare(writerStream.Length, beginning, "Before Flush");

                // Flush mid-way
                w.Flush();
                CError.WriteLine("File Size After Flush: {0}", writerStream.Length);
                CError.Compare(writerStream.Length, expectedLength1, "After Flush");

                w.WriteStartElement("twoChars");
                w.WriteString("ab");
                w.WriteEndElement();
                w.WriteEndElement();
                w.Dispose();

                // Now check that Close() called Flush()
                CError.WriteLine("File Size After Writer.Close: {0}", writerStream.Length);
                CError.Compare(writerStream.Length, expectedLength2, "After Writer.Close");

                // Finally, close the underlying stream, it should be flushed now
                writerStream.Flush();
                CError.WriteLine("File Size After Stream.Close: {0}", writerStream.Length);
                CError.Compare(writerStream.Length, expectedLength2, "After Stream.Close");
            }
            catch (XmlException)
            {
                Assert.True(false);
            }
            finally
            {
                if (writerStream != null)
                {
                    writerStream.Dispose();
                }
                if (w != null)
                {
                    w.Dispose();
                }
            }

            return;
        }