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.guice.web;
20  
21  import com.google.inject.Key;
22  import org.apache.shiro.web.filter.mgt.DefaultFilter;
23  import org.junit.Test;
24  
25  import javax.servlet.Filter;
26  import java.lang.reflect.Field;
27  import java.lang.reflect.Modifier;
28  import java.util.EnumSet;
29  
30  import static org.junit.Assert.fail;
31  
32  public class DefaultFiltersTest {
33      @Test
34      public void checkDefaultFilters() throws Exception {
35          EnumSet<DefaultFilter> defaultFilters = EnumSet.allOf(DefaultFilter.class);
36          for(Field field: ShiroWebModule.class.getFields()) {
37              if(Modifier.isStatic(field.getModifiers()) && Key.class.isAssignableFrom(field.getType())) {
38                  Class<? extends Filter> filterType = ((Key)field.get(null)).getTypeLiteral().getRawType();
39                  boolean found = false;
40                  for(DefaultFilter filter: defaultFilters) {
41                      if(filterType.equals(filter.getFilterClass())) {
42                          found = true;
43                          defaultFilters.remove(filter);
44                          break;
45                      }
46                  }
47                  if(!found) {
48                      fail("Guice ShiroWebModule containts a default filter that Shiro proper does not. (" + filterType.getName() + ")");
49                  }
50              }
51          }
52          if(!defaultFilters.isEmpty()) {
53              fail("Guice ShiroWebModule is missing one or more filters. " + defaultFilters);
54          }
55      }
56  
57  }