View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.authc;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.shiro.subject.PrincipalCollection;
31  import org.junit.Test;
32  
33  
34  /**
35   * @since 0.9
36   */
37  public class SimpleAuthenticationInfoTest {
38  
39      @Test
40      public void testMergeWithEmptyInstances() {
41          SimpleAuthenticationInfo aggregate = new SimpleAuthenticationInfo();
42          SimpleAuthenticationInfo local = new SimpleAuthenticationInfo();
43          aggregate.merge(local);
44      }
45  
46      /**
47       * Verifies fix for JSEC-122
48       */
49      @Test
50      public void testMergeWithAggregateNullCredentials() {
51          SimpleAuthenticationInfo aggregate = new SimpleAuthenticationInfo();
52          SimpleAuthenticationInfo local = new SimpleAuthenticationInfo("username", "password", "testRealm");
53          aggregate.merge(local);
54      }
55      
56      @SuppressWarnings("serial")
57      @Test
58      public void testMergeWithImmutablePrincipalCollection() {
59          SimpleAuthenticationInfo aggregate = new SimpleAuthenticationInfo();
60          // Make a quick test fixture that does *not* implement MutablePrincipalCollection 
61          PrincipalCollection principalCollection = new PrincipalCollection() {
62  	    @SuppressWarnings("unchecked")
63  	    public List asList() { return null;}
64  	    @SuppressWarnings("unchecked")
65  	    public Set asSet() {return null;}
66  	    public <T> Collection<T> byType(Class<T> type) {return null;}
67  	    @SuppressWarnings("unchecked")
68  	    public Collection fromRealm(String realmName) {
69  		Collection<Object> principals = new HashSet<Object>();
70  		principals.add("testprincipal");
71  		return principals;
72  	    }
73  	    public Object getPrimaryPrincipal() {return null;}
74  	    public Set<String> getRealmNames() {
75  		Set<String> realms = new HashSet<String>();
76  		realms.add("testrealm");
77  		return realms;
78  	    }
79  	    public boolean isEmpty() {return false;}
80  	    public <T> T oneByType(Class<T> type) {return null;}
81  	    @SuppressWarnings("unchecked")
82  	    public Iterator iterator() {return null;}
83              
84          };
85          aggregate.setPrincipals(principalCollection);
86          SimpleAuthenticationInfo local = new SimpleAuthenticationInfo("username", "password", "testRealm");
87          aggregate.merge(local);
88          assertEquals(2, aggregate.getPrincipals().asList().size());
89      }
90      
91  }