1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }