1 16 package org.apache.commons.vfs.test; 17 18 import junit.extensions.TestSetup; 19 import junit.framework.Test; 20 import junit.framework.TestSuite; 21 import org.apache.commons.AbstractVfsTestCase; 22 import org.apache.commons.vfs.FileName; 23 import org.apache.commons.vfs.FileObject; 24 import org.apache.commons.vfs.impl.DefaultFileReplicator; 25 import org.apache.commons.vfs.impl.DefaultFileSystemManager; 26 import org.apache.commons.vfs.impl.PrivilegedFileReplicator; 27 import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider; 28 29 import java.io.File ; 30 import java.lang.reflect.Field ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Modifier ; 33 import java.util.ArrayList ; 34 import java.util.Enumeration ; 35 import java.util.List ; 36 37 43 public class AbstractTestSuite 44 extends TestSetup 45 { 46 private final ProviderTestConfig providerConfig; 47 private final String prefix; 48 private TestSuite testSuite; 49 50 private FileObject baseFolder; 51 private FileObject readFolder; 52 private FileObject writeFolder; 53 private DefaultFileSystemManager manager; 54 private File tempDir; 55 56 private Thread [] startThreadSnapshot; 57 private Thread [] endThreadSnapshot; 58 59 62 public AbstractTestSuite(final ProviderTestConfig providerConfig) throws Exception 63 { 64 this(providerConfig, "", false); 65 } 66 67 protected AbstractTestSuite(final ProviderTestConfig providerConfig, 68 final String prefix, 69 final boolean nested) 70 throws Exception 71 { 72 super(new TestSuite()); 73 testSuite = (TestSuite) fTest; 74 this.providerConfig = providerConfig; 75 this.prefix = prefix; 76 addBaseTests(); 77 if (!nested) 78 { 79 } 84 } 85 86 89 protected void addBaseTests() throws Exception 90 { 91 } 92 93 99 public void addTests(final Class testClass) throws Exception 100 { 101 if (!AbstractProviderTestCase.class.isAssignableFrom(testClass)) 103 { 104 throw new Exception ("Test class " + testClass.getName() + " is not assignable to " + AbstractProviderTestCase.class.getName()); 105 } 106 107 final Method [] methods = testClass.getMethods(); 109 for (int i = 0; i < methods.length; i++) 110 { 111 final Method method = methods[i]; 112 if (!method.getName().startsWith("test") 113 || Modifier.isStatic(method.getModifiers()) 114 || method.getReturnType() != Void.TYPE 115 || method.getParameterTypes().length != 0) 116 { 117 continue; 118 } 119 120 final AbstractProviderTestCase testCase = (AbstractProviderTestCase) testClass.newInstance(); 122 testCase.setMethod(method); 123 testCase.setName(prefix + method.getName()); 124 testSuite.addTest(testCase); 125 } 126 } 127 128 protected void setUp() throws Exception 129 { 130 startThreadSnapshot = createThreadSnapshot(); 131 132 tempDir = AbstractVfsTestCase.getTestDirectory("temp"); 134 checkTempDir("Temp dir not empty before test"); 135 136 manager = new DefaultFileSystemManager(); 138 manager.setFilesCache(providerConfig.getFilesCache()); 139 140 final DefaultFileReplicator replicator = new DefaultFileReplicator(tempDir); 141 manager.setReplicator(new PrivilegedFileReplicator(replicator)); 142 manager.setTemporaryFileStore(replicator); 143 144 providerConfig.prepare(manager); 145 146 if (!manager.hasProvider("file")) 147 { 148 manager.addProvider("file", new DefaultLocalFileProvider()); 149 } 150 151 manager.init(); 152 153 baseFolder = providerConfig.getBaseTestFolder(manager); 155 readFolder = baseFolder.resolveFile("read-tests"); 156 writeFolder = baseFolder.resolveFile("write-tests"); 157 158 assertTrue("Folder does not exist: " + readFolder, readFolder.exists()); 160 assertFalse(readFolder.getName().getPath().equals(FileName.ROOT_PATH)); 161 162 final Enumeration tests = testSuite.tests(); 164 while (tests.hasMoreElements()) 165 { 166 final Test test = (Test) tests.nextElement(); 167 if (test instanceof AbstractProviderTestCase) 168 { 169 final AbstractProviderTestCase providerTestCase = (AbstractProviderTestCase) test; 170 providerTestCase.setConfig(manager, baseFolder, readFolder, writeFolder); 171 } 172 } 173 } 174 175 protected void tearDown() throws Exception 176 { 177 readFolder.close(); 178 writeFolder.close(); 179 baseFolder.close(); 180 181 readFolder = null; 182 writeFolder = null; 183 baseFolder = null; 184 testSuite = null; 185 fTest = null; 186 187 System.err.println("."); 189 System.gc(); 190 Thread.sleep(1000); 191 System.err.println("."); 192 System.gc(); 193 Thread.sleep(1000); 194 System.err.println("."); 195 System.gc(); 196 Thread.sleep(1000); 197 System.err.println("."); 198 System.gc(); 199 Thread.sleep(1000); 200 201 manager.freeUnusedResources(); 202 endThreadSnapshot = createThreadSnapshot(); 203 204 Thread [] diffThreadSnapshot = diffThreadSnapshot(startThreadSnapshot, endThreadSnapshot); 205 if (diffThreadSnapshot.length > 0) 206 { 207 String message = dumpThreadSnapshot(diffThreadSnapshot); 208 219 System.out.println(message); 220 } 222 224 manager.close(); 225 226 checkTempDir("Temp dir not empty after test"); 228 } 229 230 233 private void checkTempDir(final String assertMsg) 234 { 235 if (tempDir.exists()) 236 { 237 assertTrue(assertMsg + " (" + tempDir.getAbsolutePath() + ")", tempDir.isDirectory() && tempDir.list().length == 0); 238 } 239 } 240 241 private String dumpThreadSnapshot(Thread [] threadSnapshot) 242 { 243 StringBuffer sb = new StringBuffer (256); 244 sb.append("created threads still running:\n"); 245 246 Field threadTargetField = null; 247 try 248 { 249 threadTargetField = Thread .class.getDeclaredField("target"); 250 threadTargetField.setAccessible(true); 251 } 252 catch (NoSuchFieldException e) 253 { 254 ; 255 } 256 257 for (int iter = 0; iter < threadSnapshot.length; iter++) 258 { 259 Thread thread = threadSnapshot[iter]; 260 if (thread == null) 261 { 262 continue; 263 } 264 265 sb.append("#"); 266 sb.append(iter + 1); 267 sb.append(": "); 268 sb.append(thread.getThreadGroup().getName()); 269 sb.append("\t"); 270 sb.append(thread.getName()); 271 sb.append("\t"); 272 if (thread.isDaemon()) 273 { 274 sb.append("daemon"); 275 } 276 else 277 { 278 sb.append("not_a_daemon"); 279 } 280 281 if (threadTargetField != null) 282 { 283 sb.append("\t"); 284 try 285 { 286 Object threadTarget = threadTargetField.get(thread); 287 if (threadTarget != null) 288 { 289 sb.append(threadTarget.getClass()); 290 } 291 else 292 { 293 sb.append("null"); 294 } 295 } 296 catch (IllegalAccessException e) 297 { 298 sb.append("unknown class"); 299 } 300 } 301 302 sb.append("\n"); 303 } 304 305 return sb.toString(); 306 } 307 308 private Thread [] diffThreadSnapshot(Thread [] startThreadSnapshot, Thread [] endThreadSnapshot) 309 { 310 List diff = new ArrayList (10); 311 312 nextEnd: for (int iterEnd = 0; iterEnd < endThreadSnapshot.length; iterEnd++) 313 { 314 nextStart: for (int iterStart = 0; iterStart < startThreadSnapshot.length; iterStart++) 315 { 316 if (startThreadSnapshot[iterStart] == endThreadSnapshot[iterEnd]) 317 { 318 continue nextEnd; 319 } 320 } 321 322 diff.add(endThreadSnapshot[iterEnd]); 323 } 324 325 Thread ret[] = new Thread [diff.size()]; 326 diff.toArray(ret); 327 return ret; 328 } 329 330 private Thread [] createThreadSnapshot() 331 { 332 ThreadGroup tg = Thread.currentThread().getThreadGroup(); 333 while (tg.getParent() != null) 334 { 335 tg = tg.getParent(); 336 } 337 338 Thread snapshot[] = new Thread [200]; 339 tg.enumerate(snapshot, true); 340 341 return snapshot; 342 } 343 } 344 | Popular Tags |