Ejemplo n.º 1
0
        public void LobGlobalTest()
        {
            DBLob    lob              = null;
            DBLob    lob2             = null;
            DBLob    lob3             = null;
            bool     flag             = false;
            ObjectId oid1             = ObjectId.Empty;
            ObjectId oid2             = ObjectId.Empty;
            ObjectId oid3             = ObjectId.Empty;
            ObjectId oid4             = ObjectId.Empty;
            long     size1            = 0;
            long     time1            = 0;
            long     modificationTime = 0;
            int      bufSize          = 1000;
            int      readNum          = 0;
            int      retNum           = 0;
            int      offset           = 0;
            int      i = 0;

            byte[] readBuf = null;
            byte[] buf     = new byte[bufSize];
            for (i = 0; i < bufSize; i++)
            {
                buf[i] = 65;
            }
            DBCursor     cursor  = null;
            BsonDocument record  = null;
            long         lobSize = 0;

            /// case 1: create a new lob
            // CreateLob
            lob = cl.CreateLob();
            Assert.IsNotNull(lob);
            // IsClosed
            flag = true;
            flag = lob.IsClosed();
            Assert.IsFalse(flag);
            // GetID
            oid1 = lob.GetID();
            Assert.IsTrue(ObjectId.Empty != oid1);
            //
            long modificationTime1 = lob.GetModificationTime();

            // Write
            lob.Write(buf);
            // Close
            lob.Close();
            long modificationTime2 = lob.GetModificationTime();

            Assert.IsTrue(modificationTime2 > modificationTime1);
            // IsClosed
            flag = false;
            flag = lob.IsClosed();
            Assert.IsTrue(flag);

            // case 2: open an exsiting lob
            lob2 = cl.OpenLob(oid1);
            Assert.IsNotNull(lob2);
            // IsClosed
            flag = true;
            flag = lob2.IsClosed();
            Assert.IsFalse(flag);
            // GetID
            oid2 = lob2.GetID();
            Assert.IsTrue(ObjectId.Empty != oid2);
            Assert.IsTrue(oid1 == oid2);
            // GetSize
            size1 = lob2.GetSize();
            Assert.IsTrue(bufSize == size1);
            // GetCreateTime
            time1 = lob2.GetCreateTime();
            //Console.WriteLine("when open an existing lob, createTime is: " + time1);
            Assert.IsTrue(time1 > 0);
            // GetLastModificationTime
            modificationTime = lob2.GetModificationTime();
            Assert.IsTrue(modificationTime > 0);
            //Console.WriteLine("when open an existing lob, modificationTime is: " + modificationTime);
            // Read
            readNum = bufSize / 4;
            readBuf = new Byte[readNum];
            retNum  = lob2.Read(readBuf);
            Assert.IsTrue(readNum == retNum);
            // Seek
            offset = bufSize / 2;
            lob2.Seek(offset, DBLob.SDB_LOB_SEEK_CUR);
            // Read
            retNum = 0;
            retNum = lob2.Read(readBuf);
            Assert.IsTrue(readNum == retNum);
            // Close
            lob2.Close();
            // IsClosed
            flag = false;
            flag = lob2.IsClosed();
            Assert.IsTrue(flag);

            /// case 3: create a lob with specified oid
            oid3 = ObjectId.GenerateNewId();
            lob3 = cl.CreateLob(oid3);
            Assert.IsNotNull(lob3);
            // GetID
            oid4 = lob3.GetID();
            Assert.IsTrue(ObjectId.Empty != oid4);
            Assert.IsTrue(oid3 == oid4);
            // Write
            lob3.Write(buf);
            // Close
            lob3.Close();
            // IsClosed
            flag = false;
            flag = lob3.IsClosed();
            Assert.IsTrue(flag);

            /// case 4: test api in cl
            // ListLobs
            cursor = cl.ListLobs();
            Assert.IsNotNull(cursor);
            i = 0;
            while (null != (record = cursor.Next()))
            {
                i++;
                if (record.Contains("Size") && record["Size"].IsInt64)
                {
                    lobSize = record["Size"].AsInt64;
                }
                else
                {
                    Assert.Fail();
                }
            }
            Assert.IsTrue(2 == i);
            // RemoveLobs
            cl.RemoveLob(oid3);
            // ListLobs
            cursor = cl.ListLobs();
            Assert.IsNotNull(cursor);
            i = 0;
            while (null != (record = cursor.Next()))
            {
                i++;
                if (record.Contains("Size") && record["Size"].IsInt64)
                {
                    lobSize = record["Size"].AsInt64;
                }
                else
                {
                    Assert.Fail();
                }
            }
            Assert.IsTrue(1 == i);
        }