0

I've been trying to devise a file path and if possible a file name that is impossible for Java to create on Linux/MacOS/Windows using the following code:

File directory = new Directory(dir);
directory.mkdirs(); // should always fail and not affect an existing file/dir

File file = new File(dir, filename);
file.createNewFile(); // should always fail and not affect an existing file/dir

This kind of path will be used in unit tests to prove certain error conditions are being handled correctly Assume the tests are being run as root (they aren't, but I want to focus on invalid paths vice privs). So far everything I've tried will fail on one platform (usually Windows) but not another (usually Linux).

Suggestions?

PS. I know about mock objects, PowerMock, etc. but really just want to get Java's as-is File class to fail to create the directory/file.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
SingleShot
  • 18,821
  • 13
  • 71
  • 101
  • Why do you need a test case that fails on all platforms? If you have 3 test cases, each failing on one of them, you can simply run them all and require that at least one of them fails. – Petr Jan 29 '13 at 23:57
  • There is also [a similar Java 7 question](http://stackoverflow.com/questions/23679391/test-values-for-an-invalid-linux-path-name-for-java-code), using `Path` rather than `File`. – Raedwald May 15 '14 at 13:19

1 Answers1

0

There are many reasons for a file name to be illegal in Linux (and probably similar ones in iOS), different ones entirely in Windows. What do you want to check? If you try to handle an illegal name the Java functions, they will fail. If it is that what you want to catch, cook up a name that is illegal for each (the conditions arent' so simple). If you want to check if your code catches this, I'd just defer the problem to the lower level: Try to create; if it fails, complain.

If you want to know what names are illegal for each system, better ask specifically for that.

vonbrand
  • 11,412
  • 8
  • 32
  • 52
  • Well... I want one path/name that is illegal for all three so my test will run on three correctly. I'm starting to think there is no "one path/name". – SingleShot Jan 29 '13 at 23:31