View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.testhelpers;
20  
21  import static java.lang.String.format;
22  import static org.assertj.core.api.Assertions.assertThat;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.util.regex.Pattern;
29  import org.apache.commons.io.IOUtils;
30  
31  /**
32   * Utilities to assert text appears or does not appear in text.
33   */
34  public class TextUtils {
35      /** An empty list of strings */
36      public static final String[] EMPTY = {};
37  
38      /**
39       * Asserts a regular expression pattern is in a string.
40       *
41       * @param pattern the pattern to match.
42       * @param target  the string to match.
43       */
44      public static void assertPatternInTarget(String pattern, String target) {
45          assertThat(isMatching(pattern, target)).as(() -> format("Target does not match string: %s%n%s", pattern, target))
46                          .isTrue();
47      }
48  
49      /**
50       * Asserts a regular expression pattern is not in a string.
51       *
52       * @param pattern the pattern to match.
53       * @param target  the string to match.
54       */
55      public static void assertPatternNotInTarget(String pattern, String target) {
56          assertThat(isMatching(pattern, target)).as(() -> format("Target matches the pattern: %s%n%s", pattern, target))
57                  .isFalse();
58      }
59  
60      /**
61       * Determines if a regular expression pattern is in a string.
62       *
63       * @param pattern the pattern to match.
64       * @param target  the string to match.
65       * @return {@code true} if a regular expression pattern is in a string
66       */
67      public static boolean isMatching(final String pattern, final String target) {
68          return Pattern.compile(pattern, Pattern.MULTILINE).matcher(target).find();
69      }
70  
71      /**
72       * Asserts that a string is contained within another string.
73       * @param find The string to find.
74       * @param target The string to search.
75       */
76      public static void assertContains(final String find, final String target) {
77          assertThat(target.contains(find)).as(() -> format("Target does not contain the text: %s%n%s", find, target))
78                  .isTrue();
79      }
80  
81      /**
82       * Asserts that a string is contained exactly a specified number of times within another string.
83       * @param times The number of times to find the string in the target.
84       * @param find The string to find.
85       * @param target The string to search.
86       */
87      public static void assertContainsExactly(int times, String find, String target) {
88          String t = target;
89          for (int i = 0; i < times; i++) {
90              assertThat(t.contains(find)).as(() -> format("Target does not contain %s copies of %s%n%s", times, find, target))
91                      .isTrue();
92              t = t.substring(t.indexOf(find) + find.length());
93          }
94          assertThat(t.contains(find)).as(() -> format("Target contains more than %s copies of %s%n%s", times, find, target))
95                  .isFalse();
96      }
97  
98      /**
99       * Asserts that a string is not contained within another string.
100      * @param find The string to find.
101      * @param target The string to search.
102      */
103     public static void assertNotContains(final String find, final String target) {
104         assertThat(target.contains(find)).as(() -> format("Target contains the text: %s%n%s", find , target))
105                 .isFalse();
106     }
107 
108     /**
109      * Read given file as UTF-8.
110      * @param f File to read from.
111      * @return contents of the file as UTF-8.
112      * @throws IOException in case of I/O-errors.
113      */
114     public static String readFile(File f) throws IOException {
115         return String.join("\n", IOUtils.readLines(Files.newInputStream(f.toPath()), StandardCharsets.UTF_8));
116     }
117 }