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.text.similarity;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.util.Objects;
24  import java.util.stream.Stream;
25  
26  import org.apache.commons.text.TextStringBuilder;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link SimilarityInput}.
31   */
32  public class SimilarityInputTest {
33  
34      public static final class SimilarityInputFixture implements SimilarityInput<Object> {
35  
36          private final CharSequence value;
37  
38          public SimilarityInputFixture(final String value) {
39              this.value = Objects.requireNonNull(value);
40          }
41  
42          @Override
43          public Object at(final int i) {
44              return value.charAt(i);
45          }
46  
47          @Override
48          public boolean equals(final Object obj) {
49              if (this == obj) {
50                  return true;
51              }
52              if (obj == null) {
53                  return false;
54              }
55              if (getClass() != obj.getClass()) {
56                  return false;
57              }
58              final SimilarityInputFixture other = (SimilarityInputFixture) obj;
59              return Objects.equals(value, other.value);
60          }
61  
62          @Override
63          public int hashCode() {
64              return Objects.hash(value);
65          }
66  
67          @Override
68          public int length() {
69              return value.length();
70          }
71  
72      }
73  
74      static SimilarityInput<Object> build(final Class<?> fixtureClass, final String value) {
75          if (value == null) {
76              return null;
77          }
78          try {
79              // Use the Class' constructor with a single String.
80              return SimilarityInput.input((Object) fixtureClass.getConstructor(String.class).newInstance(value));
81          } catch (final ReflectiveOperationException e) {
82              throw new AssertionError(e);
83          }
84      }
85  
86      static Stream<Class<?>> similarityInputs() {
87          return Stream.of(SimilarityInputTest.SimilarityInputFixture.class, String.class, StringBuilder.class, StringBuffer.class, TextStringBuilder.class);
88      }
89  
90      static Stream<Class<?>> similarityInputsEquals() {
91          return Stream.of(SimilarityInputTest.SimilarityInputFixture.class, String.class, TextStringBuilder.class);
92      }
93  
94      @Test
95      void testInput() throws Exception {
96          final SimilarityInput<Character> input = SimilarityInput.input("a");
97          assertEquals(1, input.length());
98          assertThrows(IllegalArgumentException.class, () -> SimilarityInput.input(new Object()));
99      }
100 
101 }