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 @Override
40 protected void setUp() throws Exception {
41
42 System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
43
44 directory = createTempDirectory();
45 assertTrue(directory.exists());
46 assertTrue(directory.isDirectory());
47 }
48
49
50 protected File createDirectory( final String pName ) throws Exception {
51 final File newDirectory = new File(directory, pName);
52 assertTrue(newDirectory.mkdir());
53 assertTrue(newDirectory.exists());
54 assertTrue(newDirectory.isDirectory());
55 return newDirectory;
56 }
57
58 protected File writeFile( final String pName, final byte[] pData ) throws Exception {
59 final File file = new File(directory, pName);
60 final File parent = file.getParentFile();
61 if (!parent.mkdirs() && !parent.isDirectory()) {
62 throw new IOException("could not create" + parent);
63 }
64
65 log.debug("writing file " + pName + " (" + pData.length + " bytes)");
66
67 final FileOutputStream os = new FileOutputStream(file);
68 os.write(pData);
69 os.close();
70
71 assertTrue(file.exists());
72 assertTrue(file.isFile());
73
74 return file;
75 }
76
77 protected File writeFile( final String pName, final String pText ) throws Exception {
78 final File file = new File(directory, pName);
79 final File parent = file.getParentFile();
80 if (!parent.mkdirs() && !parent.isDirectory()) {
81 throw new IOException("could not create" + parent);
82 }
83 log.debug("writing " + file);
84 final FileWriter writer = new FileWriter(file);
85 writer.write(pText);
86 writer.close();
87
88 assertTrue(file.exists());
89 assertTrue(file.isFile());
90
91 return file;
92 }
93
94 protected void delay() {
95 try {
96 Thread.sleep(1500);
97 } catch (final InterruptedException e) {
98 }
99 }
100
101 protected File createTempDirectory() throws IOException {
102 final File tempFile = File.createTempFile("jci", null);
103
104 if (!tempFile.delete()) {
105 throw new IOException();
106 }
107
108 if (!tempFile.mkdir()) {
109 throw new IOException();
110 }
111
112 return tempFile;
113 }
114
115
116 @Override
117 protected void tearDown() throws Exception {
118 FileUtils.deleteDirectory(directory);
119 }
120 }