### Question:
ServletFilterModuleDescriptor extends BaseServletModuleDescriptor<Filter> implements StateAware { @SuppressWarnings("unchecked") public void init(Plugin plugin, Element element) throws PluginParseException { super.init(plugin, element); try { location = FilterLocation.parse(element.attributeValue("location", DEFAULT_LOCATION)); weight = Integer.valueOf(element.attributeValue("weight", DEFAULT_WEIGHT)); } catch (IllegalArgumentException ex) { throw new PluginParseException(ex); } List<Element> dispatcherElements = element.elements("dispatcher"); if (!dispatcherElements.isEmpty()) { dispatcherConditions.clear(); for (Element dispatcher : dispatcherElements) { dispatcherConditions.add(FilterDispatcherCondition.valueOf(dispatcher.getTextTrim())); } } } ServletFilterModuleDescriptor(ModuleFactory moduleFactory, ServletModuleManager servletModuleManager); @SuppressWarnings("unchecked") void init(Plugin plugin, Element element); void enabled(); void disabled(); @Override Filter getModule(); FilterLocation getLocation(); int getWeight(); Set<FilterDispatcherCondition> getDispatcherConditions(); static final Comparator<ServletFilterModuleDescriptor> byWeight; }### Answer:
@Test public void testInit() { Plugin plugin = new StaticPlugin(); plugin.setKey("somekey"); Element e = getValidConfig(); descriptor.init(plugin, e); assertEquals(FilterLocation.BEFORE_DISPATCH, descriptor.getLocation()); assertEquals(100, descriptor.getWeight()); }
@Test public void testInitWithBadDispatcher() { Plugin plugin = new StaticPlugin(); plugin.setKey("somekey"); Element e = getValidConfig(); Element badDispatcher = new DOMElement("dispatcher"); badDispatcher.setText("badValue"); e.add(badDispatcher); try { descriptor.init(plugin, e); fail("Should have thrown exception"); } catch (PluginParseException ex) { } }
@Test public void testInitWithBadLocation() { Plugin plugin = new StaticPlugin(); plugin.setKey("somekey"); Element e = getValidConfig(); e.addAttribute("location", "t23op"); try { descriptor.init(plugin, e); fail("Should have thrown exception"); } catch (PluginParseException ex) { } }
@Test public void testInitWithBadWeight() { Plugin plugin = new StaticPlugin(); plugin.setKey("somekey"); Element e = getValidConfig(); e.addAttribute("weight", "t23op"); try { descriptor.init(plugin, e); fail("Should have thrown exception"); } catch (PluginParseException ex) { } }
@Test public void testInitWithNoUrlPattern() { Plugin plugin = new StaticPlugin(); plugin.setKey("somekey"); Element e = new DOMElement("servlet-filter"); e.addAttribute("key", "key2"); e.addAttribute("class", FilterAdapter.class.getName()); try { descriptor.init(plugin, e); fail("Should have thrown exception"); } catch (PluginParseException ex) { } }
@Test public void testWithNoDispatcher() { Plugin plugin = new StaticPlugin(); plugin.setKey("somekey"); Element e = new DOMElement("servlet-filter"); e.addAttribute("key", "key2"); e.addAttribute("class", FilterAdapter.class.getName()); Element url = new DOMElement("url-pattern"); url.setText("/foo"); e.add(url); descriptor.init(plugin, e); }