001: package jdepend.framework;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.ArrayList;
006: import java.util.Collection;
007:
008: /**
009: * @author <b>Mike Clark</b>
010: * @author Clarkware Consulting, Inc.
011: */
012:
013: public class ClassFileParserTest extends JDependTestCase {
014:
015: private ClassFileParser parser;
016:
017: public ClassFileParserTest(String name) {
018: super (name);
019: }
020:
021: protected void setUp() {
022: super .setUp();
023: PackageFilter filter = new PackageFilter(new ArrayList());
024: parser = new ClassFileParser(filter);
025: }
026:
027: protected void tearDown() {
028: super .tearDown();
029: }
030:
031: public void testInvalidClassFile() {
032:
033: File f = new File(getTestDir() + getPackageSubDir()
034: + "ExampleTest.java");
035:
036: try {
037:
038: parser.parse(f);
039: fail("Invalid class file: Should raise IOException");
040:
041: } catch (IOException expected) {
042: assertTrue(true);
043: }
044: }
045:
046: public void testInterfaceClass() throws IOException {
047:
048: File f = new File(getBuildDir() + getPackageSubDir()
049: + "ExampleInterface.class");
050:
051: JavaClass clazz = parser.parse(f);
052:
053: assertTrue(clazz.isAbstract());
054:
055: assertEquals("jdepend.framework.ExampleInterface", clazz
056: .getName());
057:
058: assertEquals("ExampleInterface.java", clazz.getSourceFile());
059:
060: Collection imports = clazz.getImportedPackages();
061: assertEquals(6, imports.size());
062:
063: assertTrue(imports.contains(new JavaPackage("java.math")));
064: assertTrue(imports.contains(new JavaPackage("java.text")));
065: assertTrue(imports.contains(new JavaPackage("java.lang")));
066: assertTrue(imports.contains(new JavaPackage("java.io")));
067: assertTrue(imports.contains(new JavaPackage("java.rmi")));
068: assertTrue(imports.contains(new JavaPackage("java.util")));
069: }
070:
071: public void testAbstractClass() throws IOException {
072:
073: File f = new File(getBuildDir() + getPackageSubDir()
074: + "ExampleAbstractClass.class");
075:
076: JavaClass clazz = parser.parse(f);
077:
078: assertTrue(clazz.isAbstract());
079:
080: assertEquals("jdepend.framework.ExampleAbstractClass", clazz
081: .getName());
082:
083: assertEquals("ExampleAbstractClass.java", clazz.getSourceFile());
084:
085: Collection imports = clazz.getImportedPackages();
086: assertEquals(7, imports.size());
087:
088: assertTrue(imports.contains(new JavaPackage("java.math")));
089: assertTrue(imports.contains(new JavaPackage("java.text")));
090: assertTrue(imports.contains(new JavaPackage("java.lang")));
091: assertTrue(imports
092: .contains(new JavaPackage("java.lang.reflect")));
093: assertTrue(imports.contains(new JavaPackage("java.io")));
094: assertTrue(imports.contains(new JavaPackage("java.rmi")));
095: assertTrue(imports.contains(new JavaPackage("java.util")));
096: }
097:
098: public void testConcreteClass() throws IOException {
099:
100: File f = new File(getBuildDir() + getPackageSubDir()
101: + "ExampleConcreteClass.class");
102:
103: JavaClass clazz = parser.parse(f);
104:
105: assertFalse(clazz.isAbstract());
106:
107: assertEquals("jdepend.framework.ExampleConcreteClass", clazz
108: .getName());
109:
110: assertEquals("ExampleConcreteClass.java", clazz.getSourceFile());
111:
112: Collection imports = clazz.getImportedPackages();
113: assertEquals(9, imports.size());
114:
115: assertTrue(imports.contains(new JavaPackage("java.net")));
116: assertTrue(imports.contains(new JavaPackage("java.text")));
117: assertTrue(imports.contains(new JavaPackage("java.sql")));
118: assertTrue(imports.contains(new JavaPackage("java.lang")));
119: assertTrue(imports.contains(new JavaPackage("java.io")));
120: assertTrue(imports.contains(new JavaPackage("java.rmi")));
121: assertTrue(imports.contains(new JavaPackage("java.util")));
122: assertTrue(imports.contains(new JavaPackage("java.util.jar")));
123: assertTrue(imports.contains(new JavaPackage("java.math")));
124: }
125:
126: public void testInnerClass() throws IOException {
127:
128: File f = new File(getBuildDir() + getPackageSubDir()
129: + "ExampleConcreteClass$ExampleInnerClass.class");
130:
131: JavaClass clazz = parser.parse(f);
132:
133: assertFalse(clazz.isAbstract());
134:
135: assertEquals(
136: "jdepend.framework.ExampleConcreteClass$ExampleInnerClass",
137: clazz.getName());
138:
139: assertEquals("ExampleConcreteClass.java", clazz.getSourceFile());
140:
141: Collection imports = clazz.getImportedPackages();
142: assertEquals(1, imports.size());
143:
144: assertTrue(imports.contains(new JavaPackage("java.lang")));
145:
146: }
147:
148: public void testPackageClass() throws IOException {
149:
150: File f = new File(getBuildDir() + getPackageSubDir()
151: + "ExamplePackageClass.class");
152:
153: JavaClass clazz = parser.parse(f);
154:
155: assertFalse(clazz.isAbstract());
156:
157: assertEquals("jdepend.framework.ExamplePackageClass", clazz
158: .getName());
159:
160: assertEquals("ExampleConcreteClass.java", clazz.getSourceFile());
161:
162: Collection imports = clazz.getImportedPackages();
163: assertEquals(1, imports.size());
164:
165: assertTrue(imports.contains(new JavaPackage("java.lang")));
166:
167: }
168: }
|