Home | History | Annotate | Download | only in security
      1 package org.apache.harmony.security.tests.java.security;
      2 
      3 import junit.framework.TestCase;
      4 
      5 import java.security.AccessController;
      6 import java.security.PrivilegedAction;
      7 
      8 public class PrivilegedActionTest extends TestCase {
      9 
     10     protected void setUp() throws Exception {
     11         super.setUp();
     12     }
     13 
     14     private class MyPrivilegedAction implements PrivilegedAction<String> {
     15 
     16         private boolean called=false;
     17         public String run() {
     18             called = true;
     19             return "ok";
     20         }
     21     }
     22 
     23     private class MyPrivilegedAction2 implements PrivilegedAction<String> {
     24 
     25         private boolean called=false;
     26         public String run() {
     27             called = true;
     28             throw new RuntimeException("fail");
     29         }
     30 
     31     }
     32 
     33     public void testRun() {
     34         MyPrivilegedAction action = new MyPrivilegedAction();
     35         String result = AccessController.doPrivileged(action);
     36         assertEquals("return value not correct", "ok", result);
     37         assertTrue("run method was not called", action.called);
     38 
     39         MyPrivilegedAction2 action2 = new MyPrivilegedAction2();
     40 
     41 
     42         try {
     43             result = AccessController.doPrivileged(action2);
     44             fail("exception expected");
     45         } catch (RuntimeException e) {
     46             // expected exception
     47         }
     48     }
     49 }
     50