KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > processors > ProcessorTestBase


1 package org.objectweb.celtix.tools.processors;
2
3 import java.io.File JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.net.URLClassLoader JavaDoc;
6 import java.util.Locale JavaDoc;
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 JavaDoc output;
18         
19     public void setUp() throws Exception JavaDoc {
20         URL JavaDoc url = ProcessorTestBase.class.getResource(".");
21         output = new File JavaDoc(url.getFile());
22         output = new File JavaDoc(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 JavaDoc 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 JavaDoc msg = "Directory " + dir.getAbsolutePath()
47                     + " creation was not successful for an unknown reason";
48                 throw new ToolException(msg);
49             }
50         }
51     }
52
53     /**
54      * Attempt to fix possible race condition when creating
55      * directories on WinXP, also Windows2000. If the mkdirs does not work,
56      * wait a little and try again.
57      */

58     private boolean doMkDirs(File JavaDoc f) {
59         if (!f.mkdirs()) {
60             try {
61                 Thread.sleep(RETRY_SLEEP_MILLIS);
62                 return f.mkdirs();
63             } catch (InterruptedException JavaDoc ex) {
64                 return f.mkdirs();
65             }
66         }
67         return true;
68     }
69     
70     private void removeDir(File JavaDoc d) {
71         String JavaDoc[] list = d.list();
72         if (list == null) {
73             list = new String JavaDoc[0];
74         }
75         for (int i = 0; i < list.length; i++) {
76             String JavaDoc s = list[i];
77             File JavaDoc f = new File JavaDoc(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 JavaDoc f) {
88         if (!f.delete()) {
89             if (isWindows()) {
90                 System.gc();
91             }
92             try {
93                 Thread.sleep(RETRY_SLEEP_MILLIS);
94             } catch (InterruptedException JavaDoc ex) {
95                 // Ignore Exception
96
}
97             if (!f.delete()) {
98                 f.deleteOnExit();
99             }
100         }
101     }
102
103     private boolean isWindows() {
104         String JavaDoc osName = System.getProperty("os.name").toLowerCase(Locale.US);
105         return osName.indexOf("windows") > -1;
106     }
107     
108     protected String JavaDoc getClassPath() {
109         ClassLoader JavaDoc loader = getClass().getClassLoader();
110         StringBuffer JavaDoc classPath = new StringBuffer JavaDoc();
111         if (loader instanceof URLClassLoader JavaDoc) {
112             URLClassLoader JavaDoc urlLoader = (URLClassLoader JavaDoc)loader;
113             for (URL JavaDoc url : urlLoader.getURLs()) {
114                 String JavaDoc 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