1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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.<String>invokeStaticMethod(AbstractViolationReporter.class,
48 "getMessageBundle", "com.mycompany.checks.MyCoolCheck"))
49 .isEqualTo("com.mycompany.checks.messages");
50 }
51
52 @Test
53 public void testGetMessageBundleWithoutPackage() throws Exception {
54 assertWithMessage("violation bundle differs from expected")
55 .that(TestUtil.<String>invokeStaticMethod(AbstractViolationReporter.class,
56 "getMessageBundle", "MyCoolCheck"))
57 .isEqualTo("messages");
58 }
59
60 @Test
61 public void testCustomId() {
62 emptyCheck.setId("MyId");
63 assertWithMessage("Id differs from expected")
64 .that(emptyCheck.getId())
65 .isEqualTo("MyId");
66 }
67
68 @Test
69 public void testSeverity() throws Exception {
70 final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
71 config.addMessage("severity", "error");
72 emptyCheck.configure(config);
73
74 assertWithMessage("Invalid severity level")
75 .that(emptyCheck.getSeverityLevel())
76 .isEqualTo(SeverityLevel.ERROR);
77 assertWithMessage("Invalid severity")
78 .that(emptyCheck.getSeverity())
79 .isEqualTo("error");
80 }
81
82 @Test
83 public void testCustomMessage() throws Exception {
84 final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
85 config.addMessage("msgKey", "This is a custom violation.");
86 emptyCheck.configure(config);
87
88 emptyCheck.log(1, "msgKey");
89
90 final SortedSet<Violation> messages = emptyCheck.getViolations();
91
92 assertWithMessage("Amount of messages differs from expected")
93 .that(messages)
94 .hasSize(1);
95 assertWithMessage("violation differs from expected")
96 .that(messages.first().getViolation())
97 .isEqualTo("This is a custom violation.");
98 }
99
100 @Test
101 public void testCustomMessageWithParameters() throws Exception {
102 final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass());
103 config.addMessage("msgKey", "This is a custom violation with {0}.");
104 emptyCheck.configure(config);
105
106 emptyCheck.log(1, "msgKey", "TestParam");
107 final SortedSet<Violation> messages = emptyCheck.getViolations();
108
109 assertWithMessage("Amount of messages differs from expected")
110 .that(messages)
111 .hasSize(1);
112
113 assertWithMessage("violation differs from expected")
114 .that(messages.first().getViolation())
115 .isEqualTo("This is a custom violation with TestParam.");
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 violation {0.");
122 emptyCheck.configure(config);
123
124 try {
125 emptyCheck.log(1, "msgKey", "TestParam");
126 assertWithMessage("exception expected")
127 .fail();
128 }
129 catch (IllegalArgumentException ex) {
130 assertWithMessage("Error violation is unexpected")
131 .that(ex.getMessage())
132 .isEqualTo("Unmatched braces in the pattern.");
133 }
134 }
135
136 public static class EmptyCheck extends AbstractCheck {
137
138 @Override
139 public int[] getDefaultTokens() {
140 return CommonUtil.EMPTY_INT_ARRAY;
141 }
142
143 @Override
144 public int[] getAcceptableTokens() {
145 return CommonUtil.EMPTY_INT_ARRAY;
146 }
147
148 @Override
149 public int[] getRequiredTokens() {
150 return CommonUtil.EMPTY_INT_ARRAY;
151 }
152
153 }
154
155 }