diff --git a/CHANGES.txt b/CHANGES.txt index 9e6ca9d0a7..e1e9f24b17 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 diff --git a/src/java/org/apache/hadoop/fs/FileContext.java b/src/java/org/apache/hadoop/fs/FileContext.java index 01d401cedc..5526619369 100644 --- a/src/java/org/apache/hadoop/fs/FileContext.java +++ b/src/java/org/apache/hadoop/fs/FileContext.java @@ -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"); diff --git a/src/test/core/org/apache/hadoop/fs/FileContextTestHelper.java b/src/test/core/org/apache/hadoop/fs/FileContextTestHelper.java index ff1bac03c6..b08a328ad7 100644 --- a/src/test/core/org/apache/hadoop/fs/FileContextTestHelper.java +++ b/src/test/core/org/apache/hadoop/fs/FileContextTestHelper.java @@ -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; + } } diff --git a/src/test/core/org/apache/hadoop/fs/FileContextUtilBase.java b/src/test/core/org/apache/hadoop/fs/FileContextUtilBase.java new file mode 100644 index 0000000000..1eac238e04 --- /dev/null +++ b/src/test/core/org/apache/hadoop/fs/FileContextUtilBase.java @@ -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.*; + +/** + *

+ * 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. + *

+ *

+ * To test a given {@link FileSystem} implementation create a subclass of this + * test and override {@link #setUp()} to initialize the fc + * {@link FileContext} instance variable. + * + *

+ */ +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))); + } +} \ No newline at end of file diff --git a/src/test/core/org/apache/hadoop/fs/TestFcLocalFsUtil.java b/src/test/core/org/apache/hadoop/fs/TestFcLocalFsUtil.java new file mode 100644 index 0000000000..bc1126f231 --- /dev/null +++ b/src/test/core/org/apache/hadoop/fs/TestFcLocalFsUtil.java @@ -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(); + } +}