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.ClazzDependency;
import org.coderu.core.api.CompileUnit;
import org.coderu.core.api.Depth;
import org.coderu.core.api.Packagge;
import org.coderu.core.api.WrongDependencyExplorer;
import org.coderu.core.factory.WrongDependencyExplorerFactory;
import org.coderu.plugins.maven.utils.CoderuConfiguration;

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;

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

    /**
     * Root packages.
     * 
     * @parameter
     */
    private int depth;

    private final WrongDependencyExplorer dependencyExplorer;

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

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

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

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

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

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

    public CoderuMojo() {
        dependencyExplorer = WrongDependencyExplorerFactory.create();
    }

    public void execute() throws MojoExecutionException {

        final CoderuConfiguration config = createCoderuConfiguration();

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

        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;

        return new CoderuConfiguration(compileUnit, components, excluded,
                calculatedDepth);
    }

    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() {
        public String apply(ClazzDependency o) {
            return String.format("%s => %s", o.getFirst().getName(), o
                    .getSecond().getName());
        }
    };

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

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

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy