|
19 | 19 |
|
20 | 20 | import java.io.File; |
21 | 21 | import java.io.FileNotFoundException; |
| 22 | +import java.io.IOException; |
22 | 23 | import java.net.URI; |
23 | 24 |
|
| 25 | +import junit.framework.TestCase; |
| 26 | + |
24 | 27 | import org.apache.hadoop.conf.Configuration; |
25 | 28 | import org.apache.hadoop.fs.FileSystem; |
26 | 29 | import org.apache.hadoop.fs.Path; |
27 | 30 |
|
28 | | -import junit.framework.TestCase; |
29 | | - |
30 | 31 | public class TestGenericOptionsParser extends TestCase { |
31 | | - private static File testDir = |
32 | | - new File(System.getProperty("test.build.data", "/tmp"), "generic"); |
| 32 | + File testDir; |
| 33 | + Configuration conf; |
| 34 | + FileSystem localFs; |
| 35 | + |
33 | 36 |
|
34 | 37 | public void testFilesOption() throws Exception { |
35 | | - Configuration conf = new Configuration(); |
36 | 38 | File tmpFile = new File(testDir, "tmpfile"); |
37 | | - FileSystem localFs = FileSystem.getLocal(conf); |
38 | 39 | Path tmpPath = new Path(tmpFile.toString()); |
39 | 40 | localFs.create(tmpPath); |
40 | 41 | String[] args = new String[2]; |
@@ -74,7 +75,62 @@ public void testFilesOption() throws Exception { |
74 | 75 | th instanceof FileNotFoundException); |
75 | 76 | files = conf2.get("tmpfiles"); |
76 | 77 | assertNull("files is not null", files); |
77 | | - testDir.delete(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void setUp() throws Exception { |
| 82 | + super.setUp(); |
| 83 | + conf = new Configuration(); |
| 84 | + localFs = FileSystem.getLocal(conf); |
| 85 | + testDir = new File(System.getProperty("test.build.data", "/tmp"), "generic"); |
| 86 | + if(testDir.exists()) |
| 87 | + localFs.delete(new Path(testDir.toString()), true); |
78 | 88 | } |
79 | 89 |
|
| 90 | + @Override |
| 91 | + protected void tearDown() throws Exception { |
| 92 | + super.tearDown(); |
| 93 | + if(testDir.exists()) { |
| 94 | + localFs.delete(new Path(testDir.toString()), true); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * testing -fileCache option |
| 100 | + * @throws IOException |
| 101 | + */ |
| 102 | + public void testTokenCacheOption() throws IOException { |
| 103 | + FileSystem localFs = FileSystem.getLocal(conf); |
| 104 | + |
| 105 | + File tmpFile = new File(testDir, "tokenCacheFile"); |
| 106 | + if(tmpFile.exists()) { |
| 107 | + tmpFile.delete(); |
| 108 | + } |
| 109 | + String[] args = new String[2]; |
| 110 | + // pass a files option |
| 111 | + args[0] = "-tokenCacheFile"; |
| 112 | + args[1] = tmpFile.toString(); |
| 113 | + |
| 114 | + // test non existing file |
| 115 | + Throwable th = null; |
| 116 | + try { |
| 117 | + new GenericOptionsParser(conf, args); |
| 118 | + } catch (Exception e) { |
| 119 | + th = e; |
| 120 | + } |
| 121 | + assertNotNull(th); |
| 122 | + assertTrue("FileNotFoundException is not thrown", |
| 123 | + th instanceof FileNotFoundException); |
| 124 | + |
| 125 | + // create file |
| 126 | + Path tmpPath = new Path(tmpFile.toString()); |
| 127 | + localFs.create(tmpPath); |
| 128 | + new GenericOptionsParser(conf, args); |
| 129 | + String fileName = conf.get("tokenCacheFile"); |
| 130 | + assertNotNull("files is null", fileName); |
| 131 | + assertEquals("files option does not match", |
| 132 | + localFs.makeQualified(tmpPath).toString(), fileName); |
| 133 | + |
| 134 | + localFs.delete(new Path(testDir.getAbsolutePath()), true); |
| 135 | + } |
80 | 136 | } |
0 commit comments