KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > junit > runner > TrueZIPTestCollector


1 /*
2  * TrueZIPTestCollector.java
3  *
4  * Created on 29. November 2006, 20:56
5  */

6 /*
7  * Copyright 2004-2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.junit.runner;
23
24 import de.schlichtherle.io.*;
25
26 import java.util.*;
27
28 import junit.runner.TestCollector;
29
30 /**
31  * @author Christian Schlichtherle
32  * @version @version@
33  * @since TrueZIP 6.4
34  */

35 public class TrueZIPTestCollector implements TestCollector {
36
37     private static final int SUFFIX_LENGTH = ".class".length();
38
39     private final List tests = new LinkedList();
40
41     /** Creates a new instance of TrueZIPTestCollector */
42     public TrueZIPTestCollector() {
43         final String JavaDoc[] cp = System.getProperty("java.class.path").split(
44                 System.getProperty("path.separator"));
45         for (int i = 0; i < cp.length; i++) {
46             scan(new File(cp[i], ArchiveDetector.DEFAULT), ""); // use DEFAULT in case of reconfiguration!
47
}
48     }
49
50     private void scan(final File root, final String JavaDoc path) {
51         final File file = new File(root, path);
52         if (file.isDirectory()) {
53             final String JavaDoc[] children = file.list();
54             for (int i = 0; i < children.length; i++)
55                 scan(root, path + File.separator + children[i]);
56         } else if (file.isFile()) {
57             if (isTestClass(path))
58                 tests.add(toClassName(path));
59         }
60     }
61
62     protected boolean isTestClass(String JavaDoc path) {
63         return path.endsWith("Test.class") &&
64                 path.indexOf("junit" + File.separator) < 0 &&
65                 path.indexOf('$') < 0;
66     }
67
68     protected String JavaDoc toClassName(String JavaDoc path) {
69         return path.substring(1, path.length() - SUFFIX_LENGTH)
70                 .replace(File.separatorChar, '.');
71     }
72
73     public Enumeration collectTests() {
74         return Collections.enumeration(tests);
75     }
76 }
77
Popular Tags