-
Notifications
You must be signed in to change notification settings - Fork 166
Improve enforcer rule error messages #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
StringBuilder buf = new StringBuilder(); | ||
if (message != null) { | ||
buf.append(message + System.lineSeparator()); | ||
buf.append(message + '\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how it will be worked on windows console ....
Can anybody confirm ...
@michael-o @Bukama
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, this isn't right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this method does not print anything on the console. It throws an exception object that should be identical regardless of the platform that creates it. Where this message goes is outside the scope of this class. It might be written to a log file and never show up on a console. It might be suppressed completely.
In 2025 Windows shells behave in the obvious way if you send them a linefeed, so this works just fine across platforms if someone does happen to print this string to a console. Maybe System.lineSeparator was needed in Windows 95. It isn't now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure about cmd on Windows 10+? I don't have a Windows system handy to test but I asked Gemini and it claims:
The Windows Command Prompt (cmd.exe
) shell is typically configured to interpret a single Line Feed character (\n
or ASCII 0x0A) as a full line break, even though the native Windows line-ending convention is Carriage Return followed by Line Feed (\r\n
or ASCII 0x0D 0x0A).
Therefore, when a Java program sends a string containing only \n
characters to the cmd
console (via System.out.print
or System.err.print
):
- The text will display on new lines correctly. The console window handles the single
\n
and translates it for proper display, moving the cursor to the beginning of the next line. - No display issues like "stair-stepping" will occur. The cursor will not get "stuck" at the end of the previous line without returning to the left margin.
The Key Distinction
The behavior depends on where the output is being sent:
Destination | Line Ending Behavior | Result for \n Only |
---|---|---|
Console/Shell (cmd.exe window) |
The console driver interprets the \n and correctly advances the cursor to the start of the next line. |
Correct Multiline Display |
Plain Text File (using redirection like > file.txt ) |
The stream is often treated as raw bytes, and the file system retains the single \n (Unix-style). |
Single Line Feed is preserved. Text editors like Notepad (pre-Windows 10) may show the output as a single, long line or with empty boxes for the missing \r . Modern editors (VS Code, Notepad++, and newer Windows Notepad) will display it correctly. |
In summary, when printing directly to the cmd
window, the Java \n
will result in a clean, new line.
StringBuilder buf = new StringBuilder(); | ||
if (message != null) { | ||
buf.append(message + System.lineSeparator()); | ||
buf.append(message + '\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, this isn't right
Build (
|
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.junit.jupiter.api.Assertions.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the introduction of star imports on purpose? (here and in other test files)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should was be catched by checkstyle ... 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, not intentional. Let me fix it.
@Bukama site is broken for all maven-plugins and Maven 4.x |
assertFalse(f.exists()); | ||
assumeFalse(f.exists()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will change test logic ... at all
When file will be not deleted .. test will be marked as skipped not failed
Is it intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we don't want to test that the JDK's delete method works as expected, and if the code under test isn't even executed then the test has been skipped, not failed. This case inspired me to write https://cafe.elharo.com/testing/assume-vs-assert/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL
Ping |
f.delete(); | ||
|
||
assertFalse(f.exists()); | ||
assumeTrue(f.exists()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get this one. The file is shall not be there (method name says and f.delete
) but you assume it exists?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a mistake. Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need use a system specific line separator ....
This pull request is stale because it has been waiting for feedback for 60 days. Remove the stale label or comment on this PR, or it will be automatically closed in 30 days. |
Uh oh!
There was an error while loading. Please reload this page.