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.grammar.java21;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
24  
25  import java.io.File;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractTreeTestSupport;
30  import com.puppycrawl.tools.checkstyle.JavaParser;
31  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32  
33  public class Java21AstRegressionTest extends AbstractTreeTestSupport {
34  
35      @Override
36      public String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/grammar/java21";
38      }
39  
40      @Test
41      public void testUnnamedVariableBasic() throws Exception {
42          verifyAst(
43                  getPath(
44                          "ExpectedUnnamedVariableBasic.txt"),
45                  getNonCompilablePath(
46                          "InputUnnamedVariableBasic.java"));
47      }
48  
49      @Test
50      public void testUnnamedVariableSwitch() throws Exception {
51          verifyAst(
52                  getPath(
53                          "ExpectedUnnamedVariableSwitch.txt"),
54                  getNonCompilablePath(
55                          "InputUnnamedVariableSwitch.java"));
56      }
57  
58      @Test
59      public void testTextBlockConsecutiveEscapes() throws Exception {
60          verifyAst(
61                  getPath(
62                          "ExpectedTextBlockConsecutiveEscapes.txt"),
63                  getPath(
64                          "InputTextBlockConsecutiveEscapes.java"));
65      }
66  
67      /**
68       * Unusual test case, but important to prevent regressions. This fixture
69       * places two text blocks back-to-back with no operator between them,
70       * which is not a legal expression. We need to make sure the parser
71       * correctly rejects this rather than misinterpreting the second text
72       * block as part of the first.
73       *
74       * @throws Exception if an error occurs
75       */
76      @Test
77      public void testTextBlockParsingFail() throws Exception {
78          final File file =
79                  new File(getPath("InputTextBlockParsingFail.java.fail"));
80  
81          final Throwable throwable =
82                  getExpectedThrowable(CheckstyleException.class,
83                          () -> JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS),
84                          "Exception should be thrown due to parsing failure."
85                  );
86  
87          assertWithMessage("Cause of CheckstyleException should be IllegalStateException.")
88              .that(throwable.getCause())
89              .isInstanceOf(IllegalStateException.class);
90  
91          assertWithMessage("Message of IllegalStateException should contain the parsing failure.")
92              .that(throwable.getCause().getMessage())
93              .contains("12:12: mismatched input '\"\"\"' expecting ';'");
94  
95      }
96  
97  }