如何对预计在小程序安全管理器中运行的 Java 代码进行单元测试
我有一些 Java 库代码,有时作为未签名的小程序运行。因此,并不总是允许执行某些操作(例如检查系统属性)。
我想使用类似 Applet 的安全管理器运行一些单元测试,以便我可以验证代码是否没有执行任何受限操作,或者是否正确处理任何安全异常。
使用实际的安全管理器配置运行这些单元测试的最佳方法是什么?最好的解决方案是可以与 JUnit 集成的解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本身不是一个解决方案,但您不能实现自己的 SecurityManager 子类,也许将所有调用委托给“伪安全管理器”,而“伪安全管理器”又会被 EasyMock/类似的嘲笑?然后,您可以使用 System.setSecurityManager() 设置此 SecurityManager。
如果您知道您的小程序应该能够做什么,那么您就可以根据您的要求来通过测试。
我可能错过了你想要实现的目标,但这似乎是一种可能的方法。
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.