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 given string.
40       *
41       * @param pattern the pattern to match.
42       * @param target  the string to match.
43       * @deprecated use assertThat(target).containsPattern(pattern)
44       */
45      @Deprecated
46      public static void assertPatternInTarget(String pattern, String target) {
47          assertThat(isMatching(pattern, target)).as(() -> format("Target does not match string: %s%n%s", pattern, target))
48                          .isTrue();
49      }
50  
51      /**
52       * Asserts a regular expression pattern is not in a string.
53       *
54       * @param pattern the pattern to match.
55       * @param target  the string to match.
56       * @deprecated use assertThat(target).doesNotContainPattern(pattern)
57       */
58      @Deprecated
59      public static void assertPatternNotInTarget(String pattern, String target) {
60          assertThat(isMatching(pattern, target)).as(() -> format("Target matches the pattern: %s%n%s", pattern, target))
61                  .isFalse();
62      }
63  
64      /**
65       * Determines if a regular expression pattern is in a string.
66       *
67       * @param pattern the pattern to match.
68       * @param target  the string to match.
69       * @return {@code true} if a regular expression pattern is in a string.
70       * @deprecated use assertThat(target).matches(pattern)
71       */
72      @Deprecated
73      public static boolean isMatching(final String pattern, final String target) {
74          return Pattern.compile(pattern, Pattern.MULTILINE).matcher(target).find();
75      }
76  
77      /**
78       * Read given file as UTF-8.
79       * @param f File to read from.
80       * @return contents of the file as UTF-8.
81       * @throws IOException in case of I/O-errors.
82       */
83      public static String readFile(File f) throws IOException {
84          return String.join("\n", IOUtils.readLines(Files.newInputStream(f.toPath()), StandardCharsets.UTF_8));
85      }
86  }