View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.util.Locale;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  
30  import org.junit.jupiter.api.BeforeEach;
31  
32  public abstract class AbstractPathTestSupport {
33  
34      // we are using positive lookahead here, to convert \r\n to \n
35      // and \\r\\n to \\n (for parse tree dump files),
36      // by replacing the full match with the empty string
37      private static final String CR_FOLLOWED_BY_LF_REGEX = "(?x)\\\\r(?=\\\\n)|\\r(?=\\n)";
38  
39      private static final String EOL = System.lineSeparator();
40  
41      /**
42       * Returns the exact location for the package where the file is present.
43       *
44       * @return path for the package name for the file.
45       */
46      public abstract String getPackageLocation();
47  
48      /**
49       * Sets the English locale for all tests.
50       * Otherwise, some tests failed in other locales.
51       */
52      @BeforeEach
53      public void setEnglishLocale() {
54          Locale.setDefault(Locale.ENGLISH);
55      }
56  
57      /**
58       * Retrieves the name of the folder location for resources.
59       *
60       * @return The name of the folder.
61       */
62      protected String getResourceLocation() {
63          return "test";
64      }
65  
66      /**
67       * Returns canonical path for the file with the given file name.
68       * The path is formed base on the root location.
69       * This implementation uses 'src/test/resources/' as a root location.
70       *
71       * @param filename file name.
72       * @return canonical path for the file name.
73       * @throws IOException if I/O exception occurs while forming the path.
74       */
75      protected final String getPath(String filename) throws IOException {
76          return new File("src/" + getResourceLocation() + "/resources/" + getPackageLocation() + "/"
77                  + filename).getCanonicalPath();
78      }
79  
80      /**
81       * Returns the path for resources for the given file name.
82       *
83       * @param filename name of the file.
84       * @return the path for resources for the given file based on its package location.
85       */
86      protected final String getResourcePath(String filename) {
87          return "/" + getPackageLocation() + "/" + filename;
88      }
89  
90      /**
91       * Reads the contents of a file.
92       *
93       * @param filename the name of the file whose contents are to be read
94       * @return contents of the file with all {@code \r\n} replaced by {@code \n}
95       * @throws IOException if I/O exception occurs while reading
96       * @noinspection RedundantThrows
97       * @noinspectionreason RedundantThrows - false positive
98       */
99      protected static String readFile(String filename) throws IOException {
100         return toLfLineEnding(Files.readString(Path.of(filename)));
101     }
102 
103     /**
104      * Join given strings with {@link #EOL} delimiter and add EOL at the end.
105      *
106      * @param strings strings to join
107      * @return joined strings
108      */
109     public static String addEndOfLine(String... strings) {
110         return Stream.of(strings).collect(Collectors.joining(EOL, "", EOL));
111     }
112 
113     /**
114      * Returns a string containing "\r\n" converted to "\n" and "\\r\\n" converted to "\\n"
115      * by replacing with empty string.
116      *
117      * @param text the text string.
118      * @return the converted text string.
119      */
120     protected static String toLfLineEnding(String text) {
121         return text.replaceAll(CR_FOLLOWED_BY_LF_REGEX, "");
122     }
123 
124 }