View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.io.IOException;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.util.List;
28  import java.util.Set;
29  import java.util.stream.Stream;
30  
31  import org.junit.jupiter.api.Assumptions;
32  import org.junit.jupiter.params.ParameterizedTest;
33  import org.junit.jupiter.params.provider.MethodSource;
34  
35  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
36  
37  class IndentationTrailingCommentsVerticalAlignmentTest {
38  
39      private static final String INDENTATION_TEST_FILES_PATH =
40          "com/puppycrawl/tools/checkstyle/checks/indentation/indentation";
41  
42      private static final int TAB_WIDTH = 4;
43  
44      private static final Set<String> ALLOWED_VIOLATION_FILES = Set.of(
45          // reason: checkstyle check: Line gets longer than 100 characters
46          "InputIndentationInvalidLabelIndent.java",
47          "InputIndentationInvalidMethodIndent2.java",
48          "InputIndentationLambda2.java",
49          "InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java",
50          "InputIndentationNewChildren.java",
51          "InputIndentationNewWithForceStrictCondition.java",
52          "InputIndentationStrictCondition.java",
53          "InputIndentationTryResourcesNotStrict.java",
54          "InputIndentationTryResourcesNotStrict1.java",
55          "InputIndentationTryWithResourcesStrict.java",
56          "InputIndentationTryWithResourcesStrict1.java",
57          "InputIndentationValidClassDefIndent.java",
58          "InputIndentationValidClassDefIndent1.java",
59          "InputIndentationCorrectIfAndParameter1.java",
60          "InputIndentationPackageDeclaration3.java"
61      );
62  
63      @MethodSource("indentationTestFiles")
64      @ParameterizedTest
65      public void testTrailingCommentsAlignment(Path testFile) throws IOException {
66          final String fileName = testFile.getFileName().toString();
67          if (ALLOWED_VIOLATION_FILES.contains(fileName)) {
68              Assumptions.assumeTrue(false, "Skipping file: " + fileName);
69          }
70  
71          final List<String> lines = Files.readAllLines(testFile);
72          int expectedStartIndex = -1;
73  
74          for (int idx = 0; idx < lines.size(); idx++) {
75              final String line = lines.get(idx);
76              if (line.trim().startsWith("import ") || line.trim().startsWith("package ")) {
77                  continue;
78              }
79              final int commentStartIndex = line.indexOf("//indent:");
80              if (commentStartIndex > 0) {
81                  final String codePart = line.substring(0, commentStartIndex);
82                  if (!codePart.isBlank()) {
83                      int actualStartIndex =
84                          CommonUtil.lengthExpandedTabs(line, commentStartIndex, TAB_WIDTH);
85  
86                      // for unicode characters having supplementary code points
87                      final long extraWidth = codePart.codePoints().filter(
88                          Character::isSupplementaryCodePoint).count();
89                      actualStartIndex -= Math.toIntExact(extraWidth);
90  
91                      if (expectedStartIndex == -1) {
92                          expectedStartIndex = actualStartIndex;
93                      }
94                      else {
95                          assertWithMessage("Trailing comment alignment mismatch in file: "
96                                  + testFile + " on line " + (idx + 1))
97                                  .that(actualStartIndex)
98                                  .isEqualTo(expectedStartIndex);
99                      }
100                 }
101             }
102         }
103     }
104 
105     private static Stream<Path> indentationTestFiles() {
106         final Path resourcesDir = Path.of("src", "test", "resources");
107         final Path indentationDir = resourcesDir.resolve(INDENTATION_TEST_FILES_PATH);
108         Stream<Path> result;
109         try (Stream<Path> testFiles = Files.walk(indentationDir)) {
110             final List<Path> collected = testFiles
111                 .filter(path -> {
112                     final String fileName = path.getFileName().toString();
113                     return fileName.startsWith("InputIndentation")
114                             && fileName.endsWith(".java");
115                 }).toList();
116             result = collected.stream();
117         }
118         catch (IOException exception) {
119             assertWithMessage("Failed to find indentation test files")
120                     .that(exception)
121                     .isNull();
122             result = Stream.empty();
123         }
124         return result;
125     }
126 }