1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jci;
19
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import junit.framework.TestCase;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30
31
32
33 public abstract class AbstractTestCase extends TestCase {
34
35 private final Log log = LogFactory.getLog(AbstractTestCase.class);
36
37 protected File directory;
38
39 protected void setUp() throws Exception {
40
41 System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
42
43 directory = createTempDirectory();
44 assertTrue(directory.exists());
45 assertTrue(directory.isDirectory());
46 }
47
48
49 protected File createDirectory( final String pName ) throws Exception {
50 final File newDirectory = new File(directory, pName);
51 assertTrue(newDirectory.mkdir());
52 assertTrue(newDirectory.exists());
53 assertTrue(newDirectory.isDirectory());
54 return newDirectory;
55 }
56
57 protected File writeFile( final String pName, final byte[] pData ) throws Exception {
58 final File file = new File(directory, pName);
59 final File parent = file.getParentFile();
60 if (!parent.exists()) {
61 if (!parent.mkdirs()) {
62 throw new IOException("could not create" + parent);
63 }
64 }
65
66 log.debug("writing file " + pName + " (" + pData.length + " bytes)");
67
68 final FileOutputStream os = new FileOutputStream(file);
69 os.write(pData);
70 os.close();
71
72 assertTrue(file.exists());
73 assertTrue(file.isFile());
74
75 return file;
76 }
77
78 protected File writeFile( final String pName, final String pText ) throws Exception {
79 final File file = new File(directory, pName);
80 final File parent = file.getParentFile();
81 if (!parent.exists()) {
82 if (!parent.mkdirs()) {
83 throw new IOException("could not create" + parent);
84 }
85 }
86 log.debug("writing " + file);
87 final FileWriter writer = new FileWriter(file);
88 writer.write(pText);
89 writer.close();
90
91 assertTrue(file.exists());
92 assertTrue(file.isFile());
93
94 return file;
95 }
96
97 protected void delay() {
98 try {
99 Thread.sleep(1500);
100 } catch (final InterruptedException e) {
101 }
102 }
103
104 protected File createTempDirectory() throws IOException {
105 final File tempFile = File.createTempFile("jci", null);
106
107 if (!tempFile.delete()) {
108 throw new IOException();
109 }
110
111 if (!tempFile.mkdir()) {
112 throw new IOException();
113 }
114
115 return tempFile;
116 }
117
118
119 protected void tearDown() throws Exception {
120 FileUtils.deleteDirectory(directory);
121 }
122 }