Exemple #1
0
        public static bool CompareXml(Stream expectedStream, Stream actualStream, string xmldiffoptionvalue, DelayedWriteLogger logger)
        {
            bool bResult = false;

            // Default Diff options used by XSLT V2 driver.
            int     defaultXmlDiffOptions = (int)(XmlDiffOption.InfosetComparison | XmlDiffOption.IgnoreEmptyElement | XmlDiffOption.IgnoreAttributeOrder);
            XmlDiff diff = new XmlDiff();

            if (xmldiffoptionvalue == null || xmldiffoptionvalue.Equals(string.Empty))
            {
                diff.Option = (XmlDiffOption)defaultXmlDiffOptions;
            }
            else
            {
                if (logger != null)
                {
                    logger.LogMessage("Custom XmlDiffOptions used. Value passed is " + xmldiffoptionvalue);
                }
                diff.Option = (XmlDiffOption)Int32.Parse(xmldiffoptionvalue);
            }

            XmlParserContext context = new XmlParserContext(new NameTable(), null, "", XmlSpace.None);

            try
            {
                bResult =
                    diff.Compare(new XmlTextReader(actualStream, XmlNodeType.Element, context),
                                 new XmlTextReader(expectedStream, XmlNodeType.Element, context));
            }
            catch (Exception e)
            {
                bResult = false;
                if (logger != null)
                {
                    logger.LogMessage("Exception thrown in XmlDiff compare!");
                    logger.LogXml(e.ToString());
                    throw;
                }
            }

            if (bResult)
            {
                return(true);
            }

            if (logger != null)
            {
                logger.LogMessage("Mismatch in XmlDiff");
                logger.LogMessage("Actual result: ");
            }

            return(false);
        }
Exemple #2
0
        public static bool CompareXml(Stream expectedStream, Stream actualStream, string xmldiffoptionvalue, DelayedWriteLogger logger)
        {
            bool bResult = false;

            // Default Diff options used by XSLT V2 driver.
            int defaultXmlDiffOptions = (int)(XmlDiffOption.InfosetComparison | XmlDiffOption.IgnoreEmptyElement | XmlDiffOption.IgnoreAttributeOrder);
            XmlDiff diff = new XmlDiff();

            if (xmldiffoptionvalue == null || xmldiffoptionvalue.Equals(string.Empty))
                diff.Option = (XmlDiffOption)defaultXmlDiffOptions;
            else
            {
                if (logger != null) logger.LogMessage("Custom XmlDiffOptions used. Value passed is " + xmldiffoptionvalue);
                diff.Option = (XmlDiffOption)Int32.Parse(xmldiffoptionvalue);
            }

            XmlParserContext context = new XmlParserContext(new NameTable(), null, "", XmlSpace.None);

            try
            {
                bResult =
                   diff.Compare(new XmlTextReader(actualStream, XmlNodeType.Element, context),
                             new XmlTextReader(expectedStream, XmlNodeType.Element, context));
            }
            catch (Exception e)
            {
                bResult = false;
                if (logger != null)
                {
                    logger.LogMessage("Exception thrown in XmlDiff compare!");
                    logger.LogXml(e.ToString());
                    throw;
                }
            }

            if (bResult)
                return true;

            if (logger != null)
            {
                logger.LogMessage("Mismatch in XmlDiff");
                logger.LogMessage("Actual result: ");
            }

            return false;
        }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="startFromLine">The line to start calculating the checksum. Any text before this line is ignored. First line is 1.</param>
        /// <param name="logger"></param>
        /// <returns></returns>
        private static string CalcChecksum(string fileName, int startFromLine, DelayedWriteLogger logger)
        {
            const int BUFFERSIZE = 4096;
            Decimal   dResult    = 0;  // Numerical value of the checksum
            int       i          = 0;  // Generic counter
            int       cBytesRead = 1;  // # of bytes read at one time
            int       cTotalRead = 0;  // Total # of bytes read so far
            Decimal   dEndBuffer = 0;  // Buffer to remove from the end (This is necessary because

            // notepad adds CR/LF onto the end of every file)
            Char[] rgBuffer = new Char[BUFFERSIZE];

            string xml = "";

            StreamReader fs = null;

            try
            {
                fs = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read));

                //switch to the line to start from, lines start from 1
                for (int j = 1; j < startFromLine; j++)
                {
                    fs.ReadLine();
                }

                cBytesRead = fs.Read(rgBuffer, 0, BUFFERSIZE);

                while (cBytesRead > 0)
                {
                    // Keep XML property up to date
                    xml = String.Concat(xml, new String(rgBuffer, 0, cBytesRead));

                    // Calculate the checksum
                    for (i = 0; i < cBytesRead; i++)
                    {
                        dResult += Math.Round((Decimal)(rgBuffer[i] / (cTotalRead + i + 1.0)), 10);
                    }

                    cTotalRead += cBytesRead;
                    dEndBuffer  = 0;

                    // Keep reading (in case file is bigger than 4K)
                    cBytesRead = fs.Read(rgBuffer, 0, BUFFERSIZE);
                }
            }
            catch (Exception ex)
            {
                if (logger != null)
                {
                    logger.LogXml(ex.ToString());
                }
                return("");
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }
            return(Convert.ToString(dResult - dEndBuffer, NumberFormatInfo.InvariantInfo));
        }
Exemple #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="startFromLine">The line to start calculating the checksum. Any text before this line is ignored. First line is 1.</param>
        /// <param name="logger"></param>
        /// <returns></returns>
        private static string CalcChecksum(string fileName, int startFromLine, DelayedWriteLogger logger)
        {
            const int BUFFERSIZE = 4096;
            Decimal dResult = 0;        // Numerical value of the checksum
            int i = 0;                  // Generic counter
            int cBytesRead = 1;        // # of bytes read at one time
            int cTotalRead = 0;         // Total # of bytes read so far
            Decimal dEndBuffer = 0;     // Buffer to remove from the end (This is necessary because
            // notepad adds CR/LF onto the end of every file)
            Char[] rgBuffer = new Char[BUFFERSIZE];

            string xml = "";

            StreamReader fs = null;

            try
            {
                fs = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read));

                //switch to the line to start from, lines start from 1
                for (int j = 1; j < startFromLine; j++)
                {
                    fs.ReadLine();
                }

                cBytesRead = fs.Read(rgBuffer, 0, BUFFERSIZE);

                while (cBytesRead > 0)
                {
                    // Keep XML property up to date
                    xml = String.Concat(xml, new String(rgBuffer, 0, cBytesRead));

                    // Calculate the checksum
                    for (i = 0; i < cBytesRead; i++)
                    {
                        dResult += Math.Round((Decimal)(rgBuffer[i] / (cTotalRead + i + 1.0)), 10);
                    }

                    cTotalRead += cBytesRead;
                    dEndBuffer = 0;

                    // Keep reading (in case file is bigger than 4K)
                    cBytesRead = fs.Read(rgBuffer, 0, BUFFERSIZE);
                }
            }
            catch (Exception ex)
            {
                if (logger != null) logger.LogXml(ex.ToString());
                return "";
            }
            finally
            {
                if (fs != null) fs.Dispose();
            }
            return Convert.ToString(dResult - dEndBuffer, NumberFormatInfo.InvariantInfo);
        }