1 54 55 package junitx.util; 56 57 import junit.framework.Test; 58 import junit.framework.TestSuite; 59 60 import java.io.File ; 61 import java.io.IOException ; 62 import java.util.List ; 63 import java.util.Vector ; 64 65 100 public class DirectorySuiteBuilder 101 extends AbstractSuiteBuilder { 102 103 static final int SUFFIX_LENGTH = ".class".length(); 104 105 108 public DirectorySuiteBuilder() { 109 this(null); 110 } 111 112 115 public DirectorySuiteBuilder(TestFilter filter) { 116 super(filter); 117 } 118 119 123 public Test suite(File directory) 124 throws Exception { 125 TestSuite suite = new TestSuite(directory.getName()); 126 127 List classnames = browse(directory); 128 merge(classnames, suite); 129 130 return suite; 131 } 132 133 137 public Test suite(String directoryName) 138 throws Exception { 139 File dir = new File (directoryName); 140 return suite(dir); 141 } 142 143 149 protected List browse(File directory) 150 throws Exception { 151 List result = new Vector (); 152 153 gatherFiles(directory, result); 154 155 return result; 156 } 157 158 private void gatherFiles(File root, 159 List result) 160 throws IOException { 161 String path = root.getCanonicalPath(); 162 gather(root, path.length() + 1, result); 163 } 164 165 private void gather(File root, 166 int prefix, 167 List result) 168 throws IOException { 169 File [] files = root.listFiles(); 170 if (files == null) { 171 return; 172 } 173 174 for (int i = 0; i < files.length; i++) { 175 File file = files[i]; 176 if (file.isDirectory()) { 177 if (file.canRead()) { 178 gather(file, prefix, result); 179 } 180 } else { 181 if (isTestClass(file.getPath())) { 182 result.add(getClassName(file.getCanonicalPath().substring(prefix))); 183 } 184 } 185 } 186 } 187 188 private String getClassName(String filename) { 189 return filename.replace(File.separatorChar, '.').substring(0, filename.length() - SUFFIX_LENGTH); 190 } 191 192 } 193 | Popular Tags |