1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package com.qulice.checkstyle;
32
33 import java.text.MessageFormat;
34 import java.util.regex.Pattern;
35 import java.util.stream.Stream;
36 import org.hamcrest.Matcher;
37 import org.hamcrest.MatcherAssert;
38 import org.hamcrest.Matchers;
39 import org.junit.jupiter.params.ParameterizedTest;
40 import org.junit.jupiter.params.provider.Arguments;
41 import org.junit.jupiter.params.provider.MethodSource;
42
43
44
45
46
47 final class RequiredJavaDocTagTest {
48
49
50
51
52 private final WriterStub writer = new WriterStub();
53
54
55
56
57 private final RequiredJavaDocTag tag = new RequiredJavaDocTag(
58 "since",
59 Pattern.compile(
60 "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
61 ),
62 this.writer
63 );
64
65 @ParameterizedTest
66 @MethodSource("params")
67 void success(final String[] lines, final String reason, final Matcher<String> expected) {
68 this.tag.matchTagFormat(lines, 0, 2);
69 MatcherAssert.assertThat(
70 reason,
71 this.writer.formattedMessage(),
72 expected
73 );
74 }
75
76 private static Stream<Arguments> params() {
77 return Stream.of(
78 Arguments.arguments(
79 new String[] {" *", " * @since 0.3", " *"},
80 "Empty string expected",
81 Matchers.emptyString()
82 ),
83 Arguments.arguments(
84 new String[] {" *", " * @since 0.3", " *"},
85 "Empty string expected",
86 Matchers.emptyString()
87 ),
88 Arguments.arguments(
89 new String[] {" *", " * @sinc 0.3", " *"},
90 "Expected tag not found",
91 Matchers.equalTo("Missing '@since' tag in class/interface comment")
92 ),
93 Arguments.arguments(
94 new String[] {" *", " * @since 0.3.4.4.", " *"},
95 "Regular expression non-match expected",
96 Matchers.equalTo(
97 String.format(
98 "Tag text '0.3.4.4.' does not match the pattern '%s'",
99 "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
100 )
101 )
102 )
103 );
104 }
105
106
107
108
109
110 private static class WriterStub implements RequiredJavaDocTag.Reporter {
111
112
113
114
115 private String message;
116
117
118
119
120 WriterStub() {
121 this.message = "";
122 }
123
124 @Override
125 public void log(final int line, final String msg, final Object... args) {
126 this.message = MessageFormat.format(msg, args);
127 }
128
129 String formattedMessage() {
130 return this.message;
131 }
132 }
133 }