Exemple #1
0
        public void CanWriteAndReadXmlOfCollectionWith2Columns()
        {
            var cc = new ColumnCollection();

            cc.Add(new Column {
                ClrType = typeof(string), DbType = "varchar", Name = "abc"
            });
            cc.Add(new Column {
                ClrType = typeof(int), DbType = "int", Name = "def"
            });

            using (var w = new TestXmlWriter())
            {
                new ColumnCollectionSerializer().Serialize(w.Writer, cc);

                using (var r = new TestXmlReader(w.Xml))
                {
                    var cc2 = new ColumnCollectionSerializer().Deserialize(r.Reader);

                    Assert.IsNotNull(cc2);
                    Assert.AreEqual(2, cc2.Count);
                    Assert.AreEqual("abc", cc2[0].Name, "abc");
                    Assert.AreEqual("varchar", cc2[0].DbType, "varchar");
                    Assert.AreSame(typeof(string), cc2[0].ClrType);
                    Assert.AreEqual("def", cc2[1].Name, "def");
                    Assert.AreEqual("int", cc2[1].DbType, "int");
                    Assert.AreEqual(typeof(int), cc2[1].ClrType);
                }
            }
        }
Exemple #2
0
        public void CanWriteXmlForColimnsWith2Columns()
        {
            var xml = new StringBuilder();

            xml.Append("<Columns>");
            xml.Append("<Column name=\"cola\" dbType=\"varchar\" clrType=\"System.String\" />");
            xml.Append("<Column name=\"colb\" dbType=\"int\" clrType=\"System.Int32\" />");
            xml.Append("</Columns>");

            var cc = new ColumnCollection();

            cc.Add(new Column {
                ClrType = typeof(string), DbType = "varchar", Name = "cola"
            });
            cc.Add(new Column {
                ClrType = typeof(int), DbType = "int", Name = "colb"
            });

            using (var w = new TestXmlWriter())
            {
                new ColumnCollectionSerializer().Serialize(w.Writer, cc);

                Assert.AreEqual(xml.ToString(), w.Xml);
            }
        }
Exemple #3
0
        public void CanWriteAndReadResultSetXmlForResultSetWith2ColumnsAnd2Rows()
        {
            var rs = new ResultSet();

            rs.Schema.Columns.Add(new Column {
                Name = "Name", DbType = "nvarchar", ClrType = typeof(string)
            });
            rs.Schema.Columns.Add(new Column {
                Name = "Age", DbType = "int", ClrType = typeof(int)
            });
            rs.Rows.Add(new ResultSetRow());
            rs.Rows.Add(new ResultSetRow());

            rs.Rows[0]["Name"] = "tim";
            rs.Rows[0]["Age"]  = 14;
            rs.Rows[1]["Name"] = "Smith";
            rs.Rows[1]["Age"]  = 15;

            using (var w = new TestXmlWriter())
            {
                new ResultSetSerializer().Serialize(w.Writer, rs);

                using (var r = new TestXmlReader(w.Xml))
                {
                    var rs2 = new ResultSetSerializer().Deserialize(r.Reader);

                    Assert.IsNotNull(rs2);
                    Assert.IsNotNull(rs2.Schema);
                    Assert.IsNotNull(rs2.Schema.Columns);
                    Assert.AreEqual(2, rs2.Schema.Columns.Count);
                    Assert.IsNotNull(rs2.Rows);
                    Assert.AreEqual(2, rs2.Rows.Count);
                }
            }
        }
Exemple #4
0
        private void WriteXml(object context)
        {
            var ctx = context as ThreadContext;

            if (ctx == null)
            {
                throw new ArgumentException("context is not ThreadContext");
            }

            try
            {
                using (var w = new TestXmlWriter())
                {
                    new ResultSetRowSerializer().Serialize(w.Writer, ctx.Row, new ResultSetRowSerializerContext {
                        Schema = ctx.Schema
                    });

                    ctx.Xml = w.Xml;
                }
            }
            catch (Exception ex)
            {
                ctx.Exception = ex;
                throw;
            }
        }
Exemple #5
0
        public void CanWriteXmlForResultSetWith2ColumnsAnd2Rows()
        {
            var rs = new ResultSet();

            rs.Schema.Columns.Add(new Column {
                Name = "Name", DbType = "nvarchar", ClrType = typeof(string)
            });
            rs.Schema.Columns.Add(new Column {
                Name = "Age", DbType = "int", ClrType = typeof(int)
            });
            rs.Rows.Add(new ResultSetRow());
            rs.Rows.Add(new ResultSetRow());

            rs.Rows[0]["Name"] = "tim";
            rs.Rows[0]["Age"]  = 14;
            rs.Rows[1]["Name"] = "Smith";
            rs.Rows[1]["Age"]  = 15;

            using (var w = new TestXmlWriter())
            {
                new ResultSetSerializer().Serialize(w.Writer, rs);

                var expectedXml = "<ResultSet><Schema><Columns><Column name=\"Name\" dbType=\"nvarchar\" clrType=\"System.String\" /><Column name=\"Age\" dbType=\"int\" clrType=\"System.Int32\" /></Columns></Schema><Rows><Row><Name>tim</Name><Age>14</Age></Row><Row><Name>Smith</Name><Age>15</Age></Row></Rows></ResultSet>";
                Assert.AreEqual(expectedXml, w.Xml);
            }
        }
        public void CanWriteAndReadXmlForResultSetSchema()
        {
            var rss = new ResultSetSchema();

            rss.Columns.Add(new Column {
                ClrType = typeof(int), DbType = "int", Name = "colc"
            });

            using (var w = new TestXmlWriter())
            {
                new ResultSetSchemaSerializer().Serialize(w.Writer, rss);

                using (var r = new TestXmlReader(w.Xml))
                {
                    var rss2 = new ResultSetSchemaSerializer().Deserialize(r.Reader);

                    Assert.IsNotNull(rss2);
                    Assert.IsNotNull(rss2.Columns);
                    Assert.AreEqual(1, rss2.Columns.Count);
                    Assert.AreEqual("colc", rss2.Columns[0].Name);
                    Assert.AreEqual("int", rss2.Columns[0].DbType);
                    Assert.AreSame(typeof(int), rss2.Columns[0].ClrType);
                }
            }
        }
Exemple #7
0
        public void CanSerializeActionResultWith2ResultSets()
        {
            var dt = new DateTime(2009, 12, 25, 11, 48, 33).AddMilliseconds(983);

            var actionResult = new ActionResult();

            var rs1 = new ResultSet();

            actionResult.ResultSets.Add(rs1);
            rs1.Schema.Columns.Add(new Column {
                Name = "Id", DbType = "int", ClrType = typeof(int)
            });
            rs1.Schema.Columns.Add(new Column {
                Name = "Name", DbType = "varchar", ClrType = typeof(string)
            });
            rs1.Schema.Columns.Add(new Column {
                Name = "Price", DbType = "decimal", ClrType = typeof(decimal)
            });
            rs1.Schema.Columns.Add(new Column {
                Name = "Expiry", DbType = "date", ClrType = typeof(DateTime)
            });

            rs1.Rows.Add(new ResultSetRow());
            rs1.Rows[0]["Id"]     = 12;
            rs1.Rows[0]["Name"]   = "NVC001";
            rs1.Rows[0]["Price"]  = 12.6m;
            rs1.Rows[0]["Expiry"] = dt;

            rs1.Rows.Add(new ResultSetRow());
            rs1.Rows[1]["Id"]     = 12;
            rs1.Rows[1]["Name"]   = "NVC001";
            rs1.Rows[1]["Price"]  = 12.6m;
            rs1.Rows[1]["Expiry"] = null;

            // 2nd resultset
            var rs2 = new ResultSet();

            actionResult.ResultSets.Add(rs2);
            rs2.Schema.Columns.Add(new Column {
                Name = "Cola", DbType = "int", ClrType = typeof(int)
            });
            rs2.Schema.Columns.Add(new Column {
                Name = "Colb", DbType = "varchar", ClrType = typeof(string)
            });

            rs2.Rows.Add(new ResultSetRow());
            rs2.Rows[0]["Cola"] = 19;
            rs2.Rows[0]["Colb"] = null;

            // declaration of actionresult node is ommitted. this is handled by the .net serializer
            var expectedXml = "<ResultSets><ResultSet><Schema><Columns><Column name=\"Id\" dbType=\"int\" clrType=\"System.Int32\" /><Column name=\"Name\" dbType=\"varchar\" clrType=\"System.String\" /><Column name=\"Price\" dbType=\"decimal\" clrType=\"System.Decimal\" /><Column name=\"Expiry\" dbType=\"date\" clrType=\"System.DateTime\" /></Columns></Schema><Rows><Row><Id>12</Id><Name>NVC001</Name><Price>12.6</Price><Expiry>2009-12-25T11:48:33.983</Expiry></Row><Row><Id>12</Id><Name>NVC001</Name><Price>12.6</Price></Row></Rows></ResultSet><ResultSet><Schema><Columns><Column name=\"Cola\" dbType=\"int\" clrType=\"System.Int32\" /><Column name=\"Colb\" dbType=\"varchar\" clrType=\"System.String\" /></Columns></Schema><Rows><Row><Cola>19</Cola></Row></Rows></ResultSet></ResultSets>";

            using (var w = new TestXmlWriter())
            {
                new ActionResultSerializer().WriteXml(w.Writer, actionResult);

                Assert.AreEqual(expectedXml, w.Xml);
            }
        }
Exemple #8
0
        public void CanWriteAndReadRow()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(int), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "colc", ClrType = typeof(DateTime), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "cold", ClrType = typeof(int), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "cole", ClrType = typeof(int), DbType = ""
            });

            var row = new ResultSetRow();

            row["cola"] = 12;
            row["colb"] = "hallo";
            row["colc"] = new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123);
            row["cold"] = null;
            row["cole"] = DBNull.Value;

            using (var writer = new TestXmlWriter())
            {
                new ResultSetRowSerializer().Serialize(writer.Writer, row, new ResultSetRowSerializerContext {
                    Schema = schema
                });

                using (var r = new TestXmlReader(writer.Xml))
                {
                    var row2 = new ResultSetRowSerializer().Deserialize(r.Reader, new ResultSetRowSerializerContext {
                        Schema = schema
                    });

                    Assert.IsNotNull(row2);
                    Assert.AreEqual(5, row2.Count);

                    Assert.AreEqual(12, row2["cola"]);
                    Assert.AreSame(typeof(int), row2["cola"].GetType());
                    Assert.AreEqual("hallo", row2["colb"]);
                    Assert.AreSame(typeof(string), row2["colb"].GetType());
                    Assert.AreEqual(new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123), row2["colc"]);
                    Assert.AreSame(typeof(DateTime), row2["colc"].GetType());
                    Assert.AreEqual(DBNull.Value, row2["cold"]);
                    Assert.AreSame(DBNull.Value.GetType(), row2["cold"].GetType());
                    Assert.AreEqual(DBNull.Value, row2["cole"]);
                    Assert.AreSame(DBNull.Value.GetType(), row2["cole"].GetType());
                }
            }
        }
Exemple #9
0
 public void WriteXmlTHrowsWithNullActionResult()
 {
     using (var w = new TestXmlWriter())
     {
         Assert.ThrowsException <ArgumentNullException>(() =>
         {
             new ActionResultSerializer().WriteXml(w.Writer, null);
         }, "actionResult");
     }
 }
Exemple #10
0
 public void WriteShouldThrowWithNullResultSet()
 {
     using (var w = new TestXmlWriter())
     {
         Assert.ThrowsException <ArgumentNullException>(() =>
         {
             new ResultSetSerializer().Serialize(w.Writer, null);
         });
     }
 }
Exemple #11
0
        public void CanWriteXmlForEmptyResultSet()
        {
            using (var w = new TestXmlWriter())
            {
                new ResultSetSerializer().Serialize(w.Writer, new ResultSet());

                var expectedXml = "<ResultSet><Schema><Columns /></Schema><Rows /></ResultSet>";
                Assert.AreEqual(expectedXml, w.Xml);
            }
        }
Exemple #12
0
 public void WriteXmlTHrowsWithNullContext()
 {
     using (var w = new TestXmlWriter())
     {
         Assert.ThrowsException <ArgumentNullException>(() =>
         {
             new ResultSetRowSerializer().Serialize(w.Writer, new ResultSetRow(), null);
         }, "context");
     }
 }
Exemple #13
0
 public void WriteXmlThrowsWithNullSchemaInContext()
 {
     using (var w = new TestXmlWriter())
     {
         Assert.ThrowsException <ArgumentNullException>(() =>
         {
             new ResultSetRowCollectionSerializer().Serialize(w.Writer, new ResultSetRowCollection(), new ResultSetRowCollectionSerializerContext {
                 Schema = null
             });
         }, "context.Schema");
     }
 }
Exemple #14
0
        public void CanWrite0ZeroRows()
        {
            using (var writer = new TestXmlWriter())
            {
                new ResultSetRowSerializer().Serialize(writer.Writer, new ResultSetRow(), new ResultSetRowSerializerContext {
                    Schema = new ResultSetSchema()
                });

                var expectedXml = "<Row />";
                Assert.AreEqual(expectedXml, writer.Xml);
            }
        }
Exemple #15
0
 public void WriteXmlThrowsWithNullRow()
 {
     using (var w = new TestXmlWriter())
     {
         Assert.ThrowsException <ArgumentNullException>(() =>
         {
             new ResultSetRowSerializer().Serialize(w.Writer, null, new ResultSetRowSerializerContext {
                 Schema = new ResultSetSchema()
             });
         }, "row");
     }
 }
        public void CannotWriteXmlForInvalidColumn()
        {
            var c = new Column();

            using (var w = new TestXmlWriter())
            {
                Assert.ThrowsException <InvalidOperationException>(() =>
                {
                    new ColumnSerializer().Serialize(w.Writer, new Column());
                });
            }
        }
Exemple #17
0
        public void CanWriteXmlFor0Rows()
        {
            using (var w = new TestXmlWriter())
            {
                var rows = new ResultSetRowCollection();

                new ResultSetRowCollectionSerializer().Serialize(w.Writer, rows, new ResultSetRowCollectionSerializerContext {
                    Schema = new ResultSetSchema()
                });

                Assert.AreEqual("<Rows />", w.Xml);
            }
        }
Exemple #18
0
        public void CanWriteXmlForColumnCollectionWithoutColumns()
        {
            var xml = new StringBuilder();

            xml.Append("<Columns />");

            using (var w = new TestXmlWriter())
            {
                new ColumnCollectionSerializer().Serialize(w.Writer, new ColumnCollection());

                Assert.AreEqual("<Columns />", w.Xml);
            }
        }
Exemple #19
0
        public void WriteXmlTHrowsWithActionResultWithNullResultSets()
        {
            using (var w = new TestXmlWriter())
            {
                var ar = new ActionResult();
                ar.ResultSets = null;

                Assert.ThrowsException <InvalidOperationException>(() =>
                {
                    new ActionResultSerializer().WriteXml(w.Writer, ar);
                }, "actionResult.ResultSets is null");
            }
        }
Exemple #20
0
        public void CanSerializeActionResultWithoutResultSets()
        {
            var actionResult = new ActionResult();

            // declaration of actionresult node is ommitted. this is handled by the .net serializer
            var expectedXml = "<ResultSets />";

            using (var w = new TestXmlWriter())
            {
                new ActionResultSerializer().WriteXml(w.Writer, actionResult);

                Assert.AreEqual(expectedXml, w.Xml);
            }
        }
Exemple #21
0
        public void CanWriteAndReadXmlOfCollectionWithoutColumns()
        {
            var cc = new ColumnCollection();

            using (var w = new TestXmlWriter())
            {
                new ColumnCollectionSerializer().Serialize(w.Writer, cc);

                using (var r = new TestXmlReader(w.Xml))
                {
                    var cc2 = new ColumnCollectionSerializer().Deserialize(r.Reader);

                    Assert.IsNotNull(cc2);
                    Assert.AreEqual(0, cc2.Count);
                }
            }
        }
        public void CanWriteXmlForColumn()
        {
            var expectedXml = "<Column name=\"cola\" dbType=\"varchar\" clrType=\"System.String\" />";

            using (var w = new TestXmlWriter())
            {
                new ColumnSerializer().Serialize(w.Writer,
                                                 new Column
                {
                    ClrType = typeof(string),
                    DbType  = "varchar",
                    Name    = "cola"
                });

                Assert.AreEqual(expectedXml, w.Xml);
            }
        }
Exemple #23
0
        public void CanWriteAndReadResultSetXmlForEmptyResultSet()
        {
            using (var w = new TestXmlWriter())
            {
                new ResultSetSerializer().Serialize(w.Writer, new ResultSet());

                using (var r = new TestXmlReader(w.Xml))
                {
                    var rs = new ResultSetSerializer().Deserialize(r.Reader);

                    Assert.IsNotNull(rs);
                    Assert.IsNotNull(rs.Schema);
                    Assert.IsNotNull(rs.Schema.Columns);
                    Assert.AreEqual(0, rs.Schema.Columns.Count);
                    Assert.IsNotNull(rs.Rows);
                    Assert.AreEqual(0, rs.Rows.Count);
                }
            }
        }
Exemple #24
0
        public static string GetTestXml(this string xml, Dictionary <string, List <string> > ignoredAttributesByElementName = null)
        {
            string formattedXml;

            using (var sw = new StringWriter())
                using (var writer = new TestXmlWriter(sw))
                {
                    var doc = new XmlDocument();
                    doc.LoadXml(xml);

                    if (ignoredAttributesByElementName != null)
                    {
                        HandleIgnoredAttributes(doc, ignoredAttributesByElementName);
                    }

                    doc.Save(writer);
                    formattedXml = sw.ToString();
                }

            return(Regex.Replace(formattedXml, " xmlns(:[^=]+)?='[^']*'", ""));
        }
Exemple #25
0
        public void CanwriteXmlFor2Rows()
        {
            using (var w = new TestXmlWriter())
            {
                var rows = new ResultSetRowCollection();
                rows.Add(new ResultSetRow());
                rows.Add(new ResultSetRow());
                rows[0]["Name"] = "john";
                rows[1]["Name"] = "smith";

                var schema = new ResultSetSchema();
                schema.Columns.Add(new Column {
                    ClrType = typeof(string), DbType = "varchar", Name = "Name"
                });

                new ResultSetRowCollectionSerializer().Serialize(w.Writer, rows, new ResultSetRowCollectionSerializerContext {
                    Schema = schema
                });

                Assert.AreEqual("<Rows><Row><Name>john</Name></Row><Row><Name>smith</Name></Row></Rows>", w.Xml);
            }
        }
        public void WriteOfInvalidSchemaShouldThrow()
        {
            var ex1 = new Exception();

            using (var w = new TestXmlWriter())
            {
                try
                {
                    new ResultSetSchemaSerializer().Serialize(w.Writer, new ResultSetSchemaSerializerValidationTest {
                        ValidationException = ex1
                    });
                }
                catch (Exception ex2)
                {
                    Assert.AreSame(typeof(InvalidOperationException), ex2.GetType());
                    Assert.AreSame(ex1, ex2.InnerException);
                    return;
                }

                Assert.Fail("WriteXml did not throw exception for invalid ResultSetSchema");
            }
        }
Exemple #27
0
        public void CannotWriteXmlForColumnIfColumnIsInvalid()
        {
            using (var w = new TestXmlWriter())
            {
                var ex1 = new Exception();
                try
                {
                    new ColumnCollectionSerializer().Serialize(w.Writer, new ValidationTestColumnCollection {
                        ValidationException = ex1
                    });
                }
                catch (Exception ex2)
                {
                    Assert.AreSame(typeof(InvalidOperationException), ex2.GetType());
                    Assert.AreSame(ex1, ex2.InnerException);

                    return;
                }

                Assert.Fail("Validation exception was not thrown");
            }
        }
Exemple #28
0
        public void WriteShouldThrowIfResultSetIsInvalid()
        {
            var ex1 = new Exception();

            using (var w = new TestXmlWriter())
            {
                try
                {
                    new ResultSetSerializer().Serialize(w.Writer, new TestResultSet {
                        ValidationException = ex1
                    });
                }
                catch (Exception ex2)
                {
                    Assert.AreSame(typeof(InvalidOperationException), ex2.GetType());
                    Assert.AreSame(ex1, ex2.InnerException);
                    return;
                }

                Assert.Fail("Expected exception not thrown");
            }
        }
Exemple #29
0
        public void WriteXmlTHrowsWithInvalidActionResult()
        {
            var ar = new TestActionResult {
                Exception = new Exception()
            };

            using (var w = new TestXmlWriter())
            {
                try
                {
                    new ActionResultSerializer().WriteXml(w.Writer, ar);
                } catch (Exception ex)
                {
                    Assert.AreSame(typeof(InvalidOperationException), ex.GetType());
                    Assert.AreEqual("ActionResult validation failed", ex.Message);
                    Assert.AreSame(ar.Exception, ex.InnerException);
                    return;
                }

                Assert.Fail("Expected exception not thrown");
            }
        }
Exemple #30
0
        public void CanWriteRowWithVariousTypes()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(int), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "colc", ClrType = typeof(DateTime), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "cold", ClrType = typeof(int), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "cole", ClrType = typeof(int), DbType = ""
            });

            var row = new ResultSetRow();

            row["cola"] = 12;
            row["colb"] = "hallo";
            row["colc"] = new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123);
            row["cold"] = null;
            row["cole"] = DBNull.Value;

            using (var writer = new TestXmlWriter())
            {
                new ResultSetRowSerializer().Serialize(writer.Writer, row, new ResultSetRowSerializerContext {
                    Schema = schema
                });

                var expectedXml = "<Row><cola>12</cola><colb>hallo</colb><colc>2011-09-14T18:58:44.123</colc></Row>";
                Assert.AreEqual(expectedXml, writer.Xml);
            }
        }