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