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: * @author Alexey V. Varlamov
019: * @version $Revision$
020: */package org.apache.harmony.security.tests;
021:
022: import java.security.Principal;
023:
024: import org.apache.harmony.security.UnresolvedPrincipal;
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests for <code>UnresolvedPrincipal</code>
029: *
030: */
031:
032: public class UnresolvedPrincipalTest extends TestCase {
033:
034: public static void main(String[] args) {
035: junit.textui.TestRunner.run(UnresolvedPrincipalTest.class);
036: }
037:
038: public void testCtor() {
039: String klass = "abc";
040: String name = "NAME";
041: UnresolvedPrincipal up = new UnresolvedPrincipal(klass, name);
042: assertEquals(klass, up.getClassName());
043: assertEquals(name, up.getName());
044:
045: up = new UnresolvedPrincipal(klass, null);
046: assertEquals(klass, up.getClassName());
047: assertNull(up.getName());
048:
049: try {
050: up = new UnresolvedPrincipal(null, name);
051: fail("No IllegalArgumentException is thrown");
052: } catch (IllegalArgumentException ok) {
053: }
054: }
055:
056: public void testEquals_Principal() {
057: String name = "sgrt";
058: FakePrincipal fp = new FakePrincipal(name);
059:
060: assertTrue(new UnresolvedPrincipal(FakePrincipal.class
061: .getName(), name).equals(fp));
062: assertTrue(new UnresolvedPrincipal(
063: UnresolvedPrincipal.WILDCARD, name).equals(fp));
064: assertTrue(new UnresolvedPrincipal(FakePrincipal.class
065: .getName(), UnresolvedPrincipal.WILDCARD).equals(fp));
066:
067: assertFalse(new UnresolvedPrincipal(FakePrincipal.class
068: .getName(), "sdkljfgbkwe").equals(fp));
069: }
070:
071: public void testEquals_Common() {
072: String klass = "abc";
073: String name = "NAME";
074: UnresolvedPrincipal up = new UnresolvedPrincipal(klass, name);
075: UnresolvedPrincipal up2 = new UnresolvedPrincipal(klass, name);
076: UnresolvedPrincipal up3 = new UnresolvedPrincipal(name, klass);
077:
078: assertTrue(up.equals(up));
079: assertTrue(up.equals(up2));
080: assertEquals(up.hashCode(), up2.hashCode());
081: assertFalse(up.equals(up3));
082: assertFalse(up.equals(null));
083: assertFalse(up.equals(new Object()));
084: }
085:
086: public void testImplies() {
087: String name = "sgrt";
088: FakePrincipal fp = new FakePrincipal(name);
089: assertTrue(new UnresolvedPrincipal(FakePrincipal.class
090: .getName(), name).implies(fp));
091: assertTrue(new UnresolvedPrincipal(
092: UnresolvedPrincipal.WILDCARD, name).implies(fp));
093: assertTrue(new UnresolvedPrincipal(FakePrincipal.class
094: .getName(), UnresolvedPrincipal.WILDCARD).implies(fp));
095: assertTrue(new UnresolvedPrincipal(
096: UnresolvedPrincipal.WILDCARD,
097: UnresolvedPrincipal.WILDCARD).implies(fp));
098:
099: assertFalse(new UnresolvedPrincipal(UnresolvedPrincipal.class
100: .getName(), name).implies(fp));
101: assertFalse(new UnresolvedPrincipal(FakePrincipal.class
102: .getName(), "hgfuytr765").implies(fp));
103: }
104: }
105:
106: class FakePrincipal implements Principal {
107:
108: private String name;
109:
110: public FakePrincipal(String name) {
111: this .name = name;
112: }
113:
114: public String getName() {
115: return name;
116: }
117: }
|