diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/AbstractRequireFiles.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/AbstractRequireFiles.java index da9019f4..63f248d1 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/AbstractRequireFiles.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/AbstractRequireFiles.java @@ -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 failures = new ArrayList<>(); @@ -93,21 +93,36 @@ private void fail(List failures) throws EnforcerRuleException { StringBuilder buf = new StringBuilder(); if (message != null) { - buf.append(message + System.lineSeparator()); + buf.append(message + '\n'); + } + 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 failures) { + for (File file : failures) { + if (file != null) { + return true; + } + } + return false; + } + @Override public String getCacheId() { return Integer.toString(files.hashCode()); diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesDontExist.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesDontExist.java index 539bd457..8e7d1e98 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesDontExist.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesDontExist.java @@ -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"; } } diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesExist.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesExist.java index a34ef90e..069318ec 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesExist.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesExist.java @@ -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"; } /** diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesSize.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesSize.java index f8395080..53ae6fa4 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesSize.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesSize.java @@ -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(); @@ -76,6 +75,11 @@ public String getCacheId() { return null; } + @Override + String perFileMessage(File file) { + return ""; + } + @Override boolean checkFile(File file) { if (file == null) { @@ -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 { @@ -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; } } diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesDontExist.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesDontExist.java index 980d0d50..2611309f 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesDontExist.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesDontExist.java @@ -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. @@ -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); } } @@ -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()); } } @@ -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)); @@ -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); diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesExist.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesExist.java index b8c811d6..f1fd5071 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesExist.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesExist.java @@ -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. @@ -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(); @@ -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 @@ -100,12 +102,12 @@ 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 @@ -113,11 +115,11 @@ 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); diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesSize.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesSize.java index 994075a8..49242166 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesSize.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesSize.java @@ -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; /** @@ -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(); @@ -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()); 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()); } } @@ -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."); } } @@ -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()); } } @@ -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);