View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import com.qulice.spi.Violation;
8   import org.hamcrest.Description;
9   import org.hamcrest.TypeSafeMatcher;
10  
11  /**
12   * Matcher for {@link Violation} produced by {@link CheckstyleValidator}
13   * in unit tests. Matches by substring of the message, suffix of the file
14   * path and, optionally, exact line number and check name.
15   *
16   * @since 0.1
17   */
18  final class ViolationMatcher extends TypeSafeMatcher<Violation> {
19  
20      /**
21       * Message to check.
22       */
23      private final String message;
24  
25      /**
26       * File to check.
27       */
28      private final String file;
29  
30      /**
31       * Expected line.
32       */
33      private final String line;
34  
35      /**
36       * Check name.
37       */
38      private final String check;
39  
40      /**
41       * Constructor.
42       *
43       * @param message Message to check
44       * @param file File to check
45       */
46      ViolationMatcher(final String message, final String file) {
47          this(message, file, "", "");
48      }
49  
50      /**
51       * Constructor.
52       *
53       * @param message Message to check
54       * @param file File to check
55       * @param line Line to check
56       * @param check Check name
57       */
58      ViolationMatcher(final String message, final String file,
59          final String line, final String check) {
60          super();
61          this.message = message;
62          this.file = file;
63          this.line = line;
64          this.check = check;
65      }
66  
67      @Override
68      public boolean matchesSafely(final Violation item) {
69          return item.message().contains(this.message)
70              && item.file().endsWith(this.file)
71              && this.lineMatches(item)
72              && this.checkMatches(item);
73      }
74  
75      @Override
76      public void describeTo(final Description description) {
77          description.appendText("doesn't match");
78      }
79  
80      private boolean checkMatches(final Violation item) {
81          return this.check.isEmpty()
82              || !this.check.isEmpty() && item.name().equals(this.check);
83      }
84  
85      private boolean lineMatches(final Violation item) {
86          return this.line.isEmpty()
87              || !this.line.isEmpty() && item.lines().equals(this.line);
88      }
89  }