Esempio n. 1
0
        private bool CheckExceptions(IXmlSerializable instance)
        {
            try
            {
                // ReSharper disable ReturnValueOfPureMethodIsNotUsed
                instance.GetSchema();

                // ReSharper restore ReturnValueOfPureMethodIsNotUsed
                throw new UnitTestException(string.Format(CultureInfo.InvariantCulture, Resources.XmlSerialization_GetSchema, Type.Name));
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                // ReSharper disable ReturnValueOfPureMethodIsNotUsed
                instance.ReadXml(null);

                // ReSharper restore ReturnValueOfPureMethodIsNotUsed
                throw new UnitTestException(string.Format(CultureInfo.InvariantCulture, Resources.XmlSerialization_ReadXmlNull, Type.Name));
            }
            catch (ArgumentNullException)
            {
            }

            try
            {
                // ReSharper disable ReturnValueOfPureMethodIsNotUsed
                instance.WriteXml(null);

                // ReSharper restore ReturnValueOfPureMethodIsNotUsed
                throw new UnitTestException(string.Format(CultureInfo.InvariantCulture, Resources.XmlSerialization_WriteXmlNull, Type.Name));
            }
            catch (ArgumentNullException)
            {
            }

            return true;
        }
Esempio n. 2
0
        public void GetSchema_None_IsNull()
        {
            IXmlSerializable obj = TestStruct;

            Assert.IsNull(obj.GetSchema());
        }
Esempio n. 3
0
        internal XmlSerializableMapping(string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
            : base(elementName, ns, typeData, xmlType, xmlTypeNamespace)
        {
#if NET_2_0
            XmlSchemaProviderAttribute schemaProvider = (XmlSchemaProviderAttribute)Attribute.GetCustomAttribute(typeData.Type, typeof(XmlSchemaProviderAttribute));

            if (schemaProvider != null)
            {
                string     method = schemaProvider.MethodName;
                MethodInfo mi     = typeData.Type.GetMethod(method, BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                if (mi == null)
                {
                    throw new InvalidOperationException(String.Format("Type '{0}' must implement public static method '{1}'", typeData.Type, method));
                }
                if (!typeof(XmlQualifiedName).IsAssignableFrom(mi.ReturnType) &&
                    // LAMESPEC: it is undocumented. (We don't have to tell users about it in the error message.)
                    // Also do not add such a silly compatibility test to assert that it does not raise an error.
                    !typeof(XmlSchemaComplexType).IsAssignableFrom(mi.ReturnType))
                {
                    throw new InvalidOperationException(String.Format("Method '{0}' indicated by XmlSchemaProviderAttribute must have its return type as XmlQualifiedName", method));
                }
                XmlSchemaSet xs     = new XmlSchemaSet();
                object       retVal = mi.Invoke(null, new object [] { xs });
                _schemaTypeName = XmlQualifiedName.Empty;
                if (retVal == null)
                {
                    return;
                }

                if (retVal is XmlSchemaComplexType)
                {
                    _schemaType = (XmlSchemaComplexType)retVal;
                    if (!_schemaType.QualifiedName.IsEmpty)
                    {
                        _schemaTypeName = _schemaType.QualifiedName;
                    }
                    else
                    {
                        _schemaTypeName = new XmlQualifiedName(xmlType, xmlTypeNamespace);
                    }
                }
                else if (retVal is XmlQualifiedName)
                {
                    _schemaTypeName = (XmlQualifiedName)retVal;
                }
                else
                {
                    throw new InvalidOperationException(
                              String.Format("Method {0}.{1}() specified by XmlSchemaProviderAttribute has invalid signature: return type must be compatible with System.Xml.XmlQualifiedName.", typeData.Type.Name, method));
                }

                // defaultNamespace at XmlReflectionImporter takes precedence for Namespace, but not for XsdTypeNamespace.
                UpdateRoot(new XmlQualifiedName(_schemaTypeName.Name, Namespace ?? _schemaTypeName.Namespace));
                XmlTypeNamespace = _schemaTypeName.Namespace;
                XmlType          = _schemaTypeName.Name;

                if (!_schemaTypeName.IsEmpty && xs.Count > 0)
                {
                    XmlSchema [] schemas = new XmlSchema [xs.Count];
                    xs.CopyTo(schemas, 0);
                    _schema = schemas [0];
                }

                return;
            }
#endif
            IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance(typeData.Type, true);
            _schema = serializable.GetSchema();
            if (_schema != null)
            {
                if (_schema.Id == null || _schema.Id.Length == 0)
                {
                    throw new InvalidOperationException("Schema Id is missing. The schema returned from " + typeData.Type.FullName + ".GetSchema() must have an Id.");
                }
            }
        }
Esempio n. 4
0
        private void RetrieveSerializableSchema()
        {
            if (_needSchema)
            {
                _needSchema = false;
                if (_getSchemaMethod != null)
                {
                    // get the type info
                    if (_schemas == null)
                    {
                        _schemas = new XmlSchemaSet();
                    }
                    object typeInfo = _getSchemaMethod.Invoke(null, new object[] { _schemas });
                    _xsiType = XmlQualifiedName.Empty;

                    if (typeInfo != null)
                    {
                        if (typeof(XmlSchemaType).IsAssignableFrom(_getSchemaMethod.ReturnType))
                        {
                            _xsdType = (XmlSchemaType)typeInfo;
                            // check if type is named
                            _xsiType = _xsdType.QualifiedName;
                        }
                        else if (typeof(XmlQualifiedName).IsAssignableFrom(_getSchemaMethod.ReturnType))
                        {
                            _xsiType = (XmlQualifiedName)typeInfo;
                            if (_xsiType.IsEmpty)
                            {
                                throw new InvalidOperationException(SR.Format(SR.XmlGetSchemaEmptyTypeName, _type.FullName, _getSchemaMethod.Name));
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlGetSchemaMethodReturnType, _type.Name, _getSchemaMethod.Name, typeof(XmlSchemaProviderAttribute).Name, typeof(XmlQualifiedName).FullName));
                        }
                    }
                    else
                    {
                        _any = true;
                    }

                    // make sure that user-specified schemas are valid
                    _schemas.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackWithErrorCode);
                    _schemas.Compile();
                    // at this point we verified that the information returned by the IXmlSerializable is valid
                    // Now check to see if the type was referenced before:
                    // UNDONE check for the duplcate types
                    if (!_xsiType.IsEmpty)
                    {
                        // try to find the type in the schemas collection
                        if (_xsiType.Namespace != XmlSchema.Namespace)
                        {
                            ArrayList srcSchemas = (ArrayList)_schemas.Schemas(_xsiType.Namespace);

                            if (srcSchemas.Count == 0)
                            {
                                throw new InvalidOperationException(SR.Format(SR.XmlMissingSchema, _xsiType.Namespace));
                            }
                            if (srcSchemas.Count > 1)
                            {
                                throw new InvalidOperationException(SR.Format(SR.XmlGetSchemaInclude, _xsiType.Namespace, _getSchemaMethod.DeclaringType.FullName, _getSchemaMethod.Name));
                            }
                            XmlSchema s = (XmlSchema)srcSchemas[0];
                            if (s == null)
                            {
                                throw new InvalidOperationException(SR.Format(SR.XmlMissingSchema, _xsiType.Namespace));
                            }
                            _xsdType = (XmlSchemaType)s.SchemaTypes[_xsiType];
                            if (_xsdType == null)
                            {
                                throw new InvalidOperationException(SR.Format(SR.XmlGetSchemaTypeMissing, _getSchemaMethod.DeclaringType.FullName, _getSchemaMethod.Name, _xsiType.Name, _xsiType.Namespace));
                            }
                            _xsdType = _xsdType.Redefined != null ? _xsdType.Redefined : _xsdType;
                        }
                    }
                }
                else
                {
                    IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance(_type);
                    _schema = serializable.GetSchema();

                    if (_schema != null)
                    {
                        if (_schema.Id == null || _schema.Id.Length == 0)
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlSerializableNameMissing1, _type.FullName));
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        public void GetSchema_ReturnsNull(ITestItem <Day> testItem)
        {
            IXmlSerializable day = testItem.Create();

            Assert.IsNull(day.GetSchema());
        }
Esempio n. 6
0
        public void GetSchema_ReturnsNull(ITestItem <HalfHour> testItem)
        {
            IXmlSerializable halfHour = testItem.Create();

            Assert.IsNull(halfHour.GetSchema());
        }
        public void GetSchema_None_IsNull()
        {
            IXmlSerializable obj = GetTestInstance();

            Assert.IsNull(obj.GetSchema());
        }
Esempio n. 8
0
        public void GetSchema_ReturnsNull(ITestItem <QuarterHour> testItem)
        {
            IXmlSerializable quarterHour = testItem.Create();

            Assert.IsNull(quarterHour.GetSchema());
        }