1 | /** this file originally downloaded from
|
---|
2 | *http://www.javaworld.com/javaworld/jw-12-2000/junit/jw-1221-junit.zip
|
---|
3 | *
|
---|
4 | */
|
---|
5 |
|
---|
6 | package org.greenstone.testing;
|
---|
7 |
|
---|
8 | import java.util.*;
|
---|
9 | import java.io.*;
|
---|
10 | /*
|
---|
11 | * This used to use JCF. However, JCF seems to have dissapeared from the web.
|
---|
12 | * import lti.java.jcf.*;
|
---|
13 | */
|
---|
14 |
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This class is responsible for searching a directory for class files. It builds
|
---|
18 | * a list of fully qualified class names from the class files in the directory tree.
|
---|
19 | */
|
---|
20 | public class ClassFinder {
|
---|
21 | final private Vector<String> classNameList = new Vector<String> ();
|
---|
22 | final private int startPackageName;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Construct the class finder and locate all the classes in the directory structured
|
---|
26 | * pointed to by <code>classPathRoot</code>. Only classes in the package <code>packageRoot</code>
|
---|
27 | * are considered.
|
---|
28 | */
|
---|
29 | public ClassFinder(final File classPathRoot, final String packageRoot) throws IOException {
|
---|
30 | startPackageName = classPathRoot.getAbsolutePath().length() + 1;
|
---|
31 | String directoryOffset = packageRoot.replace ('.', File.separatorChar);
|
---|
32 | findAndStoreTestClasses (new File (classPathRoot, directoryOffset));
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Given a file name, guess the fully qualified class name.
|
---|
37 | */
|
---|
38 | private String computeClassName (final File file) {
|
---|
39 | String absPath = file.getAbsolutePath();
|
---|
40 | String packageBase = absPath.substring (startPackageName, absPath.length () - 6);
|
---|
41 | String className;
|
---|
42 | className = packageBase.replace(File.separatorChar, '.');
|
---|
43 | return className;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * This method does all the work. It runs down the directory structure looking
|
---|
48 | * for java classes.
|
---|
49 | */
|
---|
50 | private void findAndStoreTestClasses (final File currentDirectory) throws IOException {
|
---|
51 | String files[] = currentDirectory.list();
|
---|
52 | for(int i = 0;i < files.length;i++) {
|
---|
53 | File file = new File(currentDirectory, files[i]);
|
---|
54 | String fileBase = file.getName ();
|
---|
55 | int idx = fileBase.indexOf(".class");
|
---|
56 | final int CLASS_EXTENSION_LENGTH = 6;
|
---|
57 |
|
---|
58 | if(idx != -1 && (fileBase.length() - idx) == CLASS_EXTENSION_LENGTH) {
|
---|
59 | /*
|
---|
60 | * This used to use JCF. However, JCF seems to have dissapeared from the web so we fallback
|
---|
61 | * to a less elegant method. We compute the class name from the file name :-(
|
---|
62 | * JcfClassInputStream inputStream = new JcfClassInputStream(new FileInputStream (file));
|
---|
63 | * JcfClassFile classFile = new JcfClassFile (inputStream);
|
---|
64 | * System.out.println ("Processing: " + classFile.getFullName ().replace ('/','.'));
|
---|
65 | * classNameList.add (classFile.getFullName ().replace ('/','.'));
|
---|
66 | */
|
---|
67 | String className = computeClassName (file);
|
---|
68 | classNameList.add (className);
|
---|
69 | } else {
|
---|
70 | if(file.isDirectory()) {
|
---|
71 | findAndStoreTestClasses (file);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Return the found classes.
|
---|
79 | */
|
---|
80 | public Iterator<String> getClasses () {
|
---|
81 | return classNameList.iterator ();
|
---|
82 | }
|
---|
83 | }
|
---|