org.coderu.adapters.junit.CoderuAssert Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coderu-junit-adapter Show documentation
Show all versions of coderu-junit-adapter Show documentation
Simplifies integration of CODERU as a junit test
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());
}
};
}