KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jdepend > framework > DependencyConstraint


1 package jdepend.framework;
2
3 import java.util.*;
4
5 import jdepend.framework.JavaPackage;
6
7 /**
8  * The <code>DependencyConstraint</code> class is a constraint that tests
9  * whether two package-dependency graphs are equivalent.
10  * <p>
11  * This class is useful for writing package dependency assertions (e.g. JUnit).
12  * For example, the following JUnit test will ensure that the 'ejb' and 'web'
13  * packages only depend upon the 'util' package, and no others:
14  * <p>
15  * <blockquote>
16  *
17  * <pre>
18  *
19  * public void testDependencyConstraint() {
20  *
21  * JDepend jdepend = new JDepend();
22  * jdepend.addDirectory(&quot;/path/to/classes&quot;);
23  * Collection analyzedPackages = jdepend.analyze();
24  *
25  * DependencyConstraint constraint = new DependencyConstraint();
26  *
27  * JavaPackage ejb = constraint.addPackage(&quot;com.xyz.ejb&quot;);
28  * JavaPackage web = constraint.addPackage(&quot;com.xyz.web&quot;);
29  * JavaPackage util = constraint.addPackage(&quot;com.xyz.util&quot;);
30  *
31  * ejb.dependsUpon(util);
32  * web.dependsUpon(util);
33  *
34  * assertEquals(&quot;Dependency mismatch&quot;, true, constraint
35  * .match(analyzedPackages));
36  * }
37  * </pre>
38  *
39  * </blockquote>
40  * </p>
41  *
42  * @author <b>Mike Clark</b>
43  * @author Clarkware Consulting, Inc.
44  */

45
46 public class DependencyConstraint {
47
48     private HashMap packages;
49
50     public DependencyConstraint() {
51         packages = new HashMap();
52     }
53
54     public JavaPackage addPackage(String JavaDoc packageName) {
55         JavaPackage jPackage = (JavaPackage) packages.get(packageName);
56         if (jPackage == null) {
57             jPackage = new JavaPackage(packageName);
58             addPackage(jPackage);
59         }
60         return jPackage;
61     }
62
63     public void addPackage(JavaPackage jPackage) {
64         if (!packages.containsValue(jPackage)) {
65             packages.put(jPackage.getName(), jPackage);
66         }
67     }
68
69     public Collection getPackages() {
70         return packages.values();
71     }
72
73     /**
74      * Indicates whether the specified packages match the
75      * packages in this constraint.
76      *
77      * @return <code>true</code> if the packages match this constraint
78      */

79     public boolean match(Collection expectedPackages) {
80
81         if (packages.size() == expectedPackages.size()) {
82             
83             for (Iterator i = expectedPackages.iterator(); i.hasNext();) {
84                 Object JavaDoc next = i.next();
85                 if (next instanceof JavaPackage) {
86                     JavaPackage nextPackage = (JavaPackage) next;
87                     if (!matchPackage(nextPackage)) {
88                         return false;
89                     }
90                 } else {
91                     break;
92                 }
93
94                 return true;
95             }
96         }
97
98         return false;
99     }
100
101     private boolean matchPackage(JavaPackage expectedPackage) {
102
103         JavaPackage actualPackage = (JavaPackage) packages.get(expectedPackage
104                 .getName());
105
106         if (actualPackage != null) {
107             if (equalsDependencies(actualPackage, expectedPackage)) {
108                 return true;
109             }
110         }
111
112         return false;
113     }
114
115     private boolean equalsDependencies(JavaPackage a, JavaPackage b) {
116         return equalsAfferents(a, b) && equalsEfferents(a, b);
117     }
118
119     private boolean equalsAfferents(JavaPackage a, JavaPackage b) {
120
121         if (a.equals(b)) {
122
123             Collection otherAfferents = b.getAfferents();
124
125             if (a.getAfferents().size() == otherAfferents.size()) {
126                 for (Iterator i = a.getAfferents().iterator(); i.hasNext();) {
127                     JavaPackage afferent = (JavaPackage)i.next();
128                     if (!otherAfferents.contains(afferent)) {
129                         return false;
130                     }
131                 }
132
133                 return true;
134             }
135         }
136
137         return false;
138     }
139
140     private boolean equalsEfferents(JavaPackage a, JavaPackage b) {
141
142         if (a.equals(b)) {
143
144             Collection otherEfferents = b.getEfferents();
145
146             if (a.getEfferents().size() == otherEfferents.size()) {
147                 for (Iterator i = a.getEfferents().iterator(); i.hasNext();) {
148                     JavaPackage efferent = (JavaPackage)i.next();
149                     if (!otherEfferents.contains(efferent)) {
150                         return false;
151                     }
152                 }
153
154                 return true;
155             }
156         }
157
158         return false;
159     }
160 }
Popular Tags