1 package org.objectweb.celtix.tools.processors; 2 3 import java.io.File ; 4 import java.net.URL ; 5 import java.net.URLClassLoader ; 6 import java.util.Locale ; 7 8 import junit.framework.TestCase; 9 10 import org.objectweb.celtix.tools.common.ProcessorEnvironment; 11 import org.objectweb.celtix.tools.common.ToolException; 12 13 public class ProcessorTestBase extends TestCase { 14 15 private static final int RETRY_SLEEP_MILLIS = 10; 16 protected ProcessorEnvironment env = new ProcessorEnvironment(); 17 protected File output; 18 19 public void setUp() throws Exception { 20 URL url = ProcessorTestBase.class.getResource("."); 21 output = new File (url.getFile()); 22 output = new File (output, "/resources"); 23 mkDir(output); 24 } 25 26 public void tearDown() { 27 removeDir(output); 28 output = null; 29 env = null; 30 } 31 32 private void mkDir(File dir) { 33 if (dir == null) { 34 throw new ToolException("dir attribute is required"); 35 } 36 37 if (dir.isFile()) { 38 throw new ToolException("Unable to create directory as a file " 39 + "already exists with that name: " 40 + dir.getAbsolutePath()); 41 } 42 43 if (!dir.exists()) { 44 boolean result = doMkDirs(dir); 45 if (!result) { 46 String msg = "Directory " + dir.getAbsolutePath() 47 + " creation was not successful for an unknown reason"; 48 throw new ToolException(msg); 49 } 50 } 51 } 52 53 58 private boolean doMkDirs(File f) { 59 if (!f.mkdirs()) { 60 try { 61 Thread.sleep(RETRY_SLEEP_MILLIS); 62 return f.mkdirs(); 63 } catch (InterruptedException ex) { 64 return f.mkdirs(); 65 } 66 } 67 return true; 68 } 69 70 private void removeDir(File d) { 71 String [] list = d.list(); 72 if (list == null) { 73 list = new String [0]; 74 } 75 for (int i = 0; i < list.length; i++) { 76 String s = list[i]; 77 File f = new File (d, s); 78 if (f.isDirectory()) { 79 removeDir(f); 80 } else { 81 delete(f); 82 } 83 } 84 delete(d); 85 } 86 87 private void delete(File f) { 88 if (!f.delete()) { 89 if (isWindows()) { 90 System.gc(); 91 } 92 try { 93 Thread.sleep(RETRY_SLEEP_MILLIS); 94 } catch (InterruptedException ex) { 95 } 97 if (!f.delete()) { 98 f.deleteOnExit(); 99 } 100 } 101 } 102 103 private boolean isWindows() { 104 String osName = System.getProperty("os.name").toLowerCase(Locale.US); 105 return osName.indexOf("windows") > -1; 106 } 107 108 protected String getClassPath() { 109 ClassLoader loader = getClass().getClassLoader(); 110 StringBuffer classPath = new StringBuffer (); 111 if (loader instanceof URLClassLoader ) { 112 URLClassLoader urlLoader = (URLClassLoader )loader; 113 for (URL url : urlLoader.getURLs()) { 114 String file = url.getFile(); 115 if (file.indexOf("junit") == -1) { 116 classPath.append(url.getFile()); 117 classPath.append(System.getProperty("path.separator")); 118 } 119 } 120 } 121 return classPath.toString(); 122 } 123 124 } 125 | Popular Tags |