您的位置:首页 > 运维架构

hadoop权威指南 ShowFileStatusTest的运行调试

2017-01-04 13:01 267 查看
之前学到的实例都是有main函数的,一开始看到范例3-5 ShowFileStatusTest时,我以为这就是段示例代码,不能运行的,后来网上查到才知,这是可以调试的,需要用junit来运行(不知道为什么这些东西书里都不写,那些看这本书的大神们一上来就知道这些范例该怎么运行么?);我主要参考的文章是这篇

参考文章:http://blog.csdn.net/norriszhang/article/details/39648857

另外,这里顺便也放一下JUnit4使用教程-快速入门,能有个初步了解。

首先,参考的文章http://blog.csdn.net/norriszhang/article/details/39648857中说这个范例是一个test case,不知道是怎么看出来的,如果把方法也写出来就好了,以下是我实验的步骤

1. 需要添加以下jar包到classpath中:

hadoop-hdfs-2.7.0-tests.jar(MiniDFSCluster在这里,但不知是怎么知道的?文档里都查不到,就连HowToDevelopUnitTests
- Hadoop Wiki都只提到import语句,没说是哪个包)

hadoop-hdfs-2.7.0.jar(这个后来我删了,也能过)

junit-4.11.jar(这个自然了,最后要用它来做测试的)

2.编译总提示assertThat等等符号找不到,参照文章添加(当然,这个在HowToDevelopUnitTests
- Hadoop Wiki也有说)

import static org.junit.Assert.*;  

import static org.hamcrest.CoreMatchers.*; 

3.@Before,@After,@Test等提示找不到符号,分别添加import.org.junit.Before/After。。。解决,参照百度问答https://zhidao.baidu.com/question/541206548.html,分别引入以下

import org.junit.Before;

import org.junit.After;

import org.junit.Test;

4. 提示lessThanOrEqualTo符号没找到,这个死活没查到,先注释掉用到它的两行(根据is的用法,猜测可能是要自己实现Matcher<T>接口,现在先记下)

5.  编译提示使用或覆盖了已过时的API,网上说是警告,不管,执行, 

$HADOOP_HOME/bin/hadoop org.junit.runner.JUnitCore HDFS.ShowFileStatusTest

权限测试出错,将期忘权限改成实际权限,同样的blocksize也出错,同样的改过来,测试OK, 当然测试不是这么测的,随便改测试期望值是不妥的,应该是根据错误来修正实际值,但此时是为了跑通程序往下走,先不管那个了。

至此,测试程序本身能运行,顺便贴下代码

//file: ShowFileStatusTest.java

package HDFS;

import static org.junit.Assert.*;  

import static org.hamcrest.CoreMatchers.*; 

import org.junit.Before;

import org.junit.After;

import org.junit.Test;

import java.net.URI;

import java.io.*;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.fs.FileStatus;

import org.apache.hadoop.hdfs.MiniDFSCluster;

public class ShowFileStatusTest {

private MiniDFSCluster cluster;

private FileSystem fs;

@Before

public
void setUp() throws IOException {

Configuration conf = new Configuration();

if (System.getProperty("test.build.data") ==
null) {

System.setProperty("test.build.data",
"/tmp");

}

cluster = new MiniDFSCluster(conf,
1, true,
null);

fs = cluster.getFileSystem();

OutputStream out = fs.create(new Path("/dir/file"));

out.write("content".getBytes("UTF-8"));

out.close();

}

@After

public
void tearDown() throws IOException {

if( fs !=
null) {

fs.close();

}

if (cluster !=
null) {

cluster.shutdown();

}

}

//有几个@Test就会做几个测试,应该是标记其后的代码是要执行测试的

@Test(expected = FileNotFoundException.class)

public
void ThrowsFileNotFoundForNonExistentFile()
throws IOException {

fs.getFileStatus(new Path("no-such-file"));

}

@Test

public
void fileStatusForFile()
throws IOException {

Path file = new Path("/dir/file");

FileStatus stat = fs.getFileStatus(file);

assertThat(stat.getPath().toUri().getPath(), is("/dir/file"));

assertThat(stat.isDir(), is(false));

assertThat(stat.getLen(), is(7L));

//assertThat(stat.getModificationTime(), is(lessThanOrEqualTo(System.currentTimeMillis())));

assertThat(stat.getReplication(), is((short)1));

assertThat(stat.getBlockSize(), is(134217728L));

assertThat(stat.getOwner(), is("lishengda"));

assertThat(stat.getGroup(), is("supergroup"));

assertThat(stat.getPermission().toString(), is("rwxr-xr-x"));

}

@Test

public
void fileStatusForDirectory()
throws IOException {

Path dir = new Path("/dir");

FileStatus stat = fs.getFileStatus(dir);

assertThat(stat.getPath().toUri().getPath(), is("/dir"));

assertThat(stat.isDir(), is(true));

assertThat(stat.getLen(), is(0L));

// assertThat(stat.getModificationTime(), is(lessThanOrEqualTo(System.currentTimeMillis())));

assertThat(stat.getReplication(), is((short)
0));

assertThat(stat.getBlockSize(), is(0L));

assertThat(stat.getOwner(), is("lishengda"));

assertThat(stat.getGroup(), is("supergroup"));

assertThat(stat.getPermission().toString(), is("rwxr-xr-x"));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  junit