View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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  
40      private final AbstractCheck emptyCheck = new EmptyCheck();
41  
42      private static Method getGetMessageBundleMethod() throws Exception {
43          final Class<AbstractViolationReporter> abstractViolationReporterClass =
44              AbstractViolationReporter.class;
45          final Method getMessageBundleMethod =
46              abstractViolationReporterClass.getDeclaredMethod("getMessageBundle", String.class);
47          getMessageBundleMethod.setAccessible(true);
48          return getMessageBundleMethod;
49      }
50  
51      protected static DefaultConfiguration createModuleConfig(Class<?> clazz) {
52          return new DefaultConfiguration(clazz.getName());
53      }
54  
55      @Test
56      public void testGetMessageBundleWithPackage() throws Exception {
57          assertEquals("Message bundle differs from expected",
58                  "com.mycompany.checks.messages",
59              getGetMessageBundleMethod().invoke(null, "com.mycompany.checks.MyCoolCheck"));
60      }
61  
62      @Test
63      public void testGetMessageBundleWithoutPackage() throws Exception {
64          assertEquals("Message bundle differs from expected",
65                  "messages",
66              getGetMessageBundleMethod().invoke(null, "MyCoolCheck"));
67      }
68  
69      @Test
70      public void testCustomId() {
71          emptyCheck.setId("MyId");
72          assertEquals("Id differs from expected", "MyId", emptyCheck.getId());
73      }
74  
75      @Test
76      public void testCustomMessage() throws Exception {
77          final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
78          config.addMessage("msgKey", "This is a custom message.");
79          emptyCheck.configure(config);
80  
81          emptyCheck.log(0, "msgKey");
82  
83          final SortedSet<LocalizedMessage> messages = emptyCheck.getMessages();
84  
85          assertEquals("Amount of messages differs from expected",
86                  1, messages.size());
87          assertEquals("Message differs from expected",
88                  "This is a custom message.", messages.first()
89                  .getMessage());
90      }
91  
92      @Test
93      public void testCustomMessageWithParameters() throws Exception {
94          final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
95          config.addMessage("msgKey", "This is a custom message with {0}.");
96          emptyCheck.configure(config);
97  
98          emptyCheck.log(0, "msgKey", "TestParam");
99          final SortedSet<LocalizedMessage> messages = emptyCheck.getMessages();
100 
101         assertEquals("Amount of messages differs from expected",
102                 1, messages.size());
103 
104         assertEquals("Message differs from expected",
105                 "This is a custom message with TestParam.",
106                 messages.first().getMessage());
107     }
108 
109     @Test
110     public void testCustomMessageWithParametersNegative() throws Exception {
111         final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
112         config.addMessage("msgKey", "This is a custom message {0.");
113         emptyCheck.configure(config);
114 
115         try {
116             emptyCheck.log(0, "msgKey", "TestParam");
117             fail("exception expected");
118         }
119         catch (IllegalArgumentException ex) {
120             assertEquals("Error message is unexpected",
121                     "Unmatched braces in the pattern.", ex.getMessage());
122         }
123     }
124 
125     private static class EmptyCheck extends AbstractCheck {
126 
127         @Override
128         public int[] getDefaultTokens() {
129             return CommonUtils.EMPTY_INT_ARRAY;
130         }
131 
132         @Override
133         public int[] getAcceptableTokens() {
134             return CommonUtils.EMPTY_INT_ARRAY;
135         }
136 
137         @Override
138         public int[] getRequiredTokens() {
139             return CommonUtils.EMPTY_INT_ARRAY;
140         }
141 
142     }
143 
144 }