View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.CommonUtils;
32  
33  /**
34   * Tests to ensure that default message bundle is determined correctly.
35   *
36   * @author lkuehne
37   */
38  public class AbstractViolationReporterTest {
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 testCustomMessage() throws Exception {
76          final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
77          config.addMessage("msgKey", "This is a custom message.");
78          emptyCheck.configure(config);
79  
80          emptyCheck.log(0, "msgKey");
81  
82          final SortedSet<LocalizedMessage> messages = emptyCheck.getMessages();
83  
84          assertEquals("Amount of messages differs from expected",
85                  1, messages.size());
86          assertEquals("Message differs from expected",
87                  "This is a custom message.", messages.first()
88                  .getMessage());
89      }
90  
91      @Test
92      public void testCustomMessageWithParameters() throws Exception {
93          final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
94          config.addMessage("msgKey", "This is a custom message with {0}.");
95          emptyCheck.configure(config);
96  
97          emptyCheck.log(0, "msgKey", "TestParam");
98          final SortedSet<LocalizedMessage> messages = emptyCheck.getMessages();
99  
100         assertEquals("Amount of messages differs from expected",
101                 1, messages.size());
102 
103         assertEquals("Message differs from expected",
104                 "This is a custom message with TestParam.",
105                 messages.first().getMessage());
106     }
107 
108     @Test
109     public void testCustomMessageWithParametersNegative() throws Exception {
110         final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
111         config.addMessage("msgKey", "This is a custom message {0.");
112         emptyCheck.configure(config);
113 
114         try {
115             emptyCheck.log(0, "msgKey", "TestParam");
116             fail("exception expected");
117         }
118         catch (IllegalArgumentException ex) {
119             assertEquals("Error message is unexpected",
120                     "Unmatched braces in the pattern.", ex.getMessage());
121         }
122     }
123 
124     private static class EmptyCheck extends AbstractCheck {
125         @Override
126         public int[] getDefaultTokens() {
127             return CommonUtils.EMPTY_INT_ARRAY;
128         }
129 
130         @Override
131         public int[] getAcceptableTokens() {
132             return CommonUtils.EMPTY_INT_ARRAY;
133         }
134 
135         @Override
136         public int[] getRequiredTokens() {
137             return CommonUtils.EMPTY_INT_ARRAY;
138         }
139     }
140 }