OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / security / tests / java / security / PrivilegedActionTest.java
1 package org.apache.harmony.security.tests.java.security;
2
3 import dalvik.annotation.TestLevel;
4 import dalvik.annotation.TestTargetClass;
5 import dalvik.annotation.TestTargetNew;
6
7 import junit.framework.TestCase;
8
9 import java.security.AccessController;
10 import java.security.PrivilegedAction;
11
12 @TestTargetClass(PrivilegedAction.class)
13 public class PrivilegedActionTest extends TestCase {
14
15     protected void setUp() throws Exception {
16         super.setUp();
17     }
18
19     private class MyPrivilegedAction implements PrivilegedAction<String> {
20
21         private boolean called=false;
22         public String run() {
23             called = true;
24             return "ok";
25         }
26     }
27
28     private class MyPrivilegedAction2 implements PrivilegedAction<String> {
29
30         private boolean called=false;
31         public String run() {
32             called = true;
33             throw new RuntimeException("fail");
34         }
35
36     }
37
38     @TestTargetNew(
39             level=TestLevel.COMPLETE,
40             method="run"
41     )
42     public void testRun() {
43         MyPrivilegedAction action = new MyPrivilegedAction();
44         String result = AccessController.doPrivileged(action);
45         assertEquals("return value not correct", "ok", result);
46         assertTrue("run method was not called", action.called);
47
48         MyPrivilegedAction2 action2 = new MyPrivilegedAction2();
49
50
51         try {
52             result = AccessController.doPrivileged(action2);
53             fail("exception expected");
54         } catch (RuntimeException e) {
55             // expected exception
56         }
57     }
58 }