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

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract class AbstractRequireFiles extends AbstractStandardEnforcerRule {
public void execute() throws EnforcerRuleException {

if (!allowNulls && files.isEmpty()) {
throw new EnforcerRuleError("The file list is empty and Null files are disabled.");
throw new EnforcerRuleError("The file list is empty, and null files are disabled.");
}

List<File> failures = new ArrayList<>();
Expand Down Expand Up @@ -93,21 +93,36 @@ private void fail(List<File> failures) throws EnforcerRuleException {

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.

}
if (getErrorMsg() != null && containsNonNull(failures)) {
buf.append(getErrorMsg());
}
buf.append(getErrorMsg());

for (File file : failures) {
if (file != null) {
buf.append(file.getAbsolutePath() + System.lineSeparator());
if (file == null) {
buf.append("A null filename was given and allowNulls is false.");
} else {
buf.append("(an empty filename was given and allowNulls is false)" + System.lineSeparator());
buf.append(perFileMessage(file));
}
}

throw new EnforcerRuleException(buf.toString());
}

String perFileMessage(File file) {
return file.getPath() + "\n";
}

private static boolean containsNonNull(List<File> failures) {
for (File file : failures) {
if (file != null) {
return true;
}
}
return false;
}

@Override
public String getCacheId() {
return Integer.toString(files.hashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ boolean checkFile(File file) {

@Override
String getErrorMsg() {
return "Some files should not exist:" + System.lineSeparator();
return "Some files should not exist:\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ boolean checkFile(File file) {

@Override
String getErrorMsg() {
return "Some required files are missing:" + System.lineSeparator();
return "Some required files are missing:\n";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public RequireFilesSize(MavenProject project) {

@Override
public void execute() throws EnforcerRuleException {

// if the file is already defined, use that. Otherwise get the main artifact.
// if the file is already defined, use that. Otherwise, get the main artifact.
if (getFiles().isEmpty()) {
setFilesList(Collections.singletonList(project.getArtifact().getFile()));
super.execute();
Expand All @@ -76,6 +75,11 @@ public String getCacheId() {
return null;
}

@Override
String perFileMessage(File file) {
return "";
}

@Override
boolean checkFile(File file) {
if (file == null) {
Expand All @@ -87,10 +91,10 @@ boolean checkFile(File file) {
if (file.exists()) {
long length = computeLength(file);
if (length < minsize) {
this.errorMsg = (file + " size (" + length + ") too small. Min. is " + minsize);
this.errorMsg = (file + " size (" + length + ") too small. Minimum is " + minsize + ".");
return false;
} else if (length > maxsize) {
this.errorMsg = (file + " size (" + length + ") too large. Max. is " + maxsize);
this.errorMsg = (file + " size (" + length + ") too large. Maximum is " + maxsize + ".");
return false;
} else {

Expand All @@ -99,14 +103,14 @@ boolean checkFile(File file) {
+ length
+ ") is OK ("
+ (minsize == maxsize || minsize == 0
? ("max. " + maxsize)
? ("maximum " + maxsize)
: ("between " + minsize + " and " + maxsize))
+ " byte).");
+ " bytes).");

return true;
}
} else {
this.errorMsg = (file + " does not exist!");
this.errorMsg = (file + " does not exist.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
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.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

/**
* Test the "require files don't exist" rule.
Expand All @@ -53,30 +55,31 @@ void testFileExists() throws IOException {
rule.execute();
fail("Expected an Exception.");
} catch (EnforcerRuleException e) {
assertNotNull(e.getMessage());
assertEquals("Some files should not exist:\n" + f.getPath() + "\n", e.getMessage());
} finally {
f.delete();
}
f.delete();
}

@Test
void testEmptyFile() {
void testNullFile() {
rule.setFilesList(Collections.singletonList(null));
try {
rule.execute();
fail("Should get exception");
} catch (EnforcerRuleException e) {
assertNotNull(e.getMessage());
assertEquals("A null filename was given and allowNulls is false.", e.getMessage());
}
}

@Test
void testEmptyFileAllowNull() {
void testNullFileAllowed() {
rule.setFilesList(Collections.singletonList(null));
rule.setAllowNulls(true);
try {
rule.execute();
} catch (EnforcerRuleException e) {
fail("Unexpected Exception:" + e.getLocalizedMessage());
fail("Unexpected Exception:" + e.getLocalizedMessage(), e);
}
}

Expand All @@ -88,7 +91,7 @@ void testEmptyFileList() {
rule.execute();
fail("Should get exception");
} catch (EnforcerRuleException e) {
assertNotNull(e.getMessage());
assertEquals("The file list is empty, and null files are disabled.", e.getMessage());
}
}

Expand All @@ -109,7 +112,7 @@ void testFileDoesNotExist() throws EnforcerRuleException, IOException {
File f = File.createTempFile("junit", null, temporaryFolder);
f.delete();

assertFalse(f.exists());
assumeFalse(f.exists());

rule.setFilesList(Collections.singletonList(f));

Expand All @@ -121,11 +124,11 @@ void testFileDoesNotExistSatisfyAny() throws EnforcerRuleException, IOException
File f = File.createTempFile("junit", null, temporaryFolder);
f.delete();

assertFalse(f.exists());
assumeFalse(f.exists());

File g = File.createTempFile("junit", null, temporaryFolder);

assertTrue(g.exists());
assumeTrue(g.exists());

rule.setFilesList(Arrays.asList(f, g.getCanonicalFile()));
rule.setSatisfyAny(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

/**
* Test the "require files exist" rule.
Expand Down Expand Up @@ -62,16 +64,16 @@ void testFileOsIndependentExists() {
}

@Test
void testEmptyFile() {
void testNullFile() {
rule.setFilesList(Collections.singletonList(null));

EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());

assertNotNull(e.getMessage());
assertEquals("A null filename was given and allowNulls is false.", e.getMessage());
}

@Test
void testEmptyFileAllowNull() throws Exception {
void testNullFileAllowNull() throws Exception {
rule.setFilesList(Collections.singletonList(null));
rule.setAllowNulls(true);
rule.execute();
Expand All @@ -84,7 +86,7 @@ void testEmptyFileList() {

EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());

assertNotNull(e.getMessage());
assertEquals("The file list is empty, and null files are disabled.", e.getMessage());
}

@Test
Expand All @@ -100,24 +102,24 @@ void testFileDoesNotExist() throws Exception {
File f = File.createTempFile("junit", null, temporaryFolder);
f.delete();

assertFalse(f.exists());
assumeFalse(f.exists());
rule.setFilesList(Collections.singletonList(f));

EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());

assertNotNull(e.getMessage());
assertEquals("Some required files are missing:\n" + f.getPath() + "\n", e.getMessage());
}

@Test
void testFileExistsSatisfyAny() throws EnforcerRuleException, IOException {
File f = File.createTempFile("junit", null, temporaryFolder);
f.delete();

assertFalse(f.exists());
assumeFalse(f.exists());

File g = File.createTempFile("junit", null, temporaryFolder);

assertTrue(g.exists());
assumeTrue(g.exists());

rule.setFilesList(Arrays.asList(f, g.getCanonicalFile()));
rule.setSatisfyAny(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -83,18 +85,18 @@ void testFileExists() throws EnforcerRuleException, IOException {
}

@Test
void testEmptyFile() {
void testNullFile() {
rule.setFilesList(Collections.singletonList(null));
try {
rule.execute();
fail("Should get exception");
} catch (EnforcerRuleException e) {
assertNotNull(e.getMessage());
assertEquals("A null filename was given and allowNulls is false.", e.getMessage());
}
}

@Test
void testEmptyFileAllowNull() throws EnforcerRuleException {
void testNullFileAllowNull() throws EnforcerRuleException {
rule.setFilesList(Collections.singletonList(null));
rule.setAllowNulls(true);
rule.execute();
Expand Down Expand Up @@ -124,14 +126,14 @@ void testEmptyFileList() throws EnforcerRuleException, IOException {
void testFileDoesNotExist() throws IOException {
File f = File.createTempFile("junit", null, temporaryFolder);
f.delete();
assertFalse(f.exists());
assumeFalse(f.exists());
Comment on lines -127 to +129
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/

rule.setFilesList(Collections.singletonList(f));

try {
rule.execute();
fail("Should get exception");
} catch (EnforcerRuleException e) {
assertNotNull(e.getMessage());
assertEquals(f.getPath() + " does not exist.", e.getMessage());
}
}

Expand All @@ -144,7 +146,7 @@ void testFileTooSmall() throws IOException {
rule.execute();
fail("Should get exception");
} catch (EnforcerRuleException e) {
assertNotNull(e.getMessage());
assertEquals(e.getMessage(), f.getPath() + " size (0) too small. Minimum is 10.");
}
}

Expand All @@ -157,12 +159,12 @@ void testFileTooBig() throws IOException {

rule.setFilesList(Collections.singletonList(f));
rule.setMaxsize(10);
assertTrue(f.length() > 10);
assumeTrue(f.length() > 10);
try {
rule.execute();
fail("Should get exception");
} catch (EnforcerRuleException e) {
assertNotNull(e.getMessage());
assertEquals(f.getPath() + " size (21) too large. Maximum is 10.", e.getMessage());
}
}

Expand All @@ -172,7 +174,7 @@ void testRequireFilesSizeSatisfyAny() throws EnforcerRuleException, IOException
try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
out.write("123456789101112131415");
}
assertTrue(f.length() > 10);
assumeTrue(f.length() > 10);

File g = File.createTempFile("junit", null, temporaryFolder);

Expand Down
Loading