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    *      https://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.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNotSame;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.apache.commons.codec.EncoderException;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link BeiderMorseEncoder.Builder} and {@link BeiderMorseEncoder#builder()}.
31   */
32  class BeiderMorseEncoderBuilderTest {
33  
34      /**
35       * Tests that each call to {@code get()} on the same builder returns a distinct encoder instance.
36       */
37      @Test
38      void testBuilderGetReturnsDifferentInstances() {
39          final BeiderMorseEncoder.Builder builder = BeiderMorseEncoder.builder();
40          assertNotSame(builder.get(), builder.get());
41      }
42  
43      /**
44       * Tests that the builder's {@code get()} method returns a non-null encoder with default settings.
45       */
46      @Test
47      void testBuilderGetReturnsNonNull() {
48          assertNotNull(BeiderMorseEncoder.builder().get());
49      }
50  
51      /**
52       * Tests that {@link BeiderMorseEncoder#builder()} returns a non-null builder.
53       */
54      @Test
55      void testBuilderIsNotNull() {
56          assertNotNull(BeiderMorseEncoder.builder());
57      }
58  
59      /**
60       * Tests that each call to {@link BeiderMorseEncoder#builder()} returns a distinct builder instance.
61       */
62      @Test
63      void testBuilderReturnsDifferentInstances() {
64          assertNotSame(BeiderMorseEncoder.builder(), BeiderMorseEncoder.builder());
65      }
66  
67      /**
68       * Tests that an encoder built with concat disabled produces the expected behavior.
69       */
70      @Test
71      void testBuilderWithConcatDisabled() {
72          final PhoneticEngine engine = PhoneticEngine.builder().setConcat(false).get();
73          final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().setPhoneticEngine(engine).get();
74          assertFalse(encoder.isConcat());
75      }
76  
77      /**
78       * Tests that the builder with EXACT rule type produces a non-empty encoding for a known name.
79       */
80      @Test
81      void testBuilderWithExactRuleType() throws EncoderException {
82          final PhoneticEngine engine = PhoneticEngine.builder().setRuleType(RuleType.EXACT).get();
83          final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().setPhoneticEngine(engine).get();
84          assertEquals(RuleType.EXACT, encoder.getRuleType());
85          final String result = encoder.encode("Cohen");
86          assertNotNull(result);
87          assertFalse(result.isEmpty());
88      }
89  
90      /**
91       * Tests that the default encoder has concat enabled.
92       */
93      @Test
94      void testDefaultConcat() {
95          final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().get();
96          assertTrue(encoder.isConcat());
97      }
98  
99      /**
100      * Tests that the default encoder built with no configuration produces a non-empty encoding.
101      */
102     @Test
103     void testDefaultEncoderEncodes() throws EncoderException {
104         final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().get();
105         final String result = encoder.encode("Smith");
106         assertNotNull(result);
107         assertFalse(result.isEmpty());
108     }
109 
110     /**
111      * Tests that the default encoder has the default name type (GENERIC).
112      */
113     @Test
114     void testDefaultNameType() {
115         final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().get();
116         assertEquals(NameType.GENERIC, encoder.getNameType());
117     }
118 
119     /**
120      * Tests that the default encoder has the default rule type (APPROX).
121      */
122     @Test
123     void testDefaultRuleType() {
124         final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().get();
125         assertEquals(RuleType.APPROX, encoder.getRuleType());
126     }
127 
128     /**
129      * Tests that an encoder built via the builder encodes {@code null} to {@code null}.
130      */
131     @Test
132     void testEncodeNullReturnsNull() throws EncoderException {
133         final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().get();
134         assertNotNull(encoder);
135         // encode(null) should return null per BeiderMorseEncoder implementation.
136         assertEquals(null, encoder.encode((String) null));
137     }
138 
139     /**
140      * Tests that passing {@code null} to {@link BeiderMorseEncoder.Builder#setPhoneticEngine(PhoneticEngine)} falls back to the default engine.
141      */
142     @Test
143     void testSetPhoneticEngineNullFallsBackToDefault() {
144         final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().setPhoneticEngine(null).get();
145         assertNotNull(encoder);
146         assertEquals(NameType.GENERIC, encoder.getNameType());
147         assertEquals(RuleType.APPROX, encoder.getRuleType());
148         assertTrue(encoder.isConcat());
149     }
150 
151     /**
152      * Tests that {@link BeiderMorseEncoder.Builder#setPhoneticEngine(PhoneticEngine)} supports chaining.
153      */
154     @Test
155     void testSetPhoneticEngineReturnsBuilder() {
156         final PhoneticEngine engine = PhoneticEngine.builder().get();
157         final BeiderMorseEncoder.Builder builder = BeiderMorseEncoder.builder();
158         assertNotNull(builder.setPhoneticEngine(engine));
159     }
160 
161     /**
162      * Tests building an encoder with a custom {@link PhoneticEngine} using the ASHKENAZI name type.
163      */
164     @Test
165     void testSetPhoneticEngineWithAshkenazi() {
166         // @formatter:off
167         final PhoneticEngine engine = PhoneticEngine.builder()
168                 .setNameType(NameType.ASHKENAZI)
169                 .setRuleType(RuleType.EXACT)
170                 .get();
171         // @formatter:on
172         final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().setPhoneticEngine(engine).get();
173         assertEquals(NameType.ASHKENAZI, encoder.getNameType());
174         assertEquals(RuleType.EXACT, encoder.getRuleType());
175     }
176 
177     /**
178      * Tests building an encoder with a custom {@link PhoneticEngine} using the SEPHARDIC name type.
179      */
180     @Test
181     void testSetPhoneticEngineWithSephardic() {
182         final PhoneticEngine engine = PhoneticEngine.builder().setNameType(NameType.SEPHARDIC).get();
183         final BeiderMorseEncoder encoder = BeiderMorseEncoder.builder().setPhoneticEngine(engine).get();
184         assertEquals(NameType.SEPHARDIC, encoder.getNameType());
185     }
186 
187     /**
188      * Tests that two encoders built independently with identical configurations produce the same encoding.
189      */
190     @Test
191     void testTwoDefaultBuildersProduceSameEncoding() throws EncoderException {
192         final BeiderMorseEncoder enc1 = BeiderMorseEncoder.builder().get();
193         final BeiderMorseEncoder enc2 = BeiderMorseEncoder.builder().get();
194         assertEquals(enc1.encode("Levy"), enc2.encode("Levy"));
195     }
196 }