source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/fs/FileSystemContractBaseTest.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 14.1 KB
Line 
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
19package org.apache.hadoop.fs;
20
21import java.io.FileNotFoundException;
22import java.io.IOException;
23
24import junit.framework.TestCase;
25
26import org.apache.hadoop.fs.FSDataInputStream;
27import org.apache.hadoop.fs.FSDataOutputStream;
28import org.apache.hadoop.fs.FileStatus;
29import org.apache.hadoop.fs.FileSystem;
30import org.apache.hadoop.fs.Path;
31
32/**
33 * <p>
34 * A collection of tests for the contract of the {@link FileSystem}.
35 * This test should be used for general-purpose implementations of
36 * {@link FileSystem}, that is, implementations that provide implementations
37 * of all of the functionality of {@link FileSystem}.
38 * </p>
39 * <p>
40 * To test a given {@link FileSystem} implementation create a subclass of this
41 * test and override {@link #setUp()} to initialize the <code>fs</code>
42 * {@link FileSystem} instance variable.
43 * </p>
44 */
45public abstract class FileSystemContractBaseTest extends TestCase {
46 
47  protected FileSystem fs;
48  private byte[] data = new byte[getBlockSize() * 2]; // two blocks of data
49  {
50    for (int i = 0; i < data.length; i++) {
51      data[i] = (byte) (i % 10);
52    }
53  }
54 
55  @Override
56  protected void tearDown() throws Exception {
57    fs.delete(path("/test"), true);
58  }
59 
60  protected int getBlockSize() {
61    return 1024;
62  }
63 
64  protected String getDefaultWorkingDirectory() {
65    return "/user/" + System.getProperty("user.name");
66  }
67
68  protected boolean renameSupported() {
69    return true;
70  }
71 
72  public void testWorkingDirectory() throws Exception {
73
74    Path workDir = path(getDefaultWorkingDirectory());
75    assertEquals(workDir, fs.getWorkingDirectory());
76
77    fs.setWorkingDirectory(path("."));
78    assertEquals(workDir, fs.getWorkingDirectory());
79
80    fs.setWorkingDirectory(path(".."));
81    assertEquals(workDir.getParent(), fs.getWorkingDirectory());
82
83    Path relativeDir = path("hadoop");
84    fs.setWorkingDirectory(relativeDir);
85    assertEquals(relativeDir, fs.getWorkingDirectory());
86   
87    Path absoluteDir = path("/test/hadoop");
88    fs.setWorkingDirectory(absoluteDir);
89    assertEquals(absoluteDir, fs.getWorkingDirectory());
90
91  }
92 
93  public void testMkdirs() throws Exception {
94    Path testDir = path("/test/hadoop");
95    assertFalse(fs.exists(testDir));
96    assertFalse(fs.isFile(testDir));
97
98    assertTrue(fs.mkdirs(testDir));
99
100    assertTrue(fs.exists(testDir));
101    assertFalse(fs.isFile(testDir));
102   
103    assertTrue(fs.mkdirs(testDir));
104
105    assertTrue(fs.exists(testDir));
106    assertFalse(fs.isFile(testDir));
107
108    Path parentDir = testDir.getParent();
109    assertTrue(fs.exists(parentDir));
110    assertFalse(fs.isFile(parentDir));
111
112    Path grandparentDir = parentDir.getParent();
113    assertTrue(fs.exists(grandparentDir));
114    assertFalse(fs.isFile(grandparentDir));
115   
116  }
117 
118  public void testMkdirsFailsForSubdirectoryOfExistingFile() throws Exception {
119    Path testDir = path("/test/hadoop");
120    assertFalse(fs.exists(testDir));
121    assertTrue(fs.mkdirs(testDir));
122    assertTrue(fs.exists(testDir));
123   
124    createFile(path("/test/hadoop/file"));
125   
126    Path testSubDir = path("/test/hadoop/file/subdir");
127    try {
128      fs.mkdirs(testSubDir);
129      fail("Should throw IOException.");
130    } catch (IOException e) {
131      // expected
132    }
133    assertFalse(fs.exists(testSubDir));
134   
135    Path testDeepSubDir = path("/test/hadoop/file/deep/sub/dir");
136    try {
137      fs.mkdirs(testDeepSubDir);
138      fail("Should throw IOException.");
139    } catch (IOException e) {
140      // expected
141    }
142    assertFalse(fs.exists(testDeepSubDir));
143   
144  }
145 
146  public void testGetFileStatusThrowsExceptionForNonExistentFile() 
147    throws Exception {
148    try {
149      fs.getFileStatus(path("/test/hadoop/file"));
150      fail("Should throw FileNotFoundException");
151    } catch (FileNotFoundException e) {
152      // expected
153    }
154  }
155 
156  public void testListStatusReturnsNullForNonExistentFile() throws Exception {
157    assertNull(fs.listStatus(path("/test/hadoop/file")));
158  }
159 
160  public void testListStatus() throws Exception {
161    Path[] testDirs = { path("/test/hadoop/a"),
162                        path("/test/hadoop/b"),
163                        path("/test/hadoop/c/1"), };
164    assertFalse(fs.exists(testDirs[0]));
165
166    for (Path path : testDirs) {
167      assertTrue(fs.mkdirs(path));
168    }
169
170    FileStatus[] paths = fs.listStatus(path("/test"));
171    assertEquals(1, paths.length);
172    assertEquals(path("/test/hadoop"), paths[0].getPath());
173
174    paths = fs.listStatus(path("/test/hadoop"));
175    assertEquals(3, paths.length);
176    assertEquals(path("/test/hadoop/a"), paths[0].getPath());
177    assertEquals(path("/test/hadoop/b"), paths[1].getPath());
178    assertEquals(path("/test/hadoop/c"), paths[2].getPath());
179
180    paths = fs.listStatus(path("/test/hadoop/a"));
181    assertEquals(0, paths.length);
182  }
183 
184  public void testWriteReadAndDeleteEmptyFile() throws Exception {
185    writeReadAndDelete(0);
186  }
187
188  public void testWriteReadAndDeleteHalfABlock() throws Exception {
189    writeReadAndDelete(getBlockSize() / 2);
190  }
191
192  public void testWriteReadAndDeleteOneBlock() throws Exception {
193    writeReadAndDelete(getBlockSize());
194  }
195 
196  public void testWriteReadAndDeleteOneAndAHalfBlocks() throws Exception {
197    writeReadAndDelete(getBlockSize() + (getBlockSize() / 2));
198  }
199 
200  public void testWriteReadAndDeleteTwoBlocks() throws Exception {
201    writeReadAndDelete(getBlockSize() * 2);
202  }
203 
204  private void writeReadAndDelete(int len) throws IOException {
205    Path path = path("/test/hadoop/file");
206   
207    fs.mkdirs(path.getParent());
208
209    FSDataOutputStream out = fs.create(path, false,
210        fs.getConf().getInt("io.file.buffer.size", 4096), 
211        (short) 1, getBlockSize());
212    out.write(data, 0, len);
213    out.close();
214
215    assertTrue("Exists", fs.exists(path));
216    assertEquals("Length", len, fs.getFileStatus(path).getLen());
217
218    FSDataInputStream in = fs.open(path);
219    byte[] buf = new byte[len];
220    in.readFully(0, buf);
221    in.close();
222
223    assertEquals(len, buf.length);
224    for (int i = 0; i < buf.length; i++) {
225      assertEquals("Position " + i, data[i], buf[i]);
226    }
227   
228    assertTrue("Deleted", fs.delete(path, false));
229   
230    assertFalse("No longer exists", fs.exists(path));
231
232  }
233 
234  public void testOverwrite() throws IOException {
235    Path path = path("/test/hadoop/file");
236   
237    fs.mkdirs(path.getParent());
238
239    createFile(path);
240   
241    assertTrue("Exists", fs.exists(path));
242    assertEquals("Length", data.length, fs.getFileStatus(path).getLen());
243   
244    try {
245      fs.create(path, false);
246      fail("Should throw IOException.");
247    } catch (IOException e) {
248      // Expected
249    }
250   
251    FSDataOutputStream out = fs.create(path, true);
252    out.write(data, 0, data.length);
253    out.close();
254   
255    assertTrue("Exists", fs.exists(path));
256    assertEquals("Length", data.length, fs.getFileStatus(path).getLen());
257   
258  }
259 
260  public void testWriteInNonExistentDirectory() throws IOException {
261    Path path = path("/test/hadoop/file");
262    assertFalse("Parent doesn't exist", fs.exists(path.getParent()));
263    createFile(path);
264   
265    assertTrue("Exists", fs.exists(path));
266    assertEquals("Length", data.length, fs.getFileStatus(path).getLen());
267    assertTrue("Parent exists", fs.exists(path.getParent()));
268  }
269
270  public void testDeleteNonExistentFile() throws IOException {
271    Path path = path("/test/hadoop/file");   
272    assertFalse("Doesn't exist", fs.exists(path));
273    assertFalse("No deletion", fs.delete(path, true));
274  }
275 
276  public void testDeleteRecursively() throws IOException {
277    Path dir = path("/test/hadoop");
278    Path file = path("/test/hadoop/file");
279    Path subdir = path("/test/hadoop/subdir");
280   
281    createFile(file);
282    assertTrue("Created subdir", fs.mkdirs(subdir));
283   
284    assertTrue("File exists", fs.exists(file));
285    assertTrue("Dir exists", fs.exists(dir));
286    assertTrue("Subdir exists", fs.exists(subdir));
287   
288    try {
289      fs.delete(dir, false);
290      fail("Should throw IOException.");
291    } catch (IOException e) {
292      // expected
293    }
294    assertTrue("File still exists", fs.exists(file));
295    assertTrue("Dir still exists", fs.exists(dir));
296    assertTrue("Subdir still exists", fs.exists(subdir));
297   
298    assertTrue("Deleted", fs.delete(dir, true));
299    assertFalse("File doesn't exist", fs.exists(file));
300    assertFalse("Dir doesn't exist", fs.exists(dir));
301    assertFalse("Subdir doesn't exist", fs.exists(subdir));
302  }
303 
304  public void testDeleteEmptyDirectory() throws IOException {
305    Path dir = path("/test/hadoop");
306    assertTrue(fs.mkdirs(dir));
307    assertTrue("Dir exists", fs.exists(dir));
308    assertTrue("Deleted", fs.delete(dir, false));
309    assertFalse("Dir doesn't exist", fs.exists(dir));
310  }
311 
312  public void testRenameNonExistentPath() throws Exception {
313    if (!renameSupported()) return;
314   
315    Path src = path("/test/hadoop/path");
316    Path dst = path("/test/new/newpath");
317    rename(src, dst, false, false, false);
318  }
319
320  public void testRenameFileMoveToNonExistentDirectory() throws Exception {
321    if (!renameSupported()) return;
322   
323    Path src = path("/test/hadoop/file");
324    createFile(src);
325    Path dst = path("/test/new/newfile");
326    rename(src, dst, false, true, false);
327  }
328
329  public void testRenameFileMoveToExistingDirectory() throws Exception {
330    if (!renameSupported()) return;
331   
332    Path src = path("/test/hadoop/file");
333    createFile(src);
334    Path dst = path("/test/new/newfile");
335    fs.mkdirs(dst.getParent());
336    rename(src, dst, true, false, true);
337  }
338
339  public void testRenameFileAsExistingFile() throws Exception {
340    if (!renameSupported()) return;
341   
342    Path src = path("/test/hadoop/file");
343    createFile(src);
344    Path dst = path("/test/new/newfile");
345    createFile(dst);
346    rename(src, dst, false, true, true);
347  }
348
349  public void testRenameFileAsExistingDirectory() throws Exception {
350    if (!renameSupported()) return;
351   
352    Path src = path("/test/hadoop/file");
353    createFile(src);
354    Path dst = path("/test/new/newdir");
355    fs.mkdirs(dst);
356    rename(src, dst, true, false, true);
357    assertTrue("Destination changed",
358        fs.exists(path("/test/new/newdir/file")));
359  }
360 
361  public void testRenameDirectoryMoveToNonExistentDirectory() 
362    throws Exception {
363    if (!renameSupported()) return;
364   
365    Path src = path("/test/hadoop/dir");
366    fs.mkdirs(src);
367    Path dst = path("/test/new/newdir");
368    rename(src, dst, false, true, false);
369  }
370 
371  public void testRenameDirectoryMoveToExistingDirectory() throws Exception {
372    if (!renameSupported()) return;
373   
374    Path src = path("/test/hadoop/dir");
375    fs.mkdirs(src);
376    createFile(path("/test/hadoop/dir/file1"));
377    createFile(path("/test/hadoop/dir/subdir/file2"));
378   
379    Path dst = path("/test/new/newdir");
380    fs.mkdirs(dst.getParent());
381    rename(src, dst, true, false, true);
382   
383    assertFalse("Nested file1 exists",
384        fs.exists(path("/test/hadoop/dir/file1")));
385    assertFalse("Nested file2 exists",
386        fs.exists(path("/test/hadoop/dir/subdir/file2")));
387    assertTrue("Renamed nested file1 exists",
388        fs.exists(path("/test/new/newdir/file1")));
389    assertTrue("Renamed nested exists",
390        fs.exists(path("/test/new/newdir/subdir/file2")));
391  }
392 
393  public void testRenameDirectoryAsExistingFile() throws Exception {
394    if (!renameSupported()) return;
395   
396    Path src = path("/test/hadoop/dir");
397    fs.mkdirs(src);
398    Path dst = path("/test/new/newfile");
399    createFile(dst);
400    rename(src, dst, false, true, true);
401  }
402 
403  public void testRenameDirectoryAsExistingDirectory() throws Exception {
404    if (!renameSupported()) return;
405   
406    Path src = path("/test/hadoop/dir");
407    fs.mkdirs(src);
408    createFile(path("/test/hadoop/dir/file1"));
409    createFile(path("/test/hadoop/dir/subdir/file2"));
410   
411    Path dst = path("/test/new/newdir");
412    fs.mkdirs(dst);
413    rename(src, dst, true, false, true);
414    assertTrue("Destination changed",
415        fs.exists(path("/test/new/newdir/dir")));   
416    assertFalse("Nested file1 exists",
417        fs.exists(path("/test/hadoop/dir/file1")));
418    assertFalse("Nested file2 exists",
419        fs.exists(path("/test/hadoop/dir/subdir/file2")));
420    assertTrue("Renamed nested file1 exists",
421        fs.exists(path("/test/new/newdir/dir/file1")));
422    assertTrue("Renamed nested exists",
423        fs.exists(path("/test/new/newdir/dir/subdir/file2")));
424  }
425
426  public void testInputStreamClosedTwice() throws IOException {
427    //HADOOP-4760 according to Closeable#close() closing already-closed
428    //streams should have no effect.
429    Path src = path("/test/hadoop/file");
430    createFile(src);
431    FSDataInputStream in = fs.open(src);
432    in.close();
433    in.close();
434  }
435 
436  public void testOutputStreamClosedTwice() throws IOException {
437    //HADOOP-4760 according to Closeable#close() closing already-closed
438    //streams should have no effect.
439    Path src = path("/test/hadoop/file");
440    FSDataOutputStream out = fs.create(src);
441    out.writeChar('H'); //write some data
442    out.close();
443    out.close();
444  }
445 
446  protected Path path(String pathString) {
447    return new Path(pathString).makeQualified(fs);
448  }
449 
450  protected void createFile(Path path) throws IOException {
451    FSDataOutputStream out = fs.create(path);
452    out.write(data, 0, data.length);
453    out.close();
454  }
455 
456  private void rename(Path src, Path dst, boolean renameSucceeded,
457      boolean srcExists, boolean dstExists) throws IOException {
458    assertEquals("Rename result", renameSucceeded, fs.rename(src, dst));
459    assertEquals("Source exists", srcExists, fs.exists(src));
460    assertEquals("Destination exists", dstExists, fs.exists(dst));
461  }
462}
Note: See TracBrowser for help on using the repository browser.