Skip to content

Commit 5ff445c

Browse files
committed
HADOOP-6730. Bug in FileContext#copy and provide base class for FileContext tests. (Ravi Phulari via jghoman)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@940112 13f79535-47bb-0310-9956-ffa450edef68
1 parent e8c0beb commit 5ff445c

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed

CHANGES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ Hadoop Change Log
22

33
Trunk (unreleased changes)
44

5+
BUG FIXES
6+
7+
HADOOP-6730. Bug in FileContext#copy and provide base class for FileContext
8+
tests. (Ravi Phulari via jghoman)
9+
510
Release 0.21.0 - Unreleased
611

712
INCOMPATIBLE CHANGES

src/java/org/apache/hadoop/fs/FileContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2050,8 +2050,8 @@ private void error(final String s, final String pattern, final int pos) {
20502050
*/
20512051
private void checkDest(String srcName, Path dst, boolean overwrite)
20522052
throws AccessControlException, IOException {
2053-
FileStatus dstFs = getFileStatus(dst);
20542053
try {
2054+
FileStatus dstFs = getFileStatus(dst);
20552055
if (dstFs.isDir()) {
20562056
if (null == srcName) {
20572057
throw new IOException("Target " + dst + " is a directory");

src/test/core/org/apache/hadoop/fs/FileContextTestHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
*/
1818
package org.apache.hadoop.fs;
1919

20+
import java.io.BufferedReader;
21+
import java.io.DataInputStream;
2022
import java.io.IOException;
2123
import java.io.FileNotFoundException;
24+
import java.io.InputStreamReader;
2225
import java.util.EnumSet;
2326

2427
import org.apache.hadoop.fs.Options.CreateOpts;
2528
import org.apache.hadoop.fs.Options.CreateOpts.BlockSize;
29+
import org.apache.hadoop.io.IOUtils;
2630

2731
/**
2832
* Helper class for unit tests.
@@ -145,4 +149,19 @@ public static boolean isSymlink(FileContext fc, Path p) throws IOException {
145149
return false;
146150
}
147151
}
152+
153+
public static void writeFile(FileContext fc, Path path,byte b[]) throws Exception {
154+
FSDataOutputStream out =
155+
fc.create(path,EnumSet.of(CreateFlag.CREATE), CreateOpts.createParent());
156+
out.write(b);
157+
out.close();
158+
}
159+
160+
public static byte[] readFile(FileContext fc, Path path, int len ) throws Exception {
161+
DataInputStream dis = fc.open(path);
162+
byte[] buffer = new byte[len];
163+
IOUtils.readFully(dis, buffer, 0, len);
164+
dis.close();
165+
return buffer;
166+
}
148167
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 java.util.Arrays;
21+
22+
import org.apache.hadoop.util.StringUtils;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import static org.junit.Assert.*;
27+
28+
import static org.apache.hadoop.fs.FileContextTestHelper.*;
29+
30+
/**
31+
* <p>
32+
* A collection of Util tests for the {@link FileContext#util()}.
33+
* This test should be used for testing an instance of {@link FileContext#util()}
34+
* that has been initialized to a specific default FileSystem such a
35+
* LocalFileSystem, HDFS,S3, etc.
36+
* </p>
37+
* <p>
38+
* To test a given {@link FileSystem} implementation create a subclass of this
39+
* test and override {@link #setUp()} to initialize the <code>fc</code>
40+
* {@link FileContext} instance variable.
41+
*
42+
* </p>
43+
*/
44+
public abstract class FileContextUtilBase {
45+
protected FileContext fc;
46+
47+
{
48+
try {
49+
((org.apache.commons.logging.impl.Log4JLogger)FileSystem.LOG).getLogger()
50+
.setLevel(org.apache.log4j.Level.DEBUG);
51+
} catch(Exception e) {
52+
System.out.println("Cannot change log level\n"
53+
+ StringUtils.stringifyException(e));
54+
}
55+
}
56+
57+
@Before
58+
public void setUp() throws Exception {
59+
fc.mkdir(getTestRootPath(fc), FileContext.DEFAULT_PERM, true);
60+
}
61+
62+
@After
63+
public void tearDown() throws Exception {
64+
fc.delete(getTestRootPath(fc), true);
65+
}
66+
67+
@Test
68+
public void testFcCopy() throws Exception{
69+
final String ts = "some random text";
70+
Path file1 = getTestRootPath(fc, "file1");
71+
Path file2 = getTestRootPath(fc, "file2");
72+
73+
writeFile(fc, file1, ts.getBytes());
74+
assertTrue(fc.util().exists(file1));
75+
fc.util().copy(file1, file2);
76+
77+
// verify that newly copied file2 exists
78+
assertTrue("Failed to copy file2 ", fc.util().exists(file2));
79+
// verify that file2 contains test string
80+
assertTrue("Copied files does not match ",Arrays.equals(ts.getBytes(),
81+
readFile(fc,file2,ts.getBytes().length)));
82+
}
83+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.junit.Before;
21+
22+
/**
23+
* Test Util for localFs using FileContext API.
24+
*/
25+
public class TestFcLocalFsUtil extends
26+
FileContextUtilBase {
27+
28+
@Before
29+
public void setUp() throws Exception {
30+
fc = FileContext.getLocalFSFileContext();
31+
super.setUp();
32+
}
33+
}

0 commit comments

Comments
 (0)