views:

187

answers:

2

I have some Java library code which sometimes runs as an unsigned applet. Because of this it is not always allowed to do some operations (for instance checking for system properties).

I would like to run some unit tests with an Applet-like security manager so that I can verify that the code is either not performing any restricted operations, or correctly handling any security exceptions.

What is the best way to run these unit-tests with a realistic Security-Manager configuration? Preferable the solution would be something that can integrate with JUnit.

+2  A: 

Not a solution as such, but couldn't you implement your own subclass of SecurityManager, perhaps delegating all calls to a 'pseudo security manager' which would in turn be mocked by EasyMock/similar? You could then set this SecurityManager using System.setSecurityManager().

Provided you know what your applet should be able to do, you can then fail the test depending on your requirements.

I may have missed what you want to achieve, but that seems like one possible approach.

Rich
Do you know of a way to create a realistic Applet security manager? I am bit loathed to try to reproduce the Applet security rules myself, even if I got them right, I might struggle to keep them up to date. The approach I have taken so far is to define a thin adapter layer between the security privileged operations and the rest of my code, and then I mock that layer. But I think I really want to test the integration between my code and the real (or a realistic) SecurityManager.
flamingpenguin
I'm afraid I don't - is it possible to instantiate the Applet security manager by classname? Perhaps you could find that out from a test applet?
Rich
A: 

check out if this can help you

http://stackoverflow.com/questions/433065/what-is-the-best-mechanism-for-testing-applets

rsilva
Thanks. I'm not trying to test an applet as such (e.g. there is no UI in my code), it is some library code that is sometimes linked into and run within an applet (sometimes by someone else). I can't see anything in the FEST documentation about the SecurityManager, but I'll look around a bit more.
flamingpenguin