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