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:
Jakob Homan 2010-05-01 21:08:34 +00:00
parent 14f02ef0f5
commit fe49f6e473
5 changed files with 141 additions and 1 deletions

View File

@ -2,6 +2,11 @@ Hadoop Change Log
Trunk (unreleased changes) 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 Release 0.21.0 - Unreleased
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -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) private void checkDest(String srcName, Path dst, boolean overwrite)
throws AccessControlException, IOException { throws AccessControlException, IOException {
FileStatus dstFs = getFileStatus(dst);
try { try {
FileStatus dstFs = getFileStatus(dst);
if (dstFs.isDir()) { if (dstFs.isDir()) {
if (null == srcName) { if (null == srcName) {
throw new IOException("Target " + dst + " is a directory"); throw new IOException("Target " + dst + " is a directory");

View File

@ -17,12 +17,16 @@
*/ */
package org.apache.hadoop.fs; package org.apache.hadoop.fs;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.EnumSet; import java.util.EnumSet;
import org.apache.hadoop.fs.Options.CreateOpts; import org.apache.hadoop.fs.Options.CreateOpts;
import org.apache.hadoop.fs.Options.CreateOpts.BlockSize; import org.apache.hadoop.fs.Options.CreateOpts.BlockSize;
import org.apache.hadoop.io.IOUtils;
/** /**
* Helper class for unit tests. * Helper class for unit tests.
@ -145,4 +149,19 @@ public static boolean isSymlink(FileContext fc, Path p) throws IOException {
return false; 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;
}
} }

View 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)));
}
}

View 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();
}
}