java.security.Guard Java Examples

The following examples show how to use java.security.Guard. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GuardedObjectTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/** Test real guard can both allow and deny access. */
public void testGuard() {
    final String message = "test message";
    final StringBuffer objBuffer = new StringBuffer("235345 t");
    GuardedObject go = new GuardedObject(objBuffer, new Guard() {

        public void checkGuard(Object object) throws SecurityException {
            if (object == objBuffer && objBuffer.length() == 0) {
                throw new SecurityException(message);
            }
        }
    });
    assertEquals(objBuffer, go.getObject());

    objBuffer.setLength(0);
    try {
        go.getObject();
        fail("SecurityException is not thrown");
    } catch (Exception ok) {
        assertEquals(message, ok.getMessage());
    }
}