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 junit.framework.TestCase;
024:
025: /**
026: * Tests for <code>UnresolvedPermission</code> class fields and methods
027: *
028: */
029:
030: public class UnresolvedPermission_ImplTest extends TestCase {
031:
032: /**
033: * This test is valid since 1.5 release only. Checks that UnresolvedPermission returns the proper
034: * data for target permission. For non-empty certificates array,
035: * returns a new array each time this method is called.
036: */
037: public void testTargetData() {
038: String type = "laskjhlsdk 2345346";
039: String name = "^%#UHVKU^%V 887y";
040: String action = "JHB ^%(*&T klj3h4";
041: UnresolvedPermission up = new UnresolvedPermission(type, name,
042: action, null);
043: assertEquals(type, up.getUnresolvedType());
044: assertEquals(name, up.getUnresolvedName());
045: assertEquals(action, up.getUnresolvedActions());
046: assertNull(up.getUnresolvedCerts());
047:
048: up = new UnresolvedPermission(type, name, action,
049: new java.security.cert.Certificate[0]);
050: assertNull("Empty array should be the same as null", up
051: .getUnresolvedCerts());
052: // case of trivial collection: {null}
053: up = new UnresolvedPermission(type, name, action,
054: new java.security.cert.Certificate[3]);
055: assertNull(up.getUnresolvedCerts());
056: //assertNotSame(up.getUnresolvedCerts(), up.getUnresolvedCerts());
057: //assertEquals(1, up.getUnresolvedCerts().length);
058: }
059:
060: public void testEquals() {
061: String type = "KJHGUiy 24y";
062: String name = "kjhsdkfj ";
063: String action = "T klj3h4";
064: UnresolvedPermission up = new UnresolvedPermission(type, name,
065: action, null);
066: UnresolvedPermission up2 = new UnresolvedPermission(type, name,
067: action, null);
068: assertFalse(up.equals(null));
069: assertFalse(up.equals(new Object()));
070: assertFalse(up.equals(new BasicPermission("df") {
071: }));
072: assertTrue(up.equals(up));
073: assertTrue(up.equals(up2));
074: assertTrue(up.hashCode() == up2.hashCode());
075: up2 = new UnresolvedPermission(type, name, action,
076: new java.security.cert.Certificate[0]);
077: assertTrue(
078: "null and empty certificates should be considered equal",
079: up.equals(up2));
080: assertTrue(up.hashCode() == up2.hashCode());
081: up2 = new UnresolvedPermission(type, name, action,
082: new java.security.cert.Certificate[2]);
083: assertTrue(up.equals(up2));
084: //case of trivial collections {null}
085: up = new UnresolvedPermission(type, name, action,
086: new java.security.cert.Certificate[10]);
087: assertTrue(up.equals(up2));
088: assertTrue(up.hashCode() == up2.hashCode());
089: }
090:
091: /**
092: * resolve the unresolved permission to the permission of specified class.
093: */
094: public void testResolve() {
095: String name = "abc";
096: UnresolvedPermission up = new UnresolvedPermission(
097: "java.security.SecurityPermission", name, null, null);
098: Permission expected = new SecurityPermission(name);
099: //test valid input
100: assertEquals(expected, up.resolve(SecurityPermission.class));
101:
102: //test invalid class
103: assertNull(up.resolve(Object.class));
104:
105: //test invalid signers
106: //up = new UnresolvedPermission("java.security.SecurityPermission", name, null, new java.security.cert.Certificate[1]);
107: //assertNull(up.resolve(SecurityPermission.class));
108:
109: //another valid case
110: up = new UnresolvedPermission("java.security.AllPermission",
111: null, null, new java.security.cert.Certificate[0]);
112: assertEquals(new AllPermission(name, ""), up
113: .resolve(AllPermission.class));
114: }
115: }
|