Skip to content

Commit 587a4bf

Browse files
committed
Fix tests for Windows EOL check
1 parent 6eeeb5a commit 587a4bf

File tree

4 files changed

+100
-51
lines changed

4 files changed

+100
-51
lines changed

qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java

Lines changed: 87 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,19 @@ public void setRule() {
9999
@Test
100100
void catchesCheckstyleViolationsInLicense() throws Exception {
101101
final Environment.Mock mock = new Environment.Mock();
102-
final File license = this.rule.savePackageInfo(
103-
new File(mock.basedir(), CheckstyleValidatorTest.DIRECTORY)
104-
).withLines("License-1.", "", "License-2.")
105-
.withEol("\n")
106-
.file();
107-
final String content =
108-
// @checkstyle StringLiteralsConcatenation (4 lines)
109-
// @checkstyle RegexpSingleline (1 line)
110-
"/" + "**\n * License-3.\n *\n * License-2.\n */\n"
111-
+ "package foo;\n"
112-
+ "public class Foo { }\n";
113-
final String name = "Foo.java";
114-
final Environment env = mock.withParam(
115-
CheckstyleValidatorTest.LICENSE_PROP,
116-
this.toUrl(license)
117-
).withFile(String.format("src/main/java/foo/%s", name), content);
102+
final File license = this.createLicense(
103+
mock, "\n", "License-1.", "", "License-2."
104+
);
105+
final String file = "LicenseViolations.java";
106+
final Environment env = this.configureEnvironment(mock, license, file);
118107
final Collection<Violation> results =
119108
new CheckstyleValidator(env)
120-
.validate(env.files(name));
109+
.validate(env.files(file));
121110
MatcherAssert.assertThat(
122111
results,
123112
Matchers.hasItem(
124113
new ViolationMatcher(
125-
"Line does not match expected header line of", name
114+
"Line does not match expected header line of", file
126115
)
127116
)
128117
);
@@ -450,23 +439,51 @@ void allowsOnlyProperlyOrderedAtClauses() throws Exception {
450439
* @throws Exception If something wrong happens inside
451440
*/
452441
@Test
453-
@Disabled
454-
void passesWindowsEndsOfLineWithoutException() throws Exception {
455-
this.validate("WindowsEol.java", false, "LICENSE found:");
442+
void prohibitWindowsEndsOfLine() throws Exception {
443+
final String file = "WindowsEol.java";
444+
final Collection<Violation> results = this.runValidation(file, false);
445+
final String message = "Lines in file should end with Unix-like end of line";
446+
final String name = "RegexpMultilineCheck";
447+
MatcherAssert.assertThat(
448+
results,
449+
Matchers.contains(
450+
new ViolationMatcher(
451+
"Expected line ending for file is LF(\\n), but CRLF(\\r\\n) is detected",
452+
file, "1", "NewlineAtEndOfFileCheck"
453+
),
454+
new ViolationMatcher(
455+
message, file, "9", name
456+
),
457+
new ViolationMatcher(
458+
message, file, "10", name
459+
)
460+
)
461+
);
456462
}
457463

458464
/**
459465
* Fail validation with Windows-style formatting of the license and
460466
* Linux-style formatting of the sources.
461467
* @throws Exception If something wrong happens inside
462-
* @todo #61:30min This test and passesWindowsEndsOfLineWithoutException
463-
* should be refactored to gather log4j logs and validate that they work
464-
* correctly. (see changes done in #61)
465468
*/
466469
@Test
467-
@Disabled
468470
void testWindowsEndsOfLineWithLinuxSources() throws Exception {
469-
this.runValidation("WindowsEolLinux.java", false);
471+
final String file = "WindowsEolLinux.java";
472+
final Environment.Mock mock = new Environment.Mock();
473+
final File license = this.createLicense(mock, "\r\n", "Hello.", "World.");
474+
final Environment env = this.configureEnvironment(mock, license, file);
475+
final Collection<Violation> results =
476+
new CheckstyleValidator(env).validate(
477+
env.files(file)
478+
);
479+
final String message = "Line does not match expected header line of ' *'";
480+
final String name = "HeaderCheck";
481+
MatcherAssert.assertThat(
482+
results,
483+
Matchers.contains(
484+
new ViolationMatcher(message, file, "3", name)
485+
)
486+
);
470487
}
471488

472489
/**
@@ -830,24 +847,8 @@ private void validate(final String file, final boolean result,
830847
private Collection<Violation> runValidation(final String file,
831848
final boolean passes) throws IOException {
832849
final Environment.Mock mock = new Environment.Mock();
833-
final File license = this.rule.savePackageInfo(
834-
new File(mock.basedir(), CheckstyleValidatorTest.DIRECTORY)
835-
).withLines(CheckstyleValidatorTest.LICENSE)
836-
.withEol("\n").file();
837-
final Environment env = mock.withParam(
838-
CheckstyleValidatorTest.LICENSE_PROP,
839-
this.toUrl(license)
840-
)
841-
.withFile(
842-
String.format("src/main/java/foo/%s", file),
843-
new IoCheckedText(
844-
new TextOf(
845-
new ResourceOf(
846-
new FormattedText("com/qulice/checkstyle/%s", file)
847-
)
848-
)
849-
).asString()
850-
);
850+
final File license = this.createLicense(mock, "\n", CheckstyleValidatorTest.LICENSE);
851+
final Environment env = this.configureEnvironment(mock, license, file);
851852
final Collection<Violation> results =
852853
new CheckstyleValidator(env).validate(
853854
env.files(file)
@@ -866,6 +867,48 @@ private Collection<Violation> runValidation(final String file,
866867
return results;
867868
}
868869

870+
/**
871+
* Returns environment with attached license and file with sources to process with validator.
872+
* @param env Environment mock to configure.
873+
* @param license File with licence content.
874+
* @param file File with sources.
875+
* @return Configured environment mock with license and file.
876+
* @throws IOException In case of error.
877+
*/
878+
private Environment configureEnvironment(final Environment.Mock env, final File license,
879+
final String file) throws IOException {
880+
return env.withParam(
881+
CheckstyleValidatorTest.LICENSE_PROP,
882+
this.toUrl(license)
883+
)
884+
.withFile(
885+
String.format("%s/%s", CheckstyleValidatorTest.DIRECTORY, file),
886+
new IoCheckedText(
887+
new TextOf(
888+
new ResourceOf(
889+
new FormattedText("com/qulice/checkstyle/%s", file)
890+
)
891+
)
892+
).asString()
893+
);
894+
}
895+
896+
/**
897+
* Creates a licence file in provided environment.
898+
* @param env Environment where the licence file will be created.
899+
* @param eol End of line symbol which will be used in license file.
900+
* @param lines Text of license.
901+
* @return File with license content attached to given environment.
902+
* @throws IOException In case of error.
903+
*/
904+
private File createLicense(final Environment env, final String eol, final String... lines)
905+
throws IOException {
906+
return this.rule.savePackageInfo(
907+
new File(env.basedir(), CheckstyleValidatorTest.DIRECTORY)
908+
).withLines(lines)
909+
.withEol(eol).file();
910+
}
911+
869912
/**
870913
* Validation results matcher.
871914
*
@@ -952,7 +995,5 @@ private boolean lineMatches(final Violation item) {
952995
return this.line.isEmpty()
953996
|| !this.line.isEmpty() && item.lines().equals(this.line);
954997
}
955-
956998
}
957-
958999
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* License-3.
3+
*
4+
* License-2.
5+
*/
6+
package foo;
7+
/**
8+
* Simple class.
9+
* @since 1.0
10+
*/
11+
public class LicenseViolations { }

qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
22
* Hello.
3-
*
4-
* World.
53
*/
64
package foo;
75
/**

qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEolLinux.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
2-
* Welcome.
3-
*
4-
* Friend.
2+
* Hello.
3+
* World.
54
*/
65
package foo;
76
/**

0 commit comments

Comments
 (0)