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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Alexey V. Varlamov
23 package org.apache.harmony.security.tests.java.security;
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
30 import java.security.Guard;
31 import java.security.GuardedObject;
33 import junit.framework.TestCase;
34 @TestTargetClass(GuardedObject.class)
36 * Tests for <code>GuardedObject</code>
40 public class GuardedObjectTest extends TestCase {
42 /** Null guard imposes no restriction. */
45 level = TestLevel.PARTIAL_COMPLETE,
47 method = "GuardedObject",
48 args = {java.lang.Object.class, java.security.Guard.class}
51 level = TestLevel.PARTIAL_COMPLETE,
57 public void testNoGuard() {
59 GuardedObject go = new GuardedObject(obj, null);
60 assertNull(go.getObject());
63 go = new GuardedObject(obj, null);
64 assertEquals(obj, go.getObject());
67 /** Test real guard can both allow and deny access. */
70 level = TestLevel.PARTIAL_COMPLETE,
72 method = "GuardedObject",
73 args = {java.lang.Object.class, java.security.Guard.class}
76 level = TestLevel.PARTIAL_COMPLETE,
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() {
87 public void checkGuard(Object object) throws SecurityException {
88 if (object == objBuffer && objBuffer.length() == 0) {
89 throw new SecurityException(message);
93 assertEquals(objBuffer, go.getObject());
95 objBuffer.setLength(0);
98 fail("SecurityException is not thrown");
99 } catch (Exception ok) {
100 assertEquals(message, ok.getMessage());