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"));
}
}
参考文章: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中获取测试类及方法的名称实现方法
- 程序员方面对于测试的一些实践介绍
- SpringMVC 单元测试
- Android单元测试 笔记
- 单元测试基本
- 使用 Spring 进行单元测试
- junit最佳实践
- Eclipse快速上手指南之使用JUnit
- JUnit的框架设计及其使用的设计模式(精)
- 【转载】基于 JUnit 的 InstallAnywhere 安装程序图形界面自动测试框架
- myeclipse7.5配置junit
- JUnit
- junit 实例
- 使用Junit对Android应用进行单元测试
- Android Junit的使用
- 用TestSuite管理Junit单元测试用例
- ActivityInstrumentationTestCase2的研究成果
- 10款常用Java测试工具
- Junit中参数化的使用
- Junit的打包测试