hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r1179484 [2/2] - in /hadoop/common/branches/HDFS-1623/hadoop-common-project/hadoop-common: ./ src/main/docs/ src/main/java/ src/main/java/org/apache/hadoop/conf/ src/main/java/org/apache/hadoop/http/ src/main/java/org/apache/hadoop/io/retry...
Date Thu, 06 Oct 2011 01:16:56 GMT
Modified: hadoop/common/branches/HDFS-1623/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-1623/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java?rev=1179484&r1=1179483&r2=1179484&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-1623/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
(original)
+++ hadoop/common/branches/HDFS-1623/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
Thu Oct  6 01:16:48 2011
@@ -17,10 +17,15 @@
  */
 package org.apache.hadoop.net;
 
+import junit.framework.AssertionFailedError;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
 
+import java.io.IOException;
+import java.net.BindException;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.Socket;
@@ -34,6 +39,12 @@ import org.apache.hadoop.conf.Configurat
 
 public class TestNetUtils {
 
+  private static final Log LOG = LogFactory.getLog(TestNetUtils.class);
+  private static final int DEST_PORT = 4040;
+  private static final String DEST_PORT_NAME = Integer.toString(DEST_PORT);
+  private static final int LOCAL_PORT = 8080;
+  private static final String LOCAL_PORT_NAME = Integer.toString(LOCAL_PORT);
+
   /**
    * Test that we can't accidentally connect back to the connecting socket due
    * to a quirk in the TCP spec.
@@ -120,4 +131,100 @@ public class TestNetUtils {
     }
     assertFalse(NetUtils.isLocalAddress(InetAddress.getByName("8.8.8.8")));
   }
+
+  @Test
+  public void testWrapConnectException() throws Throwable {
+    IOException e = new ConnectException("failed");
+    IOException wrapped = verifyExceptionClass(e, ConnectException.class);
+    assertInException(wrapped, "failed");
+    assertWikified(wrapped);
+    assertInException(wrapped, "localhost");
+    assertRemoteDetailsIncluded(wrapped);
+    assertInException(wrapped, "/ConnectionRefused");
+  }
+
+  @Test
+  public void testWrapBindException() throws Throwable {
+    IOException e = new BindException("failed");
+    IOException wrapped = verifyExceptionClass(e, BindException.class);
+    assertInException(wrapped, "failed");
+    assertLocalDetailsIncluded(wrapped);
+    assertNotInException(wrapped, DEST_PORT_NAME);
+    assertInException(wrapped, "/BindException");
+  }
+
+  @Test
+  public void testWrapUnknownHostException() throws Throwable {
+    IOException e = new UnknownHostException("failed");
+    IOException wrapped = verifyExceptionClass(e, UnknownHostException.class);
+    assertInException(wrapped, "failed");
+    assertWikified(wrapped);
+    assertInException(wrapped, "localhost");
+    assertRemoteDetailsIncluded(wrapped);
+    assertInException(wrapped, "/UnknownHost");
+  }
+
+  private void assertRemoteDetailsIncluded(IOException wrapped)
+      throws Throwable {
+    assertInException(wrapped, "desthost");
+    assertInException(wrapped, DEST_PORT_NAME);
+  }
+
+  private void assertLocalDetailsIncluded(IOException wrapped)
+      throws Throwable {
+    assertInException(wrapped, "localhost");
+    assertInException(wrapped, LOCAL_PORT_NAME);
+  }
+
+  private void assertWikified(Exception e) throws Throwable {
+    assertInException(e, NetUtils.HADOOP_WIKI);
+  }
+
+  private void assertInException(Exception e, String text) throws Throwable {
+    String message = extractExceptionMessage(e);
+    if (!(message.contains(text))) {
+      throw new AssertionFailedError("Wrong text in message "
+                                         + "\"" + message + "\""
+                                         + " expected \"" + text + "\"")
+          .initCause(e);
+    }
+  }
+
+  private String extractExceptionMessage(Exception e) throws Throwable {
+    assertNotNull("Null Exception", e);
+    String message = e.getMessage();
+    if (message == null) {
+      throw new AssertionFailedError("Empty text in exception " + e)
+          .initCause(e);
+    }
+    return message;
+  }
+
+  private void assertNotInException(Exception e, String text)
+      throws Throwable{
+    String message = extractExceptionMessage(e);
+    if (message.contains(text)) {
+      throw new AssertionFailedError("Wrong text in message "
+                                         + "\"" + message + "\""
+                                         + " did not expect \"" + text + "\"")
+          .initCause(e);
+    }
+  }
+
+  private IOException verifyExceptionClass(IOException e,
+                                           Class expectedClass)
+      throws Throwable {
+    assertNotNull("Null Exception", e);
+    IOException wrapped =
+        NetUtils.wrapException("desthost", DEST_PORT,
+                               "localhost", LOCAL_PORT,
+                               e);
+    LOG.info(wrapped.toString(), wrapped);
+    if(!(wrapped.getClass().equals(expectedClass))) {
+      throw new AssertionFailedError("Wrong exception class; expected "
+                                         + expectedClass
+                                         + " got " + wrapped.getClass() + ": " + wrapped).initCause(wrapped);
+    }
+    return wrapped;
+  }
 }



Mime
View raw message