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 org.checkstyle.suppressionxpathfilter.coding;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.AvoidDoubleBraceInitializationCheck;
31  
32  public class XpathRegressionAvoidDoubleBraceInitializationTest extends AbstractXpathTestSupport {
33  
34      private static final Class<AvoidDoubleBraceInitializationCheck> CLAZZ =
35          AvoidDoubleBraceInitializationCheck.class;
36  
37      @Override
38      protected String getCheckName() {
39          return CLAZZ.getSimpleName();
40      }
41  
42      @Override
43      protected String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/coding/avoiddoublebraceinitialization";
45      }
46  
47      @Test
48      public void testClassFields() throws Exception {
49          final File fileToProcess = new File(
50              getPath("InputXpathAvoidDoubleBraceInitializationClassFields.java"));
51  
52          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
53  
54          final String[] expectedViolation = {
55              "6:41: " + getCheckMessage(CLAZZ, AvoidDoubleBraceInitializationCheck.MSG_KEY),
56          };
57  
58          final List<String> expectedXpathQueries = Arrays.asList(
59              "/COMPILATION_UNIT/CLASS_DEF"
60                  + "[./IDENT[@text='InputXpathAvoidDoubleBraceInitializationClassFields']]"
61                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='list']]/ASSIGN/EXPR/"
62                  + "LITERAL_NEW[./IDENT[@text='ArrayList']]/OBJBLOCK",
63              "/COMPILATION_UNIT/CLASS_DEF"
64                  + "[./IDENT[@text='InputXpathAvoidDoubleBraceInitializationClassFields']]"
65                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='list']]/ASSIGN/EXPR/"
66                  + "LITERAL_NEW[./IDENT[@text='ArrayList']]/OBJBLOCK/LCURLY"
67          );
68  
69          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
70      }
71  
72      @Test
73      public void testMethodDef() throws Exception {
74          final File fileToProcess = new File(
75              getPath("InputXpathAvoidDoubleBraceInitializationMethodDef.java"));
76  
77          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
78  
79          final String[] expectedViolation = {
80              "7:31: " + getCheckMessage(CLAZZ, AvoidDoubleBraceInitializationCheck.MSG_KEY),
81          };
82  
83          final List<String> expectedXpathQueries = Arrays.asList(
84              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
85                      + "'InputXpathAvoidDoubleBraceInitializationMethodDef']]"
86                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
87                  + "/SLIST/EXPR/LITERAL_NEW[./IDENT[@text='HashSet']]"
88                  + "/OBJBLOCK",
89              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
90                      + "'InputXpathAvoidDoubleBraceInitializationMethodDef']]"
91                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
92                  + "/SLIST/EXPR/LITERAL_NEW[./IDENT[@text='HashSet']]"
93                  + "/OBJBLOCK/LCURLY"
94          );
95  
96          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
97      }
98  
99  }