001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.awt;
018:
019: import java.awt.AWTPermission;
020: import java.awt.Frame;
021: import java.awt.Window;
022: import java.security.Permission;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Test case for java.awt.AWTPermission
028: */
029: public class AWTPermissionTest extends TestCase {
030:
031: SecurityManager originalSM = null;
032:
033: MockSecurityManager sm = null;
034:
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038: originalSM = System.getSecurityManager();
039: sm = new MockSecurityManager();
040: System.setSecurityManager(sm);
041: }
042:
043: @Override
044: protected void tearDown() throws Exception {
045: super .tearDown();
046: System.setSecurityManager(originalSM);
047: }
048:
049: /**
050: * @tests java.awt.AWTPermission#AWTPermission((String) test case for
051: * "accessEventQueue"
052: */
053: @SuppressWarnings("nls")
054: public void test_checkAwtEventQueueAccess() {
055: try {
056: sm.checkAwtEventQueueAccess();
057: fail("Should throw a SecurityException.");
058: } catch (SecurityException e) {
059: // expected
060: }
061: }
062:
063: /**
064: * @tests java.awt.AWTPermission#AWTPermission((String) test case for
065: * "accessClipboard"
066: */
067: @SuppressWarnings("nls")
068: public void test_checkSystemClipboardAccess() {
069: try {
070: sm.checkSystemClipboardAccess();
071: fail("Should throw a SecurityException.");
072: } catch (SecurityException e) {
073: // expected
074: }
075: }
076:
077: /**
078: * @tests java.awt.AWTPermission#AWTPermission((String) test case for
079: * "showWindowWithoutWarningBanner"
080: */
081: public void test_checkTopLevelWindowLjava_lang_Object() {
082: assertFalse(sm.checkTopLevelWindow(new Window(new Frame())));
083: }
084:
085: /**
086: * Sub-SecurityManager to test some actions of AWTPermission.
087: */
088: class MockSecurityManager extends SecurityManager {
089: /**
090: * @see java.lang.SecurityManager#checkPermission(java.security.Permission)
091: */
092: @Override
093: @SuppressWarnings("nls")
094: public void checkPermission(Permission permission) {
095: Permission[] denied = new Permission[] {
096: new AWTPermission("accessEventQueue"),
097: new AWTPermission("accessClipboard"),
098: new AWTPermission("showWindowWithoutWarningBanner") };
099: for (Permission per : denied) {
100: if (null != per && per.implies(permission)) {
101: throw new SecurityException("Denied " + permission);
102: }
103: }
104: }
105: }
106: }
|