001: package jdepend.framework;
002:
003: import java.io.IOException;
004: import java.text.NumberFormat;
005:
006: /**
007: * @author <b>Mike Clark</b>
008: * @author Clarkware Consulting, Inc.
009: */
010:
011: public class MetricTest extends JDependTestCase {
012:
013: private JDepend jdepend;
014: private static NumberFormat formatter;
015:
016: static {
017: formatter = NumberFormat.getInstance();
018: formatter.setMaximumFractionDigits(2);
019: }
020:
021: public MetricTest(String name) {
022: super (name);
023: }
024:
025: protected void setUp() {
026: super .setUp();
027:
028: PackageFilter filter = new PackageFilter();
029: filter.addPackage("java.*");
030: filter.addPackage("javax.*");
031:
032: jdepend = new JDepend(filter);
033: jdepend.analyzeInnerClasses(false);
034: }
035:
036: protected void tearDown() {
037: super .tearDown();
038: }
039:
040: public void testAnalyzeClassFiles() throws IOException {
041: jdepend.addDirectory(getBuildDir());
042: assertAnalyzePackages();
043: }
044:
045: private void assertAnalyzePackages() {
046:
047: assertEquals(39, jdepend.countClasses());
048:
049: PackageFilter filter = jdepend.getFilter();
050: filter.addPackage("junit.*");
051:
052: jdepend.analyze();
053:
054: assertFrameworkPackage();
055: assertTextUIPackage();
056: assertSwingUIPackage();
057: assertXmlUIPackage();
058: }
059:
060: private void assertFrameworkPackage() {
061:
062: JavaPackage p = jdepend.getPackage("jdepend.framework");
063: assertNotNull(p);
064:
065: assertEquals(25, p.getConcreteClassCount());
066: assertEquals(4, p.getAbstractClassCount());
067: assertEquals(3, p.afferentCoupling());
068: assertEquals(0, p.efferentCoupling());
069: assertEquals(format(0.14f), format(p.abstractness()));
070: assertEquals(format(0.0f), format(p.instability()));
071: assertEquals(format(0.86f), format(p.distance()));
072: assertEquals(1, p.getVolatility());
073: }
074:
075: private void assertTextUIPackage() {
076:
077: JavaPackage p = jdepend.getPackage("jdepend.textui");
078: assertNotNull(p);
079:
080: assertEquals(1, p.getConcreteClassCount());
081: assertEquals(0, p.getAbstractClassCount());
082: assertEquals(1, p.efferentCoupling());
083: assertEquals("0", format(p.abstractness()));
084: assertEquals(1, p.afferentCoupling());
085: assertEquals(format(0.5f), format(p.instability()));
086: assertEquals(format(0.5f), format(p.distance()));
087: assertEquals(1, p.getVolatility());
088: }
089:
090: private void assertSwingUIPackage() {
091:
092: JavaPackage p = jdepend.getPackage("jdepend.swingui");
093: assertNotNull(p);
094:
095: assertEquals(7, p.getConcreteClassCount());
096: assertEquals(1, p.getAbstractClassCount());
097: assertEquals(0, p.afferentCoupling());
098: assertEquals(1, p.efferentCoupling());
099: assertEquals(format(0.12f), format(p.abstractness()));
100: assertEquals("1", format(p.instability()));
101: assertEquals(format(0.12f), format(p.distance()));
102: assertEquals(1, p.getVolatility());
103: }
104:
105: private void assertXmlUIPackage() {
106:
107: JavaPackage p = jdepend.getPackage("jdepend.xmlui");
108: assertNotNull(p);
109:
110: assertEquals(1, p.getConcreteClassCount());
111: assertEquals(0, p.getAbstractClassCount());
112: assertEquals(0, p.afferentCoupling());
113: assertEquals(2, p.efferentCoupling());
114: assertEquals(format(0.0f), format(p.abstractness()));
115: assertEquals("1", format(p.instability()));
116: assertEquals(format(0.0f), format(p.distance()));
117: assertEquals(1, p.getVolatility());
118: }
119:
120: public void testConfiguredVolatility() throws IOException {
121:
122: jdepend.addDirectory(getBuildDir());
123:
124: JavaPackage pkg = new JavaPackage("jdepend.swingui");
125: pkg.setVolatility(0);
126:
127: jdepend.addPackage(pkg);
128:
129: jdepend.analyze();
130:
131: JavaPackage analyzedPkg = jdepend.getPackage(pkg.getName());
132: assertEquals(0, analyzedPkg.getVolatility());
133: assertEquals(format(0.0f), format(analyzedPkg.distance()));
134: assertEquals(7, analyzedPkg.getConcreteClassCount());
135: }
136:
137: private String format(float f) {
138: return formatter.format(f);
139: }
140: }
|