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  package org.apache.commons.text.diff;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.stream.Stream;
25  
26  import org.apache.commons.lang3.ArrayUtils;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.params.ParameterizedTest;
29  import org.junit.jupiter.params.provider.Arguments;
30  import org.junit.jupiter.params.provider.MethodSource;
31  
32  /**
33   * Tests for the ReplacementsFinder.
34   */
35  class ReplacementsFinderTest {
36  
37      // Helper ReplacementsHandler implementation for testing
38      private static final class SimpleHandler implements ReplacementsHandler<Character> {
39          private int skipped;
40          private final List<Character> from;
41          private final List<Character> to;
42  
43          SimpleHandler() {
44              skipped = 0;
45              from = new ArrayList<>();
46              to = new ArrayList<>();
47          }
48  
49          public List<Character> getFrom() {
50              return from;
51          }
52  
53          public int getSkipped() {
54              return skipped;
55          }
56  
57          public List<Character> getTo() {
58              return to;
59          }
60  
61          @Override
62          public void handleReplacement(final int skipped, final List<Character> from, final List<Character> to) {
63              this.skipped += skipped;
64              this.from.addAll(from);
65              this.to.addAll(to);
66          }
67      }
68  
69      public static Stream<Arguments> parameters() {
70          return Stream.of(Arguments.of("branco", "blanco", 1, new Character[] {'r'}, new Character[] {'l'}),
71              Arguments.of("test the blocks before you use it", "try the blocks before you put it", 25,
72                  new Character[] {'e', 's', 't', 's', 'e'}, new Character[] {'r', 'y', 'p', 't'}));
73      }
74  
75      private SimpleHandler handler;
76  
77      @BeforeEach
78      public void setUp() {
79          handler = new SimpleHandler();
80      }
81  
82      @ParameterizedTest
83      @MethodSource("parameters")
84      void testReplacementsHandler(final String left, final String right, final int skipped,
85          final Character[] from, final Character[] to) {
86          final StringsComparator sc = new StringsComparator(left, right);
87          final ReplacementsFinder<Character> replacementFinder = new ReplacementsFinder<>(handler);
88          sc.getScript().visit(replacementFinder);
89          assertEquals(skipped, handler.getSkipped(), "Skipped characters do not match");
90          assertArrayEquals(handler.getFrom().toArray(ArrayUtils.EMPTY_CHARACTER_OBJECT_ARRAY), from,
91              "From characters do not match");
92          assertArrayEquals(to, handler.getTo().toArray(ArrayUtils.EMPTY_CHARACTER_OBJECT_ARRAY),
93              "To characters do not match");
94      }
95  }