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
This commit is contained in:
parent
14f02ef0f5
commit
fe49f6e473
@ -2,6 +2,11 @@ Hadoop Change Log
|
||||
|
||||
Trunk (unreleased changes)
|
||||
|
||||
BUG FIXES
|
||||
|
||||
HADOOP-6730. Bug in FileContext#copy and provide base class for FileContext
|
||||
tests. (Ravi Phulari via jghoman)
|
||||
|
||||
Release 0.21.0 - Unreleased
|
||||
|
||||
INCOMPATIBLE CHANGES
|
||||
|
@ -2050,8 +2050,8 @@ private void error(final String s, final String pattern, final int pos) {
|
||||
*/
|
||||
private void checkDest(String srcName, Path dst, boolean overwrite)
|
||||
throws AccessControlException, IOException {
|
||||
FileStatus dstFs = getFileStatus(dst);
|
||||
try {
|
||||
FileStatus dstFs = getFileStatus(dst);
|
||||
if (dstFs.isDir()) {
|
||||
if (null == srcName) {
|
||||
throw new IOException("Target " + dst + " is a directory");
|
||||
|
@ -17,12 +17,16 @@
|
||||
*/
|
||||
package org.apache.hadoop.fs;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.apache.hadoop.fs.Options.CreateOpts;
|
||||
import org.apache.hadoop.fs.Options.CreateOpts.BlockSize;
|
||||
import org.apache.hadoop.io.IOUtils;
|
||||
|
||||
/**
|
||||
* Helper class for unit tests.
|
||||
@ -145,4 +149,19 @@ public static boolean isSymlink(FileContext fc, Path p) throws IOException {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeFile(FileContext fc, Path path,byte b[]) throws Exception {
|
||||
FSDataOutputStream out =
|
||||
fc.create(path,EnumSet.of(CreateFlag.CREATE), CreateOpts.createParent());
|
||||
out.write(b);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static byte[] readFile(FileContext fc, Path path, int len ) throws Exception {
|
||||
DataInputStream dis = fc.open(path);
|
||||
byte[] buffer = new byte[len];
|
||||
IOUtils.readFully(dis, buffer, 0, len);
|
||||
dis.close();
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
83
src/test/core/org/apache/hadoop/fs/FileContextUtilBase.java
Normal file
83
src/test/core/org/apache/hadoop/fs/FileContextUtilBase.java
Normal file
@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.hadoop.fs;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.hadoop.util.StringUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import static org.apache.hadoop.fs.FileContextTestHelper.*;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A collection of Util tests for the {@link FileContext#util()}.
|
||||
* This test should be used for testing an instance of {@link FileContext#util()}
|
||||
* that has been initialized to a specific default FileSystem such a
|
||||
* LocalFileSystem, HDFS,S3, etc.
|
||||
* </p>
|
||||
* <p>
|
||||
* To test a given {@link FileSystem} implementation create a subclass of this
|
||||
* test and override {@link #setUp()} to initialize the <code>fc</code>
|
||||
* {@link FileContext} instance variable.
|
||||
*
|
||||
* </p>
|
||||
*/
|
||||
public abstract class FileContextUtilBase {
|
||||
protected FileContext fc;
|
||||
|
||||
{
|
||||
try {
|
||||
((org.apache.commons.logging.impl.Log4JLogger)FileSystem.LOG).getLogger()
|
||||
.setLevel(org.apache.log4j.Level.DEBUG);
|
||||
} catch(Exception e) {
|
||||
System.out.println("Cannot change log level\n"
|
||||
+ StringUtils.stringifyException(e));
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fc.mkdir(getTestRootPath(fc), FileContext.DEFAULT_PERM, true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
fc.delete(getTestRootPath(fc), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFcCopy() throws Exception{
|
||||
final String ts = "some random text";
|
||||
Path file1 = getTestRootPath(fc, "file1");
|
||||
Path file2 = getTestRootPath(fc, "file2");
|
||||
|
||||
writeFile(fc, file1, ts.getBytes());
|
||||
assertTrue(fc.util().exists(file1));
|
||||
fc.util().copy(file1, file2);
|
||||
|
||||
// verify that newly copied file2 exists
|
||||
assertTrue("Failed to copy file2 ", fc.util().exists(file2));
|
||||
// verify that file2 contains test string
|
||||
assertTrue("Copied files does not match ",Arrays.equals(ts.getBytes(),
|
||||
readFile(fc,file2,ts.getBytes().length)));
|
||||
}
|
||||
}
|
33
src/test/core/org/apache/hadoop/fs/TestFcLocalFsUtil.java
Normal file
33
src/test/core/org/apache/hadoop/fs/TestFcLocalFsUtil.java
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.hadoop.fs;
|
||||
|
||||
import org.junit.Before;
|
||||
|
||||
/**
|
||||
* Test Util for localFs using FileContext API.
|
||||
*/
|
||||
public class TestFcLocalFsUtil extends
|
||||
FileContextUtilBase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fc = FileContext.getLocalFSFileContext();
|
||||
super.setUp();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user