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.hdfs; |
---|
19 | |
---|
20 | import java.io.*; |
---|
21 | |
---|
22 | import org.apache.hadoop.conf.Configuration; |
---|
23 | import org.apache.hadoop.fs.FileStatus; |
---|
24 | import org.apache.hadoop.fs.FileSystem; |
---|
25 | import org.apache.hadoop.fs.Path; |
---|
26 | |
---|
27 | public class TestDFSRename extends junit.framework.TestCase { |
---|
28 | static int countLease(MiniDFSCluster cluster) { |
---|
29 | return cluster.getNameNode().namesystem.leaseManager.countLease(); |
---|
30 | } |
---|
31 | |
---|
32 | final Path dir = new Path("/test/rename/"); |
---|
33 | |
---|
34 | void list(FileSystem fs, String name) throws IOException { |
---|
35 | FileSystem.LOG.info("\n\n" + name); |
---|
36 | for(FileStatus s : fs.listStatus(dir)) { |
---|
37 | FileSystem.LOG.info("" + s.getPath()); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | public void testRename() throws Exception { |
---|
42 | Configuration conf = new Configuration(); |
---|
43 | MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null); |
---|
44 | try { |
---|
45 | FileSystem fs = cluster.getFileSystem(); |
---|
46 | assertTrue(fs.mkdirs(dir)); |
---|
47 | |
---|
48 | { //test lease |
---|
49 | Path a = new Path(dir, "a"); |
---|
50 | Path aa = new Path(dir, "aa"); |
---|
51 | Path b = new Path(dir, "b"); |
---|
52 | |
---|
53 | DataOutputStream a_out = fs.create(a); |
---|
54 | a_out.writeBytes("something"); |
---|
55 | a_out.close(); |
---|
56 | |
---|
57 | //should not have any lease |
---|
58 | assertEquals(0, countLease(cluster)); |
---|
59 | |
---|
60 | DataOutputStream aa_out = fs.create(aa); |
---|
61 | aa_out.writeBytes("something"); |
---|
62 | |
---|
63 | //should have 1 lease |
---|
64 | assertEquals(1, countLease(cluster)); |
---|
65 | list(fs, "rename0"); |
---|
66 | fs.rename(a, b); |
---|
67 | list(fs, "rename1"); |
---|
68 | aa_out.writeBytes(" more"); |
---|
69 | aa_out.close(); |
---|
70 | list(fs, "rename2"); |
---|
71 | |
---|
72 | //should not have any lease |
---|
73 | assertEquals(0, countLease(cluster)); |
---|
74 | } |
---|
75 | |
---|
76 | { // test non-existent destination |
---|
77 | Path dstPath = new Path("/c/d"); |
---|
78 | assertFalse(fs.exists(dstPath)); |
---|
79 | assertFalse(fs.rename(dir, dstPath)); |
---|
80 | } |
---|
81 | |
---|
82 | { // test rename /a/b to /a/b/c |
---|
83 | Path src = new Path("/a/b"); |
---|
84 | Path dst = new Path("/a/b/c"); |
---|
85 | |
---|
86 | DataOutputStream a_out = fs.create(new Path(src, "foo")); |
---|
87 | a_out.writeBytes("something"); |
---|
88 | a_out.close(); |
---|
89 | |
---|
90 | assertFalse(fs.rename(src, dst)); |
---|
91 | } |
---|
92 | |
---|
93 | fs.delete(dir, true); |
---|
94 | } finally { |
---|
95 | if (cluster != null) {cluster.shutdown();} |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|