KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jdepend > framework > ExampleTest


1 package jdepend.framework;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import junit.framework.TestCase;
9
10 /**
11  * The <code>ExampleTest</code> is an example <code>TestCase</code>
12  * that demonstrates tests for measuring the distance from the
13  * main sequence (D), package dependency constraints, and the
14  * existence of cyclic package dependencies.
15  * <p>
16  * This test analyzes the JDepend class files.
17  *
18  * @author <b>Mike Clark</b>
19  * @author Clarkware Consulting, Inc.
20  */

21
22 public class ExampleTest extends TestCase {
23
24     private JDepend jdepend;
25
26     public String JavaDoc jdependHomeDirectory;
27
28     public ExampleTest(String JavaDoc name) {
29         super(name);
30     }
31
32     protected void setUp() throws IOException JavaDoc {
33
34         jdependHomeDirectory = System.getProperty("jdepend.home");
35         if (jdependHomeDirectory == null) {
36             fail("Property 'jdepend.home' not defined");
37         }
38
39         PackageFilter filter = new PackageFilter();
40         filter.addPackage("java.*");
41         filter.addPackage("javax.*");
42         jdepend = new JDepend(filter);
43
44         String JavaDoc classesDir =
45             jdependHomeDirectory + File.separator + "build";
46
47         jdepend.addDirectory(classesDir);
48     }
49
50     /**
51      * Tests the conformance of a single package to a distance
52      * from the main sequence (D) within a tolerance.
53      */

54     public void testOnePackageDistance() {
55
56         double ideal = 0.0;
57         double tolerance = 0.8;
58
59         jdepend.analyze();
60
61         JavaPackage p = jdepend.getPackage("jdepend.framework");
62
63         assertEquals("Distance exceeded: " + p.getName(),
64                      ideal, p.distance(), tolerance);
65     }
66
67     /**
68      * Tests that a single package does not contain any
69      * package dependency cycles.
70      */

71     public void testOnePackageHasNoCycles() {
72
73         jdepend.analyze();
74
75         JavaPackage p = jdepend.getPackage("jdepend.framework");
76
77         assertEquals("Cycles exist: " + p.getName(),
78                      false, p.containsCycle());
79     }
80
81     /**
82      * Tests the conformance of all analyzed packages to a
83      * distance from the main sequence (D) within a tolerance.
84      */

85     public void testAllPackagesDistance() {
86
87         double ideal = 0.0;
88         double tolerance = 1.0;
89
90         Collection JavaDoc packages = jdepend.analyze();
91
92         for (Iterator JavaDoc iter = packages.iterator(); iter.hasNext();) {
93             JavaPackage p = (JavaPackage)iter.next();
94             assertEquals("Distance exceeded: " + p.getName(),
95                          ideal, p.distance(), tolerance);
96         }
97     }
98
99     /**
100      * Tests that a package dependency cycle does not exist
101      * for any of the analyzed packages.
102      */

103     public void testAllPackagesHaveNoCycles() {
104
105         Collection JavaDoc packages = jdepend.analyze();
106
107         assertEquals("Cycles exist", false, jdepend.containsCycles());
108     }
109
110     /**
111      * Tests that a package dependency constraint is matched
112      * for the analyzed packages.
113      * <p>
114      * Fails if any package dependency other than those declared
115      * in the dependency constraints are detected.
116      */

117     public void testDependencyConstraint() {
118
119         DependencyConstraint constraint = new DependencyConstraint();
120
121         JavaPackage junitframework = constraint.addPackage("junit.framework");
122         JavaPackage junitui = constraint.addPackage("junit.textui");
123         JavaPackage framework = constraint.addPackage("jdepend.framework");
124         JavaPackage text = constraint.addPackage("jdepend.textui");
125         JavaPackage xml = constraint.addPackage("jdepend.xmlui");
126         JavaPackage swing = constraint.addPackage("jdepend.swingui");
127
128         framework.dependsUpon(junitframework);
129         framework.dependsUpon(junitui);
130         text.dependsUpon(framework);
131         xml.dependsUpon(text);
132         swing.dependsUpon(framework);
133
134         jdepend.analyze();
135
136         assertEquals("Constraint mismatch",
137                      true, jdepend.dependencyMatch(constraint));
138     }
139
140     public static void main(String JavaDoc[] args) {
141         junit.textui.TestRunner.run(ExampleTest.class);
142     }
143 }
Popular Tags