All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.coderu.plugins.maven.CoderuMojo Maven / Gradle / Ivy

package org.coderu.plugins.maven;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import static org.coderu.common.utils.CollectionUtils.transform;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.coderu.common.utils.CollectionUtils;
import org.coderu.core.api.Aliases;
import org.coderu.core.api.ClazzDependency;
import org.coderu.core.api.CompileUnit;
import org.coderu.core.api.DependencyOnSameLevelAllowed;
import org.coderu.core.api.Depth;
import org.coderu.core.api.Packagge;
import org.coderu.core.api.WrongDependencyExplorer;
import org.coderu.core.api.aliases.Api;
import org.coderu.core.api.aliases.Common;
import org.coderu.core.api.aliases.Factory;
import org.coderu.core.api.aliases.Impl;
import org.coderu.core.factory.WrongDependencyExplorerFactory;
import org.coderu.plugins.maven.utils.CoderuConfiguration;
import org.slf4j.impl.StaticLoggerBinder;

import com.google.common.base.Function;

/**
 * Runs CODERU test
 * 
 * @requiresDependencyResolution test
 * @goal test
 * @phase test
 */
public class CoderuMojo extends AbstractMojo {

    /**
     * Location of classes.
     * 
     * @parameter default-value="${project.build.outputDirectory}"
     * @required
     */
    private String classPath;

    /**
     * @parameter
     */
    private Collection classPaths;

    /**
     * Root package.
     * 
     * @parameter
     */
    private String rootPackage;

    /**
     * Root packages.
     * 
     * @parameter
     */
    private List rootPackages;

    /**
     * excluded packages.
     * 
     * @parameter
     */
    private List excludedPackages;

    /**
     * Depth of the check .
     * 
     * @parameter
     */
    private int depth;

    /**
     * Public alias.
     * 
     * @parameter alias="public" expression="api"
     */
    private String publicAlias;

    /**
     * Private alias.
     * 
     * @parameter alias="private" expression="impl"
     */
    private String privateAlias;

    /**
     * Common alias.
     * 
     * @parameter alias="common" expression="common"
     */
    private String commonAlias;

    /**
     * Factory alias.
     * 
     * @parameter alias="factory" expression="factory"
     */
    private String factoryAlias;
    /**
     * The Classes in the package are allowed to reference one other in the same package.
     * 
     * @parameter default-value="true"
     */
    private boolean classesDependencyInPackageAllowed;
    

    public void setClassPath(final String classPath) {
        this.classPath = classPath;
    }

    public void setClassPaths(final Collection classPaths) {
        this.classPaths = classPaths;
    }

    public void setclassesDependencyInPackageAllowed(final boolean classesDependencyInPackageAllowed) {
        this.classesDependencyInPackageAllowed = classesDependencyInPackageAllowed;
    }

    public void setRootPackage(final String rootPackage) {
        this.rootPackage = rootPackage;
    }

    public void setRootPackages(final List rootPackages) {
        this.rootPackages = rootPackages;
    }

    public void setExcludedPackages(final List excludedPackages) {
        this.excludedPackages = excludedPackages;
    }

    public void setDepth(final int depth) {
        this.depth = depth;
    }

    public void setCommonAlias(final String commonAlias) {
		this.commonAlias = commonAlias;
	}
    
    public void setPrivateAlias(final String privateAlias) {
		this.privateAlias = privateAlias;
	}
    
    public void setPublicAlias(final String publicAlias) {
		this.publicAlias = publicAlias;
	}
    
    public void setFactoryAlias(final String factoryAlias) {
		this.factoryAlias = factoryAlias;
	}
    
    public CoderuMojo() {
        this.classesDependencyInPackageAllowed=true;
        this.commonAlias = Common.DEFAULT.getValue();
        this.publicAlias = Api.DEFAULT.getValue();
        this.privateAlias = Impl.DEFAULT.getValue();
        this.factoryAlias = Factory.DEFAULT.getValue();
    }

    @Override
	public void execute() throws MojoExecutionException {


        StaticLoggerBinder.getSingleton().setLog(getLog());
        
        final CoderuConfiguration config = createCoderuConfiguration();

        final WrongDependencyExplorer dependencyExplorer = 
        		WrongDependencyExplorerFactory.create(config.getAliases());

        final Collection wrongDependencies = dependencyExplorer
                .calculate(config.getCompileUnit(), config.getComponents(),
                        config.getExcluded(), config.getCalculatedDepth(),
                        config.getDependencyOnSameLevelAllowed());

        checkEmpty(wrongDependencies);

    }

    CoderuConfiguration createCoderuConfiguration() {

        final Collection roots = calculateRootPackages();
        final Collection excludedPack = calculateExcluded();
        final Collection paths = calculateClassPaths();

        final Collection compileUnit = CollectionUtils.transform(
                paths, CompileUnit.FROM_STRING);
        final Collection components = CollectionUtils.transform(
                roots, Packagge.FROM_STRING);
        final Collection excluded = CollectionUtils.transform(
                excludedPack, Packagge.FROM_STRING);
        final Depth calculatedDepth = (depth > 0) ? new Depth(depth)
                : Depth.UNLIMITED;

        final Aliases aliases = 
        		new Aliases(
        				new Api(publicAlias), 
        				new Common(commonAlias), 
        				new Factory(factoryAlias), 
        				new Impl(privateAlias));
        
        return new CoderuConfiguration(compileUnit, components, excluded,
                calculatedDepth,DependencyOnSameLevelAllowed.getBy(classesDependencyInPackageAllowed), aliases);
    }

    private Collection calculateExcluded() {
        return excludedPackages == null ? Collections. emptyList()
                : excludedPackages;
    }

    private Collection calculateRootPackages() {
        final Collection packages = new ArrayList();

        if (!StringUtils.isEmpty(rootPackage)) {
            packages.add(rootPackage);
        }
        if (rootPackages != null) {
            packages.addAll(rootPackages);
        }
        if (packages.isEmpty()) {
            packages.add("");
        }
        return packages;
    }

    private Collection calculateClassPaths() {
        final Collection result = new ArrayList();
        if (!StringUtils.isEmpty(this.classPath)) {
            result.add(classPath);
        }
        if (classPaths != null) {
            result.addAll(this.classPaths);
        }
        return result;
    }

    private static void checkEmpty(
            final Collection wrongDependencies)
            throws CoderuRuleViolationException {
        if (!wrongDependencies.isEmpty()) {
            final Collection strings = transform(wrongDependencies,
                    ACCESS_RIGHT_2_STRING);
            throw new CoderuRuleViolationException(IOUtils.LINE_SEPARATOR_UNIX
                    + "not allowed dependencies:" + IOUtils.LINE_SEPARATOR_UNIX
                    + StringUtils.join(strings, IOUtils.LINE_SEPARATOR_UNIX));
        }
    }

    private static final Function ACCESS_RIGHT_2_STRING = 
            new Function() {
        @Override
		public String apply(final ClazzDependency o) {
            return String.format("%s => %s", o.getFirst().getName(), o
                    .getSecond().getName());
        }
    };

    @SuppressWarnings("serial")
    private static final class CoderuRuleViolationException extends
            MojoExecutionException {

        public CoderuRuleViolationException(final String message) {
            super(message);
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy