NoExitSecurityManagerTest.java :  » Testing » abbot-1.0.1 » abbot » Java Open Source

Java Open Source » Testing » abbot 1.0.1 
abbot 1.0.1 » abbot » NoExitSecurityManagerTest.java
package abbot;

import javax.swing.JFrame;

import junit.extensions.abbot.TestHelper;
import junit.framework.TestCase;

public class NoExitSecurityManagerTest extends TestCase {

    private SecurityManager oldsm;
    protected void setUp() {
        oldsm = System.getSecurityManager();
        System.setSecurityManager(new NoExitSecurityManager() {
            public void exitCalled(int code) {
            }
        });
    }

    protected void tearDown() {
        System.setSecurityManager(oldsm);
    }

    public void testExitPrevented() {
        try {
            System.exit(0);
        }
        catch(ExitException ee) {
            assertEquals("Wrong exit code", 0, ee.getStatus());
        }
        try {
            System.exit(1);
        }
        catch(ExitException ee) {
            assertEquals("Wrong exit code", 1, ee.getStatus());
        }
    }

    public void testNonExitAllowed() {
        JFrame frame = new JFrame(getName());
        try {
            // Somebody used to call checkExit on this
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Runtime.getRuntime().runFinalizersOnExit(true);
        }
        catch(SecurityException se) {
            fail("Code should be allowed to set the default frame operation.");
        }
    }

    public NoExitSecurityManagerTest(String name) {
        super(name);
    }

    public static void main(String[] args) {
        TestHelper.runTests(args, NoExitSecurityManagerTest.class);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.