|
@@ -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.DFSInputStream;
|
|
|
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 FSInputStream {
|
|
|
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);
|