Ejemplo n.º 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void doTestTokenFilter(String tokenfilter) throws java.io.IOException
        private void doTestTokenFilter(string tokenfilter)
        {
//JAVA TO C# CONVERTER TODO TASK: Java wildcard generics are not converted to .NET:
//ORIGINAL LINE: Class<? extends org.apache.lucene.analysis.util.TokenFilterFactory> factoryClazz = org.apache.lucene.analysis.util.TokenFilterFactory.lookupClass(tokenfilter);
            Type <?>           factoryClazz = TokenFilterFactory.lookupClass(tokenfilter);
            TokenFilterFactory factory      = (TokenFilterFactory)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 MultiTermAwareComponent)
                {
                    AbstractAnalysisFactory mtc = ((MultiTermAwareComponent)factory).MultiTermComponent;
                    assertNotNull(mtc);
                    // its not ok to return a charfilter or tokenizer here, this makes no sense
                    assertTrue(mtc is TokenFilterFactory);
                }

                // beast it just a little, it shouldnt throw exceptions:
                // (it should have thrown them in initialize)
                checkRandomData(random(), new FactoryAnalyzer(assertingTokenizer, factory, null), 100, 20, false, false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// tries to initialize a factory with no arguments </summary>
        private static AbstractAnalysisFactory Initialize(Type factoryClazz) // LUCENENET: CA1822: Mark members as static
        {
            IDictionary <string, string> args =
                new Dictionary <string, string> {
                ["luceneMatchVersion"] = TEST_VERSION_CURRENT_STRING
            };

            ConstructorInfo ctor;

            try
            {
                ctor = factoryClazz.GetConstructor(new Type[] { typeof(IDictionary <string, string>) });
            }
            catch (Exception e)
            {
                throw new Exception("factory '" + factoryClazz + "' does not have a proper ctor!", e);
            }

            AbstractAnalysisFactory factory = null;

            try
            {
                factory = (AbstractAnalysisFactory)ctor.Invoke(new object[] { args });
            }
            catch (TypeInitializationException e)
            {
                throw new Exception(e.Message, e);
            }
            catch (MethodAccessException e)
            {
                throw new Exception(e.Message, e);
            }
            catch (TargetInvocationException e)
            {
                if (e.InnerException is ArgumentException)
                {
                    // its ok if we dont provide the right parameters to throw this
                    return(null);
                }
            }

            if (factory is IResourceLoaderAware aware)
            {
                try
                {
                    aware.Inform(new StringMockResourceLoader(""));
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (IOException)
                {
                    // its ok if the right files arent available or whatever to throw this
                }
                catch (ArgumentException)
                {
                    // is this ok? I guess so
                }
#pragma warning restore CA1031 // Do not catch general exception types
            }
            return(factory);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// tries to initialize a factory with no arguments </summary>
        private static AbstractAnalysisFactory Initialize(Type factoryClazz) // LUCENENET: CA1822: Mark members as static
        {
            IDictionary <string, string> args =
                new Dictionary <string, string> {
                ["luceneMatchVersion"] = TEST_VERSION_CURRENT_STRING
            };

            ConstructorInfo ctor;

            try
            {
                ctor = factoryClazz.GetConstructor(new Type[] { typeof(IDictionary <string, string>) });
            }
            catch (Exception e) when(e.IsException())
            {
                throw RuntimeException.Create("factory '" + factoryClazz + "' does not have a proper ctor!", e);
            }

            AbstractAnalysisFactory factory = null;

            try
            {
                factory = (AbstractAnalysisFactory)ctor.Invoke(new object[] { args });
            }
            catch (Exception e) when(e.IsInstantiationException())
            {
                throw RuntimeException.Create(e);
            }
            catch (Exception e) when(e.IsIllegalAccessException())
            {
                throw RuntimeException.Create(e);
            }
            catch (Exception e) when(e.IsInvocationTargetException())
            {
                if (e.InnerException is ArgumentException)
                {
                    // its ok if we dont provide the right parameters to throw this
                    return(null);
                }
            }

            if (factory is IResourceLoaderAware aware)
            {
                try
                {
                    aware.Inform(new StringMockResourceLoader(""));
                }
                catch (Exception ignored) when(ignored.IsIOException())
                {
                    // its ok if the right files arent available or whatever to throw this
                }
                catch (Exception ignored) when(ignored.IsIllegalArgumentException())
                {
                    // is this ok? I guess so
                }
            }
            return(factory);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// tries to initialize a factory with no arguments </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private org.apache.lucene.analysis.util.AbstractAnalysisFactory initialize(Class<? extends org.apache.lucene.analysis.util.AbstractAnalysisFactory> factoryClazz) throws java.io.IOException
        private AbstractAnalysisFactory initialize <T1>(Type <T1> factoryClazz) where T1 : org.apache.lucene.analysis.util.AbstractAnalysisFactory
        {
            IDictionary <string, string> args = new Dictionary <string, string>();

            args["luceneMatchVersion"] = TEST_VERSION_CURRENT.ToString();
//JAVA TO C# CONVERTER TODO TASK: Java wildcard generics are not converted to .NET:
//ORIGINAL LINE: Constructor<? extends org.apache.lucene.analysis.util.AbstractAnalysisFactory> ctor;
            Constructor <?> ctor;

            try
            {
                ctor = factoryClazz.GetConstructor(typeof(IDictionary));
            }
            catch (Exception)
            {
                throw new Exception("factory '" + factoryClazz + "' does not have a proper ctor!");
            }

            AbstractAnalysisFactory factory = null;

            try
            {
                factory = ctor.newInstance(args);
            }
            catch (InstantiationException e)
            {
                throw new Exception(e);
            }
            catch (IllegalAccessException e)
            {
                throw new Exception(e);
            }
            catch (InvocationTargetException e)
            {
                if (e.InnerException is System.ArgumentException)
                {
                    // its ok if we dont provide the right parameters to throw this
                    return(null);
                }
            }

            if (factory is ResourceLoaderAware)
            {
                try
                {
                    ((ResourceLoaderAware)factory).inform(new StringMockResourceLoader(""));
                }
                catch (IOException)
                {
                    // its ok if the right files arent available or whatever to throw this
                }
                catch (System.ArgumentException)
                {
                    // is this ok? I guess so
                }
            }
            return(factory);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// tries to initialize a factory with no arguments </summary>
        private AbstractAnalysisFactory Initialize(Type factoryClazz)
        {
            IDictionary <string, string> args = new Dictionary <string, string>();

            args["luceneMatchVersion"] = TEST_VERSION_CURRENT.ToString();

            ConstructorInfo ctor;

            try
            {
                ctor = factoryClazz.GetConstructor(new Type[] { typeof(IDictionary <string, string>) });
            }
            catch (Exception)
            {
                throw new Exception("factory '" + factoryClazz + "' does not have a proper ctor!");
            }

            AbstractAnalysisFactory factory = null;

            try
            {
                factory = (AbstractAnalysisFactory)ctor.Invoke(new object[] { args });
            }
            catch (TypeInitializationException e)
            {
                throw new Exception(e.Message, e);
            }
            catch (MethodAccessException e)
            {
                throw new Exception(e.Message, e);
            }
            catch (TargetInvocationException e)
            {
                if (e.InnerException is System.ArgumentException)
                {
                    // its ok if we dont provide the right parameters to throw this
                    return(null);
                }
            }

            if (factory is IResourceLoaderAware)
            {
                try
                {
                    ((IResourceLoaderAware)factory).Inform(new StringMockResourceLoader(""));
                }
                catch (IOException)
                {
                    // its ok if the right files arent available or whatever to throw this
                }
                catch (System.ArgumentException)
                {
                    // is this ok? I guess so
                }
            }
            return(factory);
        }
Ejemplo n.º 6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private AbstractAnalysisFactory analysisFactory(Class<? extends AbstractAnalysisFactory> clazz, org.apache.lucene.util.Version matchVersion, ResourceLoader loader, String... keysAndValues) throws Exception
        private AbstractAnalysisFactory analysisFactory <T1>(Type <T1> clazz, Version matchVersion, ResourceLoader loader, params string[] keysAndValues) where T1 : AbstractAnalysisFactory
        {
            if (keysAndValues.Length % 2 == 1)
            {
                throw new System.ArgumentException("invalid keysAndValues map");
            }
            IDictionary <string, string> args = new Dictionary <string, string>();

            for (int i = 0; i < keysAndValues.Length; i += 2)
            {
                string previous = args[keysAndValues[i]] = keysAndValues[i + 1];
                assertNull("duplicate values for key: " + keysAndValues[i], previous);
            }
            if (matchVersion != null)
            {
                string previous = args["luceneMatchVersion"] = matchVersion.ToString();
                assertNull("duplicate values for key: luceneMatchVersion", previous);
            }
            AbstractAnalysisFactory factory = null;

            try
            {
                factory = clazz.GetConstructor(typeof(IDictionary)).newInstance(args);
            }
            catch (InvocationTargetException e)
            {
                // to simplify tests that check for illegal parameters
                if (e.InnerException is System.ArgumentException)
                {
                    throw (System.ArgumentException)e.InnerException;
                }
                else
                {
                    throw e;
                }
            }
            if (factory is ResourceLoaderAware)
            {
                ((ResourceLoaderAware)factory).inform(loader);
            }
            return(factory);
        }
Ejemplo n.º 7
0
        private void DoTestTokenFilter(string tokenfilter)
        {
            var factoryClazz           = TokenFilterFactory.LookupClass(tokenfilter);
            TokenFilterFactory factory = (TokenFilterFactory)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 charfilter or tokenizer here, this makes no sense
                    assertTrue(mtc is TokenFilterFactory);
                }

                // beast it just a little, it shouldnt throw exceptions:
                // (it should have thrown them in initialize)
                CheckRandomData(Random, new FactoryAnalyzer(assertingTokenizer, factory, null), 100, 20, false, false);
            }
        }
Ejemplo n.º 8
0
        private void DoTestTokenizer(string tokenizer)
        {
            var factoryClazz         = TokenizerFactory.LookupClass(tokenizer);
            TokenizerFactory factory = (TokenizerFactory)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)
                {
                    AbstractAnalysisFactory mtc = ((IMultiTermAwareComponent)factory).GetMultiTermComponent();
                    assertNotNull(mtc);
                    // its not ok to return e.g. a charfilter here: but a tokenizer could wrap a filter around it
                    assertFalse(mtc is CharFilterFactory);
                }

                // beast it just a little, it shouldnt throw exceptions:
                // (it should have thrown them in initialize)
                CheckRandomData(Random(), new FactoryAnalyzer(factory, null, null), 100, 20, false, false);
            }
        }