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

org.coderu.adapters.junit.CoderuAssert Maven / Gradle / Ivy

There is a newer version: 1.2.12
Show newest version
package org.coderu.adapters.junit;

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

import java.util.Collection;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
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.junit.jupiter.api.Assertions;

import com.google.common.base.Function;


public final class CoderuAssert {

    public void assertNoViolations(
        final Collection unit, 
        final Collection package2test, 
        final Collection package2excude){

        assertNoViolations(unit, package2test, package2excude, Depth.UNLIMITED, DependencyOnSameLevelAllowed.YES);
    }

    public void assertNoViolations(
        final Collection unit, 
        final Collection package2test, 
        final Collection package2excude,
        final Depth depth,
        final DependencyOnSameLevelAllowed classDep){
        
        final Collection notAllowedDependencies = 
            dependencyExplorer.calculate(
                unit, package2test, package2excude, depth, classDep);

        assertEmpty(notAllowedDependencies);
    }

    public CoderuAssert(final WrongDependencyExplorer dependencyExplorer) {
        this.dependencyExplorer = dependencyExplorer;
    }

    private final WrongDependencyExplorer dependencyExplorer;

    private static void assertEmpty(final Collection notAllowedDependencies) {

        final Collection strings = 
            transform(notAllowedDependencies, ACCESS_RIGHT_2_STRING);

        Assertions.assertEquals("not allowed dependencies exist","", 
            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());
        }
    };

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy