Exemple #1
0
        private void DoTestCharFilter(string charfilter)
        {
            var factoryClazz          = CharFilterFactory.LookupClass(charfilter);
            CharFilterFactory factory = (CharFilterFactory)Initialize(factoryClazz);

            if (factory != null)
            {
                // we managed to fully create an instance. check a few more things:

                // if it implements MultiTermAware, sanity check its impl
                if (factory is IMultiTermAwareComponent multiTermAwareComponent)
                {
                    AbstractAnalysisFactory mtc = multiTermAwareComponent.GetMultiTermComponent();
                    assertNotNull(mtc);
                    // its not ok to return a tokenizer or tokenfilter here, this makes no sense
                    assertTrue(mtc is CharFilterFactory);
                }

                // beast it just a little, it shouldnt throw exceptions:
                // (it should have thrown them in initialize)
                CheckRandomData(Random, new FactoryAnalyzer(assertingTokenizer, null, factory), 100, 20, false, false);
            }
        }
Exemple #2
0
        /// <summary>
        /// This method looks up a class with its fully qualified name (FQN), or a short-name
        /// class-simplename, or with a package suffix, assuming "Lucene.Net.Analysis."
        /// as the namespace prefix (e.g. "standard.ClassicTokenizerFactory" ->
        /// "Lucene.Net.Analysis.Standard.ClassicTokenizerFactory").
        /// </summary>
        /// <remarks>
        /// If <paramref name="className"/> contains a period, the class is first looked up as-is, assuming that it
        /// is an FQN.  If this fails, lookup is retried after prepending the Lucene analysis
        /// package prefix to the class name.
        /// <para/>
        /// If <paramref name="className"/> does not contain a period, the analysis SPI *Factory.LookupClass()
        /// methods are used to find the class.
        /// </remarks>
        /// <param name="className">The namespace qualified name or the short name of the class.</param>
        /// <param name="expectedType">The superclass <paramref name="className"/> is expected to extend. </param>
        /// <returns>The loaded type.</returns>
        /// <exception cref="TypeLoadException">If lookup fails.</exception>
        public virtual Type LookupAnalysisClass(string className, Type expectedType)
        {
            if (className.Contains("."))
            {
                // First, try className == FQN
                Type result = Type.GetType(className);
                if (result is null)
                {
                    // Second, retry lookup after prepending the Lucene analysis package prefix
                    result = Type.GetType(LUCENE_ANALYSIS_PACKAGE_PREFIX + className);

                    if (result is null)
                    {
                        throw ClassNotFoundException.Create("Can't find class '" + className
                                                            + "' or '" + LUCENE_ANALYSIS_PACKAGE_PREFIX + className + "'");
                    }
                }
                return(result);
            }
            // No dot - use analysis SPI lookup
            string analysisComponentName = ANALYSIS_COMPONENT_SUFFIX_PATTERN.Replace(className, "", 1);

            if (typeof(CharFilterFactory).IsAssignableFrom(expectedType))
            {
                return(CharFilterFactory.LookupClass(analysisComponentName));
            }
            else if (typeof(TokenizerFactory).IsAssignableFrom(expectedType))
            {
                return(TokenizerFactory.LookupClass(analysisComponentName));
            }
            else if (typeof(TokenFilterFactory).IsAssignableFrom(expectedType))
            {
                return(TokenFilterFactory.LookupClass(analysisComponentName));
            }

            throw ClassNotFoundException.Create("Can't find class '" + className + "'");
        }
Exemple #3
0
        public virtual void Test()
        {
            IList <Type> analysisClasses = typeof(StandardAnalyzer).Assembly.GetTypes()
                                           .Where(c =>
            {
                var typeInfo = c;

                return(!typeInfo.IsAbstract && typeInfo.IsPublic && !typeInfo.IsInterface && typeInfo.IsClass && (typeInfo.GetCustomAttribute <ObsoleteAttribute>() == null) &&
                       !testComponents.Contains(c) && !crazyComponents.Contains(c) && !oddlyNamedComponents.Contains(c) && !deprecatedDuplicatedComponents.Contains(c) &&
                       (typeInfo.IsSubclassOf(typeof(Tokenizer)) || typeInfo.IsSubclassOf(typeof(TokenFilter)) || typeInfo.IsSubclassOf(typeof(CharFilter))));
            })
                                           .ToList();


            foreach (Type c in analysisClasses)
            {
                IDictionary <string, string> args = new Dictionary <string, string>();
                args["luceneMatchVersion"] = TEST_VERSION_CURRENT.ToString();

                if (c.IsSubclassOf(typeof(Tokenizer)))
                {
                    string clazzName = c.Name;
                    assertTrue(clazzName.EndsWith("Tokenizer", StringComparison.Ordinal));
                    string simpleName = clazzName.Substring(0, clazzName.Length - 9);
                    assertNotNull(TokenizerFactory.LookupClass(simpleName));
                    TokenizerFactory instance = null;
                    try
                    {
                        instance = TokenizerFactory.ForName(simpleName, args);
                        assertNotNull(instance);
                        if (instance is IResourceLoaderAware resourceLoaderAware)
                        {
                            resourceLoaderAware.Inform(loader);
                        }
                        assertSame(c, instance.Create(new StringReader("")).GetType());
                    }
                    catch (Exception e) when(e.IsIllegalArgumentException())
                    {
                        if (e.InnerException.IsNoSuchMethodException())
                        {
                            // there is no corresponding ctor available
                            throw; // LUCENENET: CA2200: Rethrow to preserve stack details (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details)
                        }
                        // TODO: For now pass because some factories have not yet a default config that always works
                    }
                }
                else if (c.IsSubclassOf(typeof(TokenFilter)))
                {
                    string clazzName = c.Name;
                    assertTrue(clazzName.EndsWith("Filter", StringComparison.Ordinal));
                    string simpleName = clazzName.Substring(0, clazzName.Length - (clazzName.EndsWith("TokenFilter", StringComparison.Ordinal) ? 11 : 6));
                    assertNotNull(TokenFilterFactory.LookupClass(simpleName));
                    TokenFilterFactory instance = null;
                    try
                    {
                        instance = TokenFilterFactory.ForName(simpleName, args);
                        assertNotNull(instance);
                        if (instance is IResourceLoaderAware resourceLoaderAware)
                        {
                            resourceLoaderAware.Inform(loader);
                        }
                        Type createdClazz = instance.Create(new KeywordTokenizer(new StringReader(""))).GetType();
                        // only check instance if factory have wrapped at all!
                        if (typeof(KeywordTokenizer) != createdClazz)
                        {
                            assertSame(c, createdClazz);
                        }
                    }
                    catch (Exception e) when(e.IsIllegalArgumentException())
                    {
                        if (e.InnerException.IsNoSuchMethodException())
                        {
                            // there is no corresponding ctor available
                            throw; // LUCENENET: CA2200: Rethrow to preserve stack details (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details)
                        }
                        // TODO: For now pass because some factories have not yet a default config that always works
                    }
                }
                else if (c.IsSubclassOf(typeof(CharFilter)))
                {
                    string clazzName = c.Name;
                    assertTrue(clazzName.EndsWith("CharFilter", StringComparison.Ordinal));
                    string simpleName = clazzName.Substring(0, clazzName.Length - 10);
                    assertNotNull(CharFilterFactory.LookupClass(simpleName));
                    CharFilterFactory instance = null;
                    try
                    {
                        instance = CharFilterFactory.ForName(simpleName, args);
                        assertNotNull(instance);
                        if (instance is IResourceLoaderAware resourceLoaderAware)
                        {
                            resourceLoaderAware.Inform(loader);
                        }
                        Type createdClazz = instance.Create(new StringReader("")).GetType();
                        // only check instance if factory have wrapped at all!
                        if (typeof(StringReader) != createdClazz)
                        {
                            assertSame(c, createdClazz);
                        }
                    }
                    catch (Exception e) when(e.IsIllegalArgumentException())
                    {
                        if (e.InnerException.IsNoSuchMethodException())
                        {
                            // there is no corresponding ctor available
                            throw; // LUCENENET: CA2200: Rethrow to preserve stack details (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details)
                        }
                        // TODO: For now pass because some factories have not yet a default config that always works
                    }
                }
            }
        }
        public virtual void Test()
        {
            IList <Type> analysisClasses = new List <Type>(
                typeof(StandardAnalyzer).Assembly.GetTypes()
                .Where(c => !c.IsAbstract && c.IsPublic && !c.IsInterface && c.IsClass && (c.GetCustomAttribute <ObsoleteAttribute>() == null) &&
                       !testComponents.Contains(c) && !crazyComponents.Contains(c) && !oddlyNamedComponents.Contains(c) && !deprecatedDuplicatedComponents.Contains(c) &&
                       (c.IsSubclassOf(typeof(Tokenizer)) || c.IsSubclassOf(typeof(TokenFilter)) || c.IsSubclassOf(typeof(CharFilter)))
                       ));


            foreach (Type c in analysisClasses)
            {
                IDictionary <string, string> args = new Dictionary <string, string>();
                args["luceneMatchVersion"] = TEST_VERSION_CURRENT.ToString();

                if (c.IsSubclassOf(typeof(Tokenizer)))
                {
                    string clazzName = c.Name;
                    assertTrue(clazzName.EndsWith("Tokenizer", StringComparison.Ordinal));
                    string simpleName = clazzName.Substring(0, clazzName.Length - 9);
                    assertNotNull(TokenizerFactory.LookupClass(simpleName));
                    TokenizerFactory instance = null;
                    try
                    {
                        instance = TokenizerFactory.ForName(simpleName, args);
                        assertNotNull(instance);
                        if (instance is IResourceLoaderAware)
                        {
                            ((IResourceLoaderAware)instance).Inform(loader);
                        }
                        assertSame(c, instance.Create(new StringReader("")).GetType());
                    }
                    catch (System.ArgumentException e)
                    {
                        if (e.InnerException is MissingMethodException)
                        {
                            // there is no corresponding ctor available
                            throw e;
                        }
                        // TODO: For now pass because some factories have not yet a default config that always works
                    }
                }
                else if (c.IsSubclassOf(typeof(TokenFilter)))
                {
                    string clazzName = c.Name;
                    assertTrue(clazzName.EndsWith("Filter", StringComparison.Ordinal));
                    string simpleName = clazzName.Substring(0, clazzName.Length - (clazzName.EndsWith("TokenFilter", StringComparison.Ordinal) ? 11 : 6));
                    assertNotNull(TokenFilterFactory.LookupClass(simpleName));
                    TokenFilterFactory instance = null;
                    try
                    {
                        instance = TokenFilterFactory.ForName(simpleName, args);
                        assertNotNull(instance);
                        if (instance is IResourceLoaderAware)
                        {
                            ((IResourceLoaderAware)instance).Inform(loader);
                        }
                        Type createdClazz = instance.Create(new KeywordTokenizer(new StringReader(""))).GetType();
                        // only check instance if factory have wrapped at all!
                        if (typeof(KeywordTokenizer) != createdClazz)
                        {
                            assertSame(c, createdClazz);
                        }
                    }
                    catch (System.ArgumentException e)
                    {
                        if (e.InnerException is MissingMethodException)
                        {
                            // there is no corresponding ctor available
                            throw e;
                        }
                        // TODO: For now pass because some factories have not yet a default config that always works
                    }
                }
                else if (c.IsSubclassOf(typeof(CharFilter)))
                {
                    string clazzName = c.Name;
                    assertTrue(clazzName.EndsWith("CharFilter", StringComparison.Ordinal));
                    string simpleName = clazzName.Substring(0, clazzName.Length - 10);
                    assertNotNull(CharFilterFactory.LookupClass(simpleName));
                    CharFilterFactory instance = null;
                    try
                    {
                        instance = CharFilterFactory.ForName(simpleName, args);
                        assertNotNull(instance);
                        if (instance is IResourceLoaderAware)
                        {
                            ((IResourceLoaderAware)instance).Inform(loader);
                        }
                        Type createdClazz = instance.Create(new StringReader("")).GetType();
                        // only check instance if factory have wrapped at all!
                        if (typeof(StringReader) != createdClazz)
                        {
                            assertSame(c, createdClazz);
                        }
                    }
                    catch (System.ArgumentException e)
                    {
                        if (e.InnerException is MissingMethodException)
                        {
                            // there is no corresponding ctor available
                            throw e;
                        }
                        // TODO: For now pass because some factories have not yet a default config that always works
                    }
                }
            }
        }