001: package org.uispec4j.utils;
002:
003: import java.io.*;
004: import java.util.zip.ZipEntry;
005: import java.util.zip.ZipOutputStream;
006:
007: public class FileTestUtils {
008: private static final File TMP_DIR = new File("tmp");
009:
010: public static File getFile(String filename) {
011: return new File(TMP_DIR, filename);
012: }
013:
014: public static File dumpStringToFile(String filename, String content)
015: throws Exception {
016: File file = getFile(filename);
017: dumpStringToFile(file, content);
018: return file;
019: }
020:
021: public static String loadTextFileToString(File file)
022: throws Exception {
023: BufferedReader reader = new BufferedReader(new FileReader(file));
024: try {
025: return readerToString(reader);
026: } finally {
027: close(reader);
028: }
029: }
030:
031: public static void clearTestDir() throws Exception {
032: emptyDirectory(TMP_DIR);
033: }
034:
035: private static void emptyDirectory(File dir) {
036: File[] list = dir.listFiles();
037: if (list != null) {
038: for (int j = 0; j < list.length; j++) {
039: if (list[j].isDirectory()) {
040: emptyDirectory(list[j]);
041: }
042: if (!list[j].delete()) {
043: throw new RuntimeException("Unable to delete "
044: + list[j].getPath());
045: }
046: }
047: }
048: }
049:
050: private static void dumpStringToFile(File file, String content)
051: throws Exception {
052: prepareDir(file);
053: file.createNewFile();
054:
055: // Necessary to ensure the file is fully created before writing on it ('createNewFile()' being asynchronous)
056: Thread.sleep(10);
057:
058: Writer writer = null;
059: try {
060: writer = new BufferedWriter(new FileWriter(file));
061: writer.write(content);
062: writer.flush();
063: } finally {
064: close(writer);
065: }
066: }
067:
068: private static String readerToString(Reader anyReader)
069: throws Exception {
070: BufferedReader reader = null;
071: StringBuffer buffer;
072: try {
073: reader = new BufferedReader(anyReader);
074: buffer = new StringBuffer();
075: String line = null;
076: boolean firstLine = true;
077: while ((line = reader.readLine()) != null) {
078: if (!firstLine) {
079: buffer.append(Utils.LINE_SEPARATOR);
080: } else {
081: firstLine = false;
082: }
083: buffer.append(line);
084: }
085: } finally {
086: close(reader, 10);
087: }
088: return buffer.toString();
089: }
090:
091: private static void close(Writer writer) {
092: if (writer != null) {
093: try {
094: writer.close();
095: } catch (IOException e) {
096: throw new RuntimeException(e);
097: }
098: }
099: }
100:
101: private static void close(Reader reader) throws Exception {
102: close(reader, 5);
103: }
104:
105: private static void close(Reader reader, int delay)
106: throws Exception {
107: if (reader != null) {
108: Thread.sleep(delay);
109: reader.close();
110: }
111: }
112:
113: private static void prepareDir(File file) {
114: File parentFile = file.getParentFile();
115: if (parentFile != null) {
116: parentFile.mkdirs();
117: }
118: }
119:
120: public static File createZipArchive(File archiveFile, File[] files)
121: throws IOException {
122: ZipOutputStream outputStream = new ZipOutputStream(
123: new FileOutputStream(archiveFile));
124: byte data[] = new byte[2048];
125: try {
126: for (int i = 0; i < files.length; i++) {
127: File file = files[i];
128: FileInputStream inputStream = new FileInputStream(file);
129: try {
130: ZipEntry e = new ZipEntry(file.getName());
131: outputStream.putNextEntry(e);
132: int count;
133: while ((count = inputStream.read(data, 0, 2048)) != -1) {
134: outputStream.write(data, 0, count);
135: }
136: outputStream.closeEntry();
137: } finally {
138: inputStream.close();
139: }
140: }
141: } finally {
142: outputStream.close();
143: }
144: return archiveFile;
145: }
146: }
|