Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Jan 9, 2025
1 parent 78dd344 commit d1fa7a2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 40 deletions.
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 @@ -95,19 +95,34 @@ private void fail(List<File> failures) throws EnforcerRuleException {
if (message != null) {
buf.append(message + '\n');
}
buf.append(getErrorMsg());
if (getErrorMsg() != null && containsNonNull(failures)) {
buf.append(getErrorMsg());
}

for (File file : failures) {
if (file != null) {
buf.append(file.getAbsolutePath() + '\n');
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)\n");
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 @@ -57,8 +57,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 @@ -73,6 +72,11 @@ public String getCacheId() {
return null;
}

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

@Override
boolean checkFile(File file) {
if (file == null) {
Expand All @@ -84,10 +88,10 @@ boolean checkFile(File file) {
if (file.exists()) {
long length = file.length();
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 @@ -96,14 +100,14 @@ boolean checkFile(File file) {
+ length
+ ") is OK ("
+ (minsize == maxsize || minsize == 0
? ("max. " + maxsize)
? ("maximum " + maxsize)
: ("between " + minsize + " and " + maxsize))
+ " 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,7 @@
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;

/**
* Test the "require files don't exist" rule.
Expand All @@ -53,24 +50,25 @@ 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 {
Expand All @@ -88,7 +86,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeFalse;

/**
* Test the "require files exist" rule.
Expand Down Expand Up @@ -59,16 +60,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 @@ -81,7 +82,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 @@ -97,20 +98,20 @@ 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@
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.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.Assertions.*;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -80,18 +75,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 @@ -128,7 +123,7 @@ void testFileDoesNotExist() throws IOException {
rule.execute();
fail("Should get exception");
} catch (EnforcerRuleException e) {
assertNotNull(e.getMessage());
assertEquals(f.getPath() + " does not exist.", e.getMessage());
}
}

Expand All @@ -141,7 +136,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 @@ -159,7 +154,7 @@ void testFileTooBig() throws IOException {
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 Down

0 comments on commit d1fa7a2

Please sign in to comment.