From 4f2db10b1958c9650c82ff35f369e6a0ea76e7a1 Mon Sep 17 00:00:00 2001
From: tom
- * This class is useful for writing package dependency assertions (e.g. JUnit).
- * For example, the following JUnit test will ensure that the 'ejb' and 'web'
- * packages only depend upon the 'util' package, and no others:
- *
- * DependencyConstraint
class is a constraint that tests
- * whether two package-dependency graphs are equivalent.
+ * This class has been replaced by DependencyPrescription.
*
- *
- *
- *
- *
- * public void testDependencyConstraint() {
- *
- * JDepend jdepend = new JDepend();
- * jdepend.addDirectory("/path/to/classes");
- * Collection analyzedPackages = jdepend.analyze();
- *
- * DependencyConstraint constraint = new DependencyConstraint();
- *
- * JavaPackage ejb = constraint.addPackage("com.xyz.ejb");
- * JavaPackage web = constraint.addPackage("com.xyz.web");
- * JavaPackage util = constraint.addPackage("com.xyz.util");
- *
- * ejb.dependsUpon(util);
- * web.dependsUpon(util);
- *
- * assertEquals("Dependency mismatch", true, constraint
- * .match(analyzedPackages));
- * }
- *
- *
- *
true
if the packages match this constraint
- */
- public boolean match(Collection expectedPackages) {
-
- if (packages.size() == expectedPackages.size()) {
-
- for (Iterator i = expectedPackages.iterator(); i.hasNext();) {
- Object next = i.next();
- if (next instanceof JavaPackage) {
- JavaPackage nextPackage = (JavaPackage) next;
- if (!matchPackage(nextPackage)) {
- return false;
- }
- } else {
- break;
- }
-
- return true;
- }
- }
-
- return false;
- }
-
- private boolean matchPackage(JavaPackage expectedPackage) {
-
- JavaPackage actualPackage = (JavaPackage) packages.get(expectedPackage
- .getName());
-
- if (actualPackage != null) {
- if (equalsDependencies(actualPackage, expectedPackage)) {
- return true;
- }
- }
-
- return false;
+ public CollectionDependencyDirective
represents a set of dependency
+ * instructions to which analysed code can be compared.
+ *
+ * Concrete subclasses implement the comparing functionality.
+ *
+ * @author Tom van den Berge
+ */
+public abstract class DependencyDirective {
+
+ protected Map
+ * This class is useful for writing package dependency assertions (e.g. JUnit).
+ * For example, the following JUnit test will ensure that the 'ejb' and 'web'
+ * packages only depend upon the 'util' package, and no others:
+ *
+ * DependencyPrescription
class is a constraint that tests
+ * whether two package-dependency graphs are equivalent.
+ *
+ *
+ *
+ *
+ *
+ * public void testDependencyConstraint() {
+ *
+ * JDepend jdepend = new JDepend();
+ * jdepend.addDirectory("/path/to/classes");
+ * Collection analyzedPackages = jdepend.analyze();
+ *
+ * DependencyPrescription constraint = new DependencyPrescription();
+ *
+ * JavaPackage ejb = constraint.addPackage("com.xyz.ejb");
+ * JavaPackage web = constraint.addPackage("com.xyz.web");
+ * JavaPackage util = constraint.addPackage("com.xyz.util");
+ *
+ * ejb.dependsUpon(util);
+ * web.dependsUpon(util);
+ *
+ * assertEquals("Dependency mismatch", true, constraint
+ * .followsDirective(analyzedPackages));
+ * }
+ *
+ *
+ *
true
if the packages match this constraint
+ */
+ @Override
+ public boolean followsDirective(CollectionDependencyProscription
defines a set of disallowed package
+ * dependencies. It can be used to verify that a set of packages do not
+ * have any unwanted dependencies.
+ *
+ * @author Tom van den Berge.
+ */
+public class DependencyProscription extends DependencyDirective {
+
+ @Override
+ public boolean followsDirective(CollectionJDepend
class analyzes directories of Java class files
@@ -234,16 +238,28 @@ public boolean containsCycles() {
}
/**
- * Indicates whether the analyzed packages match the specified
- * dependency constraint.
- *
- * @return true
if the packages match the dependency
- * constraint
- */
+ * Indicates whether the analyzed packages match the specified dependency
+ * constraint.
+ *
+ * @return true
if the packages match the dependency constraint
+ * @deprecated use {@link #followsDirective(DependencyDirective)} instead.
+ */
public boolean dependencyMatch(DependencyConstraint constraint) {
return constraint.match(getPackages());
}
+ /**
+ * Indicates whether the analyzed packages follow the specified dependency
+ * directive.
+ *
+ * @param directive
+ * the directive that is verified.
+ * @return true
if the packages follow the directive.
+ */
+ public boolean followsDirective(DependencyDirective directive) {
+ return directive.followsDirective(getPackages());
+ }
+
/**
* Registers the specified parser listener.
*
diff --git a/test/jdepend/framework/ComponentTest.java b/test/jdepend/framework/ComponentTest.java
index f509d4d..8aeaa26 100644
--- a/test/jdepend/framework/ComponentTest.java
+++ b/test/jdepend/framework/ComponentTest.java
@@ -53,13 +53,13 @@ public void testJDependComponents() throws IOException {
private void assertJDependPackage() {
JavaPackage p = jdepend.getPackage("jdepend");
assertEquals("jdepend", p.getName());
- assertEquals(36, p.getConcreteClassCount());
- assertEquals(7, p.getAbstractClassCount());
+ assertEquals(40, p.getConcreteClassCount());
+ assertEquals(8, p.getAbstractClassCount());
assertEquals(0, p.afferentCoupling());
assertEquals(5, p.efferentCoupling());
- assertEquals(format(0.16f), format(p.abstractness()));
+ assertEquals(format(0.17f), format(p.abstractness()));
assertEquals("1", format(p.instability()));
- assertEquals(format(0.16f), format(p.distance()));
+ assertEquals(format(0.17f), format(p.distance()));
assertEquals(1, p.getVolatility());
Collection efferents = p.getEfferents();
diff --git a/test/jdepend/framework/DependencyProscriptionTest.java b/test/jdepend/framework/DependencyProscriptionTest.java
new file mode 100644
index 0000000..6560bd2
--- /dev/null
+++ b/test/jdepend/framework/DependencyProscriptionTest.java
@@ -0,0 +1,52 @@
+package jdepend.framework;
+
+public class DependencyProscriptionTest extends JDependTestCase {
+
+ private JDepend jdepend;
+
+ public DependencyProscriptionTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() {
+ super.setUp();
+ }
+
+ public void testPass() {
+ DependencyDirective proscription = new DependencyProscription();
+
+ JavaPackage expectedA = proscription.addPackage("A");
+ JavaPackage expectedB = proscription.addPackage("B");
+
+ expectedA.dependsUpon(expectedB);
+
+ JavaPackage actualA = new JavaPackage("A");
+ JavaPackage actualB = new JavaPackage("B");
+
+ actualB.dependsUpon(actualA);
+
+ jdepend.addPackage(actualA);
+ jdepend.addPackage(actualB);
+
+ assertEquals(true, jdepend.followsDirective(proscription));
+ }
+
+ public void testFail() {
+ DependencyDirective proscription = new DependencyProscription();
+
+ JavaPackage forbiddenA = proscription.addPackage("A");
+ JavaPackage forbiddenB = proscription.addPackage("B");
+
+ forbiddenA.dependsUpon(forbiddenB);
+
+ JavaPackage actualA = new JavaPackage("A");
+ JavaPackage actualB = new JavaPackage("B");
+
+ actualA.dependsUpon(actualB);
+
+ jdepend.addPackage(actualA);
+ jdepend.addPackage(actualB);
+
+ assertEquals(false, jdepend.followsDirective(proscription));
+ }
+}
diff --git a/test/jdepend/framework/FileManagerTest.java b/test/jdepend/framework/FileManagerTest.java
index dddf00d..b69f208 100755
--- a/test/jdepend/framework/FileManagerTest.java
+++ b/test/jdepend/framework/FileManagerTest.java
@@ -32,7 +32,7 @@ public void testEmptyFileManager() {
public void testBuildDirectory() throws IOException {
fileManager.addDirectory(getBuildDir());
- assertEquals(43, fileManager.extractFiles().size());
+ assertEquals(50, fileManager.extractFiles().size());
}
public void testNonExistentDirectory() {
diff --git a/test/jdepend/framework/MetricTest.java b/test/jdepend/framework/MetricTest.java
index 150789b..47d5260 100755
--- a/test/jdepend/framework/MetricTest.java
+++ b/test/jdepend/framework/MetricTest.java
@@ -44,7 +44,7 @@ public void testAnalyzeClassFiles() throws IOException {
private void assertAnalyzePackages() {
- assertEquals(43, jdepend.countClasses());
+ assertEquals(56, jdepend.countClasses());
PackageFilter filter = jdepend.getFilter();
filter.addPackage("junit.*");
@@ -62,13 +62,13 @@ private void assertFrameworkPackage() {
JavaPackage p = jdepend.getPackage("jdepend.framework");
assertNotNull(p);
- assertEquals(25, p.getConcreteClassCount());
- assertEquals(5, p.getAbstractClassCount());
+ assertEquals(29, p.getConcreteClassCount());
+ assertEquals(6, p.getAbstractClassCount());
assertEquals(3, p.afferentCoupling());
assertEquals(5, p.efferentCoupling());
assertEquals(format(0.17f), format(p.abstractness()));
assertEquals(format(0.62f), format(p.instability()));
- assertEquals(format(0.21f), format(p.distance()));
+ assertEquals(format(0.2f), format(p.distance()));
assertEquals(1, p.getVolatility());
}
From fbeb5b9f340a35e34e3569fe64ceb55bfecffc2d Mon Sep 17 00:00:00 2001
From: Tom van den Berge