1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.fs; |
---|
19 | |
---|
20 | import org.apache.hadoop.conf.Configuration; |
---|
21 | import java.io.*; |
---|
22 | import junit.framework.*; |
---|
23 | |
---|
24 | /** |
---|
25 | * This class tests the local file system via the FileSystem abstraction. |
---|
26 | */ |
---|
27 | public class TestLocalFileSystem extends TestCase { |
---|
28 | private static String TEST_ROOT_DIR |
---|
29 | = System.getProperty("test.build.data","build/test/data/work-dir/localfs"); |
---|
30 | |
---|
31 | |
---|
32 | static void writeFile(FileSystem fs, Path name) throws IOException { |
---|
33 | FSDataOutputStream stm = fs.create(name); |
---|
34 | stm.writeBytes("42\n"); |
---|
35 | stm.close(); |
---|
36 | } |
---|
37 | |
---|
38 | static String readFile(FileSystem fs, Path name) throws IOException { |
---|
39 | byte[] b = new byte[1024]; |
---|
40 | int offset = 0; |
---|
41 | FSDataInputStream in = fs.open(name); |
---|
42 | for(int remaining, n; |
---|
43 | (remaining = b.length - offset) > 0 && (n = in.read(b, offset, remaining)) != -1; |
---|
44 | offset += n); |
---|
45 | in.close(); |
---|
46 | |
---|
47 | String s = new String(b, 0, offset); |
---|
48 | System.out.println("s=" + s); |
---|
49 | return s; |
---|
50 | } |
---|
51 | |
---|
52 | private void cleanupFile(FileSystem fs, Path name) throws IOException { |
---|
53 | assertTrue(fs.exists(name)); |
---|
54 | fs.delete(name, true); |
---|
55 | assertTrue(!fs.exists(name)); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Test the capability of setting the working directory. |
---|
60 | */ |
---|
61 | public void testWorkingDirectory() throws IOException { |
---|
62 | Configuration conf = new Configuration(); |
---|
63 | FileSystem fileSys = FileSystem.getLocal(conf); |
---|
64 | Path origDir = fileSys.getWorkingDirectory(); |
---|
65 | Path subdir = new Path(TEST_ROOT_DIR, "new"); |
---|
66 | try { |
---|
67 | // make sure it doesn't already exist |
---|
68 | assertTrue(!fileSys.exists(subdir)); |
---|
69 | // make it and check for it |
---|
70 | assertTrue(fileSys.mkdirs(subdir)); |
---|
71 | assertTrue(fileSys.isDirectory(subdir)); |
---|
72 | |
---|
73 | fileSys.setWorkingDirectory(subdir); |
---|
74 | |
---|
75 | // create a directory and check for it |
---|
76 | Path dir1 = new Path("dir1"); |
---|
77 | assertTrue(fileSys.mkdirs(dir1)); |
---|
78 | assertTrue(fileSys.isDirectory(dir1)); |
---|
79 | |
---|
80 | // delete the directory and make sure it went away |
---|
81 | fileSys.delete(dir1, true); |
---|
82 | assertTrue(!fileSys.exists(dir1)); |
---|
83 | |
---|
84 | // create files and manipulate them. |
---|
85 | Path file1 = new Path("file1"); |
---|
86 | Path file2 = new Path("sub/file2"); |
---|
87 | writeFile(fileSys, file1); |
---|
88 | fileSys.copyFromLocalFile(file1, file2); |
---|
89 | assertTrue(fileSys.exists(file1)); |
---|
90 | assertTrue(fileSys.isFile(file1)); |
---|
91 | cleanupFile(fileSys, file2); |
---|
92 | fileSys.copyToLocalFile(file1, file2); |
---|
93 | cleanupFile(fileSys, file2); |
---|
94 | |
---|
95 | // try a rename |
---|
96 | fileSys.rename(file1, file2); |
---|
97 | assertTrue(!fileSys.exists(file1)); |
---|
98 | assertTrue(fileSys.exists(file2)); |
---|
99 | fileSys.rename(file2, file1); |
---|
100 | |
---|
101 | // try reading a file |
---|
102 | InputStream stm = fileSys.open(file1); |
---|
103 | byte[] buffer = new byte[3]; |
---|
104 | int bytesRead = stm.read(buffer, 0, 3); |
---|
105 | assertEquals("42\n", new String(buffer, 0, bytesRead)); |
---|
106 | stm.close(); |
---|
107 | } finally { |
---|
108 | fileSys.setWorkingDirectory(origDir); |
---|
109 | fileSys.delete(subdir, true); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | public void testCopy() throws IOException { |
---|
114 | Configuration conf = new Configuration(); |
---|
115 | LocalFileSystem fs = FileSystem.getLocal(conf); |
---|
116 | Path src = new Path(TEST_ROOT_DIR, "dingo"); |
---|
117 | Path dst = new Path(TEST_ROOT_DIR, "yak"); |
---|
118 | writeFile(fs, src); |
---|
119 | assertTrue(FileUtil.copy(fs, src, fs, dst, true, false, conf)); |
---|
120 | assertTrue(!fs.exists(src) && fs.exists(dst)); |
---|
121 | assertTrue(FileUtil.copy(fs, dst, fs, src, false, false, conf)); |
---|
122 | assertTrue(fs.exists(src) && fs.exists(dst)); |
---|
123 | assertTrue(FileUtil.copy(fs, src, fs, dst, true, true, conf)); |
---|
124 | assertTrue(!fs.exists(src) && fs.exists(dst)); |
---|
125 | fs.mkdirs(src); |
---|
126 | assertTrue(FileUtil.copy(fs, dst, fs, src, false, false, conf)); |
---|
127 | Path tmp = new Path(src, dst.getName()); |
---|
128 | assertTrue(fs.exists(tmp) && fs.exists(dst)); |
---|
129 | assertTrue(FileUtil.copy(fs, dst, fs, src, false, true, conf)); |
---|
130 | assertTrue(fs.delete(tmp, true)); |
---|
131 | fs.mkdirs(tmp); |
---|
132 | try { |
---|
133 | FileUtil.copy(fs, dst, fs, src, true, true, conf); |
---|
134 | fail("Failed to detect existing dir"); |
---|
135 | } catch (IOException e) { } |
---|
136 | } |
---|
137 | |
---|
138 | public void testHomeDirectory() throws IOException { |
---|
139 | Configuration conf = new Configuration(); |
---|
140 | FileSystem fileSys = FileSystem.getLocal(conf); |
---|
141 | Path home = new Path(System.getProperty("user.home")) |
---|
142 | .makeQualified(fileSys); |
---|
143 | Path fsHome = fileSys.getHomeDirectory(); |
---|
144 | assertEquals(home, fsHome); |
---|
145 | } |
---|
146 | |
---|
147 | public void testPathEscapes() throws IOException { |
---|
148 | Configuration conf = new Configuration(); |
---|
149 | FileSystem fs = FileSystem.getLocal(conf); |
---|
150 | Path path = new Path(TEST_ROOT_DIR, "foo%bar"); |
---|
151 | writeFile(fs, path); |
---|
152 | FileStatus status = fs.getFileStatus(path); |
---|
153 | assertEquals(path.makeQualified(fs), status.getPath()); |
---|
154 | cleanupFile(fs, path); |
---|
155 | } |
---|
156 | } |
---|