View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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 com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.utils.CommonUtil.EMPTY_OBJECT_ARRAY;
24  
25  import org.junit.jupiter.api.Test;
26  import org.junitpioneer.jupiter.DefaultLocale;
27  
28  import com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck;
29  import com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationOnSameLineCheck;
30  import nl.jqno.equalsverifier.EqualsVerifier;
31  import nl.jqno.equalsverifier.EqualsVerifierReport;
32  
33  public class ViolationTest {
34  
35      @Test
36      public void testEqualsAndHashCode() {
37          final EqualsVerifierReport ev = EqualsVerifier.forClass(Violation.class)
38                  .usingGetClass().report();
39          assertWithMessage("Error: " + ev.getMessage())
40                  .that(ev.isSuccessful())
41                  .isTrue();
42      }
43  
44      @Test
45      public void testGetSeverityLevel() {
46          final Violation violation = createSampleViolation();
47  
48          assertWithMessage("Invalid severity level")
49              .that(violation.getSeverityLevel())
50              .isEqualTo(SeverityLevel.ERROR);
51      }
52  
53      @Test
54      public void testGetModuleId() {
55          final Violation violation = createSampleViolation();
56  
57          assertWithMessage("Invalid module id")
58              .that(violation.getModuleId())
59              .isEqualTo("module");
60      }
61  
62      @Test
63      public void testGetSourceName() {
64          final Violation violation = createSampleViolation();
65  
66          assertWithMessage("Invalid source name")
67              .that(violation.getSourceName())
68              .isEqualTo("com.puppycrawl.tools.checkstyle.api.Violation");
69      }
70  
71      @DefaultLocale("en")
72      @Test
73      public void testMessageInEnglish() {
74          final Violation violation = createSampleViolation();
75  
76          assertWithMessage("Invalid violation")
77              .that(violation.getViolation())
78              .isEqualTo("Empty statement.");
79      }
80  
81      @DefaultLocale("fr")
82      @Test
83      public void testGetKey() {
84          final Violation violation = createSampleViolation();
85  
86          assertWithMessage("Invalid violation key")
87              .that(violation.getKey())
88              .isEqualTo("empty.statement");
89      }
90  
91      @Test
92      public void testTokenType() {
93          final Violation violation1 = new Violation(1, 1, TokenTypes.CLASS_DEF,
94                  "messages.properties", "key", null, SeverityLevel.ERROR, null,
95                  getClass(), null);
96          final Violation violation2 = new Violation(1, 1, TokenTypes.OBJBLOCK,
97                  "messages.properties", "key", EMPTY_OBJECT_ARRAY, SeverityLevel.ERROR, null,
98                  getClass(), null);
99  
100         assertWithMessage("Invalid token type")
101             .that(violation1.getTokenType())
102             .isEqualTo(TokenTypes.CLASS_DEF);
103         assertWithMessage("Invalid token type")
104             .that(violation2.getTokenType())
105             .isEqualTo(TokenTypes.OBJBLOCK);
106     }
107 
108     @Test
109     public void testGetColumnCharIndex() {
110         final Violation violation1 = new Violation(1, 1, 123,
111                 TokenTypes.CLASS_DEF, "messages.properties", "key", null, SeverityLevel.ERROR,
112                 null, getClass(), null);
113 
114         assertWithMessage("Invalid column char index")
115             .that(violation1.getColumnCharIndex())
116             .isEqualTo(123);
117     }
118 
119     @Test
120     public void testCompareToWithDifferentModuleId() {
121         final Violation message1 = createSampleViolationWithId("module1");
122         final Violation message2 = createSampleViolationWithId("module2");
123         final Violation messageNull = createSampleViolationWithId(null);
124 
125         assertWithMessage("Invalid comparing result")
126                 .that(message1.compareTo(messageNull) > 0)
127                 .isTrue();
128         assertWithMessage("Invalid comparing result")
129                 .that(messageNull.compareTo(message1) < 0)
130                 .isTrue();
131         assertWithMessage("Invalid comparing result")
132                 .that(message1.compareTo(message2) < 0)
133                 .isTrue();
134     }
135 
136     @Test
137     public void testCompareToWithDifferentClass() {
138         final Violation message1 = createSampleViolationWithClass(AnnotationLocationCheck.class);
139         final Violation message2 = createSampleViolationWithClass(AnnotationOnSameLineCheck.class);
140         final Violation messageNull = createSampleViolationWithClass(null);
141 
142         assertWithMessage("Invalid comparing result")
143                 .that(message1.compareTo(messageNull) > 0)
144                 .isTrue();
145         assertWithMessage("Invalid comparing result")
146                 .that(messageNull.compareTo(message1) < 0)
147                 .isTrue();
148         assertWithMessage("Invalid comparing result")
149                 .that(message1.compareTo(message2) < 0)
150                 .isTrue();
151     }
152 
153     @Test
154     public void testCompareToWithDifferentLines() {
155         final Violation message1 = createSampleViolationWithLine(1);
156         final Violation message1a = createSampleViolationWithLine(1);
157         final Violation message2 = createSampleViolationWithLine(2);
158 
159         assertWithMessage("Invalid comparing result")
160                 .that(message1.compareTo(message2) < 0)
161                 .isTrue();
162         assertWithMessage("Invalid comparing result")
163                 .that(message2.compareTo(message1) > 0)
164                 .isTrue();
165         final int actual = message1.compareTo(message1a);
166         assertWithMessage("Invalid comparing result")
167             .that(actual)
168             .isEqualTo(0);
169     }
170 
171     @Test
172     public void testCompareToWithDifferentColumns() {
173         final Violation message1 = createSampleViolationWithColumn(1);
174         final Violation message1a = createSampleViolationWithColumn(1);
175         final Violation message2 = createSampleViolationWithColumn(2);
176 
177         assertWithMessage("Invalid comparing result")
178                 .that(message1.compareTo(message2) < 0)
179                 .isTrue();
180         assertWithMessage("Invalid comparing result")
181                 .that(message2.compareTo(message1) > 0)
182                 .isTrue();
183         final int actual = message1.compareTo(message1a);
184         assertWithMessage("Invalid comparing result")
185             .that(actual)
186             .isEqualTo(0);
187     }
188 
189     private static Violation createSampleViolation() {
190         return createSampleViolationWithId("module");
191     }
192 
193     private static Violation createSampleViolationWithClass(Class<?> clss) {
194         return new Violation(1, "com.puppycrawl.tools.checkstyle.checks.coding.messages",
195                 "empty.statement", EMPTY_OBJECT_ARRAY, null, clss, null);
196     }
197 
198     private static Violation createSampleViolationWithId(String id) {
199         return new Violation(1, "com.puppycrawl.tools.checkstyle.checks.coding.messages",
200                 "empty.statement", EMPTY_OBJECT_ARRAY, id, Violation.class, null);
201     }
202 
203     private static Violation createSampleViolationWithLine(int line) {
204         return new Violation(line, "com.puppycrawl.tools.checkstyle.checks.coding.messages",
205                 "empty.statement", EMPTY_OBJECT_ARRAY, "module", Violation.class, null);
206     }
207 
208     private static Violation createSampleViolationWithColumn(int column) {
209         return new Violation(1, column,
210                 "com.puppycrawl.tools.checkstyle.checks.coding.messages", "empty.statement",
211                 EMPTY_OBJECT_ARRAY, "module", Violation.class, null);
212     }
213 
214 }