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 / GuardedObjectTest.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 /**
19 * @author Alexey V. Varlamov
20 * @version $Revision$
21 */
22
23 package org.apache.harmony.security.tests.java.security;
24
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
29
30 import java.security.Guard;
31 import java.security.GuardedObject;
32
33 import junit.framework.TestCase;
34 @TestTargetClass(GuardedObject.class)
35 /**
36  * Tests for <code>GuardedObject</code>
37  *
38  */
39
40 public class GuardedObjectTest extends TestCase {
41
42     /** Null guard imposes no restriction. */
43     @TestTargets({
44         @TestTargetNew(
45             level = TestLevel.PARTIAL_COMPLETE,
46             notes = "",
47             method = "GuardedObject",
48             args = {java.lang.Object.class, java.security.Guard.class}
49         ),
50         @TestTargetNew(
51             level = TestLevel.PARTIAL_COMPLETE,
52             notes = "",
53             method = "getObject",
54             args = {}
55         )
56     })
57     public void testNoGuard() {
58         Object obj = null;
59         GuardedObject go = new GuardedObject(obj, null);
60         assertNull(go.getObject());
61
62         obj = "ewte rtw3456";
63         go = new GuardedObject(obj, null);
64         assertEquals(obj, go.getObject());
65     }
66
67     /** Test real guard can both allow and deny access. */
68     @TestTargets({
69         @TestTargetNew(
70             level = TestLevel.PARTIAL_COMPLETE,
71             notes = "",
72             method = "GuardedObject",
73             args = {java.lang.Object.class, java.security.Guard.class}
74         ),
75         @TestTargetNew(
76             level = TestLevel.PARTIAL_COMPLETE,
77             notes = "",
78             method = "getObject",
79             args = {}
80         )
81     })
82     public void testGuard() {
83         final String message = "test message";
84         final StringBuffer objBuffer = new StringBuffer("235345 t");
85         GuardedObject go = new GuardedObject(objBuffer, new Guard() {
86
87             public void checkGuard(Object object) throws SecurityException {
88                 if (object == objBuffer && objBuffer.length() == 0) {
89                     throw new SecurityException(message);
90                 }
91             }
92         });
93         assertEquals(objBuffer, go.getObject());
94
95         objBuffer.setLength(0);
96         try {
97             go.getObject();
98             fail("SecurityException is not thrown");
99         } catch (Exception ok) {
100             assertEquals(message, ok.getMessage());
101         }
102     }
103 }