Skip to content

Commit

Permalink
Enable checkstyle and spotless plugins - code reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Feb 20, 2023
1 parent 8a9addf commit f97cc25
Show file tree
Hide file tree
Showing 32 changed files with 1,873 additions and 2,372 deletions.
34 changes: 18 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

<name>Extra Enforcer Rules</name>

<description>
Extra Enforcer Rules. These are extra rules for Apache Maven's Enforcer Plugin.
</description>
<description>Extra Enforcer Rules. These are extra rules for Apache Maven's Enforcer Plugin.</description>
<url>https://www.mojohaus.org/extra-enforcer-rules/</url>

<inceptionYear>2011</inceptionYear>
Expand All @@ -27,13 +25,6 @@
</license>
</licenses>

<scm>
<connection>scm:git:https://github.com/mojohaus/extra-enforcer-rules.git</connection>
<developerConnection>scm:git:ssh://[email protected]/mojohaus/extra-enforcer-rules.git</developerConnection>
<url>https://github.com/mojohaus/extra-enforcer-rules/tree/master</url>
<tag>HEAD</tag>
</scm>

<developers>
<developer>
<name>Stephen Connolly</name>
Expand All @@ -58,6 +49,13 @@
</developer>
</developers>

<scm>
<connection>scm:git:https://github.com/mojohaus/extra-enforcer-rules.git</connection>
<developerConnection>scm:git:ssh://[email protected]/mojohaus/extra-enforcer-rules.git</developerConnection>
<tag>HEAD</tag>
<url>https://github.com/mojohaus/extra-enforcer-rules/tree/master</url>
</scm>

<properties>
<!-- some of plugins requires JDK 11 -->
<recommendedJavaBuildVersion>11</recommendedJavaBuildVersion>
Expand Down Expand Up @@ -159,12 +157,12 @@
</build>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
</plugin>
</plugins>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
</plugin>
</plugins>
</reporting>

<profiles>
Expand Down Expand Up @@ -220,6 +218,10 @@
<jdk>[11,)</jdk>
</activation>

<properties>
<checkstyle.violation.ignore>MagicNumber</checkstyle.violation.ignore>
</properties>

<build>
<!--- newer versions of plugins requires JDK 11 -->
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@
import org.apache.maven.enforcer.rule.api.EnforcerLevel;
import org.apache.maven.enforcer.rule.api.EnforcerRule2;

abstract class AbstractMojoHausEnforcerRule
implements EnforcerRule2
{
abstract class AbstractMojoHausEnforcerRule implements EnforcerRule2 {
private EnforcerLevel level = EnforcerLevel.ERROR;

@Override
public EnforcerLevel getLevel()
{
public EnforcerLevel getLevel() {
return level;
}

public void setLevel( EnforcerLevel level )
{
public void setLevel(EnforcerLevel level) {
this.level = level;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
* @author Mirko Friedenhagen
* @since 1.0-alpha-3
*/
abstract class AbstractRequireRoles<T extends Contributor> extends AbstractMojoHausEnforcerRule
{
abstract class AbstractRequireRoles<T extends Contributor> extends AbstractMojoHausEnforcerRule {
/**
* Specify the required roles as comma separated list.
*/
Expand All @@ -54,51 +53,45 @@ abstract class AbstractRequireRoles<T extends Contributor> extends AbstractMojoH

/**
* Execute the rule.
*
*
* @param helper the helper
* @throws EnforcerRuleException the enforcer rule exception
*/
public void execute( EnforcerRuleHelper helper )
throws EnforcerRuleException
{
MavenProject mavenProject = getMavenProject( helper );
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
MavenProject mavenProject = getMavenProject(helper);

// Trying to prevent side-effects with unmodifiable sets (already got burned)
final Set<String> requiredRolesSet = Collections.unmodifiableSet( getRolesFromString( requiredRoles ) );
final Set<String> rolesFromProject = Collections.unmodifiableSet(getRolesFromProject( mavenProject ));
final Set<String> requiredRolesSet = Collections.unmodifiableSet(getRolesFromString(requiredRoles));
final Set<String> rolesFromProject = Collections.unmodifiableSet(getRolesFromProject(mavenProject));

checkRequiredRoles( requiredRolesSet, rolesFromProject );
checkValidRoles( requiredRolesSet, rolesFromProject );
checkRequiredRoles(requiredRolesSet, rolesFromProject);
checkValidRoles(requiredRolesSet, rolesFromProject);
}

private void checkRequiredRoles( final Set<String> requiredRolesSet, final Set<String> rolesFromProject )
throws EnforcerRuleException
{
private void checkRequiredRoles(final Set<String> requiredRolesSet, final Set<String> rolesFromProject)
throws EnforcerRuleException {
final Set<String> copyOfRequiredRolesSet = new LinkedHashSet<>(requiredRolesSet);
copyOfRequiredRolesSet.removeAll( rolesFromProject );
if ( copyOfRequiredRolesSet.size() > 0 )
{
copyOfRequiredRolesSet.removeAll(rolesFromProject);
if (copyOfRequiredRolesSet.size() > 0) {
final String message =
String.format( "Found no %s representing role(s) '%s'", getRoleName(), copyOfRequiredRolesSet );
throw new EnforcerRuleException( message );
String.format("Found no %s representing role(s) '%s'", getRoleName(), copyOfRequiredRolesSet);
throw new EnforcerRuleException(message);
}
}

private void checkValidRoles( final Set<String> requiredRolesSet, final Set<String> rolesFromProject )
throws EnforcerRuleException
{
private void checkValidRoles(final Set<String> requiredRolesSet, final Set<String> rolesFromProject)
throws EnforcerRuleException {
final Set<String> copyOfRolesFromProject = new LinkedHashSet<>(rolesFromProject);
final Set<String> allowedRoles = getRolesFromString( validRoles );
if ( !allowedRoles.contains( "*" ) )
{
allowedRoles.addAll( requiredRolesSet );
final Set<String> allowedRoles = getRolesFromString(validRoles);
if (!allowedRoles.contains("*")) {
allowedRoles.addAll(requiredRolesSet);

// results in invalid roles
copyOfRolesFromProject.removeAll( allowedRoles );
if ( copyOfRolesFromProject.size() > 0 )
{
final String message = String.format( "Found invalid %s role(s) '%s'", getRoleName(), copyOfRolesFromProject );
throw new EnforcerRuleException( message );
copyOfRolesFromProject.removeAll(allowedRoles);
if (copyOfRolesFromProject.size() > 0) {
final String message =
String.format("Found invalid %s role(s) '%s'", getRoleName(), copyOfRolesFromProject);
throw new EnforcerRuleException(message);
}
}
}
Expand All @@ -109,19 +102,16 @@ private void checkValidRoles( final Set<String> requiredRolesSet, final Set<Stri
* @param mavenProject
* @return roles from POM.
*/
@SuppressWarnings( "unchecked" )
final Set<String> getRolesFromProject( MavenProject mavenProject )
{
@SuppressWarnings("unchecked")
final Set<String> getRolesFromProject(MavenProject mavenProject) {
final Set<String> result = new HashSet<>();
for ( final T roleFromPom : getRoles( mavenProject ) )
{
for (final T roleFromPom : getRoles(mavenProject)) {
List<String> roles = roleFromPom.getRoles();
result.addAll(roles);
}
return result;
}


/**
* Returns the rolename.
*
Expand All @@ -135,26 +125,23 @@ final Set<String> getRolesFromProject( MavenProject mavenProject )
* @param mavenProject
* @return the list of {@link Contributor}s or {@link Developer}s.
*/
protected abstract List<T> getRoles( final MavenProject mavenProject );
protected abstract List<T> getRoles(MavenProject mavenProject);

/**
* Returns the set of required roles from the property.
*
* @param csRoles comma-separated roles to be split
* @return
*/
Set<String> getRolesFromString( final String csRoles )
{
return splitCsvToSet( csRoles );
Set<String> getRolesFromString(final String csRoles) {
return splitCsvToSet(csRoles);
}

static Set<String> splitCsvToSet( final String csv )
{
final String [] splitValues = StringUtils.split( csv, "," );
static Set<String> splitCsvToSet(final String csv) {
final String[] splitValues = StringUtils.split(csv, ",");
final Set<String> result = new HashSet<>();
for ( String value : splitValues )
{
result.add( value.trim() );
for (String value : splitValues) {
result.add(value.trim());
}
return result;
}
Expand All @@ -167,56 +154,46 @@ static Set<String> splitCsvToSet( final String csv )
*
* @throws EnforcerRuleException
*/
MavenProject getMavenProject( EnforcerRuleHelper helper ) throws EnforcerRuleException
{
try
{
return ( MavenProject ) helper.evaluate( "${project}" );
}
catch ( ExpressionEvaluationException eee )
{
throw new EnforcerRuleException( "Unable to get project.", eee );
MavenProject getMavenProject(EnforcerRuleHelper helper) throws EnforcerRuleException {
try {
return (MavenProject) helper.evaluate("${project}");
} catch (ExpressionEvaluationException eee) {
throw new EnforcerRuleException("Unable to get project.", eee);
}
}

// HELPER methods for unittests.
/**
* @param requiredRoles the requiredRoles to set.
*/
void setRequiredRoles( String requiredRoles )
{
void setRequiredRoles(String requiredRoles) {
this.requiredRoles = requiredRoles;
}

void setValidRoles( String validRoles )
{

void setValidRoles(String validRoles) {
this.validRoles = validRoles;
}


//*********************
// *********************

/**
* {@inheritDoc}
*/
public String getCacheId()
{
public String getCacheId() {
return "0";
}

/**
* {@inheritDoc}
*/
public boolean isCacheable()
{
public boolean isCacheable() {
return false;
}

/**
* {@inheritDoc}
*/
public boolean isResultValid( EnforcerRule cachedRule )
{
public boolean isResultValid(EnforcerRule cachedRule) {
return false;
}
}
}
Loading

0 comments on commit f97cc25

Please sign in to comment.