View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.spi;
6   
7   import java.util.Arrays;
8   import java.util.Collections;
9   import java.util.List;
10  import org.hamcrest.MatcherAssert;
11  import org.hamcrest.Matchers;
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * Test case for {@link Violation}.
16   *
17   * @since 0.24
18   */
19  final class ViolationTest {
20  
21      /**
22       * Validator name reused across the cases below.
23       */
24      private static final String VALIDATOR = "checkstyle";
25  
26      /**
27       * Check name reused across the cases below.
28       */
29      private static final String CHECK = "Indent";
30  
31      /**
32       * Path to a sample source file used in equality cases.
33       */
34      private static final String FOO = "/src/main/java/Foo.java";
35  
36      /**
37       * Validation message reused for ordering cases.
38       */
39      private static final String MESSAGE = "msg";
40  
41      @Test
42      void distinguishesViolationsWithSameValidator() {
43          MatcherAssert.assertThat(
44              "two violations differing only by file or line must not tie",
45              new Violation.Default(
46                  ViolationTest.VALIDATOR, "MissingJavadoc",
47                  "/src/main/java/Bar.java", "10", "alpha"
48              ).compareTo(
49                  new Violation.Default(
50                      ViolationTest.VALIDATOR, "MissingJavadoc",
51                      "/src/main/java/Baz.java", "20", "beta"
52                  )
53              ),
54              Matchers.not(Matchers.equalTo(0))
55          );
56      }
57  
58      @Test
59      void ordersViolationsByFileWhenValidatorMatches() {
60          final List<Violation> sorted = Arrays.asList(
61              new Violation.Default(
62                  ViolationTest.VALIDATOR, ViolationTest.CHECK,
63                  "/src/main/java/Beta.java", "1", ViolationTest.MESSAGE
64              ),
65              new Violation.Default(
66                  ViolationTest.VALIDATOR, ViolationTest.CHECK,
67                  "/src/main/java/Alpha.java", "1", ViolationTest.MESSAGE
68              )
69          );
70          Collections.sort(sorted);
71          MatcherAssert.assertThat(
72              "violations sharing a validator must be sorted by file path",
73              sorted.get(0).file(),
74              Matchers.equalTo("/src/main/java/Alpha.java")
75          );
76      }
77  
78      @Test
79      void ordersViolationsByLineWhenFileMatches() {
80          final List<Violation> sorted = Arrays.asList(
81              new Violation.Default(
82                  ViolationTest.VALIDATOR, ViolationTest.CHECK,
83                  ViolationTest.FOO, "42", ViolationTest.MESSAGE
84              ),
85              new Violation.Default(
86                  ViolationTest.VALIDATOR, ViolationTest.CHECK,
87                  ViolationTest.FOO, "5", ViolationTest.MESSAGE
88              )
89          );
90          Collections.sort(sorted);
91          MatcherAssert.assertThat(
92              "violations from the same file must be sorted by line number",
93              sorted.get(0).lines(),
94              Matchers.equalTo("5")
95          );
96      }
97  
98      @Test
99      void comparesEqualWhenAllFieldsMatch() {
100         MatcherAssert.assertThat(
101             "violations with identical fields must compare as equal",
102             new Violation.Default(
103                 "pmd", "UnusedLocal", ViolationTest.FOO, "7", "unused"
104             ).compareTo(
105                 new Violation.Default(
106                     "pmd", "UnusedLocal", ViolationTest.FOO, "7", "unused"
107                 )
108             ),
109             Matchers.equalTo(0)
110         );
111     }
112 
113     @Test
114     void keepsValidatorAsPrimarySortKey() {
115         final List<Violation> sorted = Arrays.asList(
116             new Violation.Default(
117                 "pmd", "X", "/src/main/java/Z.java", "999", "z"
118             ),
119             new Violation.Default(
120                 ViolationTest.VALIDATOR, "Y",
121                 "/src/main/java/A.java", "1", "a"
122             )
123         );
124         Collections.sort(sorted);
125         MatcherAssert.assertThat(
126             "validator name must remain the primary ordering key",
127             sorted.get(0).validator(),
128             Matchers.equalTo(ViolationTest.VALIDATOR)
129         );
130     }
131 }