我想编写一个JUnit测试,确保我们自己实现的xmllint就像"xmllint --c14n“一样工作。
ProcessBuilder pb = new ProcessBuilder("xmllint", "--c14n", "-");
Process p = pb.start();如何向进程提供测试文件,并获得类似于unix管道和过滤器的输出以进行比较?
发布于 2011-11-09 16:52:56
使用Java7,您可以使用ProcessBuilder.redirectInput(java.io.File)方法:
ProcessBuilder pb = new ProcessBuilder( ... );
pb.redirectInput("/path/to/testFile.txt");
Process p = pb.start();使用Java6时,您需要使用I/O流自己完成此操作。
import org.apache.commons.io.IOUtils;
FileInputStream testFile = ...
OutputStream processInput = p.getOutputStream();
IOUtils.copy(testFile, processInput);
InputStream processOutput = p.getInputStream();
// Either parse this, or IOUtils.copy it to a file and do a diff of some kind.https://stackoverflow.com/questions/8062243
复制相似问题