开发者

Is it possible to simulate a file creation exception in Java on Linux?

开发者 https://www.devze.com 2023-03-06 01:54 出处:网络
I had a simple test to make sure that an ant task was behaving properly, and so put in illegal file characters for windows and linux like so:

I had a simple test to make sure that an ant task was behaving properly, and so put in illegal file characters for windows and linux like so:

@Test(expected=BuildException.class)
public void destination开发者_JS百科DirectoryCreationException() throws Exception {
    backupTask.setSrcDir(testResourceDirectory);
    backupTask.setDestDir(new File("?/"));
    backupTask.execute();
}

Unfortunately, the reason that / is an illegal file character is that it's the path separator, so this will simply create the directory as normal.

Also char 0 or nul is interpreted by various apis (notably the native file apis and eclipse's debug variable explorer) as the end of stream and so just ignores everything afterwards.


No. Linux is far too awesome to fail for your pathetic little test.

Ok, so osgx actually proposed a solution that will work for my test in particular (if (s)he made an answer, he could get 15 rep ;)); make the directory a sub-directory of one that doesn't exist. e.g. backupTask.setDestDir(new File("/\\/?/"));. The only problem is that it's mildly different from the original test, but I'm ok with that.

Also note that (I believe) it will still succeed in creating the folder if .mkdirs() was called instead of mkdir()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号