Skip to content

Commit

Permalink
[MENFORCER-458] Move RequireFiles* to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jan 10, 2023
1 parent 48f5458 commit 48ca986
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @author Slawomir Jaranowski
* @since 3.2.0
*/
abstract class AbstractStandardEnforcerRule extends AbstractEnforcerRule {
public abstract class AbstractStandardEnforcerRule extends AbstractEnforcerRule {

private String message;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.enforcer;
package org.apache.maven.enforcer.rules.files;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;

/**
* Contains the common code to compare an array of files against a requirement.
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
public abstract class AbstractRequireFiles extends AbstractStandardEnforcerRule {
abstract class AbstractRequireFiles extends AbstractStandardEnforcerRule {

/** Array of files to check. */
private File[] files;
/** List of files to check. */
private List<File> files = Collections.emptyList();

/** if null file handles should be allowed. If they are allowed, it means treat it as a success. */
private boolean allowNulls = false;
Expand All @@ -60,10 +61,10 @@ public abstract class AbstractRequireFiles extends AbstractStandardEnforcerRule
abstract String getErrorMsg();

@Override
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
public void execute() throws EnforcerRuleException {

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

List<File> failures = new ArrayList<>();
Expand All @@ -76,7 +77,7 @@ public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
}

if (satisfyAny) {
int passed = files.length - failures.size();
int passed = files.size() - failures.size();
if (passed == 0) {
fail(failures);
}
Expand Down Expand Up @@ -109,58 +110,31 @@ private void fail(List<File> failures) throws EnforcerRuleException {

@Override
public String getCacheId() {
return Integer.toString(hashCode(files));
return Integer.toString(files.hashCode());
}

/**
* Calculates a hash code for the specified array as <code>Arrays.hashCode()</code> would do. Unfortunately, the
* mentioned method is only available for Java 1.5 and later.
*
* @param items The array for which to compute the hash code, may be <code>null</code>.
* @return The hash code for the array.
*/
private static int hashCode(Object[] items) {
int hash = 0;
if (items != null) {
hash = 1;
for (Object item : items) {
hash = 31 * hash + (item == null ? 0 : item.hashCode());
}
}
return hash;
void setFilesList(List<File> files) {
this.files = files;
}

@Override
public boolean isCacheable() {
return true;
}
// method using for testing purpose ...

@Override
public boolean isResultValid(EnforcerRule cachedRule) {
return true;
}

public File[] getFiles() {
List<File> getFiles() {
return files;
}

public void setFiles(File[] files) {
this.files = files;
}

public boolean isAllowNulls() {
return allowNulls;
}

public void setAllowNulls(boolean allowNulls) {
void setAllowNulls(boolean allowNulls) {
this.allowNulls = allowNulls;
}

public boolean isSatisfyAny() {
return satisfyAny;
void setSatisfyAny(boolean satisfyAny) {
this.satisfyAny = satisfyAny;
}

public void setSatisfyAny(boolean satisfyAny) {
this.satisfyAny = satisfyAny;
@Override
public String toString() {
return String.format(
"%s[message=%s, files=%s, allowNulls=%b, satisfyAny=%b, level=%s]",
getClass().getSimpleName(), getMessage(), files, allowNulls, satisfyAny, getLevel());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.enforcer;
package org.apache.maven.enforcer.rules.files;

import javax.inject.Named;

import java.io.File;

/**
* The Class RequireFilesDontExist.
*/
public class RequireFilesDontExist extends AbstractRequireFiles {
@Named("requireFilesDontExist")
public final class RequireFilesDontExist extends AbstractRequireFiles {
@Override
boolean checkFile(File file) {
// if we get here and the handle is null, treat it as a success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.enforcer;
package org.apache.maven.enforcer.rules.files;

import javax.inject.Named;

import java.io.File;
import java.io.IOException;

/**
* The Class RequireFilesExist.
*/
public class RequireFilesExist extends AbstractRequireFiles {
@Named("requireFilesExist")
public final class RequireFilesExist extends AbstractRequireFiles {
@Override
boolean checkFile(File file) {
// if we get here and the handle is null, treat it as a success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.enforcer;
package org.apache.maven.enforcer.rules.files;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.File;
import java.util.Collections;
import java.util.Objects;

import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;

/**
* Rule to validate the main artifact is within certain size constraints.
*
* @author brianf
* @author Roman Stumm
*/
public class RequireFilesSize extends AbstractRequireFiles {
@Named("requireFilesSize")
public final class RequireFilesSize extends AbstractRequireFiles {

private static final long MAXSIZE = 10000;

Expand All @@ -46,37 +48,29 @@ public class RequireFilesSize extends AbstractRequireFiles {
/** The error msg. */
private String errorMsg;

/** The log. */
private Log log;
private final MavenProject project;

@Inject
public RequireFilesSize(MavenProject project) {
this.project = Objects.requireNonNull(project);
}

@Override
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
this.log = helper.getLog();
public void execute() throws EnforcerRuleException {

// if the file is already defined, use that. Otherwise get the main artifact.
if (getFiles().length == 0) {
try {
MavenProject project = (MavenProject) helper.evaluate("${project}");
setFiles(new File[1]);
getFiles()[0] = project.getArtifact().getFile();

super.execute(helper);
} catch (ExpressionEvaluationException e) {
throw new EnforcerRuleException("Unable to retrieve the project.", e);
}
if (getFiles().isEmpty()) {
setFilesList(Collections.singletonList(project.getArtifact().getFile()));
super.execute();
} else {
super.execute(helper);
super.execute();
}
}

@Override
public boolean isCacheable() {
return false;
}

@Override
public boolean isResultValid(EnforcerRule cachedRule) {
return false;
public String getCacheId() {
// non cached rule - return null
return null;
}

@Override
Expand All @@ -97,7 +91,7 @@ boolean checkFile(File file) {
return false;
} else {

this.log.debug(file
getLog().debug(() -> file
+ " size ("
+ length
+ ") is OK ("
Expand All @@ -119,18 +113,10 @@ String getErrorMsg() {
return this.errorMsg;
}

public long getMaxsize() {
return maxsize;
}

public void setMaxsize(long maxsize) {
this.maxsize = maxsize;
}

public long getMinsize() {
return minsize;
}

public void setMinsize(long minsize) {
this.minsize = minsize;
}
Expand Down
8 changes: 4 additions & 4 deletions enforcer-rules/src/site/apt/requireFilesDontExist.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ Require Files Don't Exist

The following parameters are supported by this rule:

* message - an optional message to the user if the rule fails.
* <<message>> - an optional message to the user if the rule fails.

* files - A list of files to check.
* <<files>> - A list of files to check.

* allowNulls - If null files should be allowed. If allowed, they will be treated as if they do not exist. Default is false.
* <<allowNulls>> - If null files should be allowed. If allowed, they will be treated as if they do not exist. Default is false.

* satisfyAny - Allows that one of files can make the rule pass, instead of all the files. Default is false.
* <<satisfyAny>> - Allows that one of files can make the rule pass, instead of all the files. Default is false.

[]

Expand Down
8 changes: 4 additions & 4 deletions enforcer-rules/src/site/apt/requireFilesExist.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ Require Files Exist

The following parameters are supported by this rule:

* message - an optional message to the user if the rule fails.
* <<message>> - an optional message to the user if the rule fails.

* files - A list of files to check.
* <<files>> - A list of files to check.

* allowNulls - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false.
* <<allowNulls>> - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false.

* satisfyAny - Allows that one of files can make the rule pass, instead of all the files. Default is false.
* <<satisfyAny>> - Allows that one of files can make the rule pass, instead of all the files. Default is false.

[]

Expand Down
12 changes: 6 additions & 6 deletions enforcer-rules/src/site/apt/requireFilesSize.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ Require File Size

The following parameters are supported by this rule:

* message - an optional message to the user if the rule fails.
* <<message>> - an optional message to the user if the rule fails.

* files - A list of files to check. If this list is empty, the main project artifact will be checked.
* <<files>> - A list of files to check. If this list is empty, the main project artifact will be checked.

* maxsize - maximum size in bytes for this file.
* <<maxsize>> - maximum size in bytes for this file.

* minsize - minimum size in bytes for this file.
* <<minsize>> - minimum size in bytes for this file.

* allowNulls - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false.
* <<allowNulls>> - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false.

* satisfyAny - Allows that one of files can make the rule pass, instead of all the files. Default is false.
* <<satisfyAny>> - Allows that one of files can make the rule pass, instead of all the files. Default is false.

[]

Expand Down
Loading

0 comments on commit 48ca986

Please sign in to comment.