[mapguide-commits] r7673 - in trunk/MgDev/UnitTest/WebTier/DotNet: TestCommon TestCommon/ResourceService TestMapGuideApi TestMapGuideApi/DrawingService

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Mon Jul 8 07:35:51 PDT 2013


Author: jng
Date: 2013-07-08 07:35:50 -0700 (Mon, 08 Jul 2013)
New Revision: 7673

Modified:
   trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/CommonUtility.cs
   trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/ResourceService/Operations.cs
   trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/TestResult.cs
   trunk/MgDev/UnitTest/WebTier/DotNet/TestMapGuideApi/DrawingService/Operations.cs
   trunk/MgDev/UnitTest/WebTier/DotNet/TestMapGuideApi/MapGuideTestExecutorCollection.cs
Log:
#2307: Still at 3 failures, but we've refined the binary equality check to walk the full byte arrays of both expected and actual results comparing each byte as we go. We've also added lots of debug code to try to hunt down the cause of these 3 failures, which currently points to an incorrect implementation of RemoveDwfSectionName(). Sadly, looking at the PHP implementation does not help us because its uses voodoo black magic to treat strings and byte arrays as the same thing!

Modified: trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/CommonUtility.cs
===================================================================
--- trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/CommonUtility.cs	2013-07-08 09:43:54 UTC (rev 7672)
+++ trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/CommonUtility.cs	2013-07-08 14:35:50 UTC (rev 7673)
@@ -1,4 +1,7 @@
-using SqliteDotNet;
+#if DEBUG
+#define DEBUG_BINARY_COMPARISON
+#endif
+using SqliteDotNet;
 using System;
 using System.Collections.Generic;
 using System.Collections.Specialized;
@@ -438,31 +441,32 @@
         private static object RemoveDwfSectionName(object resultData, Encoding enc)
         {
             bool bFromByteArray = false;
-
+            byte[] bResultData = resultData as byte[];
             string strResultData = resultData as string;
+            /*
             if (strResultData == null)
             {
-                byte[] b = resultData as byte[];
-                if (b != null)
+                if (bResultData != null)
                 {
-                    strResultData = enc.GetString(b);
+                    strResultData = enc.GetString(bResultData);
                     bFromByteArray = true;
                 }
-            }
+            }*/
+
             if (strResultData != null)
             {
-                Console.WriteLine("RemoveDwfSectionName: length = {0}", strResultData.Length);
+                //Console.WriteLine("RemoveDwfSectionName: length = {0}", strResultData.Length);
                 int idx = strResultData.IndexOf(".w2d");
-                Console.WriteLine("RemoveDwfSectionName: widx = {0}", idx);
+                //Console.WriteLine("RemoveDwfSectionName: widx = {0}", idx);
                 if (idx >= 0)
                 {
                     string newResult = strResultData.Substring(idx);
                     int eidx = newResult.IndexOf("EndOfDWF");
-                    Console.WriteLine("RemoveDwfSectionName: eidx = {0}", eidx);
+                    //Console.WriteLine("RemoveDwfSectionName: eidx = {0}", eidx);
                     if (0 != eidx)
                     {
                         newResult = newResult.Substring(0, eidx);
-                        Console.WriteLine("RemoveDwfSectionName: newlength = {0}", newResult.Length);
+                        //Console.WriteLine("RemoveDwfSectionName: newlength = {0}", newResult.Length);
                     }
                     if (bFromByteArray)
                         return enc.GetBytes(newResult);
@@ -470,6 +474,114 @@
                         return newResult;
                 }
             }
+            else if (bResultData != null)
+            {
+                byte[] bW2d = enc.GetBytes(".w2d");
+                byte[] bEOF = enc.GetBytes("EndOfDWF");
+
+                int widx = -1;
+                int eidx = -1;
+
+                int wMatches = 0;
+                int eMatches = 0;
+
+                int i = 0;
+                while(i < bResultData.Length)
+                {
+                    //Haven't found .w2d sequence
+                    if (widx < 0 && wMatches == 0) 
+                    {
+                        //We've found a "."
+                        if (bResultData[i] == bW2d[0])
+                        {
+                            wMatches++;
+                            i++;
+
+                            //Now try to follow through this sequence to see if it is ".w2d"
+                            while (wMatches < bW2d.Length)
+                            {
+                                //End of array. Abort
+                                if (i >= bResultData.Length)
+                                    break;
+
+                                //Next byte in sequence matches. Advance
+                                if (bResultData[i] == bW2d[wMatches])
+                                {
+                                    //Increment matches
+                                    wMatches++;
+                                    
+                                    //Check if full sequence matches
+                                    if (wMatches == bW2d.Length)
+                                    {
+                                        //Match. Record index which is current position minus the # of consecutive matches
+                                        widx = i - wMatches;
+                                        break;
+                                    }
+                                }
+                                else //Incomplete sequence. Break this loop
+                                {
+                                    wMatches = 0; //Reset
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                    //Haven't found EndOfDWF sequence
+                    else if (eidx < 0 && eMatches == 0)
+                    {
+                        //We've found a "E"
+                        if (bResultData[i] == bEOF[0])
+                        {
+                            eMatches++;
+                            i++;
+
+                            //Now try to follow through this sequence to see if it is "EndOfDWF"
+                            while (eMatches < bEOF.Length)
+                            {
+                                //End of array. Abort
+                                if (i >= bResultData.Length)
+                                    break;
+
+                                //Next byte in sequence matches. Advance
+                                if (bResultData[i] == bEOF[eMatches])
+                                {
+                                    //Increment matches
+                                    eMatches++;
+
+                                    //Check if full sequence matches
+                                    if (eMatches == bEOF.Length)
+                                    {
+                                        //Match. Record index which is current position minus the # of consecutive matches
+                                        eidx = i - eMatches;
+                                        break;
+                                    }
+                                }
+                                else //Incomplete sequence. Break this loop
+                                {
+                                    eMatches = 0; //Reset
+                                    break;
+                                }
+                            }
+                        }
+                    }
+
+                    //Found both offsets. We're done
+                    if (widx > 0 && eidx > widx)
+                        break;
+
+                    i++;
+                }
+
+                if (widx > 0 && eidx > widx)
+                {
+                    byte[] newResult = new byte[eidx - widx];
+                    int off = 0;
+                    for (int j = widx; j <= eidx; j++)
+                    {
+                        newResult[off] = bResultData[j];
+                    }
+                }
+            }
             return resultData;
         }
 
@@ -572,5 +684,65 @@
 
             return extension;
         }
+
+        public static bool ByteArraysEqual(byte[] bExpected, byte[] bActual, string operation, string testName)
+        {
+            if (bExpected == null && bActual != null)
+                return false;
+
+            if (bExpected != null && bActual == null)
+                return false;
+
+            bool bRet = true;
+#if DEBUG_BINARY_COMPARISON
+            bool bLogged = false;
+            Guid guid = Guid.NewGuid();
+            using (StreamWriter sw1 = new StreamWriter(guid.ToString() + "_" + operation + "_" + testName + "_expected.txt", false))
+            using (StreamWriter sw2 = new StreamWriter(guid.ToString() + "_" + operation + "_" + testName + "_actual.txt", false))
+            {
+#endif
+            for (int i = 0; i < bExpected.Length; i++)
+            {
+                if (i >= bExpected.Length ||
+                    i >= bActual.Length)
+                {
+                    break;
+                }
+
+                byte b1 = bExpected[i];
+                byte b2 = bActual[i];
+
+#if DEBUG_BINARY_COMPARISON
+                sw1.WriteLine("{0} {1}", b1, Convert.ToChar(b1));
+                sw2.WriteLine("{0} {1}", b2, Convert.ToChar(b2));
+#endif
+
+                if (b1 != b2)
+                {
+#if DEBUG_BINARY_COMPARISON
+                    bRet = false;
+                    if (!bLogged)
+                    {
+                        System.Diagnostics.Debug.WriteLine(string.Format("[MgTestRunner]: Comparison {0} returned false. See logged text files", guid.ToString()));
+                        bLogged = true;
+                    }
+#else
+                    return false;
+#endif
+                }
+            }
+#if DEBUG_BINARY_COMPARISON
+            }
+
+            if (bRet)
+            {
+                File.Delete(guid.ToString() + "_" + operation + "_" + testName + "_expected.txt");
+                File.Delete(guid.ToString() + "_" + operation + "_" + testName + "_actual.txt");
+            }
+#endif
+
+            System.Diagnostics.Debug.WriteLine(string.Format("[MgTestRunner]: {0} - {1} - COMPARE: {2} with {3} = {4}", testName, operation, bExpected.Length, bActual.Length, (bRet ? 0 : 1)));
+            return bRet;
+        }
     }
 }

Modified: trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/ResourceService/Operations.cs
===================================================================
--- trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/ResourceService/Operations.cs	2013-07-08 09:43:54 UTC (rev 7672)
+++ trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/ResourceService/Operations.cs	2013-07-08 14:35:50 UTC (rev 7673)
@@ -35,7 +35,7 @@
                 }
                 MgByteReader byteReader = _resourceService.EnumerateResources(resId, Convert.ToInt32(param["DEPTH"]), param["TYPE"] ?? "");
 
-                return TestResult.FromByteReader(byteReader);
+                return TestResult.FromByteReader(byteReader, "GETRESOURCEDATA");
             }
             catch (MgException ex)
             {

Modified: trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/TestResult.cs
===================================================================
--- trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/TestResult.cs	2013-07-08 09:43:54 UTC (rev 7672)
+++ trunk/MgDev/UnitTest/WebTier/DotNet/TestCommon/TestResult.cs	2013-07-08 14:35:50 UTC (rev 7673)
@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Data.SqlTypes;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -37,7 +38,7 @@
             this.HttpStatusCode = statusCode;
         }
 
-        public static TestResult FromByteReader(MgByteReader byteReader)
+        public static TestResult FromByteReader(MgByteReader byteReader, string operation = "")
         {
             try
             {
@@ -55,9 +56,21 @@
                     }
                     else
                     {
+                        MgByteSink sink = new MgByteSink(byteReader);
+                        string path = operation + Guid.NewGuid().ToString() + "Result.bin";
+                        if (string.IsNullOrEmpty(operation))
+                            path = Path.GetTempFileName();
+                        sink.ToFile(path);
+                        res.ResultData = File.ReadAllBytes(path);
+                        if (string.IsNullOrEmpty(operation))
+                            File.Delete(path);
+                        else
+                            System.Diagnostics.Debug.WriteLine(string.Format("[MgTestRunner]: Check out {0} if binary comparison results are strange", path));
+                        /*
                         byte[] bytes = new byte[byteReader.GetLength()];
                         byteReader.Read(bytes, bytes.Length);
                         res.ResultData = bytes;
+                        */
                     }
                 }
                 return res;

Modified: trunk/MgDev/UnitTest/WebTier/DotNet/TestMapGuideApi/DrawingService/Operations.cs
===================================================================
--- trunk/MgDev/UnitTest/WebTier/DotNet/TestMapGuideApi/DrawingService/Operations.cs	2013-07-08 09:43:54 UTC (rev 7672)
+++ trunk/MgDev/UnitTest/WebTier/DotNet/TestMapGuideApi/DrawingService/Operations.cs	2013-07-08 14:35:50 UTC (rev 7673)
@@ -60,7 +60,7 @@
                 }
 
                 MgByteReader reader = _drawingService.GetDrawing(resId);
-                return TestResult.FromByteReader(reader);
+                return TestResult.FromByteReader(reader, "GETDRAWING");
             }
             catch (MgException ex)
             {
@@ -124,7 +124,7 @@
                 }
 
                 MgByteReader reader = _drawingService.GetLayer(resId, param["SECTION"], param["LAYER"]);
-                return TestResult.FromByteReader(reader);
+                return TestResult.FromByteReader(reader, "GETDRAWINGLAYER");
             }
             catch (MgException ex)
             {
@@ -155,7 +155,7 @@
                 }
 
                 MgByteReader reader = _drawingService.GetSection(resId, param["SECTION"]);
-                return TestResult.FromByteReader(reader);
+                return TestResult.FromByteReader(reader, "GETDRAWINGSECTION");
             }
             catch (MgException ex)
             {
@@ -247,7 +247,7 @@
                 }
 
                 MgByteReader reader = _drawingService.GetSectionResource(resId, param["RESOURCENAME"]);
-                return TestResult.FromByteReader(reader);
+                return TestResult.FromByteReader(reader, "GETDRAWINGSECTIONRESOURCE");
             }
             catch (MgException ex)
             {

Modified: trunk/MgDev/UnitTest/WebTier/DotNet/TestMapGuideApi/MapGuideTestExecutorCollection.cs
===================================================================
--- trunk/MgDev/UnitTest/WebTier/DotNet/TestMapGuideApi/MapGuideTestExecutorCollection.cs	2013-07-08 09:43:54 UTC (rev 7672)
+++ trunk/MgDev/UnitTest/WebTier/DotNet/TestMapGuideApi/MapGuideTestExecutorCollection.cs	2013-07-08 14:35:50 UTC (rev 7673)
@@ -407,11 +407,12 @@
                             }
                             else if (bExpected != null && bActual != null)
                             {
-                                //FIXME: Obviously PHP is doing some cryptic black magic that
-                                //causes these supposedly same byte arrays to not be equal so
-                                //we're just doing length comparison for now
-                                bEqual = bExpected.Length == bActual.Length;
+                                bEqual = CommonUtility.ByteArraysEqual(bExpected, bActual, operation, testName);
                             }
+                            else
+                            {
+                                System.Diagnostics.Debug.WriteLine(string.Format("[MgTestRunner]: {0} - {1} - Encountered disparate data types between expected and actual results. Expecting test failure :(", testName, operation));
+                            }
                             
                             //If the results are different and special validation fails then the operation failed ->mark it red
                             if (!bEqual && !CommonUtility.SpecialValidation(operation, resultData, expectedResult))



More information about the mapguide-commits mailing list