1 /* 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 */ 17 /** 18 * @author Alexey V. Varlamov 19 */ 20 21 package org.apache.harmony.security.tests; 22 23 import java.security.Principal; 24 25 import org.apache.harmony.security.UnresolvedPrincipal; 26 import junit.framework.TestCase; 27 28 29 /** 30 * Tests for <code>UnresolvedPrincipal</code> 31 * 32 */ 33 34 public class UnresolvedPrincipalTest extends TestCase { 35 testCtor()36 public void testCtor() { 37 String klass = "abc"; 38 String name = "NAME"; 39 UnresolvedPrincipal up = new UnresolvedPrincipal(klass, name); 40 assertEquals(klass, up.getClassName()); 41 assertEquals(name, up.getName()); 42 43 up = new UnresolvedPrincipal(klass, null); 44 assertEquals(klass, up.getClassName()); 45 assertNull(up.getName()); 46 47 try { 48 up = new UnresolvedPrincipal(null, name); 49 fail("No IllegalArgumentException is thrown"); 50 } catch (IllegalArgumentException ok) { 51 } 52 } 53 testEquals_Principal()54 public void testEquals_Principal() { 55 String name = "sgrt"; 56 FakePrincipal fp = new FakePrincipal(name); 57 58 assertTrue(new UnresolvedPrincipal(FakePrincipal.class.getName(), name) 59 .equals(fp)); 60 assertTrue(new UnresolvedPrincipal(UnresolvedPrincipal.WILDCARD, name) 61 .equals(fp)); 62 assertTrue(new UnresolvedPrincipal(FakePrincipal.class.getName(), 63 UnresolvedPrincipal.WILDCARD).equals(fp)); 64 65 assertFalse(new UnresolvedPrincipal(FakePrincipal.class.getName(), 66 "sdkljfgbkwe").equals(fp)); 67 } 68 testEquals_Common()69 public void testEquals_Common() { 70 String klass = "abc"; 71 String name = "NAME"; 72 UnresolvedPrincipal up = new UnresolvedPrincipal(klass, name); 73 UnresolvedPrincipal up2 = new UnresolvedPrincipal(klass, name); 74 UnresolvedPrincipal up3 = new UnresolvedPrincipal(name, klass); 75 76 assertTrue(up.equals(up)); 77 assertTrue(up.equals(up2)); 78 assertEquals(up.hashCode(), up2.hashCode()); 79 assertFalse(up.equals(up3)); 80 assertFalse(up.equals(null)); 81 assertFalse(up.equals(new Object())); 82 } 83 testImplies()84 public void testImplies() { 85 String name = "sgrt"; 86 FakePrincipal fp = new FakePrincipal(name); 87 assertTrue(new UnresolvedPrincipal(FakePrincipal.class.getName(), name) 88 .implies(fp)); 89 assertTrue(new UnresolvedPrincipal(UnresolvedPrincipal.WILDCARD, name) 90 .implies(fp)); 91 assertTrue(new UnresolvedPrincipal(FakePrincipal.class.getName(), 92 UnresolvedPrincipal.WILDCARD).implies(fp)); 93 assertTrue(new UnresolvedPrincipal(UnresolvedPrincipal.WILDCARD, 94 UnresolvedPrincipal.WILDCARD).implies(fp)); 95 96 assertFalse(new UnresolvedPrincipal( 97 UnresolvedPrincipal.class.getName(), name).implies(fp)); 98 assertFalse(new UnresolvedPrincipal(FakePrincipal.class.getName(), 99 "hgfuytr765").implies(fp)); 100 } 101 } 102 103 class FakePrincipal implements Principal { 104 105 private String name; 106 FakePrincipal(String name)107 public FakePrincipal(String name) { 108 this.name = name; 109 } 110 getName()111 public String getName() { 112 return name; 113 } 114 } 115