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