如何在 junit 中使用 jdepend 的 JavaPackage.containsCycle() 来检测我的包是否处于包周期中?
我想通过jdepend在junit中测试我的包tree
是否在包循环上(即具有直接循环依赖)。目前,它不是(请参阅下面的输出,并且 jdepend 的 Eclipse 插件未找到 tree
的循环)。但是 jdepend 手册中建议的下面的断言失败了:
// setup....
JavaPackage p = jdepend.getPackage("tree");
System.out.println(p.getName() + "'s efferent packages: ");
for (Object jp : p.getEfferents()) {
System.out.println(((JavaPackage) jp).getName());
}
assertThat(p.containsCycle(), is(false));
输出是:
tree's efferent packages:
java.util
java.lang
java.lang.reflect
java.util.logging
java.io
org.hamcrest
断言失败的原因是 containsCycle()
递归调用 getEfferents(),将生成的包放入列表中并返回 < code>true 如果某个包已经在其中。因此,containsCycle()
检查是否存在可达包循环(即树是否具有间接循环依赖)。
但是我如何才能只测试直接循环依赖关系,即树是否在包循环上(而不是,它会导致一些其他包循环,例如在 org.hamcrest 中)?
I want to test in junit via jdepend whether my package tree
is on a package cycle (i.e. has a direct cyclic dependency). Currently, it isn't (see output below, and jdepend's Eclipse plugin does not find a cycle for tree
). But the assertion below, suggested in jdepend's manual, fails:
// setup....
JavaPackage p = jdepend.getPackage("tree");
System.out.println(p.getName() + "'s efferent packages: ");
for (Object jp : p.getEfferents()) {
System.out.println(((JavaPackage) jp).getName());
}
assertThat(p.containsCycle(), is(false));
The output is:
tree's efferent packages:
java.util
java.lang
java.lang.reflect
java.util.logging
java.io
org.hamcrest
The reason that the assertion fails is that containsCycle()
recursively calls getEfferents(), puts the resulting packages in a list and returns true
if some package is already in it. So containsCycle()
checks whether there is a reachable package cycle (i.e. whether tree has an indirect cyclic dependency).
But how can I test only direct cyclic dependencies, i.e. whether tree is on a package cycle (and not, that it leads to some other package cycle, e.g. in org.hamcrest)?
以下解决方案通常不起作用,但作为一种解决方法:
您不检查树是否具有直接包依赖性,而是通过 p.containsCycle( 检查间接包依赖性(即树是否导致包循环)) ),但仅在以下代码之后:
这样,树所依赖的包(请参阅问题中列出的输出)但本身具有循环的包将被预先过滤掉。
缺点是,如果导入新包,您可能必须将包添加到过滤器中。并且您必须对不同的 jdepend-tests 进行不同的设置,因为您不想使用该过滤器来计算其他指标,例如与主序列的距离 D。最后但并非最不重要的一点是,如果 tree -> yourFilteredPackage 是直接循环的一部分,不会被检测到:(
The following solution doesn't work in general, but as a workaround:
Instead of checking whether tree has a direct package dependency, you do check indirect package dependency (i.e. whether tree leads to a package cycle) via
p.containsCycle()
, but only after the following code:That way, the packages that tree depends upon (see output listed in the question), but that have cycles themselves, are filtered out beforehand.
The downside is that you might have to add packages to the filter if you import new ones. And you have to do different setups for different jdepend-tests, because you do not want to use that filter to compute other metrics, such as the distance from the main sequence, D. Last but not least, if tree -> yourFilteredPackage is part of the direct cycle, it won't get detected :(