Lucene.Net.TestFramework.Analysis.BaseTokenStreamTestCase: Made the CheckAnalysisConsistency() method more robust by ensuring the Reset() and End() methods are called on the token stream if an exception occurs
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/8e300954 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/8e300954 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/8e300954 Branch: refs/heads/api-work Commit: 8e300954bd2b0d9faa77e0a16e86199cb7e53d1e Parents: b6b76c7 Author: Shad Storhaug <[email protected]> Authored: Mon Mar 20 02:06:30 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Mon Mar 20 02:06:30 2017 +0700 ---------------------------------------------------------------------- .../Analysis/BaseTokenStreamTestCase.cs | 88 ++++++++++++-------- 1 file changed, 54 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8e300954/src/Lucene.Net.TestFramework/Analysis/BaseTokenStreamTestCase.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Analysis/BaseTokenStreamTestCase.cs b/src/Lucene.Net.TestFramework/Analysis/BaseTokenStreamTestCase.cs index ca2d21d..502b322 100644 --- a/src/Lucene.Net.TestFramework/Analysis/BaseTokenStreamTestCase.cs +++ b/src/Lucene.Net.TestFramework/Analysis/BaseTokenStreamTestCase.cs @@ -929,46 +929,66 @@ namespace Lucene.Net.Analysis TokenStream ts; using (ts = a.GetTokenStream("dummy", useCharFilter ? (TextReader) new MockCharFilter(reader, remainder) : reader)) { - termAtt = ts.HasAttribute<ICharTermAttribute>() - ? ts.GetAttribute<ICharTermAttribute>() - : null; - offsetAtt = ts.HasAttribute<IOffsetAttribute>() - ? ts.GetAttribute<IOffsetAttribute>() - : null; - posIncAtt = ts.HasAttribute<IPositionIncrementAttribute>() - ? ts.GetAttribute<IPositionIncrementAttribute>() - : null; - posLengthAtt = ts.HasAttribute<IPositionLengthAttribute>() - ? ts.GetAttribute<IPositionLengthAttribute>() - : null; - typeAtt = ts.HasAttribute<ITypeAttribute>() ? ts.GetAttribute<ITypeAttribute>() : null; - - ts.Reset(); - - // First pass: save away "correct" tokens - while (ts.IncrementToken()) + bool isReset = false; + try { - Assert.IsNotNull(termAtt, "has no CharTermAttribute"); - tokens.Add(termAtt.ToString()); - if (typeAtt != null) - { - types.Add(typeAtt.Type); - } - if (posIncAtt != null) + termAtt = ts.HasAttribute<ICharTermAttribute>() + ? ts.GetAttribute<ICharTermAttribute>() + : null; + offsetAtt = ts.HasAttribute<IOffsetAttribute>() + ? ts.GetAttribute<IOffsetAttribute>() + : null; + posIncAtt = ts.HasAttribute<IPositionIncrementAttribute>() + ? ts.GetAttribute<IPositionIncrementAttribute>() + : null; + posLengthAtt = ts.HasAttribute<IPositionLengthAttribute>() + ? ts.GetAttribute<IPositionLengthAttribute>() + : null; + typeAtt = ts.HasAttribute<ITypeAttribute>() ? ts.GetAttribute<ITypeAttribute>() : null; + + ts.Reset(); + isReset = true; + + // First pass: save away "correct" tokens + while (ts.IncrementToken()) { - positions.Add(posIncAtt.PositionIncrement); - } - if (posLengthAtt != null) - { - positionLengths.Add(posLengthAtt.PositionLength); + Assert.IsNotNull(termAtt, "has no CharTermAttribute"); + tokens.Add(termAtt.ToString()); + if (typeAtt != null) + { + types.Add(typeAtt.Type); + } + if (posIncAtt != null) + { + positions.Add(posIncAtt.PositionIncrement); + } + if (posLengthAtt != null) + { + positionLengths.Add(posLengthAtt.PositionLength); + } + if (offsetAtt != null) + { + startOffsets.Add(offsetAtt.StartOffset); + endOffsets.Add(offsetAtt.EndOffset); + } } - if (offsetAtt != null) + ts.End(); + } + finally + { + if (!isReset) { - startOffsets.Add(offsetAtt.StartOffset); - endOffsets.Add(offsetAtt.EndOffset); + try + { + ts.Reset(); + } + catch (Exception ex) + { + // ignore + } } + ts.End(); } - ts.End(); } // verify reusing is "reproducable" and also get the normal tokenstream sanity checks
