Exemple #1
0
        private static void ValidateObjectFullyInstantiated(object owningObject, TypeCircularReference <Type> tcr)
        {
            Assert.IsNotNull(owningObject, "Root object null");

            var owningType = owningObject.GetType();

            if (tcr?.Push(owningType) == true) // No circular reference found
            {
                foreach (var info in owningType.GetProperties())
                {
                    if (info.SetMethod == null || info.Name == "ContentLength")
                    {
                        continue;
                    }

                    if (owningObject is Exception &&
                        (info.Name.Equals("HelpLink") || info.Name.Equals("Source") || info.Name.Equals("HResult") || info.Name.Equals("AmazonId2")))
                    {
                        continue;
                    }

                    var type          = info.PropertyType;
                    var propertyValue = info.GetMethod.Invoke(owningObject, new object[] { });

                    if (type == typeof(int))
                    {
                        if (info.Name == "Status" ||
                            info.Name == "StatusCode")
                        {
                            if (Enum.IsDefined(typeof(HttpStatusCode), propertyValue))
                            {
                                // Special case for GetJobOutputResponse.Status property which is unmarshalled from
                                // HttpResponse's Status code.
                                Assert.AreEqual(200, propertyValue);
                                continue;
                            }
                        }
                    }

                    ValidatePropertyValueInstantiated(type, propertyValue, info.Name, tcr);
                }
                tcr.Pop();
            }
        }
Exemple #2
0
        private static object InstantiateType(TypeCircularReference <Type> tcr, Type type)
        {
            bool pushed = false;

            if (!type.FullName.StartsWith("System."))
            {
                pushed = tcr.Push(type);
                if (!pushed)
                {
                    return(null);
                }
            }

            try
            {
                if (type == typeof(string))
                {
                    return("%2FTest-Value/~.$\\&*value");
                }
                else if (type == typeof(bool) || type == typeof(bool?))
                {
                    return(true);
                }
                else if (type == typeof(int) || type == typeof(int?))
                {
                    return(int.MaxValue);
                }
                else if (type == typeof(long) || type == typeof(long?))
                {
                    return(long.MaxValue);
                }
                else if (type == typeof(double) || type == typeof(double?))
                {
                    return(double.MaxValue);
                }
                else if (type == typeof(DateTime) || type == typeof(DateTime?))
                {
                    return(Constants.DEFAULT_DATE);
                }
                else if (type == typeof(MemoryStream))
                {
                    return(new MemoryStream(Constants.DEFAULT_BLOB));
                }
                //else if (type == typeof(Amazon.S3.S3Region))
                //{
                //    return Amazon.S3.S3Region.USW2; // S3 bucket region
                //}
                else if (type.BaseType.FullName == "Amazon.Runtime.ConstantClass")
                {
                    var value = type.GetFields()[0].GetValue(null);
                    return(value);
                }
                else if (type == typeof(Stream))
                {
                    return(new MemoryStream(Constants.DEFAULT_BLOB));
                }
                else if (type.BaseType.FullName == "System.MulticastDelegate")
                {
                    return(null); // Event Handlers
                }
                //else if (type.IsAssignableFrom(typeof(TimeSpan?)))
                //{
                //    return TimeSpan.FromHours(1); // S3 request timeout
                //}
                //else if (type == typeof(Amazon.S3.Model.ByteRange))
                //{
                //    return null; // s3 byte range
                //}
                //else if (type == typeof(Amazon.S3.Model.HeadersCollection))
                //{
                //    return null;
                //}
                //else if (type == typeof(Amazon.S3.Model.MetadataCollection))
                //{
                //    return null;
                //}
                else
                {
                    var value = Activator.CreateInstance(type);
                    if (type.GetInterface("System.Collections.IList") != null)
                    {
                        var list     = value as System.Collections.IList;
                        var listType = type.GenericTypeArguments[0];
                        if (!tcr.Contains(listType))
                        {
                            // Add some variability to the length to the array
                            for (int i = 0; i < (1 + listType.FullName.Length % 5); i++)
                            {
                                list.Add(InstantiateType(tcr, listType));
                            }
                        }
                    }
                    else if (type.GetInterface("System.Collections.IDictionary") != null)
                    {
                        var map       = value as System.Collections.IDictionary;
                        var valueType = type.GenericTypeArguments[1];
                        if (!tcr.Contains(valueType))
                        {
                            // Add some variability to the length to the array
                            for (int i = 0; i < (1 + valueType.FullName.Length % 5); i++)
                            {
                                map.Add("key" + i, InstantiateType(tcr, valueType));
                            }
                        }
                    }
                    else
                    {
                        InstantiateProperties(tcr, value);
                    }

                    return(value);
                }
            }
            finally
            {
                if (pushed)
                {
                    tcr.Pop();
                }
            }
        }
Exemple #3
0
        private static object InstantiateType(TypeCircularReference <Type> tcr, Type type)
        {
            bool pushed = false;

            if (!type.FullName.StartsWith("System"))
            {
                pushed = tcr.Push(type);
                if (!pushed)
                {
                    return(null);
                }
            }

            try
            {
                if (type == typeof(string))
                {
                    return("Test_Value");
                }
                else if (type == typeof(bool))
                {
                    return(true);
                }
                else if (type == typeof(int))
                {
                    return(int.MaxValue);
                }
                else if (type == typeof(long))
                {
                    return(long.MaxValue);
                }
                else if (type == typeof(double))
                {
                    return(double.MaxValue);
                }
                else if (type == typeof(DateTime))
                {
                    return(Constants.DEFAULT_DATE);
                }
                else if (type == typeof(MemoryStream))
                {
                    return(new MemoryStream(Constants.DEFAULT_BLOB));
                }
                else if (type.BaseType.FullName == "Amazon.Runtime.ConstantClass")
                {
                    var value = type.GetFields()[0].GetValue(null);
                    return(value);
                }
                else if (type == typeof(Stream))
                {
                    return(new MemoryStream(Constants.DEFAULT_BLOB));
                }
                else if (type.BaseType.FullName == "System.MulticastDelegate")
                {
                    return(null); // Event Handlers
                }
                else
                {
                    var value = Activator.CreateInstance(type);
                    if (type.GetInterface("System.Collections.IList") != null)
                    {
                        var list     = value as System.Collections.IList;
                        var listType = type.GenericTypeArguments[0];
                        if (!tcr.Contains(listType))
                        {
                            // Add some variability to the length to the array
                            for (int i = 0; i < (1 + listType.FullName.Length % 5); i++)
                            {
                                list.Add(InstantiateType(tcr, listType));
                            }
                        }
                    }
                    else if (type.GetInterface("System.Collections.IDictionary") != null)
                    {
                        var map       = value as System.Collections.IDictionary;
                        var valueType = type.GenericTypeArguments[1];
                        if (!tcr.Contains(valueType))
                        {
                            // Add some variability to the length to the array
                            for (int i = 0; i < (1 + valueType.FullName.Length % 5); i++)
                            {
                                map.Add("key" + i, InstantiateType(tcr, valueType));
                            }
                        }
                    }
                    else
                    {
                        InstantiateProperties(tcr, value);
                    }

                    return(value);
                }
            }
            finally
            {
                if (pushed)
                {
                    tcr.Pop();
                }
            }
        }