001: package org.objectweb.celtix.tools.processors;
002:
003: import java.io.File;
004: import java.net.URL;
005: import java.net.URLClassLoader;
006: import java.util.Locale;
007:
008: import junit.framework.TestCase;
009:
010: import org.objectweb.celtix.tools.common.ProcessorEnvironment;
011: import org.objectweb.celtix.tools.common.ToolException;
012:
013: public class ProcessorTestBase extends TestCase {
014:
015: private static final int RETRY_SLEEP_MILLIS = 10;
016: protected ProcessorEnvironment env = new ProcessorEnvironment();
017: protected File output;
018:
019: public void setUp() throws Exception {
020: URL url = ProcessorTestBase.class.getResource(".");
021: output = new File(url.getFile());
022: output = new File(output, "/resources");
023: mkDir(output);
024: }
025:
026: public void tearDown() {
027: removeDir(output);
028: output = null;
029: env = null;
030: }
031:
032: private void mkDir(File dir) {
033: if (dir == null) {
034: throw new ToolException("dir attribute is required");
035: }
036:
037: if (dir.isFile()) {
038: throw new ToolException(
039: "Unable to create directory as a file "
040: + "already exists with that name: "
041: + dir.getAbsolutePath());
042: }
043:
044: if (!dir.exists()) {
045: boolean result = doMkDirs(dir);
046: if (!result) {
047: String msg = "Directory "
048: + dir.getAbsolutePath()
049: + " creation was not successful for an unknown reason";
050: throw new ToolException(msg);
051: }
052: }
053: }
054:
055: /**
056: * Attempt to fix possible race condition when creating
057: * directories on WinXP, also Windows2000. If the mkdirs does not work,
058: * wait a little and try again.
059: */
060: private boolean doMkDirs(File f) {
061: if (!f.mkdirs()) {
062: try {
063: Thread.sleep(RETRY_SLEEP_MILLIS);
064: return f.mkdirs();
065: } catch (InterruptedException ex) {
066: return f.mkdirs();
067: }
068: }
069: return true;
070: }
071:
072: private void removeDir(File d) {
073: String[] list = d.list();
074: if (list == null) {
075: list = new String[0];
076: }
077: for (int i = 0; i < list.length; i++) {
078: String s = list[i];
079: File f = new File(d, s);
080: if (f.isDirectory()) {
081: removeDir(f);
082: } else {
083: delete(f);
084: }
085: }
086: delete(d);
087: }
088:
089: private void delete(File f) {
090: if (!f.delete()) {
091: if (isWindows()) {
092: System.gc();
093: }
094: try {
095: Thread.sleep(RETRY_SLEEP_MILLIS);
096: } catch (InterruptedException ex) {
097: // Ignore Exception
098: }
099: if (!f.delete()) {
100: f.deleteOnExit();
101: }
102: }
103: }
104:
105: private boolean isWindows() {
106: String osName = System.getProperty("os.name").toLowerCase(
107: Locale.US);
108: return osName.indexOf("windows") > -1;
109: }
110:
111: protected String getClassPath() {
112: ClassLoader loader = getClass().getClassLoader();
113: StringBuffer classPath = new StringBuffer();
114: if (loader instanceof URLClassLoader) {
115: URLClassLoader urlLoader = (URLClassLoader) loader;
116: for (URL url : urlLoader.getURLs()) {
117: String file = url.getFile();
118: if (file.indexOf("junit") == -1) {
119: classPath.append(url.getFile());
120: classPath.append(System
121: .getProperty("path.separator"));
122: }
123: }
124: }
125: return classPath.toString();
126: }
127:
128: }
|