diff --git a/sources/OpenMcdf/CFStream.cs b/sources/OpenMcdf/CFStream.cs index 8696f9a..0a8931a 100644 --- a/sources/OpenMcdf/CFStream.cs +++ b/sources/OpenMcdf/CFStream.cs @@ -155,7 +155,7 @@ public byte[] GetData() /// /// byte[] buffer = new byte[2048]; /// item.Read(buffer, 0, 2048); - /// Assert.IsTrue(Helpers.CompareBuffer(b, buffer)); + /// CollectionAssert.AreEqual(b, buffer)); /// /// /// @@ -189,7 +189,7 @@ public int Read(byte[] buffer, long position, int count) /// /// byte[] buffer = new byte[2048]; /// item.Read(buffer, 0, 2048); - /// Assert.IsTrue(Helpers.CompareBuffer(b, buffer)); + /// CollectionAssert.AreEqual(b, buffer); /// /// /// diff --git a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs index 5dcc8a4..eb596b7 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs @@ -51,7 +51,7 @@ public void Test_AS_IOSTREAM_READ() Stream s = cf.RootStorage.GetStorage("MyStorage").GetStream("MyStream").AsIOStream(); BinaryReader br = new BinaryReader(s); byte[] result = br.ReadBytes(32); - Assert.IsTrue(Helpers.CompareBuffer(Helpers.GetBuffer(32, 1), result)); + CollectionAssert.AreEqual(Helpers.GetBuffer(32, 1), result); } [TestMethod] @@ -69,7 +69,7 @@ public void Test_AS_IOSTREAM_WRITE() cf = new CompoundFile("$ACFFile.cfs"); BinaryReader br = new BinaryReader(cf.RootStorage.GetStream("ANewStream").AsIOStream()); string st = br.ReadString(); - Assert.IsTrue(st == cmp); + Assert.AreEqual(cmp, st); cf.Close(); } @@ -102,8 +102,8 @@ public void Test_AS_IOSTREAM_MULTISECTOR_WRITE() { byte[] readData = new byte[data.Length]; int readCount = br.Read(readData, 0, readData.Length); - Assert.IsTrue(readCount == readData.Length); - Assert.IsTrue(data.SequenceEqual(readData)); + Assert.AreEqual(readData.Length, readCount); + CollectionAssert.AreEqual(data, readData); cf.Close(); } } @@ -120,7 +120,7 @@ public void Test_AS_IOSTREAM_MULTISECTOR_WRITE() readData = ms.ToArray(); } - Assert.IsTrue(data.SequenceEqual(readData)); + CollectionAssert.AreEqual(data, readData); cf.Close(); } } diff --git a/sources/Test/OpenMcdf.Test/CFSTorageTest.cs b/sources/Test/OpenMcdf.Test/CFStorageTest.cs similarity index 84% rename from sources/Test/OpenMcdf.Test/CFSTorageTest.cs rename to sources/Test/OpenMcdf.Test/CFStorageTest.cs index 09a4989..3182958 100644 --- a/sources/Test/OpenMcdf.Test/CFSTorageTest.cs +++ b/sources/Test/OpenMcdf.Test/CFStorageTest.cs @@ -6,14 +6,14 @@ namespace OpenMcdf.Test { /// - /// Summary description for CFTorageTest + /// Summary description for CFStorageTest /// [TestClass] - public class CFSTorageTest + public class CFStorageTest { //const String OUTPUT_DIR = "C:\\TestOutputFiles\\"; - public CFSTorageTest() + public CFStorageTest() { } @@ -162,44 +162,23 @@ public void Test_TRY_GET_STREAM_STORAGE_NEW() [TestMethod] public void Test_VISIT_ENTRIES_CORRUPTED_FILE_VALIDATION_ON() { - CompoundFile f = null; + CompoundFile f; try { - f = new CompoundFile("CorruptedDoc_bug3547815.doc", CFSUpdateMode.ReadOnly, CFSConfiguration.NoValidationException); + f = new CompoundFile("CorruptedDoc_bug3547815.doc", CFSUpdateMode.ReadOnly, CFSConfiguration.Default); } catch { Assert.Fail("No exception has to be fired on creation due to lazy loading"); + return; } - FileStream output = null; - - try - { - output = new FileStream("LogEntriesCorrupted_1.txt", FileMode.Create); - - using (StreamWriter tw = new StreamWriter(output)) - { - Action va = delegate (CFItem item) - { - tw.WriteLine(item.Name); - }; - - f.RootStorage.VisitEntries(va, true); - tw.Flush(); - } - } - catch (Exception ex) + Assert.ThrowsException(() => { - Assert.IsTrue(ex is CFCorruptedFileException); - Assert.IsTrue(f != null && f.IsClosed); - } - finally - { - if (output != null) - output.Close(); - } + using StreamWriter tw = new StreamWriter("LogEntriesCorrupted_1.txt"); + f.RootStorage.VisitEntries(item => tw.WriteLine(item.Name), true); + }); } [TestMethod] @@ -209,7 +188,7 @@ public void Test_VISIT_ENTRIES_CORRUPTED_FILE_VALIDATION_OFF_BUT_CAN_LOAD() try { - //Corrupted file has invalid children item sid reference + // Corrupted file has invalid children item sid reference f = new CompoundFile("CorruptedDoc_bug3547815_B.doc", CFSUpdateMode.ReadOnly, CFSConfiguration.NoValidationException); } catch @@ -217,32 +196,16 @@ public void Test_VISIT_ENTRIES_CORRUPTED_FILE_VALIDATION_OFF_BUT_CAN_LOAD() Assert.Fail("No exception has to be fired on creation due to lazy loading"); } - FileStream output = null; - try { - output = new FileStream("LogEntriesCorrupted_2.txt", FileMode.Create); - - using (StreamWriter tw = new StreamWriter(output)) - { - Action va = delegate (CFItem item) - { - tw.WriteLine(item.Name); - }; - - f.RootStorage.VisitEntries(va, true); - tw.Flush(); - } + using StreamWriter tw = new StreamWriter("LogEntriesCorrupted_2.txt"); + f.RootStorage.VisitEntries(item => tw.WriteLine(item.Name), true); + tw.Flush(); } catch { Assert.Fail("Fail is corrupted but it has to be loaded anyway by test design"); } - finally - { - if (output != null) - output.Close(); - } } [TestMethod] @@ -357,15 +320,7 @@ public void Test_CHECK_DISPOSED_() CFStorage st = cf.RootStorage.GetStorage("MyStorage"); cf.Close(); - try - { - byte[] temp = st.GetStream("MyStream").GetData(); - Assert.Fail("Stream without media"); - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFDisposedException); - } + Assert.ThrowsException(() => st.GetStream("MyStream").GetData()); } [TestMethod] @@ -385,7 +340,7 @@ public void Test_LAZY_LOAD_CHILDREN_() IList i = cf.GetAllNamedEntries("Level2Stream"); Assert.IsNotNull(i[0]); Assert.IsTrue(i[0] is CFStream); - Assert.IsTrue((i[0] as CFStream).GetData().Length == 100); + Assert.AreEqual(100, (i[0] as CFStream).GetData().Length); cf.SaveAs("$Hel2"); cf.Close(); @@ -414,14 +369,11 @@ public void Test_FIX_BUG_31() cf.Close(); CompoundFile cf1 = new CompoundFile("$Hel3"); - try + + Assert.ThrowsException(() => { CFStream cs = cf1.RootStorage.GetStorage("Level_1").AddStream("Level2Stream"); - } - catch (Exception ex) - { - Assert.IsTrue(ex.GetType() == typeof(CFDuplicatedItemException)); - } + }); } [TestMethod] diff --git a/sources/Test/OpenMcdf.Test/CFSStreamTest.cs b/sources/Test/OpenMcdf.Test/CFStreamTest.cs similarity index 92% rename from sources/Test/OpenMcdf.Test/CFSStreamTest.cs rename to sources/Test/OpenMcdf.Test/CFStreamTest.cs index 061f644..faace44 100644 --- a/sources/Test/OpenMcdf.Test/CFSStreamTest.cs +++ b/sources/Test/OpenMcdf.Test/CFStreamTest.cs @@ -10,11 +10,12 @@ namespace OpenMcdf.Test /// Summary description for UnitTest1 /// [TestClass] - public class CFSStreamTest + public class CFStreamTest + { //const String TestContext.TestDir = "C:\\TestOutputFiles\\"; - public CFSStreamTest() + public CFStreamTest() { } @@ -73,11 +74,11 @@ public void Test_WRITE_STREAM() CFStream myStream = cf.RootStorage.AddStream("MyStream"); Assert.IsNotNull(myStream); - Assert.IsTrue(myStream.Size == 0); + Assert.AreEqual(0, myStream.Size); myStream.SetData(b); - Assert.IsTrue(myStream.Size == BUFFER_LENGTH, "Stream size differs from buffer size"); + Assert.AreEqual(BUFFER_LENGTH, myStream.Size, "Stream size differs from buffer size"); cf.Close(); } @@ -93,11 +94,11 @@ public void Test_WRITE_MINI_STREAM() CFStream myStream = cf.RootStorage.AddStream("MyMiniStream"); Assert.IsNotNull(myStream); - Assert.IsTrue(myStream.Size == 0); + Assert.AreEqual(0, myStream.Size); myStream.SetData(b); - Assert.IsTrue(myStream.Size == BUFFER_LENGTH, "Mini Stream size differs from buffer size"); + Assert.AreEqual(BUFFER_LENGTH, myStream.Size, "Mini Stream size differs from buffer size"); cf.Close(); } @@ -157,7 +158,7 @@ public void Test_ZERO_LENGTH_RE_WRITE_STREAM() CFStream oStream = cfo.RootStorage.GetStream("MyStream"); Assert.IsNotNull(oStream); - Assert.IsTrue(oStream.Size == 0); + Assert.AreEqual(0, oStream.Size); try { @@ -199,9 +200,9 @@ public void Test_WRITE_STREAM_WITH_DIFAT() CFStream st = cf2.RootStorage.GetStream("MyStream"); Assert.IsNotNull(cf2); - Assert.IsTrue(st.Size == SIZE); + Assert.AreEqual(SIZE, st.Size); - Assert.IsTrue(Helpers.CompareBuffer(b, st.GetData())); + CollectionAssert.AreEqual(b, st.GetData()); cf2.Close(); @@ -230,31 +231,31 @@ public void Test_WRITE_MINISTREAM_READ_REWRITE_STREAM() Assert.IsNotNull(myStream); myStream.SetData(ba1); - Assert.IsTrue(myStream.Size == BIGGER_SIZE); + Assert.AreEqual(BIGGER_SIZE, myStream.Size); CFStream myStream2 = cfa.RootStorage.AddStream("MySecondStream"); Assert.IsNotNull(myStream2); myStream2.SetData(ba2); - Assert.IsTrue(myStream2.Size == BIGGER_SIZE); + Assert.AreEqual(BIGGER_SIZE, myStream2.Size); CFStream myStream3 = cfa.RootStorage.AddStream("MyThirdStream"); Assert.IsNotNull(myStream3); myStream3.SetData(ba3); - Assert.IsTrue(myStream3.Size == BIGGER_SIZE); + Assert.AreEqual(BIGGER_SIZE, myStream3.Size); CFStream myStream4 = cfa.RootStorage.AddStream("MyFourthStream"); Assert.IsNotNull(myStream4); myStream4.SetData(ba4); - Assert.IsTrue(myStream4.Size == BIGGER_SIZE); + Assert.AreEqual(BIGGER_SIZE, myStream4.Size); CFStream myStream5 = cfa.RootStorage.AddStream("MyFifthStream"); Assert.IsNotNull(myStream5); myStream5.SetData(ba5); - Assert.IsTrue(myStream5.Size == BIGGER_SIZE); + Assert.AreEqual(BIGGER_SIZE, myStream5.Size); cfa.SaveAs("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs"); @@ -266,7 +267,7 @@ public void Test_WRITE_MINISTREAM_READ_REWRITE_STREAM() CFStream myStreamB = cfb.RootStorage.GetStream("MySecondStream"); Assert.IsNotNull(myStreamB); myStreamB.SetData(bb); - Assert.IsTrue(myStreamB.Size == MEGA_SIZE); + Assert.AreEqual(MEGA_SIZE, myStreamB.Size); byte[] bufferB = myStreamB.GetData(); cfb.SaveAs("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs"); @@ -274,10 +275,11 @@ public void Test_WRITE_MINISTREAM_READ_REWRITE_STREAM() CompoundFile cfc = new CompoundFile("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs"); CFStream myStreamC = cfc.RootStorage.GetStream("MySecondStream"); - Assert.IsTrue(myStreamC.Size == MEGA_SIZE, "DATA SIZE FAILED"); + Assert.AreEqual(MEGA_SIZE, myStreamC.Size, "DATA SIZE FAILED"); byte[] bufferC = myStreamC.GetData(); - Assert.IsTrue(Helpers.CompareBuffer(bufferB, bufferC), "DATA INTEGRITY FAILED"); + CollectionAssert.AreEqual(bb, bufferB); + CollectionAssert.AreEqual(bb, bufferC); cfc.Close(); @@ -305,7 +307,7 @@ public void Test_RE_WRITE_SMALLER_STREAM() cf = new CompoundFile("reportRW_SMALL.xls"); byte[] c = cf.RootStorage.GetStream("Workbook").GetData(); - Assert.IsTrue(c.Length == BUFFER_LENGTH); + Assert.AreEqual(BUFFER_LENGTH, c.Length); cf.Close(); if (File.Exists("reportRW_SMALL.xls")) @@ -328,8 +330,7 @@ public void Test_RE_WRITE_SMALLER_MINI_STREAM() cf = new CompoundFile("RE_WRITE_SMALLER_MINI_STREAM.xls"); byte[] c = cf.RootStorage.GetStream("\x05SummaryInformation").GetData(); - Assert.IsTrue(c.Length == TEST_LENGTH); - Assert.IsTrue(Helpers.CompareBuffer(c, b)); + CollectionAssert.AreEqual(c, b); cf.Close(); if (File.Exists("RE_WRITE_SMALLER_MINI_STREAM.xls")) @@ -389,7 +390,7 @@ public void Test_TRANSACTED_ADD_REMOVE_MULTIPLE_STREAM_TO_EXISTING_FILE() Assert.IsNotNull(addedStream, "Stream not found"); addedStream.SetData(buffer); - Assert.IsTrue(Helpers.CompareBuffer(addedStream.GetData(), buffer), "Data buffer corrupted"); + CollectionAssert.AreEqual(addedStream.GetData(), buffer); // Random commit, not on single addition //if (r.Next(0, 100) > 50) @@ -510,7 +511,7 @@ public void Test_WRITE_AND_READ_CFS() cf2.Close(); Assert.IsNotNull(sm2); - Assert.IsTrue(sm2.Size == 220); + Assert.AreEqual(220, sm2.Size); if (File.Exists(filename)) File.Delete(filename); @@ -619,8 +620,8 @@ public static void SingleTransactedChange(int size) CFStream sm2 = st2.GetStream("MyStream"); Assert.IsNotNull(sm2); - Assert.IsTrue(sm2.Size == size); - Assert.IsTrue(Helpers.CompareBuffer(sm2.GetData(), b)); + Assert.AreEqual(size, sm2.Size); + CollectionAssert.AreEqual(b, sm2.GetData()); cf2.Close(); } @@ -647,8 +648,8 @@ private static void SingleWriteReadMatching(int size) CFStream sm2 = st2.GetStream("MyStream"); Assert.IsNotNull(sm2); - Assert.IsTrue(sm2.Size == size); - Assert.IsTrue(Helpers.CompareBuffer(sm2.GetData(), b)); + Assert.AreEqual(size, sm2.Size); + CollectionAssert.AreEqual(b, sm2.GetData()); cf2.Close(); } @@ -672,8 +673,7 @@ private static void SingleWriteReadMatchingSTREAMED(int size) CFStream sm2 = st2.GetStream("MyStream"); Assert.IsNotNull(sm2); - Assert.IsTrue(sm2.Size == size); - Assert.IsTrue(Helpers.CompareBuffer(sm2.GetData(), b)); + CollectionAssert.AreEqual(b, sm2.GetData()); cf2.Close(); } @@ -697,7 +697,7 @@ public void Test_APPEND_DATA_TO_STREAM() byte[] cmp = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 }; cf = new CompoundFile(ms); byte[] data = cf.RootStorage.GetStream("MyMiniStream").GetData(); - Assert.IsTrue(Helpers.CompareBuffer(cmp, data)); + CollectionAssert.AreEqual(cmp, data); } [TestMethod] @@ -716,7 +716,7 @@ public void Test_COPY_FROM_STREAM() cf = new CompoundFile("COPY_FROM_STREAM.cfs"); byte[] data = cf.RootStorage.GetStream("MyImportedStream").GetData(); - Assert.IsTrue(Helpers.CompareBuffer(b, data)); + CollectionAssert.AreEqual(b, data); } #if LARGETEST @@ -750,11 +750,12 @@ public void Test_APPEND_DATA_TO_CREATE_LARGE_STREAM() cf = new CompoundFile("MEGALARGESSIMUSFILE.cfs"); int count = 8; byte[] data = cf.RootStorage.GetStream("MySuperLargeStream").GetData((long)b.Length * 42L, ref count); - Assert.IsTrue(Helpers.CompareBuffer(cmp, data)); + CollectionAssert.AreEqual(cmp, data); cf.Close(); } #endif + [TestMethod] public void Test_RESIZE_STREAM_NO_TRANSITION() { @@ -777,12 +778,12 @@ public void Test_RESIZE_STREAM_NO_TRANSITION() cf = new CompoundFile("$Test_RESIZE_STREAM.cfs", CFSUpdateMode.ReadOnly, CFSConfiguration.Default); item = cf.RootStorage.GetStream("AStream"); - Assert.IsTrue(item != null); - Assert.IsTrue(item.Size == INITIAL_SIZE - DELTA_SIZE); + Assert.IsNotNull(item); + Assert.AreEqual(INITIAL_SIZE - DELTA_SIZE, item.Size); byte[] buffer = new byte[INITIAL_SIZE - DELTA_SIZE]; - item.Read(buffer, 0, INITIAL_SIZE - DELTA_SIZE); - Assert.IsTrue(Helpers.CompareBuffer(b, buffer, INITIAL_SIZE - DELTA_SIZE)); + item.Read(buffer, 0, buffer.Length); + CollectionAssert.AreEqual(b.Take(buffer.Length).ToList(), buffer); cf.Close(); } @@ -810,7 +811,7 @@ public void Test_RESIZE_STREAM_TRANSITION_TO_MINI() cf.Close(); cf = new CompoundFile(FILE_NAME, CFSUpdateMode.ReadOnly, CFSConfiguration.Default); - Assert.IsTrue(Helpers.CompareBuffer(cf.RootStorage.GetStream("AStream").GetData(), b100)); + CollectionAssert.AreEqual(b100, cf.RootStorage.GetStream("AStream").GetData()); cf.Close(); if (File.Exists(FILE_NAME)) @@ -836,12 +837,12 @@ public void Test_RESIZE_STREAM_TRANSITION_TO_NORMAL() cf = new CompoundFile("$Test_RESIZE_STREAM_TRANSITION_TO_NORMAL.cfs", CFSUpdateMode.ReadOnly, CFSConfiguration.Default); item = cf.RootStorage.GetStream("AStream"); - Assert.IsTrue(item != null); - Assert.IsTrue(item.Size == 5000); + Assert.IsNotNull(item); + Assert.AreEqual(5000, item.Size); byte[] buffer = new byte[2048]; item.Read(buffer, 0, 2048); - Assert.IsTrue(Helpers.CompareBuffer(b, buffer)); + CollectionAssert.AreEqual(b, buffer); } [TestMethod] @@ -868,12 +869,11 @@ public void Test_RESIZE_MINISTREAM_NO_TRANSITION_MOD() CFStream st = cf.RootStorage.GetStream("MiniStream"); Assert.IsNotNull(st); - Assert.IsTrue(st.Size == (INITIAL_SIZE - SIZE_DELTA)); + Assert.AreEqual(INITIAL_SIZE - SIZE_DELTA, st.Size); byte[] buffer = new byte[INITIAL_SIZE - SIZE_DELTA]; - st.Read(buffer, 0, INITIAL_SIZE - SIZE_DELTA); - - Assert.IsTrue(Helpers.CompareBuffer(b, buffer, INITIAL_SIZE - SIZE_DELTA)); + st.Read(buffer, 0, buffer.Length); + CollectionAssert.AreEqual(b.Take(buffer.Length).ToList(), buffer); cf.Close(); } @@ -901,9 +901,9 @@ public void Test_RESIZE_MINISTREAM_SECTOR_RECYCLE() cf.SaveAs("$Test_RESIZE_MINISTREAM_RECYCLE2.cfs"); cf.Close(); - Assert.IsTrue( - new FileInfo("$Test_RESIZE_MINISTREAM_RECYCLE.cfs").Length - == new FileInfo("$Test_RESIZE_MINISTREAM_RECYCLE2.cfs").Length); + Assert.AreEqual( + new FileInfo("$Test_RESIZE_MINISTREAM_RECYCLE.cfs").Length, + new FileInfo("$Test_RESIZE_MINISTREAM_RECYCLE2.cfs").Length); } [TestMethod] @@ -955,7 +955,7 @@ public void TEST_STREAM_VIEW() BinaryReader br = new BinaryReader(sv); int t = br.ReadInt32(); - Assert.IsTrue(t == 1); + Assert.AreEqual(1, t); } [TestMethod] @@ -970,7 +970,7 @@ public void Test_STREAM_VIEW_2() BinaryReader br = new BinaryReader(sv); int t = br.ReadInt32(); - Assert.IsTrue(t == 1); + Assert.AreEqual(1, t); } /// @@ -995,7 +995,7 @@ public void Test_STREAM_VIEW_3() for (int i = 0; i < 200; i++) { - Assert.IsTrue(i == br.ReadInt32(), "Failed with " + i.ToString()); + Assert.AreEqual(i, br.ReadInt32(), "Failed with " + i.ToString()); } } @@ -1021,7 +1021,7 @@ public void Test_STREAM_VIEW_LARGE_DATA() for (int i = 0; i < 200; i++) { - Assert.IsTrue(i == br.ReadInt32(), "Failed with " + i.ToString()); + Assert.AreEqual(i, br.ReadInt32(), "Failed with " + i.ToString()); } } diff --git a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs index 6b955f4..3b48cb1 100644 --- a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs +++ b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs @@ -86,37 +86,19 @@ public void Test_ENTRY_NAME_LENGTH() Assert.IsNotNull(cf.RootStorage.AddStorage(maxCharactersStorageName)); CFStorage strg = cf.RootStorage.GetStorage(maxCharactersStorageName); Assert.IsNotNull(strg); - Assert.IsTrue(strg.Name == maxCharactersStorageName); + Assert.AreEqual(maxCharactersStorageName, strg.Name); // Try Stream entry name with max characters. Assert.IsNotNull(cf.RootStorage.AddStream(maxCharactersStreamName)); CFStream strm = cf.RootStorage.GetStream(maxCharactersStreamName); Assert.IsNotNull(strm); - Assert.IsTrue(strm.Name == maxCharactersStreamName); + Assert.AreEqual(maxCharactersStreamName, strm.Name); string tooManyCharactersEntryName = "12345678901234567890123456789012"; // 32 chars - try - { - // Try Storage entry name with too many characters. - cf.RootStorage.AddStorage(tooManyCharactersEntryName); - Assert.Fail(); - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFException); - } + Assert.ThrowsException(() => cf.RootStorage.AddStorage(tooManyCharactersEntryName)); - try - { - // Try Stream entry name with too many characters. - cf.RootStorage.AddStream(tooManyCharactersEntryName); - Assert.Fail(); - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFException); - } + Assert.ThrowsException(() => cf.RootStorage.AddStream(tooManyCharactersEntryName)); cf.SaveAs("EntryNameLength"); cf.Close(); @@ -167,7 +149,7 @@ public void Test_WRITE_AND_READ_CFS_VERSION_4() CFStream sm2 = st2.GetStream("MyStream"); Assert.IsNotNull(sm2); - Assert.IsTrue(sm2.Size == 220); + Assert.AreEqual(220, sm2.Size); cf2.Close(); } @@ -192,7 +174,7 @@ public void Test_WRITE_READ_CFS_VERSION_4_STREAM() CFStream sm2 = st2.GetStream("MyStream"); Assert.IsNotNull(sm2); - Assert.IsTrue(sm2.Size == b.Length); + Assert.AreEqual(b.Length, sm2.Size); cf2.Close(); } @@ -237,7 +219,7 @@ public void Test_OPEN_COMPOUND_BUG_FIX_133() var f = new CompoundFile("testbad.ole"); CFStream cfs = f.RootStorage.GetStream("\x01Ole10Native"); byte[] data = cfs.GetData(); - Assert.IsTrue(data.Length == 18140); + Assert.AreEqual(18140, data.Length); } [TestMethod] @@ -251,7 +233,7 @@ public void Test_COMPARE_DIR_ENTRY_NAME_BUG_FIX_ID_3487353() f = new CompoundFile("report_name_fix.xls", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors); cfs = f.RootStorage.GetStream("Workbook"); - Assert.IsTrue(cfs.Name == "Workbook"); + Assert.AreEqual("Workbook", cfs.Name); f.RootStorage.Delete("PoorBook"); f.Commit(); f.Close(); @@ -261,10 +243,7 @@ public void Test_COMPARE_DIR_ENTRY_NAME_BUG_FIX_ID_3487353() public void Test_GET_COMPOUND_VERSION() { var f = new CompoundFile("report_name_fix.xls"); - CFSVersion ver = f.Version; - - Assert.IsTrue(ver == CFSVersion.Ver_3); - + Assert.AreEqual(CFSVersion.Ver_3, f.Version); f.Close(); } @@ -302,8 +281,7 @@ public void Test_FUNCTIONAL_BEHAVIOUR() CFStream testSt = cfTest.RootStorage.GetStream("A"); Assert.IsNotNull(testSt); - Assert.IsTrue(testSt.Size == bA.Length); - Assert.IsTrue(Helpers.CompareBuffer(bA, testSt.GetData())); + CollectionAssert.AreEqual(bA, testSt.GetData()); cfTest.Close(); @@ -329,38 +307,31 @@ public void Test_FUNCTIONAL_BEHAVIOUR() testSt = cfTest.RootStorage.GetStream("B"); Assert.IsNotNull(testSt); - Assert.IsTrue(testSt.Size == bB.Length); - Assert.IsTrue(Helpers.CompareBuffer(bB, testSt.GetData())); + CollectionAssert.AreEqual(bB, testSt.GetData()); testSt = cfTest.RootStorage.GetStream("C"); Assert.IsNotNull(testSt); - Assert.IsTrue(testSt.Size == bC.Length); - Assert.IsTrue(Helpers.CompareBuffer(bC, testSt.GetData())); + CollectionAssert.AreEqual(bC, testSt.GetData()); testSt = cfTest.RootStorage.GetStream("D"); Assert.IsNotNull(testSt); - Assert.IsTrue(testSt.Size == bD.Length); - Assert.IsTrue(Helpers.CompareBuffer(bD, testSt.GetData())); + CollectionAssert.AreEqual(bD, testSt.GetData()); testSt = cfTest.RootStorage.GetStream("E"); Assert.IsNotNull(testSt); - Assert.IsTrue(testSt.Size == bE.Length); - Assert.IsTrue(Helpers.CompareBuffer(bE, testSt.GetData())); + CollectionAssert.AreEqual(bE, testSt.GetData()); testSt = cfTest.RootStorage.GetStream("F"); Assert.IsNotNull(testSt); - Assert.IsTrue(testSt.Size == bF.Length); - Assert.IsTrue(Helpers.CompareBuffer(bF, testSt.GetData())); + CollectionAssert.AreEqual(bF, testSt.GetData()); testSt = cfTest.RootStorage.GetStream("G"); Assert.IsNotNull(testSt); - Assert.IsTrue(testSt.Size == bG.Length); - Assert.IsTrue(Helpers.CompareBuffer(bG, testSt.GetData())); + CollectionAssert.AreEqual(bG, testSt.GetData()); testSt = cfTest.RootStorage.GetStream("H"); Assert.IsNotNull(testSt); - Assert.IsTrue(testSt.Size == bH.Length); - Assert.IsTrue(Helpers.CompareBuffer(bH, testSt.GetData())); + CollectionAssert.AreEqual(bH, testSt.GetData()); cfTest.Close(); @@ -383,34 +354,8 @@ public void Test_FUNCTIONAL_BEHAVIOUR() //Test Phase 3 cfTest = new CompoundFile("6_Streams.cfs"); - - bool catched = false; - - try - { - testSt = cfTest.RootStorage.GetStream("D"); - } - catch (Exception ex) - { - if (ex is CFItemNotFound) - catched = true; - } - - Assert.IsTrue(catched); - - catched = false; - - try - { - testSt = cfTest.RootStorage.GetStream("G"); - } - catch (Exception ex) - { - if (ex is CFItemNotFound) - catched = true; - } - - Assert.IsTrue(catched); + Assert.ThrowsException(() => cfTest.RootStorage.GetStream("D")); + Assert.ThrowsException(() => cfTest.RootStorage.GetStream("G")); cfTest.Close(); @@ -436,7 +381,7 @@ public void Test_FUNCTIONAL_BEHAVIOUR() byte[] d = ia.GetData(); Assert.IsNotNull(d); Assert.IsTrue(d.Length > 0); - Assert.IsTrue(d.Length == ia.Size); + Assert.AreEqual(ia.Size, d.Length); } }; @@ -497,28 +442,21 @@ public void Test_FUNCTIONAL_BEHAVIOUR() cfTest = new CompoundFile("6_Streams_Shrinked.cfs"); byte[] d2 = cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").GetData(); - Assert.IsTrue(d2.Length == (bE.Length + bMini.Length)); + Assert.AreEqual(bE.Length + bMini.Length, d2.Length); int cnt = 1; byte[] buf = new byte[cnt]; cnt = cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").Read(buf, bMini.Length, cnt); - Assert.IsTrue(cnt == 1); - Assert.IsTrue(buf[0] == 0x1A); + Assert.AreEqual(1, cnt); + Assert.AreEqual(0x1A, buf[0]); cnt = 1; cnt = cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").Read(buf, bMini.Length - 1, cnt); - Assert.IsTrue(cnt == 1); - Assert.IsTrue(buf[0] == 0xEE); + Assert.AreEqual(1, cnt); + Assert.AreEqual(0xEE, buf[0]); - try - { - cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt"); - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFItemNotFound); - } + Assert.ThrowsException(() => cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt")); cfTest.Close(); @@ -537,7 +475,7 @@ public void Test_FUNCTIONAL_BEHAVIOUR() cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); d2 = cf.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").GetData(); Assert.IsNotNull(d2); - Assert.IsTrue(d2.Length == bA.Length); + Assert.AreEqual(bA.Length, d2.Length); cf.Close(); @@ -565,26 +503,16 @@ public void Test_RETRIVE_ALL_NAMED_ENTRIES() var f = new CompoundFile("MultipleStorage4.cfs"); IList result = f.GetAllNamedEntries("MyStream"); - Assert.IsTrue(result.Count == 3); + Assert.AreEqual(3, result.Count); } [TestMethod] public void Test_CORRUPTED_CYCLIC_FAT_CHECK() { - CompoundFile f = null; - try - { - f = new CompoundFile("CyclicFAT.cfs"); - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFCorruptedFileException); - } - finally + Assert.ThrowsException(() => { - if (f != null) - f.Close(); - } + using CompoundFile cf = new("CyclicFAT.cfs"); + }); } [TestMethod] @@ -609,7 +537,7 @@ public void Test_DIFAT_CHECK() byte[] b2 = new byte[cnt]; cnt = f.RootStorage.GetStream("LargeStream").Read(b2, 20000000, cnt); f.Close(); - Assert.IsTrue(Helpers.CompareBuffer(b1, b2)); + CollectionAssert.AreEqual(b1, b2); } finally { @@ -645,8 +573,8 @@ public void Test_ADD_LARGE_NUMBER_OF_ITEMS() f = new CompoundFile("$ItemsLargeNumber.cfs"); CFStream cfs = f.RootStorage.GetStream("Stream" + (ITEM_NUMBER / 2).ToString()); - Assert.IsTrue(cfs != null, "Item is null"); - Assert.IsTrue(Helpers.CompareBuffer(cfs.GetData(), buffer), "Items are different"); + Assert.IsNotNull(cfs, "Item is null"); + CollectionAssert.AreEqual(buffer, cfs.GetData()); f.Close(); } catch (Exception ex) @@ -1007,14 +935,10 @@ public void Test_FIX_GH_38_B() [TestMethod] public void Test_FIX_GH_50() { - try - { - var f = new CompoundFile("64-67.numberOfMiniFATSectors.docx", CFSUpdateMode.Update, CFSConfiguration.Default); - } - catch (Exception e) + Assert.ThrowsException(() => { - Assert.IsTrue(e is CFFileFormatException); - } + using CompoundFile f = new("64-67.numberOfMiniFATSectors.docx", CFSUpdateMode.Update, CFSConfiguration.Default); + }); } [TestMethod] @@ -1250,51 +1174,31 @@ public void Test_FIX_BUG_96_CompoundFile_SaveOverwrite() File.Copy(filename, filename2); } - try + Assert.ThrowsException(() => { - CompoundFile compoundFile = new CompoundFile(filename2); + using CompoundFile compoundFile = new(filename2); var s = compoundFile.RootStorage.GetStorage(storageName).GetStream(streamName); s.Write(new byte[] { 0x0A, 0x0A }, 0); compoundFile.SaveAs(filename2); - compoundFile.Close(); - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFException, "Exception is " + ex.GetType()); - } + }); - try + Assert.ThrowsException(() => { string rootedPath = Path.GetFullPath(filename2); - CompoundFile compoundFile = new CompoundFile(rootedPath); + using CompoundFile compoundFile = new(rootedPath); var s = compoundFile.RootStorage.GetStorage(storageName).GetStream(streamName); s.Write(new byte[] { 0x0A, 0x0A }, 0); compoundFile.SaveAs(rootedPath); - compoundFile.Close(); - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFException, "Exception is " + ex.GetType()); - } - - FileStream fs = null; + }); - try + Assert.ThrowsException(() => { - CompoundFile compoundFile = new CompoundFile(filename2); - fs = new FileStream(filename2, FileMode.Open); + using CompoundFile compoundFile = new CompoundFile(filename2); + using FileStream fs = new(filename2, FileMode.Open); var s = compoundFile.RootStorage.GetStorage(storageName).GetStream(streamName); s.Write(new byte[] { 0x0A, 0x0A }, 0); compoundFile.Save(fs); - compoundFile.Close(); - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFException, "Exception is " + ex.GetType()); - } - - fs?.Close(); - fs?.Dispose(); + }); if (File.Exists(filename2)) File.Delete(filename2); diff --git a/sources/Test/OpenMcdf.Test/Helpers.cs b/sources/Test/OpenMcdf.Test/Helpers.cs index 2620c9b..bd1f275 100644 --- a/sources/Test/OpenMcdf.Test/Helpers.cs +++ b/sources/Test/OpenMcdf.Test/Helpers.cs @@ -32,31 +32,5 @@ public static byte[] GetBuffer(int count, byte c) FillBuffer(b, c); return b; } - - public static bool CompareBuffer(byte[] b, byte[] p) - { - bool res = CompareBuffer(b, p, b.Length); - return res && (b.Length == p.Length); - } - - public static bool CompareBuffer(byte[] b, byte[] p, int count) - { - if (b == null && p == null) - throw new Exception("Null buffers"); - - if (b == null && p != null) - return false; - - if (b != null && p == null) - return false; - - for (int i = 0; i < count; i++) - { - if (b[i] != p[i]) - return false; - } - - return true; - } } } diff --git a/sources/Test/OpenMcdf.Test/RBTreeTest.cs b/sources/Test/OpenMcdf.Test/RBTreeTest.cs index 6d9da48..2dee3b2 100644 --- a/sources/Test/OpenMcdf.Test/RBTreeTest.cs +++ b/sources/Test/OpenMcdf.Test/RBTreeTest.cs @@ -69,7 +69,7 @@ public void Test_RBTREE_INSERT() { rbTree.TryLookup(DirectoryEntry.Mock(i.ToString(), StgType.StgInvalid), out IRBNode c); Assert.IsTrue(c is IDirectoryEntry); - Assert.IsTrue(((IDirectoryEntry)c).Name == i.ToString()); + Assert.AreEqual(i.ToString(), ((IDirectoryEntry)c).Name); //Assert.IsTrue(c.IsStream); } } @@ -136,16 +136,16 @@ private static void VerifyProperty1(IRBNode n) private static void VerifyProperty2(IRBNode root) { - Assert.IsTrue(NodeColor(root) == Color.BLACK); + Assert.AreEqual(Color.BLACK, NodeColor(root)); } private static void VerifyProperty4(IRBNode n) { if (NodeColor(n) == Color.RED) { - Assert.IsTrue(NodeColor(n.Left) == Color.BLACK); - Assert.IsTrue(NodeColor(n.Right) == Color.BLACK); - Assert.IsTrue(NodeColor(n.Parent) == Color.BLACK); + Assert.AreEqual(Color.BLACK, NodeColor(n.Left)); + Assert.AreEqual(Color.BLACK, NodeColor(n.Right)); + Assert.AreEqual(Color.BLACK, NodeColor(n.Parent)); } if (n == null) return; @@ -173,7 +173,7 @@ private static int VerifyProperty5Helper(IRBNode n, int blackCount, int pathBlac } else { - Assert.IsTrue(blackCount == pathBlackCount); + Assert.AreEqual(blackCount, pathBlackCount); } return pathBlackCount; diff --git a/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs b/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs index 6761fc4..05d9bba 100644 --- a/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs +++ b/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs @@ -1,5 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; +using System.Collections.Generic; +using System.Linq; namespace OpenMcdf.Test { @@ -52,22 +54,18 @@ public class SectorCollectionTest [TestMethod()] public void CountTest() { - int count = 0; - SectorCollection target = new SectorCollection(); // TODO: Initialize to an appropriate value - int actual; - actual = target.Count; - Assert.IsTrue(actual == count); + Assert.AreEqual(0, target.Count); Sector s = new Sector(4096); target.Add(s); - Assert.IsTrue(target.Count == actual + 1); + Assert.AreEqual(1, target.Count); for (int i = 0; i < 5000; i++) target.Add(s); - Assert.IsTrue(target.Count == actual + 1 + 5000); + Assert.AreEqual(5001, target.Count); } /// @@ -89,25 +87,9 @@ public void ItemTest() actual = target[index]; Assert.AreEqual(expected, actual); - Assert.IsNotNull(actual); - Assert.IsTrue(actual.Id == expected.Id); - try - { - actual = target[count + 100]; - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFException); - } - - try - { - actual = target[-1]; - } - catch (Exception ex) - { - Assert.IsTrue(ex is CFException); - } + Assert.AreEqual(expected.Id, actual.Id); + Assert.ThrowsException(() => target[count + 100]); + Assert.ThrowsException(() => target[-1]); } /// @@ -119,11 +101,11 @@ public void SectorCollectionConstructorTest() SectorCollection target = new SectorCollection(); Assert.IsNotNull(target); - Assert.IsTrue(target.Count == 0); + Assert.AreEqual(0, target.Count); Sector s = new Sector(4096); target.Add(s); - Assert.IsTrue(target.Count == 1); + Assert.AreEqual(1, target.Count); } /// @@ -140,7 +122,7 @@ public void AddTest() Sector item = new Sector(4096); target.Add(item); - Assert.IsTrue(target.Count == 580); + Assert.AreEqual(580, target.Count); } /// @@ -157,15 +139,15 @@ public void GetEnumeratorTest() Sector item = new Sector(4096); target.Add(item); - Assert.IsTrue(target.Count == 580); + Assert.AreEqual(580, target.Count); - int cnt = 0; - foreach (Sector s in target) + int count = 0; + using IEnumerator enumerator = target.GetEnumerator(); + while (enumerator.MoveNext()) { - cnt++; + count++; } - - Assert.IsTrue(cnt == target.Count); + Assert.AreEqual(580, count); } } }