JDepend依赖约束失败

时间:2010-12-24 19:16:00

标签: java jdepend

我正在使用JDepend来分析我的架构并创建结构测试以验证分层架构中的依赖性。这两个相关层是com.domain和com.infrastructure。域具体取决于Infrastructure层。

为什么以下测试失败?

import java.io.IOException;
import jdepend.framework.DependencyConstraint;
import jdepend.framework.JDepend;
import jdepend.framework.JavaPackage;
import junit.framework.TestCase;

public class DependencyTest extends TestCase {

    private JDepend jdepend;

    @Override
    public void setUp() throws IOException {
        jdepend = new JDepend();

        jdepend.addDirectory("build/classes/com");
    }

    public void testDomainDependsOnInfastructure_ShouldBeTrue() {
        DependencyConstraint constraint = new DependencyConstraint();

        JavaPackage domainPackage = constraint.addPackage("com.domain");
        JavaPackage infastructurePackage = constraint.addPackage("com.infrastructure");

        domainPackage.dependsUpon(infastructurePackage);
        jdepend.analyze();

        assertEquals("Domain doesn't depend on Infrastructure layer", true, jdepend.dependencyMatch(constraint));
    }

}

jdepend.analyze()返回相关的包,所以我知道它找到了我的代码。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

想出来。 JDepend的匹配函数检查所有包,包括库。我不得不用我想要的包定制加载它。这是解决我的问题的代码,如果有人遇到这个问题。

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import jdepend.framework.DependencyConstraint;
import jdepend.framework.JDepend;
import jdepend.framework.JavaPackage;
import junit.framework.TestCase;

public class DependencyTest extends TestCase {

    private JDepend jdepend;

    @Override
    public void setUp() throws IOException {
        jdepend = new JDepend();
        jdepend.addDirectory("build/classes/com");
    }

    public void testDomainDependsOnInfastructure_ShouldBeTrue() {
        DependencyConstraint constraint = new DependencyConstraint();

        JavaPackage distribution = constraint.addPackage("com.distribution");
        JavaPackage domainPackage = constraint.addPackage("com.domain");
        JavaPackage infastructurePackage = constraint.addPackage("com.infrastructure");

        distribution.dependsUpon(domainPackage);
        domainPackage.dependsUpon(infastructurePackage);
        jdepend.analyze();

        Collection<JavaPackage> actual = new ArrayList<JavaPackage>();
        actual.add(domainPackage);
        actual.add(distribution);
        actual.add(infastructurePackage);

        assertEquals("Domain doesn't depend on Infrastructure layer", true, constraint.match(actual));
    }

}