View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.language.bm;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import org.apache.commons.codec.EncoderException;
25  import org.apache.commons.codec.StringEncoder;
26  import org.apache.commons.codec.StringEncoderAbstractTest;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Tests BeiderMorseEncoder.
32   *
33   * @since 1.6
34   */
35  public class BeiderMorseEncoderTest extends StringEncoderAbstractTest<StringEncoder> {
36      private static final char[] TEST_CHARS = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'o', 'u' };
37  
38      private void assertNotEmpty(final BeiderMorseEncoder bmpm, final String value) throws EncoderException {
39          Assert.assertFalse(value, bmpm.encode(value).equals(""));
40      }
41  
42      private BeiderMorseEncoder createGenericApproxEncoder() {
43          final BeiderMorseEncoder encoder = new BeiderMorseEncoder();
44          encoder.setNameType(NameType.GENERIC);
45          encoder.setRuleType(RuleType.APPROX);
46          return encoder;
47      }
48  
49      @Override
50      protected StringEncoder createStringEncoder() {
51          return new BeiderMorseEncoder();
52      }
53  
54      /**
55       * Tests we do not blow up.
56       *
57       * @throws EncoderException
58       */
59      @Test
60      public void testAllChars() throws EncoderException {
61          final BeiderMorseEncoder bmpm = createGenericApproxEncoder();
62          for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
63              bmpm.encode(Character.toString(c));
64          }
65      }
66  
67      @Test
68      public void testAsciiEncodeNotEmpty1Letter() throws EncoderException {
69          final BeiderMorseEncoder bmpm = createGenericApproxEncoder();
70          for (char c = 'a'; c <= 'z'; c++) {
71              final String value = Character.toString(c);
72              final String valueU = value.toUpperCase();
73              assertNotEmpty(bmpm, value);
74              assertNotEmpty(bmpm, valueU);
75          }
76      }
77  
78      @Test
79      public void testAsciiEncodeNotEmpty2Letters() throws EncoderException {
80          final BeiderMorseEncoder bmpm = createGenericApproxEncoder();
81          for (char c1 = 'a'; c1 <= 'z'; c1++) {
82              for (char c2 = 'a'; c2 <= 'z'; c2++) {
83                  final String value = new String(new char[] { c1, c2 });
84                  final String valueU = value.toUpperCase();
85                  assertNotEmpty(bmpm, value);
86                  assertNotEmpty(bmpm, valueU);
87              }
88          }
89      }
90  
91      @Test
92      public void testEncodeAtzNotEmpty() throws EncoderException {
93          final BeiderMorseEncoder bmpm = createGenericApproxEncoder();
94          //String[] names = { "ácz", "átz", "Ignácz", "Ignátz", "Ignác" };
95          final String[] names = { "\u00e1cz", "\u00e1tz", "Ign\u00e1cz", "Ign\u00e1tz", "Ign\u00e1c" };
96          for (final String name : names) {
97              assertNotEmpty(bmpm, name);
98          }
99      }
100 
101     /**
102      * Tests https://issues.apache.org/jira/browse/CODEC-125?focusedCommentId=13071566&page=com.atlassian.jira.plugin.system.issuetabpanels:
103      * comment-tabpanel#comment-13071566
104      *
105      * @throws EncoderException
106      */
107     @Test
108     public void testEncodeGna() throws EncoderException {
109         final BeiderMorseEncoder bmpm = createGenericApproxEncoder();
110         bmpm.encode("gna");
111     }
112 
113     @Test(expected = IllegalArgumentException.class)
114     public void testInvalidLangIllegalArgumentException() {
115         Rule.getInstance(NameType.GENERIC, RuleType.APPROX, "noSuchLanguage");
116     }
117 
118     @Test(expected = IllegalStateException.class)
119     public void testInvalidLangIllegalStateException() {
120         Lang.loadFromResource("thisIsAMadeUpResourceName", Languages.getInstance(NameType.GENERIC));
121     }
122 
123     @Test(expected = IllegalArgumentException.class)
124     public void testInvalidLanguageIllegalArgumentException() {
125         Languages.getInstance("thereIsNoSuchLanguage");
126     }
127 
128     @Test(timeout = 10000L)
129     public void testLongestEnglishSurname() throws EncoderException {
130         final BeiderMorseEncoder bmpm = createGenericApproxEncoder();
131         bmpm.encode("MacGhilleseatheanaich");
132     }
133 
134     @Test(expected = IndexOutOfBoundsException.class)
135     public void testNegativeIndexForRuleMatchIndexOutOfBoundsException() {
136         final Rule r = new Rule("a", "", "", new Rule.Phoneme("", Languages.ANY_LANGUAGE));
137         r.patternAndContextMatches("bob", -1);
138     }
139 
140     @Test
141     public void testOOM() throws EncoderException {
142         final String phrase = "200697900'-->&#1913348150;</  bceaeef >aadaabcf\"aedfbff<!--\'-->?>cae"
143                 + "cfaaa><?&#<!--</script>&lang&fc;aadeaf?>>&bdquo<    cc =\"abff\"    /></   afe  >"
144                 + "<script><!-- f(';<    cf aefbeef = \"bfabadcf\" ebbfeedd = fccabeb >";
145 
146         final BeiderMorseEncoder encoder = new BeiderMorseEncoder();
147         encoder.setNameType(NameType.GENERIC);
148         encoder.setRuleType(RuleType.EXACT);
149         encoder.setMaxPhonemes(10);
150 
151         final String phonemes = encoder.encode(phrase);
152         assertTrue(phonemes.length() > 0);
153 
154         final String[] phonemeArr = phonemes.split("\\|");
155         assertTrue(phonemeArr.length <= 10);
156     }
157 
158     @Test
159     public void testSetConcat() {
160         final BeiderMorseEncoder bmpm = new BeiderMorseEncoder();
161         bmpm.setConcat(false);
162         assertFalse("Should be able to set concat to false", bmpm.isConcat());
163     }
164 
165     @Test
166     public void testSetNameTypeAsh() {
167         final BeiderMorseEncoder bmpm = new BeiderMorseEncoder();
168         bmpm.setNameType(NameType.ASHKENAZI);
169         assertEquals("Name type should have been set to ash", NameType.ASHKENAZI, bmpm.getNameType());
170     }
171 
172     @Test
173     public void testSetRuleTypeExact() {
174         final BeiderMorseEncoder bmpm = new BeiderMorseEncoder();
175         bmpm.setRuleType(RuleType.EXACT);
176         assertEquals("Rule type should have been set to exact", RuleType.EXACT, bmpm.getRuleType());
177     }
178 
179     @Test(expected = IllegalArgumentException.class)
180     public void testSetRuleTypeToRulesIllegalArgumentException() {
181         final BeiderMorseEncoder bmpm = new BeiderMorseEncoder();
182         bmpm.setRuleType(RuleType.RULES);
183     }
184 
185     /**
186      * (Un)luckily, the worse performing test because of the data in {@link #TEST_CHARS}
187      *
188      * @throws EncoderException
189      */
190     @Test(/* timeout = 20000L */)
191     public void testSpeedCheck() throws EncoderException {
192         final BeiderMorseEncoder bmpm = this.createGenericApproxEncoder();
193         final StringBuilder stringBuffer = new StringBuilder();
194         stringBuffer.append(TEST_CHARS[0]);
195         for (int i = 0, j = 1; i < 40; i++, j++) {
196             if (j == TEST_CHARS.length) {
197                 j = 0;
198             }
199             bmpm.encode(stringBuffer.toString());
200             stringBuffer.append(TEST_CHARS[j]);
201         }
202     }
203 
204     @Test
205     public void testSpeedCheck2() throws EncoderException {
206         final BeiderMorseEncoder bmpm = this.createGenericApproxEncoder();
207         final String phrase = "ItstheendoftheworldasweknowitandIfeelfine";
208 
209         for (int i = 1; i <= phrase.length(); i++) {
210             bmpm.encode(phrase.subSequence(0, i));
211         }
212     }
213 
214     @Test
215     public void testSpeedCheck3() throws EncoderException {
216         final BeiderMorseEncoder bmpm = this.createGenericApproxEncoder();
217         final String phrase = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
218 
219         for (int i = 1; i <= phrase.length(); i++) {
220             bmpm.encode(phrase.subSequence(0, i));
221         }
222     }
223 }