Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

elharo
Copy link
Contributor

@elharo elharo commented Jan 9, 2025

  1. Avoid abbreviations
  2. Increase test coverage
  3. Make messages platform independent
  4. Don't double print paths to files
  5. Verify test setup with assumptions rather than asserts

@elharo elharo changed the title Make exception messages platform independent Improve enforcer rule error messages Jan 9, 2025
@elharo elharo marked this pull request as ready for review January 12, 2025 14:16
StringBuilder buf = new StringBuilder();
if (message != null) {
buf.append(message + System.lineSeparator());
buf.append(message + '\n');
Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor

@Bukama Bukama Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

at least jshell handles them the same.

image

but not cmd

Copy link
Contributor Author

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):

  1. 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.
  2. 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');
Copy link
Member

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

@Bukama
Copy link
Contributor

Bukama commented Jan 13, 2025

Build (mvn clean verify site) of this branch breaks (on Windows 10): Please have a look.

[INFO] Reactor Summary for Apache Maven Enforcer 3.6.0-SNAPSHOT:
[INFO]
[INFO] Apache Maven Enforcer ................................................................................ SUCCESS [ 28.782 s]
[INFO] Apache Maven Enforcer API ............................................................................ SUCCESS [  4.314 s]
[INFO] Apache Maven Enforcer Built-In Rules ................................................................. SUCCESS [ 13.093 s]
[INFO] Apache Maven Enforcer Plugin ......................................................................... FAILURE [ 10.733 s]
[INFO] Apache Maven Enforcer Extension ...................................................................... SKIPPED
[INFO] --------------------------------------------------------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] --------------------------------------------------------------------------------------------------------------------------
[INFO] Total time:  57.264 s
[INFO] Finished at: 2025-01-13T17:27:56+01:00
[INFO] --------------------------------------------------------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.12.1:site (default-site) on project maven-enforcer-plugin: Error generating maven-plugin-report-plugin:3.13.1:report report: Cannot invoke "org.codehaus.plexus.configuration.PlexusConfiguration.getChild(String)" because "this.configuration" is null -> [Help 1]
>mvn --version
Apache Maven 4.0.0-rc-2 (273314404f85ec3c089e295d8b4e0cb18c287cf5)
Maven home: C:\apache-maven-4.0.0-rc-2
Java version: 21.0.1, vendor: Eclipse Adoptium, runtime: C:\JDK\Java21
Default locale: de_DE, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"```

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.*;
Copy link
Contributor

@Bukama Bukama Jan 13, 2025

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)

Copy link
Member

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 ... 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

@slawekjaranowski
Copy link
Member

@Bukama site is broken for all maven-plugins and Maven 4.x
https://issues.apache.org/jira/browse/MPLUGIN-502

Comment on lines -119 to +125
assertFalse(f.exists());
assumeFalse(f.exists());
Copy link
Member

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?

Copy link
Contributor Author

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/

Copy link
Contributor Author

@elharo elharo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL

@elharo
Copy link
Contributor Author

elharo commented Feb 20, 2025

Ping

f.delete();

assertFalse(f.exists());
assumeTrue(f.exists());
Copy link
Contributor

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?

Copy link
Contributor Author

@elharo elharo Feb 21, 2025

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

Copy link
Member

@slawekjaranowski slawekjaranowski left a 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 ....

@slawekjaranowski slawekjaranowski added the waiting-for-feedback Waiting for 90 days until issues or pull request will be closed label Jun 7, 2025
Copy link

github-actions bot commented Aug 7, 2025

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.

@github-actions github-actions bot added the Stale label Aug 7, 2025
@elharo elharo removed the Stale label Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting-for-feedback Waiting for 90 days until issues or pull request will be closed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants