Java代码的无效Linux路径名的测试值 [英] Test value(s) for an invalid Linux path-name for Java code
问题描述
我正在对一些Java代码进行单元测试,这些Java代码必须将输入String
解析为路径名.我使用的是Java 7,因此解析后的值是一个Path
对象.
I'm unit testing some Java code that must parse an input String
as a path-name. I'm using Java 7, so the parsed value is a Path
object.
如果路径字符串不能转换为
Path
因此,要检查我的解析代码是否健壮,我需要一个测试值,该值将导致Paths.get
引发该异常.即,String
值不是有效路径.构成有效路径的是取决于系统(甚至是文件系统)的系统,因此,我对String
不是有效的POSIX路径,或者(失败了)不是有效的Linux路径特别感兴趣.
So, to check my parsing code is robust I need a test value that will cause Paths.get
to throw that exception. That is, a String
value that is not a valid path. What constitutes a valid path is system (even file-system) dependent, so I am specifically interested in a String
that is not a valid POSIX path, or (failing that) not a valid Linux path.
推荐答案
POSIX路径名不能包含空字符.因此,由单个NUL字符(Unicode代码点0)组成的String
将无效.即String
"\0"
.对于这种情况,我可以确认Open JDK抛出InvalidPathException
并显示消息Nul character not allowed
.
POSIX path-names may not contain null characters. Therefore a String
that consists of a single NUL character (Unicode code-point 0) will be invalid. That is, the String
"\0"
. I can confirm that Open JDK throws a InvalidPathException
, with the message Nul character not allowed
, for this case.
这篇关于Java代码的无效Linux路径名的测试值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!