Skip to content

Commit

Permalink
SWEEP: Reviewed all catch blocks where Lucene uses AssertionError and…
Browse files Browse the repository at this point in the history
… converted them to use our IsAssertionError() extension method (see apache#446).
  • Loading branch information
NightOwl888 committed Apr 14, 2021
1 parent bd56fcb commit 7a9f7fc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ internal static void CheckResetException(Analyzer a, string input)
{
//ok
}
catch (AssertionError expected) // LUCENENET: Actual AssertionError type is Lucene.Net.Diagnostics.AssertionException
catch (Exception expected) when (expected.IsAssertionError())
{
// ok: MockTokenizer
Assert.IsTrue(expected.Message != null && expected.Message.Contains("wrong state"), expected.Message);
Expand Down Expand Up @@ -1145,8 +1145,7 @@ private static void CheckAnalysisConsistency(Random random, Analyzer a, bool use
{
ts.End();
}
// LUCENENET: Actual AssertionError type is Lucene.Net.Diagnostics.AssertionException
catch (AssertionError ae) when (ae.Message.Contains("End() called before IncrementToken() returned false!"))
catch (Exception ae) when (ae.IsAssertionError() && ae.Message.Contains("End() called before IncrementToken() returned false!"))
{
// Catch & ignore MockTokenizer's
// anger...
Expand Down Expand Up @@ -1178,8 +1177,7 @@ private static void CheckAnalysisConsistency(Random random, Analyzer a, bool use
{
ts.End();
}
// LUCENENET: Actual AssertionError type is Lucene.Net.Diagnostics.AssertionException
catch (AssertionError ae) when (ae.Message.Contains("End() called before IncrementToken() returned false!"))
catch (Exception ae) when (ae.IsAssertionError() && ae.Message.Contains("End() called before IncrementToken() returned false!"))
{
// Catch & ignore MockTokenizer's
// anger...
Expand Down
35 changes: 13 additions & 22 deletions src/Lucene.Net.TestFramework/Index/BaseTermVectorsFormatTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,28 +631,19 @@ protected virtual void AssertEquals(RandomTokenStream tk, FieldType ft, Terms te
Assert.IsTrue(foundPayload);
}
}

// LUCENENET specific - In Lucene, there were assertions set up inside TVReaders which throw AssertionError
// (provided assertions are enabled), which in turn signaled this class to skip the check by catching AssertionError.
// In .NET, assertions are not included in the release and cannot be enabled, so there is nothing to catch.
// We have to explicitly exclude the types that rely on this behavior from the check. Otherwise, they would fall
// through to Assert.Fail().
//
// We also have a fake AssertionException for testing mocks. We cannot throw InvalidOperationException in those
// cases because that exception is expected in other contexts.
Assert.ThrowsAnyOf<InvalidOperationException, AssertionError>(() => docsAndPositionsEnum.NextPosition());

// try
// {
// docsAndPositionsEnum.NextPosition();
// Assert.Fail();
// }
//#pragma warning disable 168
// catch (Exception e) when (e.IsException())
//#pragma warning restore 168
// {
// // ok
// }
try
{
docsAndPositionsEnum.NextPosition();
Assert.Fail();
}
catch (Exception e) when (e.IsException())
{
// ok
}
catch (Exception e) when (e.IsAssertionError())
{
// ok
}
}
Assert.AreEqual(DocsEnum.NO_MORE_DOCS, docsAndPositionsEnum.NextDoc());
}
Expand Down
22 changes: 9 additions & 13 deletions src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -778,10 +778,10 @@ public override void Run()
{
// ok
}
catch (NullReferenceException)
{
// ok
}
//catch (NullReferenceException) // LUCENENET specific - NullReferenceException must be allowed to propagate so we can defensively avoid it in .NET
//{
// // ok
//}
finally
{
commitLock.Unlock();
Expand All @@ -801,15 +801,11 @@ public override void Run()
{
// ok
}
catch (NullReferenceException)
{
// ok
}
catch (InvalidOperationException) // LUCENENET TODO: This is being caught here because it was used in some places instead of AssertionError, but should remove this and possibly subclass InvalidOperationExcption with AssertionException instead
{
// ok
}
catch (AssertionException)
//catch (NullReferenceException) // LUCENENET specific - NullReferenceException must be allowed to propagate so we can defensively avoid it in .NET
//{
// // ok
//}
catch (Exception ae) when (ae.IsAssertionError())
{
// ok
}
Expand Down

0 comments on commit 7a9f7fc

Please sign in to comment.