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.api;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.util.SortedSet;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  /**
33   * Tests to ensure that default message bundle is determined correctly.
34   *
35   */
36  public class AbstractViolationReporterTest {
37  
38      private final AbstractCheck emptyCheck = new EmptyCheck();
39  
40      protected static DefaultConfiguration createModuleConfig(Class<?> clazz) {
41          return new DefaultConfiguration(clazz.getName());
42      }
43  
44      @Test
45      public void testGetMessageBundleWithPackage() throws Exception {
46          assertWithMessage("violation bundle differs from expected")
47                  .that(TestUtil.invokeStaticMethod(AbstractViolationReporter.class,
48                          "getMessageBundle", String.class,
49                          "com.mycompany.checks.MyCoolCheck"))
50                  .isEqualTo("com.mycompany.checks.messages");
51      }
52  
53      @Test
54      public void testGetMessageBundleWithoutPackage() throws Exception {
55          assertWithMessage("violation bundle differs from expected")
56                  .that(TestUtil.invokeStaticMethod(AbstractViolationReporter.class,
57                          "getMessageBundle", String.class, "MyCoolCheck"))
58                  .isEqualTo("messages");
59      }
60  
61      @Test
62      public void testCustomId() {
63          emptyCheck.setId("MyId");
64          assertWithMessage("Id differs from expected")
65                  .that(emptyCheck.getId())
66                  .isEqualTo("MyId");
67      }
68  
69      @Test
70      public void testSeverity() throws Exception {
71          final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
72          config.addMessage("severity", "error");
73          emptyCheck.configure(config);
74  
75          assertWithMessage("Invalid severity level")
76                  .that(emptyCheck.getSeverityLevel())
77                  .isEqualTo(SeverityLevel.ERROR);
78          assertWithMessage("Invalid severity")
79                  .that(emptyCheck.getSeverity())
80                  .isEqualTo("error");
81      }
82  
83      @Test
84      public void testCustomMessage() throws Exception {
85          final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
86          config.addMessage("msgKey", "This is a custom violation.");
87          emptyCheck.configure(config);
88  
89          emptyCheck.log(1, "msgKey");
90  
91          final SortedSet<Violation> messages = emptyCheck.getViolations();
92  
93          assertWithMessage("Amount of messages differs from expected")
94                  .that(messages)
95                  .hasSize(1);
96          assertWithMessage("violation differs from expected")
97                  .that(messages.first().getViolation())
98                  .isEqualTo("This is a custom violation.");
99      }
100 
101     @Test
102     public void testCustomMessageWithParameters() throws Exception {
103         final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
104         config.addMessage("msgKey", "This is a custom violation with {0}.");
105         emptyCheck.configure(config);
106 
107         emptyCheck.log(1, "msgKey", "TestParam");
108         final SortedSet<Violation> messages = emptyCheck.getViolations();
109 
110         assertWithMessage("Amount of messages differs from expected")
111                 .that(messages)
112                 .hasSize(1);
113 
114         assertWithMessage("violation differs from expected")
115                 .that(messages.first().getViolation())
116                 .isEqualTo("This is a custom violation with TestParam.");
117     }
118 
119     @Test
120     public void testCustomMessageWithParametersNegative() throws Exception {
121         final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
122         config.addMessage("msgKey", "This is a custom violation {0.");
123         emptyCheck.configure(config);
124 
125         try {
126             emptyCheck.log(1, "msgKey", "TestParam");
127             assertWithMessage("exception expected")
128                     .fail();
129         }
130         catch (IllegalArgumentException exc) {
131             assertWithMessage("Error violation is unexpected")
132                     .that(exc.getMessage())
133                     .isEqualTo("Unmatched braces in the pattern.");
134         }
135     }
136 
137     public static class EmptyCheck extends AbstractCheck {
138 
139         @Override
140         public int[] getDefaultTokens() {
141             return CommonUtil.EMPTY_INT_ARRAY;
142         }
143 
144         @Override
145         public int[] getAcceptableTokens() {
146             return CommonUtil.EMPTY_INT_ARRAY;
147         }
148 
149         @Override
150         public int[] getRequiredTokens() {
151             return CommonUtil.EMPTY_INT_ARRAY;
152         }
153 
154     }
155 
156 }