public async Task BeginSplittingSession_TextIteratorMustBeCleanedUpInCaseOfException() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"111", ReOptions.None)); ta.OpenIterator(0, TextAccessDirection.Forward).Returns(it); it.PositionToCharIndex(0).Returns(0); it.CurrentBuffer.Returns(_ => throw new System.Security.SecurityException()); try { await target.BeginSplittingSession(new Range(0, 100), 0, MessagesParserDirection.Forward); Assert.IsTrue(false, "We must never get here because of an exception in prev call"); } catch (System.Security.SecurityException) { } it.Received(1).Dispose(); Assert.IsTrue(target.CurrentMessageIsEmpty); }
public void BeginSplittingSession_WithStartPositionThatDoesntGetMappedToCharacterByTextAccess() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"111", ReOptions.None)); ta.OpenIterator(90, TextAccessDirection.Forward).Returns(it); it.PositionToCharIndex(90).Returns(_ => throw new ArgumentOutOfRangeException()); target.BeginSplittingSession(new Range(0, 100), 90, MessagesParserDirection.Forward); it.Received(1).Dispose(); Assert.IsTrue(target.CurrentMessageIsEmpty); TextMessageCapture capt = new TextMessageCapture(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.IsTrue(target.CurrentMessageIsEmpty); target.EndSplittingSession(); }
public void GetCurrentMessageAndMoveToNextOne_MainScenario_Backward() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None)); ta.OpenIterator(100, TextAccessDirection.Backward).Returns(it); it.PositionToCharIndex(100).Returns(29); it.CurrentBuffer.Returns( "123456 abc 283147948 abc 3498"); // | | | | | | // 0 7 10 21 24 29 - char idx // 50 61 67 85 87 100 - position it.CharIndexToPosition(21).Returns((long)85); target.BeginSplittingSession(new Range(0, 100), 100, MessagesParserDirection.Backward); Assert.IsFalse(target.CurrentMessageIsEmpty); it.CharIndexToPosition(7).Returns((long)61); it.CharIndexToPosition(29).Returns((long)100); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 3498", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(85L, capt.BeginPosition); Assert.AreEqual(100L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(8).Returns(_ => { it.CurrentBuffer.Returns( "11 abc 123456 abc 283147948 "); // | | | | | | // 0 3 6 14 17 27 - char idx // 20 33 50 61 67 85 - positions it.CharIndexToPosition(3).Returns((long)33); return(true); }); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 283147948 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(61L, capt.BeginPosition); Assert.AreEqual(85L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(14).Returns(_ => { it.CurrentBuffer.Returns( "11 abc 123456 "); // | | | | // 0 3 6 13 - char idx // 20 33 61 - pos return(true); }); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 123456 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(33L, capt.BeginPosition); Assert.AreEqual(61L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsTrue(target.CurrentMessageIsEmpty); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.IsTrue(target.CurrentMessageIsEmpty); target.EndSplittingSession(); it.Received(1).Dispose(); }
public void GetCurrentMessageAndMoveToNextOne_MainScenario_Forward() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None)); ta.OpenIterator(0, TextAccessDirection.Forward).Returns(it); it.PositionToCharIndex(0).Returns(0); it.CurrentBuffer.Returns( "123456 abc 283147948 abc 3498"); // | | | | | | // 0 7 10 21 24 28 - char idx // 0 8 16 45 56 67 - positions it.CharIndexToPosition(7).Returns((long)8); target.BeginSplittingSession(new Range(0, 100), 0, MessagesParserDirection.Forward); Assert.IsFalse(target.CurrentMessageIsEmpty); it.CharIndexToPosition(21).Returns((long)45); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 283147948 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(8L, capt.BeginPosition); Assert.AreEqual(45L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(24).Returns(_ => { it.CurrentBuffer.Returns( " 3498 abc 2626277"); // | | | // 0 6 9 - char idx // 56 72 81 - position it.CharIndexToPosition(6).Returns((long)72); return(true); }); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 3498 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(45L, capt.BeginPosition); Assert.AreEqual(72L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(9).Returns(_ => { it.CurrentBuffer.Returns( " 2626277"); // | | // 0 8 - char idx // 81 90 - position it.CharIndexToPosition(8).Returns((long)90); return(true); }); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 2626277", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(72L, capt.BeginPosition); Assert.AreEqual(90L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsTrue(target.CurrentMessageIsEmpty); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.IsTrue(target.CurrentMessageIsEmpty); target.EndSplittingSession(); it.Received(1).Dispose(); }