fix remplace/buffered input stream
[fanfix.git] / test_code / TempFilesTest.java
... / ...
CommitLineData
1package be.nikiroo.utils.test_code;
2
3import java.io.File;
4import java.io.IOException;
5
6import be.nikiroo.utils.TempFiles;
7import be.nikiroo.utils.test.TestCase;
8import be.nikiroo.utils.test.TestLauncher;
9
10class TempFilesTest extends TestLauncher {
11 public TempFilesTest(String[] args) {
12 super("TempFiles", args);
13
14 addTest(new TestCase("Name is correct") {
15 @Override
16 public void test() throws Exception {
17 RootTempFiles files = new RootTempFiles("testy");
18 try {
19 assertEquals("The root was not created", true, files
20 .getRoot().exists());
21 assertEquals(
22 "The root is not prefixed with the expected name",
23 true, files.getRoot().getName().startsWith("testy"));
24
25 } finally {
26 files.close();
27 }
28 }
29 });
30
31 addTest(new TestCase("Clean after itself no use") {
32 @Override
33 public void test() throws Exception {
34 RootTempFiles files = new RootTempFiles("testy2");
35 try {
36 assertEquals("The root was not created", true, files
37 .getRoot().exists());
38 } finally {
39 files.close();
40 assertEquals("The root was not deleted", false, files
41 .getRoot().exists());
42 }
43 }
44 });
45
46 addTest(new TestCase("Clean after itself after usage") {
47 @Override
48 public void test() throws Exception {
49 RootTempFiles files = new RootTempFiles("testy3");
50 try {
51 assertEquals("The root was not created", true, files
52 .getRoot().exists());
53 files.createTempFile("test");
54 } finally {
55 files.close();
56 assertEquals("The root was not deleted", false, files
57 .getRoot().exists());
58 assertEquals("The main root in /tmp was not deleted",
59 false, files.getRoot().getParentFile().exists());
60 }
61 }
62 });
63
64 addTest(new TestCase("Temporary directories") {
65 @Override
66 public void test() throws Exception {
67 RootTempFiles files = new RootTempFiles("testy4");
68 File file = null;
69 try {
70 File dir = files.createTempDir("test");
71 file = new File(dir, "fanfan");
72 file.createNewFile();
73 assertEquals(
74 "Cannot create a file in a temporary directory",
75 true, file.exists());
76 } finally {
77 files.close();
78 if (file != null) {
79 assertEquals(
80 "A file created in a temporary directory should be deleted on close",
81 false, file.exists());
82 }
83 assertEquals("The root was not deleted", false, files
84 .getRoot().exists());
85 }
86 }
87 });
88 }
89
90 private class RootTempFiles extends TempFiles {
91 private File root = null;
92
93 public RootTempFiles(String name) throws IOException {
94 super(name);
95 }
96
97 public File getRoot() {
98 if (root != null)
99 return root;
100 return super.root;
101 }
102
103 @Override
104 public synchronized void close() throws IOException {
105 root = super.root;
106 super.close();
107 }
108 }
109}