1   /*
2    *   @(#) $Id: HighestPrecedenceFilterTest.java 291883 2005-09-27 10:01:37Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.ldap.server.authz.support;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.Set;
27  
28  import junit.framework.Assert;
29  import junit.framework.TestCase;
30  
31  import org.apache.ldap.common.aci.ACITuple;
32  import org.apache.ldap.common.aci.AuthenticationLevel;
33  
34  /***
35   * Tests {@link HighestPrecedenceFilter}.
36   *
37   * @author The Apache Directory Project
38   * @version $Rev: 291883 $, $Date: 2005-09-27 06:01:37 -0400 (Tue, 27 Sep 2005) $
39   *
40   */
41  public class HighestPrecedenceFilterTest extends TestCase
42  {
43      private static final Collection EMPTY_COLLECTION =
44          Collections.unmodifiableCollection( new ArrayList() );
45      private static final Set EMPTY_SET =
46          Collections.unmodifiableSet( new HashSet() );
47  
48      public void testZeroTuple() throws Exception
49      {
50          HighestPrecedenceFilter filter = new HighestPrecedenceFilter();
51          Assert.assertEquals(
52                  0, filter.filter(
53                          EMPTY_COLLECTION, null, null, null, null, null, null,
54                          null, null, null, null, null ).size() );
55      }
56  
57      public void testOneTuple() throws Exception
58      {
59          HighestPrecedenceFilter filter = new HighestPrecedenceFilter();
60          Collection tuples = new ArrayList();
61          tuples.add( new ACITuple(
62                  EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
63                  EMPTY_SET, true, 10 ) );
64          tuples = Collections.unmodifiableCollection( tuples );
65          Assert.assertEquals(
66                  tuples, filter.filter(
67                          tuples, null, null, null, null, null, null,
68                          null, null, null, null, null ) );
69      }
70      
71      public void testMoreThanOneTuples() throws Exception
72      {
73          final int MAX_PRECEDENCE = 10;
74          HighestPrecedenceFilter filter = new HighestPrecedenceFilter();
75          Collection tuples = new ArrayList();
76          tuples.add( new ACITuple(
77                  EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
78                  EMPTY_SET, true, MAX_PRECEDENCE ) );
79          tuples.add( new ACITuple(
80                  EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
81                  EMPTY_SET, true, MAX_PRECEDENCE / 2 ) );
82          tuples.add( new ACITuple(
83                  EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
84                  EMPTY_SET, true, MAX_PRECEDENCE ) );
85          tuples.add( new ACITuple(
86                  EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
87                  EMPTY_SET, true, MAX_PRECEDENCE / 3 ) );
88  
89          tuples = filter.filter(
90                          tuples, null, null, null, null, null, null,
91                          null, null, null, null, null );
92          
93          for( Iterator i = tuples.iterator(); i.hasNext(); )
94          {
95              ACITuple tuple = ( ACITuple ) i.next();
96              Assert.assertEquals( MAX_PRECEDENCE, tuple.getPrecedence() );
97          }
98      }
99  }