001: package jdepend.framework;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Collection;
006: import java.util.Iterator;
007:
008: import junit.framework.TestCase;
009:
010: /**
011: * The <code>ExampleTest</code> is an example <code>TestCase</code>
012: * that demonstrates tests for measuring the distance from the
013: * main sequence (D), package dependency constraints, and the
014: * existence of cyclic package dependencies.
015: * <p>
016: * This test analyzes the JDepend class files.
017: *
018: * @author <b>Mike Clark</b>
019: * @author Clarkware Consulting, Inc.
020: */
021:
022: public class ExampleTest extends TestCase {
023:
024: private JDepend jdepend;
025:
026: public String jdependHomeDirectory;
027:
028: public ExampleTest(String name) {
029: super (name);
030: }
031:
032: protected void setUp() throws IOException {
033:
034: jdependHomeDirectory = System.getProperty("jdepend.home");
035: if (jdependHomeDirectory == null) {
036: fail("Property 'jdepend.home' not defined");
037: }
038:
039: PackageFilter filter = new PackageFilter();
040: filter.addPackage("java.*");
041: filter.addPackage("javax.*");
042: jdepend = new JDepend(filter);
043:
044: String classesDir = jdependHomeDirectory + File.separator
045: + "build";
046:
047: jdepend.addDirectory(classesDir);
048: }
049:
050: /**
051: * Tests the conformance of a single package to a distance
052: * from the main sequence (D) within a tolerance.
053: */
054: public void testOnePackageDistance() {
055:
056: double ideal = 0.0;
057: double tolerance = 0.8;
058:
059: jdepend.analyze();
060:
061: JavaPackage p = jdepend.getPackage("jdepend.framework");
062:
063: assertEquals("Distance exceeded: " + p.getName(), ideal, p
064: .distance(), tolerance);
065: }
066:
067: /**
068: * Tests that a single package does not contain any
069: * package dependency cycles.
070: */
071: public void testOnePackageHasNoCycles() {
072:
073: jdepend.analyze();
074:
075: JavaPackage p = jdepend.getPackage("jdepend.framework");
076:
077: assertEquals("Cycles exist: " + p.getName(), false, p
078: .containsCycle());
079: }
080:
081: /**
082: * Tests the conformance of all analyzed packages to a
083: * distance from the main sequence (D) within a tolerance.
084: */
085: public void testAllPackagesDistance() {
086:
087: double ideal = 0.0;
088: double tolerance = 1.0;
089:
090: Collection packages = jdepend.analyze();
091:
092: for (Iterator iter = packages.iterator(); iter.hasNext();) {
093: JavaPackage p = (JavaPackage) iter.next();
094: assertEquals("Distance exceeded: " + p.getName(), ideal, p
095: .distance(), tolerance);
096: }
097: }
098:
099: /**
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 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
122: .addPackage("junit.framework");
123: JavaPackage junitui = constraint.addPackage("junit.textui");
124: JavaPackage framework = constraint
125: .addPackage("jdepend.framework");
126: JavaPackage text = constraint.addPackage("jdepend.textui");
127: JavaPackage xml = constraint.addPackage("jdepend.xmlui");
128: JavaPackage swing = constraint.addPackage("jdepend.swingui");
129:
130: framework.dependsUpon(junitframework);
131: framework.dependsUpon(junitui);
132: text.dependsUpon(framework);
133: xml.dependsUpon(text);
134: swing.dependsUpon(framework);
135:
136: jdepend.analyze();
137:
138: assertEquals("Constraint mismatch", true, jdepend
139: .dependencyMatch(constraint));
140: }
141:
142: public static void main(String[] args) {
143: junit.textui.TestRunner.run(ExampleTest.class);
144: }
145: }
|