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.grammar.java21;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static org.junit.Assert.assertThrows;
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      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/grammar/java21";
38      }
39  
40      @Test
41      public void testUnnamedVariableBasic() throws Exception {
42          verifyAst(
43                  getNonCompilablePath(
44                          "ExpectedUnnamedVariableBasic.txt"),
45                  getNonCompilablePath(
46                          "InputUnnamedVariableBasic.java"));
47      }
48  
49      @Test
50      public void testUnnamedVariableSwitch() throws Exception {
51          verifyAst(
52                  getNonCompilablePath(
53                          "ExpectedUnnamedVariableSwitch.txt"),
54                  getNonCompilablePath(
55                          "InputUnnamedVariableSwitch.java"));
56      }
57  
58      @Test
59      public void testTextBlockConsecutiveEscapes() throws Exception {
60          verifyAst(
61                  getNonCompilablePath(
62                          "ExpectedTextBlockConsecutiveEscapes.txt"),
63                  getNonCompilablePath(
64                          "InputTextBlockConsecutiveEscapes.java"));
65      }
66  
67      /**
68       * Unusual test case, but important to prevent regressions. We need to
69       * make sure that we only consume legal escapes in text blocks, and
70       * don't unintentionally parse something that should be an
71       * escape as regular text.
72       *
73       * @throws Exception if an error occurs
74       */
75      @Test
76      public void testTextBlockParsingFail() throws Exception {
77          final File file =
78                  new File(getNonCompilablePath("InputTextBlockParsingFail.java.fail"));
79  
80          final Throwable throwable =
81                  assertThrows("Exception should be thrown due to parsing failure.",
82                          CheckstyleException.class,
83                          () -> JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)
84                  );
85  
86          final String incorrectThrowableCauseMessage =
87                  "Cause of CheckstyleException should be IllegalStateException.";
88  
89          assertWithMessage(incorrectThrowableCauseMessage)
90                  .that(throwable.getCause())
91                  .isInstanceOf(IllegalStateException.class);
92  
93          final String incorrectParsingFailureMessage =
94                  "Message of IllegalStateException should contain the parsing failure.";
95  
96          assertWithMessage(incorrectParsingFailureMessage)
97                  .that(throwable.getCause().getMessage())
98                  .contains("13:14: mismatched input '}\\n"
99                          + "            ' expecting TEXT_BLOCK_LITERAL_END");
100 
101     }
102 }