Skip to content

Commit 1f23b5f

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

15 files changed

+1848
-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: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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.ViolationMatcher;
35+
import org.hamcrest.MatcherAssert;
36+
import org.hamcrest.Matchers;
37+
import org.junit.jupiter.api.BeforeEach;
38+
import org.junit.jupiter.api.Disabled;
39+
import org.junit.jupiter.api.Test;
40+
41+
/**
42+
* Test case to check the following files.
43+
* <br/>ValidLambdaAndGenericsAtEndOfLine.java
44+
* <br/>InvalidDiamondsUsage.java
45+
* <br/>ValidDiamondsUsage.java
46+
* <br/>DiamondUsageNotNeeded.java
47+
* <br/>ValidLiteralComparisonCheck.java
48+
*
49+
* @since 0.3
50+
*/
51+
final class CheckstyleValidatorPartFiveTest {
52+
53+
/**
54+
* Test runner.
55+
*/
56+
private CheckstyleValidateFileRunner runner;
57+
58+
@BeforeEach
59+
void setRule() {
60+
this.runner = new CheckstyleValidateFileRunner();
61+
}
62+
63+
/**
64+
* CheckstyleValidator can allow final static fields and overrides
65+
* to have uppercase abbreviations.
66+
*
67+
* @throws Exception In case of error
68+
*/
69+
@Disabled
70+
@Test
71+
void checkLambdaAndGenericsAtEndOfLine() throws Exception {
72+
this.runner.runValidation("ValidLambdaAndGenericsAtEndOfLine.java", true);
73+
}
74+
75+
/**
76+
* CheckstyleValidator can reject non diamond operator usage.
77+
* @throws Exception If error
78+
*/
79+
@Test
80+
void rejectsNonDiamondOperatorUsage() throws Exception {
81+
final String file = "InvalidDiamondsUsage.java";
82+
final String name = "DiamondOperatorCheck";
83+
final String message = "Use diamond operator";
84+
MatcherAssert.assertThat(
85+
"Two diamond violations should be found",
86+
this.runner.runValidation(file, false),
87+
Matchers.hasItems(
88+
new ViolationMatcher(message, file, "19", name),
89+
new ViolationMatcher(message, file, "29", name)
90+
)
91+
);
92+
}
93+
94+
/**
95+
* CheckstyleValidator can allow diamond operator usage.
96+
* @throws Exception If error
97+
*/
98+
@Test
99+
void allowsDiamondOperatorUsage() throws Exception {
100+
this.runner.runValidation("ValidDiamondsUsage.java", true);
101+
}
102+
103+
/**
104+
* CheckstyleValidator allows class name instead of diamond in case
105+
* of return statement.
106+
* @throws Exception If error
107+
*/
108+
@Test
109+
void allowsFullGenericOperatorUsage() throws Exception {
110+
this.runner.runValidation("DiamondUsageNotNeeded.java", true);
111+
}
112+
113+
/**
114+
* CheckstyleValidator can allow usage of string literals on either sides.
115+
* E.g. both {@code txt.equals("contents")}
116+
* and {@code "contents".equals(txt)} are valid.
117+
* @throws Exception If error
118+
*/
119+
@Test
120+
void allowsStringLiteralsOnBothSideInComparisons()
121+
throws Exception {
122+
this.runner.runValidation("ValidLiteralComparisonCheck.java", true);
123+
}
124+
}

0 commit comments

Comments
 (0)