Skip to content

Commit

Permalink
SWEEP: Rename BeforeClass/AfterClass to OneTimeSetUp/OneTimeTearDown, #…
Browse files Browse the repository at this point in the history
…1016 (#1051)

* Rename BeforeClass to OneTimeSetUp, #1016

* Rename AfterClass to OneTimeTearDown, #1016

* Remove unnecessary comments; Fix docs in LuceneTestCase w/r/t setup/teardown
  • Loading branch information
paulirwin authored Nov 27, 2024
1 parent e9c865c commit abe75de
Show file tree
Hide file tree
Showing 103 changed files with 428 additions and 463 deletions.
80 changes: 40 additions & 40 deletions src/Lucene.Net.TestFramework/Index/BasePostingsFormatTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ private static SeedPostings GetSeedPostings(string term, long seed, bool withLiv
}

[NUnit.Framework.OneTimeSetUp]
public override void BeforeClass() // Renamed from CreatePostings to ensure the base class setup is called before this one
public override void OneTimeSetUp() // Renamed from CreatePostings to ensure the base class setup is called before this one
{
base.BeforeClass();
base.OneTimeSetUp();

totalPostings = 0;
totalPayloadBytes = 0;
Expand All @@ -375,7 +375,7 @@ public override void BeforeClass() // Renamed from CreatePostings to ensure the
continue;
}

fieldInfoArray[fieldUpto] = new FieldInfo(field, true, fieldUpto, false, false, true,
fieldInfoArray[fieldUpto] = new FieldInfo(field, true, fieldUpto, false, false, true,
IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS,
DocValuesType.NONE, DocValuesType.NUMERIC, null);
fieldUpto++;
Expand Down Expand Up @@ -472,13 +472,13 @@ public override void BeforeClass() // Renamed from CreatePostings to ensure the
}

[NUnit.Framework.OneTimeTearDown]
public override void AfterClass()
public override void OneTimeTearDown()
{
allTerms = null;
fieldInfos = null;
fields = null;
globalLiveDocs = null;
base.AfterClass();
base.OneTimeTearDown();
}

// TODO maybe instead of @BeforeClass just make a single test run: build postings & index & test it?
Expand Down Expand Up @@ -653,18 +653,18 @@ private class ThreadState
public DocsAndPositionsEnum ReuseDocsAndPositionsEnum { get; set; }
}

private void VerifyEnum(ThreadState threadState,
string field,
BytesRef term,
private void VerifyEnum(ThreadState threadState,
string field,
BytesRef term,
TermsEnum termsEnum,

// Maximum options (docs/freqs/positions/offsets) to test:
IndexOptions maxTestOptions,
IndexOptions maxIndexOptions,
ISet<Option> options,
IndexOptions maxTestOptions,

IndexOptions maxIndexOptions,
ISet<Option> options,
bool alwaysTestMax)

{
if (Verbose)
{
Expand Down Expand Up @@ -698,9 +698,9 @@ private void VerifyEnum(ThreadState threadState,
FieldInfo fieldInfo = currentFieldInfos.FieldInfo(field);

// NOTE: can be empty list if we are using liveDocs:
SeedPostings expected = GetSeedPostings(term.Utf8ToString(),
fields[field][term],
useLiveDocs,
SeedPostings expected = GetSeedPostings(term.Utf8ToString(),
fields[field][term],
useLiveDocs,
maxIndexOptions);
Assert.AreEqual(expected.DocFreq, termsEnum.DocFreq);

Expand All @@ -710,12 +710,12 @@ private void VerifyEnum(ThreadState threadState,
bool doCheckFreqs = allowFreqs && (alwaysTestMax || Random.Next(3) <= 2);

// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
bool allowPositions = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0 &&
bool allowPositions = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0 &&
IndexOptionsComparer.Default.Compare(maxTestOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
bool doCheckPositions = allowPositions && (alwaysTestMax || Random.Next(3) <= 2);

// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
bool allowOffsets = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >=0 &&
bool allowOffsets = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >=0 &&
IndexOptionsComparer.Default.Compare(maxTestOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
bool doCheckOffsets = allowOffsets && (alwaysTestMax || Random.Next(3) <= 2);

Expand Down Expand Up @@ -1099,9 +1099,9 @@ public override void Run()
}
}

private void TestTerms(Fields fieldsSource, ISet<Option> options,
IndexOptions maxTestOptions,
IndexOptions maxIndexOptions,
private void TestTerms(Fields fieldsSource, ISet<Option> options,
IndexOptions maxTestOptions,
IndexOptions maxIndexOptions,
bool alwaysTestMax)
{
if (options.Contains(Option.THREADS))
Expand All @@ -1124,9 +1124,9 @@ private void TestTerms(Fields fieldsSource, ISet<Option> options,
}
}

private void TestTermsOneThread(Fields fieldsSource, ISet<Option> options,
IndexOptions maxTestOptions,
IndexOptions maxIndexOptions,
private void TestTermsOneThread(Fields fieldsSource, ISet<Option> options,
IndexOptions maxTestOptions,
IndexOptions maxIndexOptions,
bool alwaysTestMax)
{
ThreadState threadState = new ThreadState();
Expand Down Expand Up @@ -1189,13 +1189,13 @@ private void TestTermsOneThread(Fields fieldsSource, ISet<Option> options,
savedTermState = true;
}

VerifyEnum(threadState,
fieldAndTerm.Field,
fieldAndTerm.Term,
termsEnum,
maxTestOptions,
maxIndexOptions,
options,
VerifyEnum(threadState,
fieldAndTerm.Field,
fieldAndTerm.Term,
termsEnum,
maxTestOptions,
maxIndexOptions,
options,
alwaysTestMax);

// Sometimes save term state after pulling the enum:
Expand All @@ -1217,13 +1217,13 @@ private void TestTermsOneThread(Fields fieldsSource, ISet<Option> options,
Console.WriteLine("TEST: try enum again on same term");
}

VerifyEnum(threadState,
fieldAndTerm.Field,
fieldAndTerm.Term,
termsEnum,
maxTestOptions,
maxIndexOptions,
options,
VerifyEnum(threadState,
fieldAndTerm.Field,
fieldAndTerm.Term,
termsEnum,
maxTestOptions,
maxIndexOptions,
options,
alwaysTestMax);
}
}
Expand Down Expand Up @@ -1348,7 +1348,7 @@ public virtual void TestRandom()

protected override void AddRandomFields(Document doc)
{

foreach (IndexOptions opts in Enum.GetValues(typeof(IndexOptions)))
{
// LUCENENET: Skip our NONE option
Expand All @@ -1372,4 +1372,4 @@ protected override void AddRandomFields(Document doc)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,12 @@ protected virtual void AssertEquals(RandomDocument doc, Fields fields)
private readonly DisposableThreadLocal<DocsAndPositionsEnum> docsAndPositionsEnum = new DisposableThreadLocal<DocsAndPositionsEnum>();

// LUCENENET specific - cleanup DisposableThreadLocal instances after running tests
public override void AfterClass()
public override void OneTimeTearDown()
{
termsEnum.Dispose();
docsEnum.Dispose();
docsAndPositionsEnum.Dispose();
base.AfterClass();
base.OneTimeTearDown();
}

protected virtual void AssertEquals(RandomTokenStream tk, FieldType ft, Terms terms)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public abstract class SearchEquivalenceTestBase : LuceneTestCase
protected static string m_stopword; // we always pick a character as a stopword

[NUnit.Framework.OneTimeSetUp]
public override void BeforeClass()
public override void OneTimeSetUp()
{
base.BeforeClass();
base.OneTimeSetUp();


Random random = Random;
Expand Down Expand Up @@ -94,7 +94,7 @@ public override void BeforeClass()
}

[NUnit.Framework.OneTimeTearDown]
public override void AfterClass()
public override void OneTimeTearDown()
{
m_reader.Dispose();
m_directory.Dispose();
Expand All @@ -103,7 +103,7 @@ public override void AfterClass()
m_directory = null;
m_analyzer = null;
m_s1 = m_s2 = null;
base.AfterClass();
base.OneTimeTearDown();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public abstract class ConfigurationSettingsTestCase : LuceneTestCase

protected abstract IConfiguration LoadConfiguration();

public override void BeforeClass()
public override void OneTimeSetUp()
{
base.BeforeClass();
base.OneTimeSetUp();
var configuration = LoadConfiguration();
// Set up mocks for ConfigurationSettings and SystemProperties
ConfigurationSettings = new ConfigurationSettingsImpl(configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ internal void DoTestFrameworkTearDown()
/// Access to the current <see cref="System.Random"/> instance. It is safe to use
/// this method from multiple threads, etc., but it should be called while within a runner's
/// scope (so no static initializers). The returned <see cref="System.Random"/> instance will be
/// <b>different</b> when this method is called inside a <see cref="LuceneTestCase.BeforeClass()"/> hook (static
/// <b>different</b> when this method is called inside a <see cref="LuceneTestCase.OneTimeSetUp()"/> hook (static
/// suite scope) and within <see cref="OneTimeSetUpAttribute"/>/ <see cref="OneTimeTearDownAttribute"/> hooks or test methods.
///
/// <para/>The returned instance must not be shared with other threads or cross a single scope's
Expand Down
24 changes: 15 additions & 9 deletions src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ namespace Lucene.Net.Util
/// <h3>Class and instance setup.</h3>
///
/// <para>
/// The preferred way to specify class (suite-level) setup/cleanup is to use
/// static methods annotated with <see cref="OneTimeSetUp"/> and <see cref="OneTimeTearDown"/>. Any
/// The preferred way to specify class (suite-level) setup/cleanup is to override
/// <see cref="OneTimeSetUp"/> and <see cref="OneTimeTearDown"/>. Be sure
/// to call <c>base.OneTimeSetUp()</c> BEFORE you initialize your class and
/// call <c>base.OneTimeTearDown()</c> AFTER you clean up your class. NUnit
/// will find the <see cref="NUnit.Framework.OneTimeSetUpAttribute"/> and
/// <see cref="NUnit.Framework.OneTimeTearDownAttribute"/> of the base class,
/// so using them on the <see cref="OneTimeSetUp"/> and
/// <see cref="OneTimeTearDown"/> method overrides is not strictly required. Any
/// code in these methods is executed within the test framework's control and
/// ensure proper setup has been made. <b>Try not to use static initializers
/// (including complex readonly field initializers).</b> Static initializers are
Expand Down Expand Up @@ -977,12 +983,12 @@ public virtual void TearDown()
/// Sets up dependency injection of codec factories for running the test class,
/// and also picks random defaults for culture, time zone, similarity, and default codec.
/// <para/>
/// If you override this method, be sure to call <c>base.BeforeClass()</c> BEFORE setting
/// If you override this method, be sure to call <c>base.OneTimeSetUp()</c> BEFORE setting
/// up your test fixture.
/// </summary>
// LUCENENET specific method for setting up dependency injection of test classes.
[OneTimeSetUp]
public virtual void BeforeClass()
public virtual void OneTimeSetUp()
{
try
{
Expand All @@ -995,19 +1001,19 @@ public virtual void BeforeClass()
catch (Exception ex)
{
// Write the stack trace so we have something to go on if an error occurs here.
throw new Exception($"An exception occurred during BeforeClass:\n{ex}", ex);
throw new Exception($"An exception occurred during OneTimeSetUp:\n{ex}", ex);
}
}

/// <summary>
/// Tears down random defaults and cleans up temporary files.
/// <para/>
/// If you override this method, be sure to call <c>base.AfterClass()</c> AFTER
/// If you override this method, be sure to call <c>base.OneTimeTearDown()</c> AFTER
/// tearing down your test fixture.
/// </summary>
// LUCENENET specific method for setting up dependency injection of test classes.
[OneTimeTearDown]
public virtual void AfterClass()
public virtual void OneTimeTearDown()
{
try
{
Expand All @@ -1017,7 +1023,7 @@ public virtual void AfterClass()
catch (Exception ex)
{
// Write the stack trace so we have something to go on if an error occurs here.
throw new Exception($"An exception occurred during AfterClass:\n{ex}", ex);
throw new Exception($"An exception occurred during OneTimeTearDown:\n{ex}", ex);
}
}

Expand All @@ -1029,7 +1035,7 @@ public virtual void AfterClass()
/// Access to the current <see cref="System.Random"/> instance. It is safe to use
/// this method from multiple threads, etc., but it should be called while within a runner's
/// scope (so no static initializers). The returned <see cref="System.Random"/> instance will be
/// <b>different</b> when this method is called inside a <see cref="BeforeClass()"/> hook (static
/// <b>different</b> when this method is called inside a <see cref="OneTimeSetUp()"/> hook (static
/// suite scope) and within <see cref="Before"/>/ <see cref="After"/> hooks or test methods.
///
/// <para/>The returned instance must not be shared with other threads or cross a single scope's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ public bool Apply(object[] args)
}

[OneTimeSetUp]
public override void BeforeClass()
public override void OneTimeSetUp()
{
base.BeforeClass();
base.OneTimeSetUp();

IEnumerable<Type> analysisClasses = typeof(StandardAnalyzer).Assembly.GetTypes()
.Where(c => {
var typeInfo = c;

return !typeInfo.IsAbstract && typeInfo.IsPublic && !typeInfo.IsInterface
return !typeInfo.IsAbstract && typeInfo.IsPublic && !typeInfo.IsInterface
&& typeInfo.IsClass && (typeInfo.GetCustomAttribute<ObsoleteAttribute>() is null)
&& (typeInfo.IsSubclassOf(typeof(Tokenizer)) || typeInfo.IsSubclassOf(typeof(TokenFilter)) || typeInfo.IsSubclassOf(typeof(CharFilter)));
})
Expand All @@ -225,19 +225,19 @@ public override void BeforeClass()

if (typeInfo.IsSubclassOf(typeof(Tokenizer)))
{
assertTrue(ctor.ToString() + " has unsupported parameter types",
assertTrue(ctor.ToString() + " has unsupported parameter types",
allowedTokenizerArgs.containsAll(ctor.GetParameters().Select(p => p.ParameterType).ToArray()));
tokenizers.Add(ctor);
}
else if (typeInfo.IsSubclassOf(typeof(TokenFilter)))
{
assertTrue(ctor.ToString() + " has unsupported parameter types",
assertTrue(ctor.ToString() + " has unsupported parameter types",
allowedTokenFilterArgs.containsAll(ctor.GetParameters().Select(p => p.ParameterType).ToArray()));
tokenfilters.Add(ctor);
}
else if (typeInfo.IsSubclassOf(typeof(CharFilter)))
{
assertTrue(ctor.ToString() + " has unsupported parameter types",
assertTrue(ctor.ToString() + " has unsupported parameter types",
allowedCharFilterArgs.containsAll(ctor.GetParameters().Select(p => p.ParameterType).ToArray()));
charfilters.Add(ctor);
}
Expand All @@ -259,15 +259,15 @@ public override void BeforeClass()
Console.WriteLine("charfilters = " + charfilters);
}
}

[OneTimeTearDown]
public override void AfterClass()
public override void OneTimeTearDown()
{
tokenizers = null;
tokenfilters = null;
charfilters = null;

base.AfterClass();
base.OneTimeTearDown();
}


Expand Down Expand Up @@ -1113,7 +1113,7 @@ public override int Read()
return m_input.Read();
}

// LUCENENET: TextReader dosn't support this overload
// LUCENENET: TextReader dosn't support this overload
//public int read(char[] cbuf)
//{
// readSomething = true;
Expand Down Expand Up @@ -1214,4 +1214,4 @@ public void TestRandomChainsWithLargeStrings()
}
}
}
}
}
Loading

0 comments on commit abe75de

Please sign in to comment.