Directory permissions in Java 5

V

Veerle

Hi,

I am writing unit tests that have to be able to run anywhere.
(developers pc's are Win XP ; the server is Linux). In order to get
the coverage to 100%, I want to test a piece of code where the
creation of a temporary file fails.

What I want to do is create a subfolder in the temporary folder (=
System.getProperty("java.io.tmpdir")) in the setup part of the unit
test. Then I want to change the permissions of the subfolder so that
the creation of a temporary file in it fails by throwing an
IOException or some other Exception. This way I can garantee that the
unit test will work on other developers pcs and on the server as well.
In the tearDown part, I clean up the folder that I've created.

I have to do this in Java 5, but when looking at the File api for java
5, I only see the possibility to set the folder to readonly (method
setReadOnly()). But appearantly, in Win XP, when a folder is readonly,
it is still possible to create a file in it (tried it in the Windows
Explorer and it works there as well). So this doesn't give me my
IOException that I want. So I need to find a way to actually change
the folder permissions so that for the current user the folder is not
accessible. In Java 6, I see in the api that File has a lot more
methods: setReadable, setWriteable which probably would of help, if I
had Java 6.

Is there a way in Java 5 to modify the folder permissions in such a
way that is OS independent?

Veerle
 
C

Claudio Nieder

Hi,
setReadOnly()). But appearantly, in Win XP, when a folder is readonly,
it is still possible to create a file in it (tried it in the Windows
Explorer and it works there as well).

If you meant to say that even if you set it to readonly in windows
explorer, then you still can create a file in it, then you can forget it,
because if the OS does not support blocking the file creation then you
will never be able to do it in Java.

If you meant to say that setting readonly through Javas setReadOnly()
does not stop file creation, but Windows itself could do it - e.g. by
changing the directory permission in windows explorer - then you are just
out of luck in doing a portable pure Java solution. You then need to use
OS calls to set the permission either through JNI or using JNA (https://
jna.dev.java.net/).

claudio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top