Author: todd
Date: Fri Jun 29 22:23:53 2012
New Revision: 1355583

URL: http://svn.apache.org/viewvc?rev=1355583&view=rev
Log:
HDFS-3446. HostsFileReader silently ignores bad includes/excludes. Contributed 
by Matthew Jacobs.

Modified:
    
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/HostsFileReader.java
    
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestHostsFileReader.java

Modified: 
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/HostsFileReader.java
URL: 
http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/HostsFileReader.java?rev=1355583&r1=1355582&r2=1355583&view=diff
==============================================================================
--- 
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/HostsFileReader.java
 (original)
+++ 
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/HostsFileReader.java
 Fri Jun 29 22:23:53 2012
@@ -50,9 +50,6 @@ public class HostsFileReader {
 
   private void readFileToSet(String filename, Set<String> set) throws 
IOException {
     File file = new File(filename);
-    if (!file.exists()) {
-      return;
-    }
     FileInputStream fis = new FileInputStream(file);
     BufferedReader reader = null;
     try {

Modified: 
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestHostsFileReader.java
URL: 
http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestHostsFileReader.java?rev=1355583&r1=1355582&r2=1355583&view=diff
==============================================================================
--- 
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestHostsFileReader.java
 (original)
+++ 
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestHostsFileReader.java
 Fri Jun 29 22:23:53 2012
@@ -18,8 +18,8 @@
 package org.apache.hadoop.util;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileWriter;
-import java.util.Set;
 
 import org.junit.*;
 import static org.junit.Assert.*;
@@ -37,8 +37,6 @@ public class TestHostsFileReader {
   File INCLUDES_FILE = new File(HOSTS_TEST_DIR, "dfs.include");
   String excludesFile = HOSTS_TEST_DIR + "/dfs.exclude";
   String includesFile = HOSTS_TEST_DIR + "/dfs.include";
-  private Set<String> includes;
-  private Set<String> excludes;
 
   @Before
   public void setUp() throws Exception {
@@ -101,6 +99,43 @@ public class TestHostsFileReader {
   }
 
   /*
+   * Test creating a new HostsFileReader with nonexistent files
+   */
+  @Test
+  public void testCreateHostFileReaderWithNonexistentFile() throws Exception {
+    try {
+      new HostsFileReader(
+          HOSTS_TEST_DIR + "/doesnt-exist",
+          HOSTS_TEST_DIR + "/doesnt-exist");
+      Assert.fail("Should throw FileNotFoundException");
+    } catch (FileNotFoundException ex) {
+      // Exception as expected
+    }
+  }
+
+  /*
+   * Test refreshing an existing HostsFileReader with an includes file that no 
longer exists
+   */
+  @Test
+  public void testRefreshHostFileReaderWithNonexistentFile() throws Exception {
+    FileWriter efw = new FileWriter(excludesFile);
+    FileWriter ifw = new FileWriter(includesFile);
+
+    efw.close();
+
+    ifw.close();
+
+    HostsFileReader hfp = new HostsFileReader(includesFile, excludesFile);
+    assertTrue(INCLUDES_FILE.delete());
+    try {
+      hfp.refresh();
+      Assert.fail("Should throw FileNotFoundException");
+    } catch (FileNotFoundException ex) {
+      // Exception as expected
+    }
+  }
+
+  /*
    * Test for null file
    */
   @Test


Reply via email to