PageRenderTime 28ms CodeModel.GetById 23ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Constraints/SerializableConstraints.cs

#
C# | 170 lines | 91 code | 28 blank | 51 comment | 6 complexity | abb29ae9114b9950a6c589da18a656fd MD5 | raw file
  1// ****************************************************************
  2// Copyright 2008, Charlie Poole
  3// This is free software licensed under the NUnit license. You may
  4// obtain a copy of the license at http://nunit.org
  5// ****************************************************************
  6
  7using System;
  8using System.IO;
  9#if !NETCF
 10using System.Runtime.Serialization;
 11using System.Runtime.Serialization.Formatters.Binary;
 12#endif
 13using System.Xml.Serialization;
 14
 15namespace NUnit.Framework.Constraints
 16{
 17#if !NETCF
 18    #region BinarySerializableConstraint
 19
 20    /// <summary>
 21    /// BinarySerializableConstraint tests whether 
 22    /// an object is serializable in binary format.
 23    /// </summary>
 24    public class BinarySerializableConstraint : Constraint
 25    {
 26        readonly BinaryFormatter serializer = new BinaryFormatter();
 27
 28        /// <summary>
 29        /// Test whether the constraint is satisfied by a given value
 30        /// </summary>
 31        /// <param name="actual">The value to be tested</param>
 32        /// <returns>True for success, false for failure</returns>
 33        public override bool Matches(object actual)
 34        {
 35            this.actual = actual;
 36
 37            if(actual == null)
 38                throw new ArgumentException();
 39
 40            MemoryStream stream = new MemoryStream();
 41
 42            try
 43            {
 44                serializer.Serialize(stream, actual);
 45
 46                stream.Seek(0, SeekOrigin.Begin);
 47
 48                object value = serializer.Deserialize(stream);
 49
 50                return value != null;
 51            }
 52            catch (SerializationException)
 53            {
 54                return false;
 55            }
 56        }
 57
 58        /// <summary>
 59        /// Write the constraint description to a MessageWriter
 60        /// </summary>
 61        /// <param name="writer">The writer on which the description is displayed</param>
 62        public override void WriteDescriptionTo(MessageWriter writer)
 63        {
 64            writer.Write("binary serializable");
 65        }
 66
 67        /// <summary>
 68        /// Write the actual value for a failing constraint test to a
 69        /// MessageWriter. The default implementation simply writes
 70        /// the raw value of actual, leaving it to the writer to
 71        /// perform any formatting.
 72        /// </summary>
 73        /// <param name="writer">The writer on which the actual value is displayed</param>
 74        public override void WriteActualValueTo(MessageWriter writer)
 75        {
 76            writer.Write("<{0}>", actual.GetType().Name);
 77        }
 78
 79        /// <summary>
 80        /// Returns the string representation
 81        /// </summary>
 82        protected override string GetStringRepresentation()
 83        {
 84            return "<binaryserializable>";
 85        }
 86    }
 87
 88    #endregion
 89#endif
 90
 91#if !NETCF_1_0
 92    #region XmlSerializableConstraint
 93
 94    /// <summary>
 95    /// BinarySerializableConstraint tests whether 
 96    /// an object is serializable in binary format.
 97    /// </summary>
 98    public class XmlSerializableConstraint : Constraint
 99    {
100        private XmlSerializer serializer;
101
102        /// <summary>
103        /// Test whether the constraint is satisfied by a given value
104        /// </summary>
105        /// <param name="actual">The value to be tested</param>
106        /// <returns>True for success, false for failure</returns>
107        public override bool Matches(object actual)
108        {
109            this.actual = actual;
110
111            if(actual == null)
112                throw new ArgumentException();
113
114            MemoryStream stream = new MemoryStream();
115
116            try
117            {
118                serializer = new XmlSerializer(actual.GetType());
119
120                serializer.Serialize(stream, actual);
121
122                stream.Seek(0, SeekOrigin.Begin);
123
124                object value = serializer.Deserialize(stream);
125
126                return value != null;
127            }
128            catch (NotSupportedException)
129            {
130                return false;
131            }
132            catch (InvalidOperationException)
133            {
134                return false;
135            }
136        }
137
138        /// <summary>
139        /// Write the constraint description to a MessageWriter
140        /// </summary>
141        /// <param name="writer">The writer on which the description is displayed</param>
142        public override void WriteDescriptionTo(MessageWriter writer)
143        {
144            writer.Write("xml serializable");
145        }
146
147        /// <summary>
148        /// Write the actual value for a failing constraint test to a
149        /// MessageWriter. The default implementation simply writes
150        /// the raw value of actual, leaving it to the writer to
151        /// perform any formatting.
152        /// </summary>
153        /// <param name="writer">The writer on which the actual value is displayed</param>
154        public override void WriteActualValueTo(MessageWriter writer)
155        {
156            writer.Write("<{0}>", actual.GetType().Name);
157        }
158
159        /// <summary>
160        /// Returns the string representation of this constraint
161        /// </summary>
162        protected override string GetStringRepresentation()
163        {
164            return "<xmlserializable>";
165        }
166    }
167
168    #endregion
169#endif
170}