Fork me on GitHub
  • API

    Show / Hide Table of Contents

    Class LuceneTestFrameworkInitializer

    LuceneTestFrameworkInitializer may be subclassed in order to use standard dependency injection techniques for classes under test. A subclass of LuceneTestFrameworkInitializer will be executed automatically.

    Only one subclass per assembly is allowed, and by convention these subclasses are usually named "Startup".

    The following abstract factories can be overridden for testing purposes:

    AbstractionFactory Method NameDefault
    Lucene.Net.Codecs.ICodecFactoryCodecFactoryTestCodecFactory
    Lucene.Net.Codecs.IDocValuesFormatFactoryDocValuesFormatFactoryTestDocValuesFormatFactory
    Lucene.Net.Codecs.IPostingsFormatFactoryPostingsFormatFactoryTestPostingsFormatFactory
    Lucene.Net.Configuration.IConfigurationFactoryConfigurationFactoryLucene.Net.Configuration.TestConfigurationFactory

    Methods are executed one time per assembly, and are executed in the following order:

    MethodDescription
    Initialize()Used to set the factories in the above table. In general, dependency injection for the test assembly is setup in this method. No randomized context is available.
    TestFrameworkSetUp()Used to set assembly-level test setup. Executed before all tests and class-level setup in the assembly. Repeatable randomized content can be generated using the Random property.
    TestFrameworkTearDown()Used to tear down assembly-level test setup. Executed after all tests and class-level tear down in the assembly. Repeatable randomized content can be generated using the Random property.

    Example:

    using RandomizedTesting.Generators;
    
    public class Startup : LuceneTestFrameworkInitializer
    {
        // Run first
        protected override void Initialize()
        {
            // Inject a custom configuration factory
            ConfigurationFactory = new MyConfigurationFactory();
    
            // Do any additional dependency injection setup here
        }
    
        // Run before all tests in the assembly and before any class-level setup
        protected overide void TestFrameworkSetUp()
        {
            // Get the random instance for the current context
            var random = Random;
    
            // Generate random content
            string content = random.NextSimpleString();
    
            // Use randomization from LuceneTestCase
            int numberOfDocuments = LuceneTestCase.AtLeast(30);
        }
    
        // Run after all tests in the assembly and after any class-level setup
        protected override void TestFrameworkTearDown()
        {
            // Tear down everything here
        }
    }
    Inheritance
    System.Object
    LuceneTestFrameworkInitializer
    Inherited Members
    System.Object.Equals(System.Object)
    System.Object.Equals(System.Object, System.Object)
    System.Object.GetHashCode()
    System.Object.GetType()
    System.Object.MemberwiseClone()
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.ToString()
    Namespace: Lucene.Net.Util
    Assembly: Lucene.Net.TestFramework.dll
    Syntax
    public abstract class LuceneTestFrameworkInitializer

    Constructors

    | Improve this Doc View Source

    LuceneTestFrameworkInitializer()

    Declaration
    protected LuceneTestFrameworkInitializer()

    Properties

    | Improve this Doc View Source

    CodecFactory

    The Lucene.Net.Codecs.ICodecFactory implementation to use to load codecs during testing.

    Declaration
    protected ICodecFactory CodecFactory { get; set; }
    Property Value
    Type Description
    Lucene.Net.Codecs.ICodecFactory
    | Improve this Doc View Source

    ConfigurationFactory

    The Lucene.Net.Configuration.IConfigurationFactory implementation to use to load configuration settings during testing. See: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/.

    Declaration
    [CLSCompliant(false)]
    protected IConfigurationFactory ConfigurationFactory { get; set; }
    Property Value
    Type Description
    Lucene.Net.Configuration.IConfigurationFactory
    | Improve this Doc View Source

    DocValuesFormatFactory

    The Lucene.Net.Codecs.IDocValuesFormatFactory implementation to use to load doc values during testing.

    Declaration
    protected IDocValuesFormatFactory DocValuesFormatFactory { get; set; }
    Property Value
    Type Description
    Lucene.Net.Codecs.IDocValuesFormatFactory
    | Improve this Doc View Source

    PostingsFormatFactory

    The Lucene.Net.Codecs.IPostingsFormatFactory implementation to use to load postings formats during testing.

    Declaration
    protected IPostingsFormatFactory PostingsFormatFactory { get; set; }
    Property Value
    Type Description
    Lucene.Net.Codecs.IPostingsFormatFactory
    | Improve this Doc View Source

    Random

    Access to the current 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 System.Random instance will be different when this method is called inside a BeforeClass() hook (static suite scope) and within NUnit.Framework.OneTimeSetUpAttribute/ NUnit.Framework.OneTimeTearDownAttribute hooks or test methods.

    The returned instance must not be shared with other threads or cross a single scope's boundary. For example, a System.Random acquired within a test method shouldn't be reused for another test case.

    There is an overhead connected with getting the System.Random for a particular context and thread. It is better to cache the System.Random locally if tight loops with multiple invocations are present or create a derivative local System.Random for millions of calls like this:

    Random random = new J2N.Randomizer(Random.NextInt64());
    // tight loop with many invocations.
    Declaration
    protected Random Random { get; }
    Property Value
    Type Description
    System.Random

    Methods

    | Improve this Doc View Source

    Initialize()

    Overridden in a derived class, provides a way to set CodecFactory, PostingsFormatFactory, DocValuesFormatFactory and ConfigurationFactory as well as other dependency injection setup.

    This method is called only one time per assembly.

    Using the Random property here will result in a test failure. To build randomized global setup, use TestFrameworkSetUp() instead.

    Declaration
    protected virtual void Initialize()
    | Improve this Doc View Source

    TestFrameworkSetUp()

    Overridden in a derived class, can be used to perform one-time initialization of the test framework setup.

    Repeatable random setup can be done by calling Random or by using methods of LuceneTestCase.

    It is not possible to set CodecFactory, PostingsFormatFactory, DocValuesFormatFactory and ConfigurationFactory from this method. Those must be called in Initialize().

    Declaration
    protected virtual void TestFrameworkSetUp()
    | Improve this Doc View Source

    TestFrameworkTearDown()

    Overridden in a derived class, can be used to perform one-time tear down of the test framework setup (whether the setup was done in Initialize() or TestFrameworkSetUp() doesn't matter).

    Repeatable random setup can be done by calling Random or by using methods of LuceneTestCase.

    Declaration
    protected virtual void TestFrameworkTearDown()
    • Improve this Doc
    • View Source
    Back to top Copyright © 2022 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.