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