Skip to content

Commit f82280d

Browse files
Vladimir.ShapkinVladimir.Shapkin
authored andcommitted
split CheckstyleValidatorTest into several test classes
1 parent ad66e10 commit f82280d

15 files changed

+1853
-1000
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2011-2024 Qulice.com
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met: 1) Redistributions of source code must retain the above
9+
* copyright notice, this list of conditions and the following
10+
* disclaimer. 2) Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following
12+
* disclaimer in the documentation and/or other materials provided
13+
* with the distribution. 3) Neither the name of the Qulice.com nor
14+
* the names of its contributors may be used to endorse or promote
15+
* products derived from this software without specific prior written
16+
* permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20+
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21+
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29+
* OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.qulice.checkstyle;
32+
33+
import com.google.common.base.Joiner;
34+
import com.qulice.checkstyle.test.extensions.FileToUrl;
35+
import com.qulice.checkstyle.test.extensions.RunnerConstant;
36+
import com.qulice.checkstyle.test.extensions.ViolationMatcher;
37+
import com.qulice.spi.Environment;
38+
import com.qulice.spi.Violation;
39+
import java.io.File;
40+
import java.util.Collection;
41+
import org.hamcrest.MatcherAssert;
42+
import org.hamcrest.Matchers;
43+
import org.junit.jupiter.api.BeforeEach;
44+
import org.junit.jupiter.api.Test;
45+
46+
/**
47+
* Test case for license.
48+
*
49+
* @since 0.3
50+
*/
51+
final class CheckstyleValidatorLicenseTest {
52+
53+
/**
54+
* Rule for testing.
55+
*/
56+
private License rule;
57+
58+
@BeforeEach
59+
void setRule() {
60+
this.rule = new License();
61+
}
62+
63+
/**
64+
* CheckstyleValidator can catch checkstyle violations.
65+
* @throws Exception If something wrong happens inside.
66+
*/
67+
@Test
68+
void catchesCheckstyleViolationsInLicense() throws Exception {
69+
final Environment.Mock mock = new Environment.Mock();
70+
final File license = this.rule.savePackageInfo(
71+
new File(mock.basedir(), RunnerConstant.DIRECTORY.value())
72+
).withLines("License-1.", "", "License-2.")
73+
.withEol("\n")
74+
.file();
75+
final String content =
76+
// @checkstyle StringLiteralsConcatenation (4 lines)
77+
"/" + "*\n * License-3.\n *\n * License-2.\n */\n"
78+
+ "package foo;\n"
79+
+ "public class Foo { }\n";
80+
final String name = "Foo.java";
81+
final Environment env = mock.withParam(
82+
RunnerConstant.LICENSE_PROP.value(),
83+
new FileToUrl(license).toUrl()
84+
).withFile(String.format("src/main/java/foo/%s", name), content);
85+
final Collection<Violation> results =
86+
new CheckstyleValidator(env)
87+
.validate(env.files(name));
88+
MatcherAssert.assertThat(
89+
"Header validation is expected",
90+
results,
91+
Matchers.hasItem(
92+
new ViolationMatcher(
93+
"Line does not match expected header line of", name
94+
)
95+
)
96+
);
97+
}
98+
99+
/**
100+
* CheckstyleValidator does not produce errors when last thing
101+
* in file are imports. The only exception that should be thrown is
102+
* qulice ValidationException.
103+
* @throws Exception In case of error.
104+
*/
105+
@Test
106+
void doesNotThrowExceptionIfImportsOnly() throws Exception {
107+
final Environment.Mock mock = new Environment.Mock();
108+
final File license = this.rule.savePackageInfo(
109+
new File(mock.basedir(), RunnerConstant.DIRECTORY.value())
110+
).withLines("License-1.", "", "License-2.")
111+
.withEol("\n")
112+
.file();
113+
final String crlf = "\r\n";
114+
final String content = Joiner.on(crlf).join(
115+
"package com.google;",
116+
crlf,
117+
"import java.util.*;"
118+
);
119+
final String name = "Foo.java";
120+
final Environment env = mock.withParam(
121+
RunnerConstant.LICENSE_PROP.value(),
122+
new FileToUrl(license).toUrl()
123+
).withFile(String.format("src/main/java/foo/%s", name), content);
124+
final Collection<Violation> results =
125+
new CheckstyleValidator(env).validate(env.files(name));
126+
MatcherAssert.assertThat(
127+
"Validation error should exist",
128+
results,
129+
Matchers.not(Matchers.<Violation>empty())
130+
);
131+
}
132+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2011-2024 Qulice.com
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met: 1) Redistributions of source code must retain the above
9+
* copyright notice, this list of conditions and the following
10+
* disclaimer. 2) Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following
12+
* disclaimer in the documentation and/or other materials provided
13+
* with the distribution. 3) Neither the name of the Qulice.com nor
14+
* the names of its contributors may be used to endorse or promote
15+
* products derived from this software without specific prior written
16+
* permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20+
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21+
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29+
* OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.qulice.checkstyle;
32+
33+
import com.qulice.checkstyle.test.extensions.CheckstyleValidateFileRunner;
34+
import com.qulice.checkstyle.test.extensions.CheckstyleValidateRunner;
35+
import com.qulice.checkstyle.test.extensions.ViolationMatcher;
36+
import org.hamcrest.MatcherAssert;
37+
import org.hamcrest.Matchers;
38+
import org.junit.jupiter.api.BeforeEach;
39+
import org.junit.jupiter.api.Disabled;
40+
import org.junit.jupiter.api.Test;
41+
42+
/**
43+
* Test case to check the following files.
44+
* <br/>ValidLambdaAndGenericsAtEndOfLine.java
45+
* <br/>InvalidDiamondsUsage.java
46+
* <br/>ValidDiamondsUsage.java
47+
* <br/>DiamondUsageNotNeeded.java
48+
* <br/>ValidLiteralComparisonCheck.java
49+
*
50+
* @since 0.3
51+
*/
52+
final class CheckstyleValidatorPartFiveTest {
53+
54+
/**
55+
* Test runner.
56+
*/
57+
private CheckstyleValidateRunner runner;
58+
59+
@BeforeEach
60+
void setRule() {
61+
this.runner = new CheckstyleValidateFileRunner();
62+
}
63+
64+
/**
65+
* CheckstyleValidator can allow final static fields and overrides
66+
* to have uppercase abbreviations.
67+
*
68+
* @throws Exception In case of error
69+
*/
70+
@Disabled
71+
@Test
72+
void checkLambdaAndGenericsAtEndOfLine() throws Exception {
73+
this.runner.runValidation("ValidLambdaAndGenericsAtEndOfLine.java", true);
74+
}
75+
76+
/**
77+
* CheckstyleValidator can reject non diamond operator usage.
78+
* @throws Exception If error
79+
*/
80+
@Test
81+
void rejectsNonDiamondOperatorUsage() throws Exception {
82+
final String file = "InvalidDiamondsUsage.java";
83+
final String name = "DiamondOperatorCheck";
84+
final String message = "Use diamond operator";
85+
MatcherAssert.assertThat(
86+
"Two diamond violations should be found",
87+
this.runner.runValidation(file, false),
88+
Matchers.hasItems(
89+
new ViolationMatcher(message, file, "19", name),
90+
new ViolationMatcher(message, file, "29", name)
91+
)
92+
);
93+
}
94+
95+
/**
96+
* CheckstyleValidator can allow diamond operator usage.
97+
* @throws Exception If error
98+
*/
99+
@Test
100+
void allowsDiamondOperatorUsage() throws Exception {
101+
this.runner.runValidation("ValidDiamondsUsage.java", true);
102+
}
103+
104+
/**
105+
* CheckstyleValidator allows class name instead of diamond in case
106+
* of return statement.
107+
* @throws Exception If error
108+
*/
109+
@Test
110+
void allowsFullGenericOperatorUsage() throws Exception {
111+
this.runner.runValidation("DiamondUsageNotNeeded.java", true);
112+
}
113+
114+
/**
115+
* CheckstyleValidator can allow usage of string literals on either sides.
116+
* E.g. both {@code txt.equals("contents")}
117+
* and {@code "contents".equals(txt)} are valid.
118+
* @throws Exception If error
119+
*/
120+
@Test
121+
void allowsStringLiteralsOnBothSideInComparisons()
122+
throws Exception {
123+
this.runner.runValidation("ValidLiteralComparisonCheck.java", true);
124+
}
125+
}

0 commit comments

Comments
 (0)