private bool TypesDifferent(CompareParms parms, Type t1, Type t2)
        {
            //Objects must be the same type and not be null
            if (!parms.Config.IgnoreObjectTypes
                && parms.Object1 != null 
                && parms.Object2 != null 
                && t1 != t2)
            {
                Difference difference = new Difference
                {
                    ParentObject1 = new WeakReference(parms.ParentObject1),
                    ParentObject2 = new WeakReference(parms.ParentObject2),
                    PropertyName = parms.BreadCrumb,
                    Object1Value = t1.FullName,
                    Object2Value = t2.FullName,
                    ChildPropertyName = "GetType()",
                    MessagePrefix = "Different Types",
                    Object1 = new WeakReference(parms.Object1),
                    Object2 = new WeakReference(parms.Object2)
                };

                AddDifference(parms.Result, difference);
                return true;
            }

            return false;
        }
示例#2
0
        public override void CompareType(CompareParms parms)
        {
            var dataRowCollection1 = parms.Object1 as DataRowCollection;
            var dataRowCollection2 = parms.Object2 as DataRowCollection;

            if (null == dataRowCollection1 || null == dataRowCollection2)
                return;

            for (int i = 0; i < Math.Min(dataRowCollection1.Count, dataRowCollection2.Count); i++)
            {
                if (!CollectionContainsMatchingRow(dataRowCollection1[i], dataRowCollection2.Cast<DataRow>()))
                {
                    Difference difference = new Difference
                    {
                        ParentObject1 = new WeakReference(parms.ParentObject1),
                        ParentObject2 = new WeakReference(parms.ParentObject2),
                        PropertyName = parms.BreadCrumb,
                        Object1Value = string.Format("Row Contents: [{0}]", PrettifyRowValues(dataRowCollection1[i])),
                        Object2Value = "[No Matching Row Found in Second DataRowCollection]",
                        ChildPropertyName = string.Format("[{0}]", i),
                        Object1 = new WeakReference(dataRowCollection1[i]),
                        Object2 = new WeakReference(dataRowCollection2[i])
                    };

                    AddDifference(parms.Result, difference);

                    if (parms.Result.ExceededDifferences)
                        return;
                }
            }
        }
        public override void CompareType(CompareParms parms)
        {
            if(parms.ParentObject1 == null || parms.ParentObject2 == null)
            {
                base.CompareType (parms);
                return;
            }

            PropertyInfo prop1 = null, prop2 = null;
            if(parms.Object1 != null)
                prop1 = parms.Object1.GetType ().GetProperty ("Id");
            if(parms.Object2 != null)
                prop2 = parms.Object2.GetType ().GetProperty ("Id");

            if (prop1 != null && prop2 != null &&
                (int)prop1.GetValue (parms.Object1, null) == (int)prop2.GetValue (parms.Object2, null)) {
                if (parms.ParentObject1 is IList)
                    base.CompareType (parms);
                return;
            }

            Difference difference = new Difference
            {
                ParentObject1 =  new WeakReference(parms.ParentObject1),
                ParentObject2 =  new WeakReference(parms.ParentObject2),
                PropertyName = parms.BreadCrumb,
                Object1Value = parms.Object1 == null ? String.Empty : HistoryMain.GetObjectTilte (parms.Object1),
                Object2Value = parms.Object2 == null ? String.Empty : HistoryMain.GetObjectTilte (parms.Object2),
                ChildPropertyName = "Id",
                Object1 = new WeakReference(parms.Object1),
                Object2 = new WeakReference(parms.Object2)
            };

            AddDifference(parms.Result, difference);
        }
        public override void CompareType(CompareParms parms)
        {
            var st1 = (SpecificTenant)parms.Object1;
            var st2 = (SpecificTenant)parms.Object2;

            if (st1.Name != st2.Name || st1.Amount > 100 || st2.Amount < 100)
            {
                AddDifference(parms);
            }
        }
        /// <summary>
        /// Compare two objects
        /// </summary>
        public bool Compare(CompareParms parms)
        {
            try
            {
                if (parms.Object1 == null && parms.Object2 == null)
                    return true;

                Type t1 = parms.Object1 != null ? parms.Object1.GetType() : null;
                Type t2 = parms.Object2 != null ? parms.Object2.GetType() : null;

                BaseTypeComparer customComparer = parms.Config.CustomComparers.FirstOrDefault(o => o.IsTypeMatch(t1, t2));

                if (customComparer != null)
                {
                    customComparer.CompareType(parms);
                }
                else
                {
                    BaseTypeComparer typeComparer = TypeComparers.FirstOrDefault(o => o.IsTypeMatch(t1, t2));

                    if (typeComparer != null)
                    {
                        if (parms.Config.IgnoreObjectTypes || !TypesDifferent(parms, t1, t2))
                        {
                            typeComparer.CompareType(parms);
                        }
                    }
                    else
                    {
                        if (EitherObjectIsNull(parms)) return false;

                        if (!parms.Config.IgnoreObjectTypes && t1 != null)
                            throw new NotSupportedException("Cannot compare object of type " + t1.Name);
                    }
                }

            }
            catch (ObjectDisposedException)
            {
                if (!parms.Config.IgnoreObjectDisposedException)
                    throw;

                return true;
            }

            return parms.Result.AreEqual;
        }
示例#6
0
        private void CompareRowCollections(CompareParms parms)
        {
            DataTable dataTable1 = parms.Object1 as DataTable;
            DataTable dataTable2 = parms.Object2 as DataTable;

            if (null == dataTable1 || null == dataTable2)
                return;

            string currentBreadCrumb = AddBreadCrumb(parms.Config, parms.BreadCrumb, "Rows");

            CompareParms childParms = new CompareParms
            {
                Result = parms.Result,
                Config = parms.Config,
                ParentObject1 = parms.Object1,
                ParentObject2 = parms.Object2,
                Object1 = dataTable1.Rows,
                Object2 = dataTable2.Rows,
                BreadCrumb = currentBreadCrumb,
            };

            RootComparer.Compare(childParms);

            if (parms.Result.ExceededDifferences)
                return;
        }
示例#7
0
        private bool ColumnCountsDifferent(CompareParms parms)
        {
            DataTable dataTable1 = parms.Object1 as DataTable;
            DataTable dataTable2 = parms.Object2 as DataTable;

            if (dataTable1.Columns.Count != dataTable2.Columns.Count)
            {
                Difference difference = new Difference
                {
                    ParentObject1 = new WeakReference(parms.ParentObject1),
                    ParentObject2 = new WeakReference(parms.ParentObject2),
                    PropertyName = parms.BreadCrumb,
                    Object1Value = dataTable1.Columns.Count.ToString(CultureInfo.InvariantCulture),
                    Object2Value = dataTable2.Columns.Count.ToString(CultureInfo.InvariantCulture),
                    ChildPropertyName = "Columns.Count",
                    Object1 = new WeakReference(parms.Object1),
                    Object2 = new WeakReference(parms.Object2)
                };

                AddDifference(parms.Result, difference);

                if (parms.Result.ExceededDifferences)
                    return true;
            }
            return false;
        }
示例#8
0
        /// <summary>
        /// Compare two datatables
        /// </summary>
        public override void CompareType(CompareParms parms)
        {
            DataTable dataTable1 = parms.Object1 as DataTable;
            DataTable dataTable2 = parms.Object2 as DataTable;

            //This should never happen, null check happens one level up
            if (dataTable1 == null || dataTable2 == null)
                return;

            //Only compare specific table names
            if (parms.Config.MembersToInclude.Count > 0 && !parms.Config.MembersToInclude.Contains(dataTable1.TableName))
                return;

            //If we should ignore it, skip it
            if (parms.Config.MembersToInclude.Count == 0 && parms.Config.MembersToIgnore.Contains(dataTable1.TableName))
                return;

            //There must be the same amount of rows in the datatable
            if (dataTable1.Rows.Count != dataTable2.Rows.Count)
            {
                Difference difference = new Difference
                {
                    ParentObject1 = new WeakReference(parms.ParentObject1),
                    ParentObject2 = new WeakReference(parms.ParentObject2),
                    PropertyName = parms.BreadCrumb,
                    Object1Value = dataTable1.Rows.Count.ToString(CultureInfo.InvariantCulture),
                    Object2Value = dataTable2.Rows.Count.ToString(CultureInfo.InvariantCulture),
                    ChildPropertyName = "Rows.Count",
                    Object1 = new WeakReference(parms.Object1),
                    Object2 = new WeakReference(parms.Object2)
                };

                AddDifference(parms.Result, difference);

                if (parms.Result.ExceededDifferences)
                    return;
            }

            if (ColumnCountsDifferent(parms)) return;

            CompareRowCollections(parms);
        }
示例#9
0
        private bool TableCountsDifferent(CompareParms parms, DataSet dataSet2, DataSet dataSet1)
        {
            if (dataSet1.Tables.Count != dataSet2.Tables.Count)
            {
                Difference difference = new Difference
                                            {
                                                ParentObject1 = new WeakReference(parms.ParentObject1),
                                                ParentObject2 = new WeakReference(parms.ParentObject2),
                                                PropertyName = parms.BreadCrumb,
                                                Object1Value = dataSet1.Tables.Count.ToString(CultureInfo.InvariantCulture),
                                                Object2Value = dataSet2.Tables.Count.ToString(CultureInfo.InvariantCulture),
                                                ChildPropertyName = "Tables.Count",
                                                Object1 = new WeakReference(parms.Object1),
                                                Object2 = new WeakReference(parms.Object2)
                                            };

                AddDifference(parms.Result, difference);

                if (parms.Result.ExceededDifferences)
                    return true;
            }
            return false;
        }
示例#10
0
        private void CompareEachTable(CompareParms parms, DataSet dataSet1, DataSet dataSet2)
        {
            for (int i = 0; i < Math.Min(dataSet1.Tables.Count, dataSet2.Tables.Count); i++)
            {
                string currentBreadCrumb = AddBreadCrumb(parms.Config, parms.BreadCrumb, "Tables", string.Empty,
                                                         dataSet1.Tables[i].TableName);

                CompareParms childParms = new CompareParms();
                childParms.Result = parms.Result;
                childParms.Config = parms.Config;
                childParms.BreadCrumb = currentBreadCrumb;
                childParms.ParentObject1 = dataSet1;
                childParms.ParentObject2 = dataSet2;
                childParms.Object1 = dataSet1.Tables[i];
                childParms.Object2 = dataSet2.Tables[i];

                RootComparer.Compare(childParms);

                if (parms.Result.ExceededDifferences)
                    return;
            }
        }
示例#11
0
        /// <summary>
        /// Compare two data sets
        /// </summary>
        public override void CompareType(CompareParms parms)
        {
            DataSet dataSet1 = parms.Object1 as DataSet;
            DataSet dataSet2 = parms.Object2 as DataSet;

            //This should never happen, null check happens one level up
            if (dataSet1 == null || dataSet2 == null)
                return;

            if (TableCountsDifferent(parms, dataSet2, dataSet1)) return;

            CompareEachTable(parms, dataSet1, dataSet2);
        }
示例#12
0
        /// <summary>
        /// Compare two objects of the same type to each other.
        /// </summary>
        /// <remarks>
        /// Check the Differences or DifferencesString Properties for the differences.
        /// Default MaxDifferences is 1 for performance
        /// </remarks>
        /// <param name="object1"></param>
        /// <param name="object2"></param>
        /// <returns>True if they are equal</returns>
        public ComparisonResult Compare(object object1, object object2)
        {
            ComparisonResult result = new ComparisonResult(Config);

            #if !PORTABLE
                result.Watch.Start();
            #endif

            RootComparer rootComparer = RootComparerFactory.GetRootComparer();

            CompareParms parms = new CompareParms
            {
                Config = Config,
                Result = result,
                Object1 = object1,
                Object2 = object2,
                BreadCrumb = string.Empty
            };

            rootComparer.Compare(parms);

            if (Config.AutoClearCache)
                ClearCache();

            #if !PORTABLE
                result.Watch.Stop();
            #endif

            return result;
        }
        private bool EitherObjectIsNull(CompareParms parms)
        {
            //Check if one of them is null
            if (parms.Object1 == null || parms.Object2 == null)
            {
                AddDifference(parms);
                return true;
            }

            return false;
        }