public void TestReadBigDocument() { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); Document expected = new Document(); expected.Append("str", "test") .Append("int", 45) .Append("long", (long)46) .Append("num", 4.5) .Append("date",DateTime.Today) .Append("_id", new OidGenerator().Generate()) .Append("code", new Code("return 1;")) .Append("subdoc", new Document().Append("a",1).Append("b",2)) .Append("array", new String[]{"a","b","c","d"}) .Append("codewscope", new CodeWScope("return 2;", new Document().Append("c",1))) .Append("binary", new Binary(new byte[]{0,1,2,3})) .Append("regex", new MongoRegex("[A-Z]")) .Append("minkey", MongoMinKey.Value) .Append("maxkey", MongoMaxKey.Value) ; writer.Write(expected); writer.Flush(); ms.Seek(0,SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); Document doc = reader.Read(); Assert.IsNotNull(doc); }
protected override void WriteBody(Stream stream) { BsonWriter writer = new BsonWriter(stream); writer.Write(0); writer.Write(this.FullCollectionName); writer.Write(this.NumberToReturn); writer.Write(this.cursorID); writer.Flush(); }
protected override void WriteBody(Stream stream) { BsonWriter writer = new BsonWriter(stream); writer.Write(0); writer.Write(this.CursorIDs.Length); foreach(long id in this.CursorIDs){ writer.Write(id); } writer.Flush(); }
public void TestFormatting() { BsonRegex reg = new BsonRegex("test"); MemoryStream buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); reg.Write(writer); writer.Flush(); Byte[] output = buf.ToArray(); String hexdump = BitConverter.ToString(output); hexdump = hexdump.Replace("-",""); Assert.AreEqual("746573740000",hexdump, "Dump not correct"); }
public void TestFormatting() { BsonString str = new BsonString("test"); MemoryStream buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); str.Write(writer); writer.Flush(); Byte[] output = buf.ToArray(); String hexdump = BitConverter.ToString(output); hexdump = hexdump.Replace("-",""); Assert.AreEqual(5,output[0],"Size didn't take into count null terminator"); Assert.AreEqual("050000007465737400",hexdump, "Dump not correct"); }
public void TestFormattingWithUKPound() { BsonString str = new BsonString("1234£56"); MemoryStream buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); str.Write(writer); writer.Flush(); Byte[] output = buf.ToArray(); String hexdump = BitConverter.ToString(output); hexdump = hexdump.Replace("-",""); Assert.AreEqual("0900000031323334C2A3353600",hexdump, "Dump not correct"); Assert.AreEqual(9,output[0],"Size didn't take into account size of pound symbol"); }
public void TestFormatting() { BsonCode bcode = new BsonCode(); bcode.Val = "return 2;"; MemoryStream buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); bcode.Write(writer); writer.Flush(); Byte[] output = buf.ToArray(); String hexdump = BitConverter.ToString(output); hexdump = hexdump.Replace("-",""); Assert.AreEqual(10,output[0],"Size didn't take into count null terminator"); Assert.AreEqual("0A00000072657475726E20323B00",hexdump, "Dump not correct"); }
public void Write(Stream stream) { MessageHeader header = this.Header; BufferedStream bstream = new BufferedStream(stream); BinaryWriter writer = new BinaryWriter(bstream); BsonWriter bwriter = new BsonWriter(bstream); Header.MessageLength += this.CalculateBodySize(bwriter); writer.Write(header.MessageLength); writer.Write(header.RequestId); writer.Write(header.ResponseTo); writer.Write((int)header.OpCode); writer.Flush(); WriteBody(bwriter); bwriter.Flush(); }
public void TestFormatting() { string expected = "290000000A00000072657475726E20313B0017000000026E73000A00000074657374732E746D700000"; CodeWScope cw = new CodeWScope(); cw.Value = "return 1;"; cw.Scope = new Document().Append("ns","tests.tmp"); BsonCodeWScope bcode = BsonConvert.From(cw); MemoryStream buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); bcode.Write(writer); writer.Flush(); Byte[] output = buf.ToArray(); String hexdump = BitConverter.ToString(output); hexdump = hexdump.Replace("-",""); Assert.AreEqual(expected.Length/2,output[0],"Size didn't take into count null terminator"); Assert.AreEqual(expected,hexdump, "Dump not correct"); }
public void TestDateUTC() { DateTime now = DateTime.UtcNow; MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); Document source = new Document(); source.Append("d",now); writer.Write(source); writer.Flush(); ms.Seek(0,SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); Document copy = reader.Read(); DateTime then = (DateTime)copy["d"]; Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right."); }
public void Write(Stream stream) { MessageHeader header = this.Header; BufferedStream bstream = new BufferedStream(stream); BinaryWriter writer = new BinaryWriter(bstream); BsonWriter bwriter = new BsonWriter(bstream); Header.MessageLength += this.CalculateBodySize(bwriter); if(Header.MessageLength > MessageBase.MaximumMessageSize){ throw new MongoException("Maximum message length exceeded"); } writer.Write(header.MessageLength); writer.Write(header.RequestId); writer.Write(header.ResponseTo); writer.Write((int)header.OpCode); writer.Flush(); WriteBody(bwriter); bwriter.Flush(); }
public void TestWrite() { MemoryStream ms = new MemoryStream(); InsertMessage im = new InsertMessage(); im.FullCollectionName ="Tests.inserts"; Document doc = new Document(); Document[] docs = new Document[]{doc}; doc.Append("a","a"); doc.Append("b",1); im.Documents = docs; Assert.AreEqual(16,im.Header.MessageLength); BsonWriter2 bwriter = new BsonWriter2(ms); Assert.AreEqual(21,bwriter.CalculateSize(doc)); im.Write(ms); Byte[] bytes = ms.ToArray(); String hexdump = BitConverter.ToString(bytes); System.Console.WriteLine(hexdump); MemoryStream ms2 = new MemoryStream(); BsonWriter b = new BsonWriter(ms2); b.Write(im.Header.MessageLength); b.Write(im.Header.RequestId); b.Write(im.Header.ResponseTo); b.Write((int)im.Header.OpCode); b.Write((int)0); b.Write(im.FullCollectionName); BsonDocument bdoc = BsonConvert.From(doc); bdoc.Write(b); b.Flush(); String hexdump2 = BitConverter.ToString(ms2.ToArray()); System.Console.WriteLine(hexdump2); Assert.AreEqual(hexdump2,hexdump); Assert.AreEqual(bytes.Length,im.Header.MessageLength); }
public void TestDBRef() { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); Document source = new Document(); source.Append("x",1).Append("ref",new DBRef("refs","ref1")); writer.Write(source); writer.Flush(); ms.Seek(0,SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); Document copy = reader.Read(); Assert.IsTrue(copy.Contains("ref")); Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef)); DBRef sref = (DBRef)source["ref"]; DBRef cref = (DBRef)copy["ref"]; Assert.AreEqual(sref.Id, cref.Id); }
public void TestNumberElements() { BsonDocument bdoc = new BsonDocument(); MemoryStream buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); Oid oid = new Oid("4a75384cfac16ea58b290350"); bdoc.Append("_id", new BsonElement("_id",new BsonOid(oid))) .Append("a", new BsonElement("a",new BsonNumber(1))) .Append("b", new BsonElement("b",new BsonNumber(2))); bdoc.Write(writer); writer.Flush(); Byte[] output = buf.ToArray(); String hexdump = BitConverter.ToString(output); hexdump = hexdump.Replace("-",""); Assert.AreEqual("2C000000075F6964004A75384CFAC16EA58B290350016100000000000000F03F016200000000000000004000",hexdump, "Dump not correct"); }
protected override void WriteBody(Stream stream) { BsonWriter writer = new BsonWriter(stream); //TODO Implement Query Options (defaulting to none. writer.Write(0); writer.Write(this.FullCollectionName); writer.Write(this.numberToSkip); writer.Write(this.NumberToReturn); this.Query.Write(writer); if(this.ReturnFieldSelector != null) this.ReturnFieldSelector.Write(writer); writer.Flush(); }
public void TestFormattingWithUKPound() { BsonDocument doc = new BsonDocument(); MemoryStream buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); doc.Add("_id", new BsonOid(new Oid("4ABBED9D1D8B0F0218000001"))); doc.Add("test", new BsonString("1234£56")); doc.Write(writer); writer.Flush(); Byte[] output = buf.ToArray(); String hexdump = BitConverter.ToString(output); hexdump = hexdump.Replace("-",""); //Assert.AreEqual(20,output[0],"Size didn't take into count null terminator"); Assert.AreEqual("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000",hexdump, "Dump not correct"); }
protected Document WriteAndRead(Document source) { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); writer.Write(source); writer.Flush(); ms.Seek(0, SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); return reader.Read(); }
/// <summary> /// Writes out a header and the chunk of documents. /// </summary> /// <param name="stream"></param> /// <param name="chunk"></param> protected void WriteChunk(Stream stream, MessageChunk chunk) { WriteHeader(new BinaryWriter(stream), chunk.Size); BsonWriter writer = new BsonWriter(stream); writer.WriteValue(BsonDataType.Integer,0); writer.WriteString(this.FullCollectionName); foreach(Document doc in chunk.Documents){ writer.Write(doc); } writer.Flush(); }
public void TestElements() { BsonDocument bdoc = new BsonDocument(); MemoryStream buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); Oid oid = new Oid("4a753ad8fac16ea58b290351"); bdoc.Append("_id", new BsonElement("_id",new BsonOid(oid))) .Append("a", new BsonElement("a",new BsonNumber(1))) .Append("b", new BsonElement("b",new BsonString("test"))); bdoc.Write(writer); writer.Flush(); Byte[] output = buf.ToArray(); String hexdump = BitConverter.ToString(output); hexdump = hexdump.Replace("-",""); //0 1 2 3 4 5 6 7 8 9 //0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 string expected = "2D000000075F6964004A753AD8FAC16EA58B290351016100000000000000F03F02620005000000746573740000"; Assert.AreEqual(expected,hexdump, "Dump not correct"); }