private static void ValidatePropertyValueInstantiated(Type type, object propertyValue, string propertyName, TypeCircularReference <Type> tcr = null) { if (type == typeof(string)) { Assert.IsTrue(propertyValue is string, "Invalid value for " + propertyName); Assert.IsTrue(((string)propertyValue).Length > 0, "Invalid value for " + propertyName); } else if (type == typeof(bool)) { Assert.IsTrue(propertyValue is bool, "Invalid value for " + propertyName); Assert.IsTrue(((bool)propertyValue), "Invalid value for " + propertyName); } else if (type == typeof(int)) { Assert.IsTrue(propertyValue is int, "Invalid value for " + propertyName); Assert.AreEqual(((int)propertyValue), int.MaxValue, "Invalid value for " + propertyName); } else if (type == typeof(long)) { Assert.IsTrue(propertyValue is long, "Invalid value for " + propertyName); Assert.AreEqual(((long)propertyValue), long.MaxValue, "Invalid value for " + propertyName); } else if (type == typeof(double)) { Assert.IsTrue(propertyValue is double, "Invalid value for " + propertyName); Assert.AreEqual(((double)propertyValue), double.MaxValue, "Invalid value for " + propertyName); } else if (type == typeof(DateTime)) { Assert.IsTrue(propertyValue is DateTime, "Invalid value for " + propertyName); Assert.AreEqual(((DateTime)propertyValue).ToUniversalTime(), Constants.DEFAULT_DATE.ToUniversalTime(), "Invalid value for " + propertyName); } else if (type == typeof(MemoryStream)) { Assert.IsTrue(propertyValue is MemoryStream, "Invalid value for " + propertyName); Assert.IsTrue(((MemoryStream)propertyValue).Length > 0, "Invalid value for " + propertyName); } else if (type == typeof(Stream)) { Assert.IsTrue(propertyValue is Stream, "Invalid value for " + propertyName); Assert.IsTrue(((Stream)propertyValue).Length > 0, "Invalid value for " + propertyName); } else if (type.BaseType.FullName == "Amazon.Runtime.ConstantClass") { Assert.IsTrue(propertyValue is Amazon.Runtime.ConstantClass, "Invalid value for " + propertyName); } else { if (type.GetInterface("System.Collections.IList") != null) { var list = propertyValue as System.Collections.IList; var listType = type.GenericTypeArguments[0]; foreach (var item in list) { ValidatePropertyValueInstantiated(listType, item, propertyValue + "_Dictionary", tcr); } } else if (type.GetInterface("System.Collections.IDictionary") != null) { var map = propertyValue as System.Collections.IDictionary; var valueType = type.GenericTypeArguments[1]; foreach (var key in map.Keys) { ValidatePropertyValueInstantiated(valueType, map[key], propertyValue + "_Dictionary", tcr); } } else if (propertyValue == null && tcr != null && tcr.Contains(type)) { // Circular reference found, stop here return; } else { ValidateObjectFullyInstantiated(propertyValue, tcr); } } }
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(); } } }
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(); } } }