You are viewing a plain text version of this content. The canonical link for it is here.
Posted to hdfs-commits@hadoop.apache.org by sh...@apache.org on 2011/07/23 02:22:09 UTC

svn commit: r1149770 - in /hadoop/common/trunk/hdfs: CHANGES.txt src/test/hdfs/org/apache/hadoop/hdfs/TestByteRangeInputStream.java src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestStreamFile.java

Author: shv
Date: Sat Jul 23 00:22:08 2011
New Revision: 1149770

URL: http://svn.apache.org/viewvc?rev=1149770&view=rev
Log:
HDFS-2116. Use Mokito in TestStreamFile and TestByteRangeInputStream. Contributed by Plamen Jeliazkov.

Modified:
    hadoop/common/trunk/hdfs/CHANGES.txt
    hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/TestByteRangeInputStream.java
    hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestStreamFile.java

Modified: hadoop/common/trunk/hdfs/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hdfs/CHANGES.txt?rev=1149770&r1=1149769&r2=1149770&view=diff
==============================================================================
--- hadoop/common/trunk/hdfs/CHANGES.txt (original)
+++ hadoop/common/trunk/hdfs/CHANGES.txt Sat Jul 23 00:22:08 2011
@@ -586,7 +586,10 @@ Trunk (unreleased changes)
 
     HDFS-2167.  Move dnsToSwitchMapping and hostsReader from FSNamesystem to
     DatanodeManager.  (szetszwo)
-    
+
+    HDFS-2116. Use Mokito in TestStreamFile and TestByteRangeInputStream.
+    (Plamen Jeliazkov via shv)
+
   OPTIMIZATIONS
 
     HDFS-1458. Improve checkpoint performance by avoiding unnecessary image

Modified: hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/TestByteRangeInputStream.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/TestByteRangeInputStream.java?rev=1149770&r1=1149769&r2=1149770&view=diff
==============================================================================
--- hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/TestByteRangeInputStream.java (original)
+++ hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/TestByteRangeInputStream.java Sat Jul 23 00:22:08 2011
@@ -17,25 +17,30 @@
  */
 package org.apache.hadoop.hdfs;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
 import java.net.URL;
 
-import org.apache.hadoop.hdfs.ByteRangeInputStream;
 import org.apache.hadoop.hdfs.ByteRangeInputStream.URLOpener;
-
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 class MockHttpURLConnection extends HttpURLConnection {
-  MockURL m;
-  
-  public MockHttpURLConnection(URL u, MockURL m) {
-    super(u); 
-    this.m = m;
+  private int responseCode = -1;
+  URL m;
+
+  public MockHttpURLConnection(URL u) {
+    super(u);
+    m = u;
   }
   
   public boolean usingProxy(){
@@ -46,7 +51,6 @@ class MockHttpURLConnection extends Http
   }
   
   public void connect() throws IOException {
-    m.setMsg("Connect: "+url+", Range: "+getRequestProperty("Range"));
   }
   
   public InputStream getInputStream() throws IOException {
@@ -64,8 +68,8 @@ class MockHttpURLConnection extends Http
   }
   
   public int getResponseCode() {
-    if (m.responseCode != -1) {
-      return m.responseCode;
+    if (responseCode != -1) {
+      return responseCode;
     } else {
       if (getRequestProperty("Range") == null) {
         return 200;
@@ -74,89 +78,67 @@ class MockHttpURLConnection extends Http
       }
     }
   }
-  
-}
 
-class MockURL extends URLOpener {
-  String msg;
-  public int responseCode = -1;
-  
-  public MockURL(URL u) {
-    super(u);
+  public void setResponseCode(int resCode) {
+    responseCode = resCode;
   }
 
-  public MockURL(String s) throws MalformedURLException {
-    this(new URL(s));
-  }
-
-  public HttpURLConnection openConnection() throws IOException {
-    return new MockHttpURLConnection(url, this);
-  }    
-
-  public void setMsg(String s) {
-    msg = s;
-  }
-  
-  public String getMsg() {
-    return msg;
-  }
 }
 
 public class TestByteRangeInputStream {
   
   @Test
-  public void testByteRange() throws IOException, InterruptedException {
-    MockURL o = new MockURL("http://test/");
-    MockURL r =  new MockURL((URL)null);
-    ByteRangeInputStream is = new ByteRangeInputStream(o, r);
+  public void testByteRange() throws IOException {
+    URLOpener ospy = spy(new URLOpener(new URL("http://test/")));
+    doReturn(new MockHttpURLConnection(ospy.getURL())).when(ospy)
+        .openConnection();
+    URLOpener rspy = spy(new URLOpener((URL) null));
+    doReturn(new MockHttpURLConnection(rspy.getURL())).when(rspy)
+        .openConnection();
+    ByteRangeInputStream is = new ByteRangeInputStream(ospy, rspy);
 
     assertEquals("getPos wrong", 0, is.getPos());
 
     is.read();
 
-    assertEquals("Initial call made incorrectly", 
-                 "Connect: http://test/, Range: null",
-                 o.getMsg());
+    assertNull("Initial call made incorrectly (Range Check)", ospy
+        .openConnection().getRequestProperty("Range"));
 
     assertEquals("getPos should be 1 after reading one byte", 1, is.getPos());
 
-    o.setMsg(null);
-
     is.read();
 
     assertEquals("getPos should be 2 after reading two bytes", 2, is.getPos());
 
-    assertNull("No additional connections should have been made (no seek)",
-               o.getMsg());
+    // No additional connections should have been made (no seek)
+
+    rspy.setURL(new URL("http://resolvedurl/"));
 
-    r.setMsg(null);
-    r.setURL(new URL("http://resolvedurl/"));
-    
     is.seek(100);
     is.read();
 
-    assertEquals("Seek to 100 bytes made incorrectly", 
-                 "Connect: http://resolvedurl/, Range: bytes=100-",
-                 r.getMsg());
+    assertEquals("Seek to 100 bytes made incorrectly (Range Check)",
+        "bytes=100-", rspy.openConnection().getRequestProperty("Range"));
 
-    assertEquals("getPos should be 101 after reading one byte", 101, is.getPos());
+    assertEquals("getPos should be 101 after reading one byte", 101,
+        is.getPos());
 
-    r.setMsg(null);
+    verify(rspy, times(2)).openConnection();
 
     is.seek(101);
     is.read();
 
-    assertNull("Seek to 101 should not result in another request", r.getMsg());
+    verify(rspy, times(2)).openConnection();
+
+    // Seek to 101 should not result in another request"
 
-    r.setMsg(null);
     is.seek(2500);
     is.read();
 
-    assertEquals("Seek to 2500 bytes made incorrectly", 
-                 "Connect: http://resolvedurl/, Range: bytes=2500-",
-                 r.getMsg());
+    assertEquals("Seek to 2500 bytes made incorrectly (Range Check)",
+        "bytes=2500-", rspy.openConnection().getRequestProperty("Range"));
 
-    r.responseCode = 200;
+    ((MockHttpURLConnection) rspy.openConnection()).setResponseCode(200);
     is.seek(500);
     
     try {
@@ -168,7 +150,7 @@ public class TestByteRangeInputStream {
                    "HTTP_PARTIAL expected, received 200", e.getMessage());
     }
 
-    r.responseCode = 206;
+    ((MockHttpURLConnection) rspy.openConnection()).setResponseCode(206);
     is.seek(0);
 
     try {

Modified: hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestStreamFile.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestStreamFile.java?rev=1149770&r1=1149769&r2=1149770&view=diff
==============================================================================
--- hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestStreamFile.java (original)
+++ hadoop/common/trunk/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestStreamFile.java Sat Jul 23 00:22:08 2011
@@ -18,6 +18,9 @@
 package org.apache.hadoop.hdfs.server.namenode;
 
 import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
@@ -32,9 +35,6 @@ import javax.servlet.ServletOutputStream
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.junit.Test;
-import static org.junit.Assert.*;
-
 import org.apache.hadoop.fs.FSInputStream;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -43,6 +43,7 @@ import org.apache.hadoop.hdfs.DFSInputSt
 import org.apache.hadoop.hdfs.HdfsConfiguration;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
 import org.apache.hadoop.hdfs.server.common.JspHelper;
+import org.junit.Test;
 import org.mockito.Mockito;
 import org.mortbay.jetty.InclusiveByteRange;
 
@@ -65,137 +66,10 @@ class MockFSInputStream extends FSInputS
   public long getPos() throws IOException {
     return currentPos;
   }
-  
-  public boolean seekToNewSource(long targetPos) throws IOException {
-    return false;
-  }
-}
-
-
-class MockHttpServletResponse implements HttpServletResponse {
-
-  private int status = -1;
-  
-  public MockHttpServletResponse() {
-  }
-  
-  public int getStatus() {
-    return status;
-  }
-  
-  
-  public void setStatus(int sc) {
-    status = sc;
-  }
-  
-  @Deprecated
-  public void setStatus(int sc, java.lang.String sm) {
-  }
-  
-  public void addIntHeader(String name, int value) {
-  }
 
-  public void setIntHeader(String name, int value) { 
-  }
-  
-  public void addHeader(String name, String value) {
-  }
-
-  public void setHeader(String name, String value) {
-  }
-  
-  public void addDateHeader(java.lang.String name, long date) {
-  }
-  
-  public void setDateHeader(java.lang.String name, long date) {
-  }
-
-  public void sendRedirect(java.lang.String location) { 
-  }
-  
-  public void sendError(int e) {
-  }
-  
-  public void sendError(int a, java.lang.String b) {
-  }
-  
-  @Deprecated
-  public String encodeRedirectUrl(java.lang.String a) {
-    return null;
-  }
-  
-  @Deprecated
-  public String encodeUrl(java.lang.String url) {
-    return null;
-  }
-  
-  public String encodeRedirectURL(java.lang.String url) {
-    return null;
-  }
-  
-  @Deprecated
-  public String encodeURL(java.lang.String url) {
-    return null;
-  }
-  
-  public boolean containsHeader(java.lang.String name) {
-    return false;
-  }
-  
-  public void addCookie(javax.servlet.http.Cookie cookie) {
-  }
-  
-  public java.util.Locale getLocale() {
-    return null;
-  }
-  
-  public void setLocale(java.util.Locale loc) {
-  }
-  
-  public void reset() {
-  }
-  
-  public boolean isCommitted() {
+  public boolean seekToNewSource(long targetPos) throws IOException {
     return false;
   }
-  
-  public void resetBuffer() {
-  }
-  
-  public void flushBuffer() {
-  }
-  
-  public int getBufferSize() {
-    return 0;
-  }
-  
-  public void setBufferSize(int size) {
-  }
-  
-  public void setContentType(java.lang.String type) {
-  }
-  
-  public void setContentLength(int len) {
-  }
-  
-  public void setCharacterEncoding(java.lang.String charset) {
-  }
-  
-  public java.io.PrintWriter getWriter() {
-    return null;
-  }
-  
-  public javax.servlet.ServletOutputStream getOutputStream() {
-    return null;
-  }
-  
-  public java.lang.String getContentType() {
-    return null;
-  }
-  
-  public java.lang.String getCharacterEncoding() {
-    return null;
-  }
 }
 
 
@@ -235,7 +109,7 @@ public class TestStreamFile {
   }
   
   @Test
-  public void testWriteTo() throws IOException, InterruptedException {
+  public void testWriteTo() throws IOException {
 
     FSInputStream fsin = new MockFSInputStream();
     ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -272,45 +146,50 @@ public class TestStreamFile {
   }
   
   @Test
-  public void testSendPartialData() throws IOException, InterruptedException {
+  public void testSendPartialData() throws IOException {
     FSInputStream in = new MockFSInputStream();
     ByteArrayOutputStream os = new ByteArrayOutputStream();
 
     // test if multiple ranges, then 416
     { 
       List<InclusiveByteRange> ranges = strToRanges("0-,10-300", 500);
-      MockHttpServletResponse response = new MockHttpServletResponse();
+      HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
       StreamFile.sendPartialData(in, os, response, 500, ranges);
-      assertEquals("Multiple ranges should result in a 416 error",
-                   416, response.getStatus());
+
+      // Multiple ranges should result in a 416 error
+      Mockito.verify(response).setStatus(416);
     }
                               
     // test if no ranges, then 416
     { 
       os.reset();
-      MockHttpServletResponse response = new MockHttpServletResponse();
+      HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
       StreamFile.sendPartialData(in, os, response, 500, null);
-      assertEquals("No ranges should result in a 416 error",
-                   416, response.getStatus());
+
+      // No ranges should result in a 416 error
+      Mockito.verify(response).setStatus(416);
     }
 
     // test if invalid single range (out of bounds), then 416
     { 
       List<InclusiveByteRange> ranges = strToRanges("600-800", 500);
-      MockHttpServletResponse response = new MockHttpServletResponse();
+      HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
       StreamFile.sendPartialData(in, os, response, 500, ranges);
-      assertEquals("Single (but invalid) range should result in a 416",
-                   416, response.getStatus());
+
+      // Single (but invalid) range should result in a 416
+      Mockito.verify(response).setStatus(416);
     }
 
       
     // test if one (valid) range, then 206
     { 
       List<InclusiveByteRange> ranges = strToRanges("100-300", 500);
-      MockHttpServletResponse response = new MockHttpServletResponse();
+      HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
       StreamFile.sendPartialData(in, os, response, 500, ranges);
-      assertEquals("Single (valid) range should result in a 206",
-                   206, response.getStatus());
+
+      // Single (valid) range should result in a 206
+      Mockito.verify(response).setStatus(206);
+
       assertArrayEquals("Byte range from 100-300",
                         getOutputArray(100, 201),
                         os.toByteArray());
@@ -380,8 +259,7 @@ public class TestStreamFile {
     }
   }
 
-  private void setUpForDoGetTest(MiniDFSCluster cluster, Path testFile)
-      throws IOException {
+  private void setUpForDoGetTest(MiniDFSCluster cluster, Path testFile) {
 
     Mockito.doReturn(CONF).when(mockServletContext).getAttribute(
         JspHelper.CURRENT_CONF);