View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.Paths;
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      protected 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/'
70       * as a root location.
71       *
72       * @param filename file name.
73       * @return canonical path for the file name.
74       * @throws IOException if I/O exception occurs while forming the path.
75       */
76      protected final String getPath(String filename) throws IOException {
77          return new File("src/" + getResourceLocation() + "/resources/" + getPackageLocation() + "/"
78                  + filename).getCanonicalPath();
79      }
80  
81      /**
82       * Returns the path for resources for the given file name.
83       *
84       * @param filename name of the file.
85       * @return the path for resources for the given file based on its package location.
86       */
87      protected final String getResourcePath(String filename) {
88          return "/" + getPackageLocation() + "/" + filename;
89      }
90  
91      /**
92       * Reads the contents of a file.
93       *
94       * @param filename the name of the file whose contents are to be read
95       * @return contents of the file with all {@code \r\n} replaced by {@code \n}
96       * @throws IOException if I/O exception occurs while reading
97       * @noinspection RedundantThrows
98       * @noinspectionreason RedundantThrows - false positive
99       */
100     protected static String readFile(String filename) throws IOException {
101         return toLfLineEnding(Files.readString(Paths.get(filename)));
102     }
103 
104     /**
105      * Join given strings with {@link #EOL} delimiter and add EOL at the end.
106      *
107      * @param strings strings to join
108      * @return joined strings
109      */
110     public static String addEndOfLine(String... strings) {
111         return Stream.of(strings).collect(Collectors.joining(EOL, "", EOL));
112     }
113 
114     /**
115      * Returns a string containing "\r\n" converted to "\n" and "\\r\\n" converted to "\\n"
116      * by replacing with empty string.
117      *
118      * @param text the text string.
119      * @return the converted text string.
120      */
121     protected static String toLfLineEnding(String text) {
122         return text.replaceAll(CR_FOLLOWED_BY_LF_REGEX, "");
123     }
124 
125 }