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:
018: /**
019: * @author Alexey V. Varlamov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.util.Enumeration;
024: import java.util.NoSuchElementException;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Tests for <code>AllPermissionCollection</code>
030: *
031: */
032:
033: public class AllPermissionCollection_ImplTest extends TestCase {
034:
035: /**
036: * Empty collection implies nothing, non-empty collection implies
037: * everything.
038: */
039: public void testImplies() {
040: PermissionCollection pc = new AllPermissionCollection();
041: Permission ap = new AllPermission();
042: Permission sp = new SecurityPermission("abc");
043: assertFalse(pc.implies(ap));
044: assertFalse(pc.implies(sp));
045: pc.add(ap);
046: assertTrue(pc.implies(ap));
047: assertTrue(pc.implies(sp));
048: }
049:
050: /**
051: * Can add any number of AllPermission instances, but no any other
052: * permissions. Cannot add if collection is read-only.
053: */
054: public void testAdd() {
055: PermissionCollection pc = new AllPermissionCollection();
056: Permission ap = new AllPermission();
057: Permission sp = new SecurityPermission("abc");
058: try {
059: pc.add(sp);
060: fail("Should not add non-AllPermission");
061: } catch (IllegalArgumentException ok) {
062: }
063: pc.add(ap);
064: pc.add(ap);
065: pc.add(new AllPermission());
066:
067: pc.setReadOnly();
068: try {
069: pc.add(ap);
070: fail("read-only flag is ignored");
071: } catch (SecurityException ok) {
072: }
073: }
074:
075: /**
076: * Should return non-null empty enumeration for empty collection. For
077: * non-empty collection, should always return 1-element enumeration.
078: */
079: public void testElements() {
080: PermissionCollection pc = new AllPermissionCollection();
081: Permission ap = new AllPermission();
082: Enumeration en = pc.elements();
083: assertNotNull(en);
084: assertFalse(en.hasMoreElements());
085:
086: pc.add(ap);
087: en = pc.elements();
088: assertTrue(en.hasMoreElements());
089: assertTrue(ap.equals(en.nextElement()));
090: assertFalse(en.hasMoreElements());
091:
092: ap = new AllPermission();
093: pc.add(ap);
094: en = pc.elements();
095: assertTrue(en.hasMoreElements());
096: assertTrue(ap.equals(en.nextElement()));
097: assertFalse(en.hasMoreElements());
098: }
099:
100: /**
101: * If nothing to enumerate, should throw NoSuchElementException
102: */
103: public void testElements_NoElements() {
104: PermissionCollection pc = new AllPermissionCollection();
105: try {
106: pc.elements().nextElement();
107: fail("should throw NoSuchElementException");
108: } catch (NoSuchElementException ok) {
109: }
110: }
111: }
|