http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1ee3a9cc/src/Lucene.Net.Tests.Analysis.Phonetic/TestPhoneticFilterFactory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests.Analysis.Phonetic/TestPhoneticFilterFactory.cs b/src/Lucene.Net.Tests.Analysis.Phonetic/TestPhoneticFilterFactory.cs new file mode 100644 index 0000000..1baedfb --- /dev/null +++ b/src/Lucene.Net.Tests.Analysis.Phonetic/TestPhoneticFilterFactory.cs @@ -0,0 +1,228 @@ +using Lucene.Net.Analysis.Phonetic.Language; +using Lucene.Net.Analysis.Util; +using Lucene.Net.Support; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.IO; + +namespace Lucene.Net.Analysis.Phonetic +{ + /* + * 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. + */ + + public class TestPhoneticFilterFactory : BaseTokenStreamTestCase + { + /** + * Case: default + */ + [Test] + public void TestFactoryDefaults() + { + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put(PhoneticFilterFactory.ENCODER, "Metaphone"); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + assertTrue(factory.GetEncoder() is Metaphone); + assertTrue(factory.inject); // default + } + + [Test] + public void TestInjectFalse() + { + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put(PhoneticFilterFactory.ENCODER, "Metaphone"); + args.Put(PhoneticFilterFactory.INJECT, "false"); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + assertFalse(factory.inject); + } + + [Test] + public void TestMaxCodeLength() + { + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put(PhoneticFilterFactory.ENCODER, "Metaphone"); + args.Put(PhoneticFilterFactory.MAX_CODE_LENGTH, "2"); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + assertEquals(2, ((Metaphone)factory.GetEncoder()).MaxCodeLen); + } + + /** + * Case: Failures and Exceptions + */ + [Test] + public void TestMissingEncoder() + { + try + { + new PhoneticFilterFactory(new Dictionary<String, String>()); + fail(); + } + catch (ArgumentException expected) + { + assertTrue(expected.Message.Contains("Configuration Error: missing parameter 'encoder'")); + } + } + [Test] + public void TestUnknownEncoder() + { + try + { + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put("encoder", "XXX"); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + fail(); + } + catch (ArgumentException expected) + { + assertTrue(expected.Message.Contains("Error loading encoder")); + } + } + + [Test] + public void TestUnknownEncoderReflection() + { + try + { + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put("encoder", "org.apache.commons.codec.language.NonExistence"); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + fail(); + } + catch (ArgumentException expected) + { + assertTrue(expected.Message.Contains("Error loading encoder")); + } + } + + /** + * Case: Reflection + */ + [Test] + public void TestFactoryReflection() + { + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put(PhoneticFilterFactory.ENCODER, "Metaphone"); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + assertTrue(factory.GetEncoder() is Metaphone); + assertTrue(factory.inject); // default + } + + /** + * we use "Caverphone2" as it is registered in the REGISTRY as Caverphone, + * so this effectively tests reflection without package name + */ + [Test] + public void TestFactoryReflectionCaverphone2() + { + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put(PhoneticFilterFactory.ENCODER, "Caverphone2"); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + assertTrue(factory.GetEncoder() is Caverphone2); + assertTrue(factory.inject); // default + } + + [Test] + public void TestFactoryReflectionCaverphone() + { + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put(PhoneticFilterFactory.ENCODER, "Caverphone"); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + assertTrue(factory.GetEncoder() is Caverphone2); + assertTrue(factory.inject); // default + } + + [Test] + public void TestAlgorithms() + { + assertAlgorithm("Metaphone", "true", "aaa bbb ccc easgasg", + new String[] { "A", "aaa", "B", "bbb", "KKK", "ccc", "ESKS", "easgasg" }); + assertAlgorithm("Metaphone", "false", "aaa bbb ccc easgasg", + new String[] { "A", "B", "KKK", "ESKS" }); + + + assertAlgorithm("DoubleMetaphone", "true", "aaa bbb ccc easgasg", + new String[] { "A", "aaa", "PP", "bbb", "KK", "ccc", "ASKS", "easgasg" }); + assertAlgorithm("DoubleMetaphone", "false", "aaa bbb ccc easgasg", + new String[] { "A", "PP", "KK", "ASKS" }); + + + assertAlgorithm("Soundex", "true", "aaa bbb ccc easgasg", + new String[] { "A000", "aaa", "B000", "bbb", "C000", "ccc", "E220", "easgasg" }); + assertAlgorithm("Soundex", "false", "aaa bbb ccc easgasg", + new String[] { "A000", "B000", "C000", "E220" }); + + + assertAlgorithm("RefinedSoundex", "true", "aaa bbb ccc easgasg", + new String[] { "A0", "aaa", "B1", "bbb", "C3", "ccc", "E034034", "easgasg" }); + assertAlgorithm("RefinedSoundex", "false", "aaa bbb ccc easgasg", + new String[] { "A0", "B1", "C3", "E034034" }); + + + assertAlgorithm("Caverphone", "true", "Darda Karleen Datha Carlene", + new String[] { "TTA1111111", "Darda", "KLN1111111", "Karleen", + "TTA1111111", "Datha", "KLN1111111", "Carlene" }); + assertAlgorithm("Caverphone", "false", "Darda Karleen Datha Carlene", + new String[] { "TTA1111111", "KLN1111111", "TTA1111111", "KLN1111111" }); + + + assertAlgorithm("ColognePhonetic", "true", "Meier Schmitt Meir Schmidt", + new String[] { "67", "Meier", "862", "Schmitt", + "67", "Meir", "862", "Schmidt" }); + assertAlgorithm("ColognePhonetic", "false", "Meier Schmitt Meir Schmidt", + new String[] { "67", "862", "67", "862" }); + } + + /** Test that bogus arguments result in exception */ + [Test] + public void TestBogusArguments() + { + try + { + new PhoneticFilterFactory(new Dictionary<String, String>() { + { "encoder", "Metaphone" }, + { "bogusArg", "bogusValue" } + }); + fail(); + } + catch (ArgumentException expected) + { + assertTrue(expected.Message.Contains("Unknown parameters")); + } + } + + internal static void assertAlgorithm(String algName, String inject, String input, + String[] expected) + { + Tokenizer tokenizer = new MockTokenizer(new StringReader(input), MockTokenizer.WHITESPACE, false); + IDictionary<String, String> args = new Dictionary<String, String>(); + args.Put("encoder", algName); + args.Put("inject", inject); + PhoneticFilterFactory factory = new PhoneticFilterFactory(args); + factory.Inform(new ClasspathResourceLoader(factory.GetType())); + TokenStream stream = factory.Create(tokenizer); + AssertTokenStreamContents(stream, expected); + } + } +}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1ee3a9cc/src/Lucene.Net.Tests.Analysis.Phonetic/project.json ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests.Analysis.Phonetic/project.json b/src/Lucene.Net.Tests.Analysis.Phonetic/project.json new file mode 100644 index 0000000..7bad539 --- /dev/null +++ b/src/Lucene.Net.Tests.Analysis.Phonetic/project.json @@ -0,0 +1,45 @@ +{ + "version": "4.8.0", + "title": "Lucene.Net.Tests.Analysis.Phonetic", + "buildOptions": { + "compile": { + "includeFiles": [ "../CommonAssemblyInfo.cs" ] + }, + "embed": { + "includeFiles": [ + ] + } + }, + "dependencies": { + "dotnet-test-nunit-teamcity": "3.4.0-beta-3", + "Lucene.Net.Analysis.Phonetic": "4.8.0", + "Lucene.Net.TestFramework": "4.8.0", + "NUnit": "3.5.0" + }, + "testRunner": "nunit-teamcity", + "frameworks": { + "netcoreapp1.0": { + "imports": "dnxcore50", + "buildOptions": { + "debugType": "portable", + "define": [ "NETSTANDARD" ], + "compile": { + "excludeFiles": [ + "Support/TestApiConsistency.cs" + ] + } + } + }, + "net451": { + "buildOptions": { + "debugType": "full", + "define": [ "FEATURE_SERIALIZABLE" ] + } + } + }, + + "runtimes": { + "win7-x86": {}, + "win7-x64": {} + } +}
