<commit_before>﻿using Lucene.Net.Analysis;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers.Flexible.Standard;
using Lucene.Net.QueryParsers.Util;
using Lucene.Net.Search;
using Lucene.Net.Support;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;

namespace Lucene.Net.QueryParsers.Classic
{
    /*
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    [TestFixture]
    public class TestQueryParser : QueryParserTestBase
    {
        public class QPTestParser : QueryParser
        {
            public QPTestParser(string f, Analyzer a)
                : base(TEST_VERSION_CURRENT, f, a)
            {
            }

            protected internal override Query GetFuzzyQuery(string field, string termStr, float minSimilarity)
            {
                throw new ParseException("Fuzzy queries not allowed");
            }

            protected internal override Query GetWildcardQuery(string field, string termStr)
            {
                throw new ParseException("Wildcard queries not allowed");
            }

        }

        public virtual QueryParser GetParser(Analyzer a)
        {
            if (a == null) a = new MockAnalyzer(Random, MockTokenizer.SIMPLE, true);
            QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, DefaultField, a);
            qp.DefaultOperator = (QueryParserBase.OR_OPERATOR);
            return qp;
        }

        public override ICommonQueryParserConfiguration GetParserConfig(Analyzer a)
        {
            return GetParser(a);
        }

        public override Query GetQuery(string query, ICommonQueryParserConfiguration cqpC)
        {
            Debug.Assert(cqpC != null, "Parameter must not be null");
            Debug.Assert(cqpC is QueryParser, "Parameter must be instance of QueryParser");
            QueryParser qp = (QueryParser)cqpC;
            return qp.Parse(query);
        }

        public override Query GetQuery(string query, Analyzer a)
        {
            return GetParser(a).Parse(query);
        }

        public override bool IsQueryParserException(Exception exception)
        {
            return exception is ParseException;
        }

        public override void SetDefaultOperatorOR(ICommonQueryParserConfiguration cqpC)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.DefaultOperator = Operator.OR;
        }

        public override void SetDefaultOperatorAND(ICommonQueryParserConfiguration cqpC)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.DefaultOperator = Operator.AND;
        }

        public override void SetAnalyzeRangeTerms(ICommonQueryParserConfiguration cqpC, bool value)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.AnalyzeRangeTerms = (value);
        }

        public override void SetAutoGeneratePhraseQueries(ICommonQueryParserConfiguration cqpC, bool value)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.AutoGeneratePhraseQueries = value;
        }

        public override void SetDateResolution(ICommonQueryParserConfiguration cqpC, string field, DateTools.Resolution value)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.SetDateResolution(field, value);
        }

        [Test]
        public override void TestDefaultOperator()
        {
            QueryParser qp = GetParser(new MockAnalyzer(Random));
            // make sure OR is the default:
            assertEquals(QueryParserBase.OR_OPERATOR, qp.DefaultOperator);
            SetDefaultOperatorAND(qp);
            assertEquals(QueryParserBase.AND_OPERATOR, qp.DefaultOperator);
            SetDefaultOperatorOR(qp);
            assertEquals(QueryParserBase.OR_OPERATOR, qp.DefaultOperator);
        }

        // LUCENE-2002: when we run javacc to regen QueryParser,
        // we also run a replaceregexp step to fix 2 of the public
        // ctors (change them to protected):
        //
        // protected QueryParser(CharStream stream)
        //
        // protected QueryParser(QueryParserTokenManager tm)
        //
        // This test is here as a safety, in case that ant step
        // doesn't work for some reason.
        [Test]
        public virtual void TestProtectedCtors()
        {
            try
            {
                typeof(QueryParser).GetConstructor(new Type[] { typeof(ICharStream) });
                fail("please switch public QueryParser(CharStream) to be protected");
            }
            catch (Exception /*nsme*/)
            {
                // expected
            }
            try
            {
                typeof(QueryParser).GetConstructor(new Type[] { typeof(QueryParserTokenManager) });
                fail("please switch public QueryParser(QueryParserTokenManager) to be protected");
            }
            catch (Exception /*nsme*/)
            {
                // expected
            }
        }

        private class TestFuzzySlopeExtendabilityQueryParser : QueryParser
        {<commit_msg>Lucene.Net.Tests.QueryParser.Classic.TestQueryParser::TestProtectedCtors(): Reflection doesn't throw exceptions when it cannot find a constructor in .NET, we need to test for null instead. (#301)<commit_after>﻿using Lucene.Net.Analysis;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers.Flexible.Standard;
using Lucene.Net.QueryParsers.Util;
using Lucene.Net.Search;
using Lucene.Net.Support;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;

namespace Lucene.Net.QueryParsers.Classic
{
    /*
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    [TestFixture]
    public class TestQueryParser : QueryParserTestBase
    {
        public class QPTestParser : QueryParser
        {
            public QPTestParser(string f, Analyzer a)
                : base(TEST_VERSION_CURRENT, f, a)
            {
            }

            protected internal override Query GetFuzzyQuery(string field, string termStr, float minSimilarity)
            {
                throw new ParseException("Fuzzy queries not allowed");
            }

            protected internal override Query GetWildcardQuery(string field, string termStr)
            {
                throw new ParseException("Wildcard queries not allowed");
            }

        }

        public virtual QueryParser GetParser(Analyzer a)
        {
            if (a == null) a = new MockAnalyzer(Random, MockTokenizer.SIMPLE, true);
            QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, DefaultField, a);
            qp.DefaultOperator = (QueryParserBase.OR_OPERATOR);
            return qp;
        }

        public override ICommonQueryParserConfiguration GetParserConfig(Analyzer a)
        {
            return GetParser(a);
        }

        public override Query GetQuery(string query, ICommonQueryParserConfiguration cqpC)
        {
            Debug.Assert(cqpC != null, "Parameter must not be null");
            Debug.Assert(cqpC is QueryParser, "Parameter must be instance of QueryParser");
            QueryParser qp = (QueryParser)cqpC;
            return qp.Parse(query);
        }

        public override Query GetQuery(string query, Analyzer a)
        {
            return GetParser(a).Parse(query);
        }

        public override bool IsQueryParserException(Exception exception)
        {
            return exception is ParseException;
        }

        public override void SetDefaultOperatorOR(ICommonQueryParserConfiguration cqpC)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.DefaultOperator = Operator.OR;
        }

        public override void SetDefaultOperatorAND(ICommonQueryParserConfiguration cqpC)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.DefaultOperator = Operator.AND;
        }

        public override void SetAnalyzeRangeTerms(ICommonQueryParserConfiguration cqpC, bool value)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.AnalyzeRangeTerms = (value);
        }

        public override void SetAutoGeneratePhraseQueries(ICommonQueryParserConfiguration cqpC, bool value)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.AutoGeneratePhraseQueries = value;
        }

        public override void SetDateResolution(ICommonQueryParserConfiguration cqpC, string field, DateTools.Resolution value)
        {
            Debug.Assert(cqpC is QueryParser);
            QueryParser qp = (QueryParser)cqpC;
            qp.SetDateResolution(field, value);
        }

        [Test]
        public override void TestDefaultOperator()
        {
            QueryParser qp = GetParser(new MockAnalyzer(Random));
            // make sure OR is the default:
            assertEquals(QueryParserBase.OR_OPERATOR, qp.DefaultOperator);
            SetDefaultOperatorAND(qp);
            assertEquals(QueryParserBase.AND_OPERATOR, qp.DefaultOperator);
            SetDefaultOperatorOR(qp);
            assertEquals(QueryParserBase.OR_OPERATOR, qp.DefaultOperator);
        }

        // LUCENE-2002: when we run javacc to regen QueryParser,
        // we also run a replaceregexp step to fix 2 of the public
        // ctors (change them to protected):
        //
        // protected QueryParser(CharStream stream)
        //
        // protected QueryParser(QueryParserTokenManager tm)
        //
        // This test is here as a safety, in case that ant step
        // doesn't work for some reason.
        [Test]
        public virtual void TestProtectedCtors()
        {
            // LUCENENET: .NET doesn't throw exceptions when public constructors don't exist, it simply returns null.

            assertNull("please switch public QueryParser(ICharStream) to be protected", typeof(QueryParser).GetConstructor(new Type[] { typeof(ICharStream) }));
            assertNull("please switch public QueryParser(QueryParserTokenManager) to be protected", typeof(QueryParser).GetConstructor(new Type[] { typeof(QueryParserTokenManager) }));

            //try
            //{
            //    typeof(QueryParser).GetConstructor(new Type[] { typeof(ICharStream) });
            //    fail("please switch public QueryParser(CharStream) to be protected");
            //}
            //catch (Exception /*nsme*/)
            //{
            //    // expected
            //}
            //try
            //{
            //    typeof(QueryParser).GetConstructor(new Type[] { typeof(QueryParserTokenManager) });
            //    fail("please switch public QueryParser(QueryParserTokenManager) to be protected");
            //}
            //catch (Exception /*nsme*/)
            //{
            //    // expected
            //}
        }

        private class TestFuzzySlopeExtendabilityQueryParser : QueryParser
        {
{'fraction_non_alphanumeric': 0.07437317335794494, 'fraction_numerical': 0.0016151361329026304, 'mean_word_length': 2.1799951088285643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7587763', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3214, 'n_words': 943}