HDFS-4512. Cover package org.apache.hadoop.hdfs.server.common with tests. Contributed by Vadim Bondarev.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1528098 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kihwal Lee 2013-10-01 14:05:16 +00:00
parent 85f5626076
commit dc2d77bde2
2 changed files with 157 additions and 3 deletions

View File

@ -62,6 +62,9 @@ Release 2.3.0 - UNRELEASED
HDFS-4517. Cover class RemoteBlockReader with unit tests. (Vadim Bondarev
and Dennis Y via kihwal)
HDFS-4512. Cover package org.apache.hadoop.hdfs.server.common with tests.
(Vadim Bondarev via kihwal)
OPTIMIZATIONS
HDFS-5239. Allow FSNamesystem lock fairness to be configurable (daryn)

View File

@ -17,13 +17,18 @@
*/
package org.apache.hadoop.hdfs.server.common;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doAnswer;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.io.StringReader;
import java.net.InetSocketAddress;
import java.text.MessageFormat;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
@ -36,10 +41,14 @@ import javax.xml.parsers.ParserConfigurationException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.HdfsConfiguration;
import org.apache.hadoop.hdfs.protocol.DatanodeID;
import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier;
import org.apache.hadoop.hdfs.server.blockmanagement.DatanodeDescriptor;
import org.apache.hadoop.hdfs.server.namenode.NameNodeHttpServer;
import org.apache.hadoop.hdfs.web.resources.DoAsParam;
import org.apache.hadoop.hdfs.web.resources.UserParam;
import org.apache.hadoop.io.DataInputBuffer;
import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
@ -51,11 +60,14 @@ import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSecret
import org.junit.Assert;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import static com.google.common.base.Strings.*;
public class TestJspHelper {
@ -439,4 +451,143 @@ public class TestJspHelper {
ugi.getAuthenticationMethod());
}
}
@Test
public void testSortNodeByFields() throws Exception {
DatanodeID dnId1 = new DatanodeID("127.0.0.1", "localhost1", "storage1", 1234, 2345, 6454);
DatanodeID dnId2 = new DatanodeID("127.0.0.1", "localhost2", "storage2", 1235, 2346, 6455);
DatanodeDescriptor dnDesc1 = new DatanodeDescriptor(dnId1, 1024, 100, 924, 100, 10, 2);
DatanodeDescriptor dnDesc2 = new DatanodeDescriptor(dnId2, 2500, 200, 1848, 200, 20, 1);
ArrayList<DatanodeDescriptor> live = new ArrayList<DatanodeDescriptor>();
live.add(dnDesc1);
live.add(dnDesc2);
JspHelper.sortNodeList(live, "unexists", "ASC");
Assert.assertEquals(dnDesc1, live.get(0));
Assert.assertEquals(dnDesc2, live.get(1));
JspHelper.sortNodeList(live, "unexists", "DSC");
Assert.assertEquals(dnDesc2, live.get(0));
Assert.assertEquals(dnDesc1, live.get(1));
// test sorting by capacity
JspHelper.sortNodeList(live, "capacity", "ASC");
Assert.assertEquals(dnDesc1, live.get(0));
Assert.assertEquals(dnDesc2, live.get(1));
JspHelper.sortNodeList(live, "capacity", "DSC");
Assert.assertEquals(dnDesc2, live.get(0));
Assert.assertEquals(dnDesc1, live.get(1));
// test sorting by used
JspHelper.sortNodeList(live, "used", "ASC");
Assert.assertEquals(dnDesc1, live.get(0));
Assert.assertEquals(dnDesc2, live.get(1));
JspHelper.sortNodeList(live, "used", "DSC");
Assert.assertEquals(dnDesc2, live.get(0));
Assert.assertEquals(dnDesc1, live.get(1));
// test sorting by nondfsused
JspHelper.sortNodeList(live, "nondfsused", "ASC");
Assert.assertEquals(dnDesc1, live.get(0));
Assert.assertEquals(dnDesc2, live.get(1));
JspHelper.sortNodeList(live, "nondfsused", "DSC");
Assert.assertEquals(dnDesc2, live.get(0));
Assert.assertEquals(dnDesc1, live.get(1));
// test sorting by remaining
JspHelper.sortNodeList(live, "remaining", "ASC");
Assert.assertEquals(dnDesc1, live.get(0));
Assert.assertEquals(dnDesc2, live.get(1));
JspHelper.sortNodeList(live, "remaining", "DSC");
Assert.assertEquals(dnDesc2, live.get(0));
Assert.assertEquals(dnDesc1, live.get(1));
}
@Test
public void testPrintMethods() throws IOException {
JspWriter out = mock(JspWriter.class);
HttpServletRequest req = mock(HttpServletRequest.class);
final StringBuffer buffer = new StringBuffer();
ArgumentCaptor<String> arg = ArgumentCaptor.forClass(String.class);
doAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invok) {
Object[] args = invok.getArguments();
buffer.append(args[0]);
return null;
}
}).when(out).print(arg.capture());
JspHelper.createTitle(out, req, "testfile.txt");
verify(out, times(1)).print(Mockito.anyString());
JspHelper.addTableHeader(out);
verify(out, times(1 + 2)).print(anyString());
JspHelper.addTableRow(out, new String[] {" row11", "row12 "});
verify(out, times(1 + 2 + 4)).print(anyString());
JspHelper.addTableRow(out, new String[] {" row11", "row12 "}, 3);
verify(out, times(1 + 2 + 4 + 4)).print(Mockito.anyString());
JspHelper.addTableRow(out, new String[] {" row21", "row22"});
verify(out, times(1 + 2 + 4 + 4 + 4)).print(anyString());
JspHelper.addTableFooter(out);
verify(out, times(1 + 2 + 4 + 4 + 4 + 1)).print(anyString());
assertFalse(isNullOrEmpty(buffer.toString()));
}
@Test
public void testReadWriteReplicaState() {
try {
DataOutputBuffer out = new DataOutputBuffer();
DataInputBuffer in = new DataInputBuffer();
for (HdfsServerConstants.ReplicaState repState : HdfsServerConstants.ReplicaState
.values()) {
repState.write(out);
in.reset(out.getData(), out.getLength());
HdfsServerConstants.ReplicaState result = HdfsServerConstants.ReplicaState
.read(in);
assertTrue("testReadWrite error !!!", repState == result);
out.reset();
in.reset();
}
} catch (Exception ex) {
fail("testReadWrite ex error ReplicaState");
}
}
@Test
public void testUpgradeStatusReport() {
short status = 6;
int version = 15;
String EXPECTED_NOTF_PATTERN = "Upgrade for version {0} has been completed.\nUpgrade is not finalized.";
String EXPECTED_PATTERN = "Upgrade for version {0} is in progress. Status = {1}%";
UpgradeStatusReport upgradeStatusReport = new UpgradeStatusReport(version,
status, true);
assertTrue(upgradeStatusReport.getVersion() == version);
assertTrue(upgradeStatusReport.getUpgradeStatus() == status);
assertTrue(upgradeStatusReport.isFinalized());
assertEquals(MessageFormat.format(EXPECTED_PATTERN, version, status),
upgradeStatusReport.getStatusText(true));
status += 100;
upgradeStatusReport = new UpgradeStatusReport(version, status, false);
assertFalse(upgradeStatusReport.isFinalized());
assertTrue(upgradeStatusReport.toString().equals(
MessageFormat.format(EXPECTED_NOTF_PATTERN, version)));
assertTrue(upgradeStatusReport.getStatusText(false).equals(
MessageFormat.format(EXPECTED_NOTF_PATTERN, version)));
assertTrue(upgradeStatusReport.getStatusText(true).equals(
MessageFormat.format(EXPECTED_NOTF_PATTERN, version)));
}
}