1
2
3
4
5 package com.qulice.checkstyle;
6
7 import com.qulice.spi.Environment;
8 import com.qulice.spi.Violation;
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.Collection;
12 import org.cactoos.io.ResourceOf;
13 import org.cactoos.text.FormattedText;
14 import org.cactoos.text.IoCheckedText;
15 import org.cactoos.text.TextOf;
16 import org.hamcrest.MatcherAssert;
17 import org.hamcrest.Matchers;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.params.ParameterizedTest;
20 import org.junit.jupiter.params.provider.ValueSource;
21
22
23
24
25
26
27
28 final class CheckstyleMethodNameTest {
29
30 @Test
31 void acceptsShortEnglishWords() throws Exception {
32 MatcherAssert.assertThat(
33 "short English words should be allowed as method names",
34 CheckstyleMethodNameTest.violations("ShortMethodNames.java"),
35 Matchers.empty()
36 );
37 }
38
39 @Test
40 void rejectsTwoLetterGibberish() throws Exception {
41 final String file = "InvalidMethodName.java";
42 MatcherAssert.assertThat(
43 "two letters that make no word should be reported",
44 CheckstyleMethodNameTest.violations(file),
45 Matchers.hasItem(
46 new ViolationMatcher(
47 "Name 'zz' must match pattern", file, "32", "MethodNameCheck"
48 )
49 )
50 );
51 }
52
53 @ParameterizedTest
54 @ValueSource(
55 strings = {
56 "JnaMappedLibrary.java",
57 "JnaDirectBinding.java",
58 "JnaStdCallBinding.java"
59 }
60 )
61 void acceptsNativeNamesInJnaBinding(final String file) throws Exception {
62 MatcherAssert.assertThat(
63 "native names in a JNA binding should not be reported",
64 CheckstyleMethodNameTest.violations(file),
65 Matchers.not(
66 Matchers.hasItem(
67 new ViolationMatcher("", file, "", "MethodNameCheck")
68 )
69 )
70 );
71 }
72
73 @Test
74 void rejectsNativeNamesOutsideJna() throws Exception {
75 final String file = "FakeLibrary.java";
76 MatcherAssert.assertThat(
77 "a Library that is not JNA's should not be trusted",
78 CheckstyleMethodNameTest.violations(file),
79 Matchers.hasItem(
80 new ViolationMatcher(
81 "Name 'GetLastError' must match pattern",
82 file,
83 "18",
84 "MethodNameCheck"
85 )
86 )
87 );
88 }
89
90 @Test
91 void obeysSuppressionByShortName() throws Exception {
92 MatcherAssert.assertThat(
93 "suppressed name should not be reported",
94 CheckstyleMethodNameTest.violations("SuppressedMethodName.java"),
95 Matchers.empty()
96 );
97 }
98
99 private static Collection<Violation> violations(final String file)
100 throws IOException {
101 final Environment.Mock mock = new Environment.Mock();
102 final Environment env = mock.withParam(
103 "license",
104 String.format(
105 "file:%s",
106 new License().savePackageInfo(
107 new File(mock.basedir(), "src/main/java/foo")
108 ).withLines("Hello.")
109 .withEol(String.valueOf('\n')).file()
110 )
111 ).withFile(
112 String.format("src/main/java/foo/%s", file),
113 new IoCheckedText(
114 new TextOf(
115 new ResourceOf(
116 new FormattedText("com/qulice/checkstyle/%s", file)
117 )
118 )
119 ).asString()
120 );
121 return new CheckstyleValidator(env).validate(env.files(file));
122 }
123 }