WebXMLDefinitionLoader extends DefaultHandler { public WebXMLDefinition loadWebXMLDefinition(URL url) throws Exception { if (url == null) { return _webXMLDefinition; } try (InputStream inputStream = url.openStream()) { SAXParser saxParser = _saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(this); xmlReader.parse(new InputSource(inputStream)); return _webXMLDefinition; } catch (SAXParseException saxParseException) { String message = saxParseException.getMessage(); if (message.contains("DOCTYPE is disallowed")) { throw new Exception( url + " must be updated to the Servlet 3.0 specification"); } throw saxParseException; } } WebXMLDefinitionLoader(
		Bundle bundle, JSPServletFactory jspServletFactory,
		SAXParserFactory saxParserFactory, Set<Class<?>> classes,
		Set<Class<?>> annotatedClasses); @Override void characters(char[] c, int start, int length); @Override void endElement(String uri, String localName, String qName); @Override void error(SAXParseException saxParseException); WebXMLDefinition loadWebXML(); WebXMLDefinition loadWebXMLDefinition(URL url); @Override void startElement(
		String uri, String localName, String qName, Attributes attributes);  }
@Test public void testLoadCustomWebAbsoluteOrdering1XML() throws Exception { WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web-absolute-ordering-1.xml"); List<String> absoluteOrderingNames = new ArrayList<>(); absoluteOrderingNames.add("fragment2"); absoluteOrderingNames.add("fragment1"); absoluteOrderingNames.add(Order.OTHERS); testWebXMLDefinition( webXMLDefinition, 1, 1, 1, null, null, absoluteOrderingNames); }
@Test public void testLoadCustomWebAbsoluteOrdering1XMLMetadataIncomplete() throws Exception { WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web-absolute-ordering-1.xml"); Assert.assertFalse(webXMLDefinition.isMetadataComplete()); }
@Test public void testLoadCustomWebFragment1XML() throws Exception { WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web-fragment-1.xml"); testWebXMLDefinition( webXMLDefinition, 1, 1, 0, "fragment1", null, null); }
@Test public void testLoadCustomWebFragment2XML() throws Exception { WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web-fragment-2.xml"); Order order = new Order(); EnumMap<Order.Path, String[]> routes = order.getRoutes(); routes.put(Order.Path.AFTER, new String[] {"fragment1"}); testWebXMLDefinition( webXMLDefinition, 0, 0, 0, "fragment2", order, null); }
@Test public void testLoadCustomWebFragment4XML() throws Exception { WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web-fragment-4.xml"); Order order = new Order(); EnumMap<Order.Path, String[]> routes = order.getRoutes(); routes.put(Order.Path.BEFORE, new String[] {Order.OTHERS}); testWebXMLDefinition( webXMLDefinition, 0, 0, 0, "fragment4", order, null); }
@Test public void testLoadCustomWebXML() throws Exception { WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web.xml"); testWebXMLDefinition(webXMLDefinition, 1, 1, 1); }
@Test public void testLoadCustomWebXMLMetadataComplete() throws Exception { WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web.xml"); Assert.assertTrue(webXMLDefinition.isMetadataComplete()); }
@Test public void testOrderBeforeAndAfterException() throws Exception { List<WebXMLDefinition> webXMLDefinitions = new ArrayList<>(); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-5.xml")); WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web.xml"); boolean threwOrderBeforeAndAfterException = false; try { OrderUtil.getOrderedWebXMLDefinitions( webXMLDefinitions, webXMLDefinition.getAbsoluteOrderingNames()); } catch (Exception exception) { if (exception instanceof OrderBeforeAndAfterException) { threwOrderBeforeAndAfterException = true; } } Assert.assertTrue(threwOrderBeforeAndAfterException); }
@Test public void testOrderCircularDependencyException() throws Exception { List<WebXMLDefinition> webXMLDefinitions = new ArrayList<>(); webXMLDefinitions.add( loadWebXMLDefinition( "dependencies/custom-web-fragment-circular-1.xml")); webXMLDefinitions.add( loadWebXMLDefinition( "dependencies/custom-web-fragment-circular-2.xml")); WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web.xml"); boolean threwOrderCircularDependencyException = false; try { OrderUtil.getOrderedWebXMLDefinitions( webXMLDefinitions, webXMLDefinition.getAbsoluteOrderingNames()); } catch (Exception exception) { if (exception instanceof OrderCircularDependencyException) { threwOrderCircularDependencyException = true; } } Assert.assertTrue(threwOrderCircularDependencyException); }
@Test public void testOrderCustomWebFragments1() throws Exception { List<WebXMLDefinition> webXMLDefinitions = new ArrayList<>(); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-3.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-1.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-2.xml")); WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web-absolute-ordering-1.xml"); List<WebXMLDefinition> orderedWebXMLDefinitions = OrderUtil.getOrderedWebXMLDefinitions( webXMLDefinitions, webXMLDefinition.getAbsoluteOrderingNames()); Assert.assertEquals( orderedWebXMLDefinitions.toString(), 3, orderedWebXMLDefinitions.size()); WebXMLDefinition firstWebXMLDefinition = orderedWebXMLDefinitions.get( 0); Assert.assertEquals( "fragment2", firstWebXMLDefinition.getFragmentName()); WebXMLDefinition secondWebXMLDefinition = orderedWebXMLDefinitions.get( 1); Assert.assertEquals( "fragment1", secondWebXMLDefinition.getFragmentName()); WebXMLDefinition thirdWebXMLDefinition = orderedWebXMLDefinitions.get( 2); Assert.assertEquals( "fragment3", thirdWebXMLDefinition.getFragmentName()); }
@Test public void testOrderCustomWebFragments2() throws Exception { List<WebXMLDefinition> webXMLDefinitions = new ArrayList<>(); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-3.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-2.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-1.xml")); WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web-absolute-ordering-2.xml"); List<WebXMLDefinition> orderedWebXMLDefinitions = OrderUtil.getOrderedWebXMLDefinitions( webXMLDefinitions, webXMLDefinition.getAbsoluteOrderingNames()); Assert.assertEquals( orderedWebXMLDefinitions.toString(), 2, orderedWebXMLDefinitions.size()); WebXMLDefinition firstWebXMLDefinition = orderedWebXMLDefinitions.get( 0); Assert.assertEquals( "fragment1", firstWebXMLDefinition.getFragmentName()); WebXMLDefinition secondWebXMLDefinition = orderedWebXMLDefinitions.get( 1); Assert.assertEquals( "fragment2", secondWebXMLDefinition.getFragmentName()); }
@Test public void testOrderCustomWebFragments3() throws Exception { List<WebXMLDefinition> webXMLDefinitions = new ArrayList<>(); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-3.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-2.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-1.xml")); WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web.xml"); List<WebXMLDefinition> orderedWebXMLDefinitions = OrderUtil.getOrderedWebXMLDefinitions( webXMLDefinitions, webXMLDefinition.getAbsoluteOrderingNames()); Assert.assertEquals( orderedWebXMLDefinitions.toString(), 3, orderedWebXMLDefinitions.size()); WebXMLDefinition firstWebXMLDefinition = orderedWebXMLDefinitions.get( 0); Assert.assertEquals( "fragment1", firstWebXMLDefinition.getFragmentName()); WebXMLDefinition secondWebXMLDefinition = orderedWebXMLDefinitions.get( 1); Assert.assertEquals( "fragment3", secondWebXMLDefinition.getFragmentName()); WebXMLDefinition thirdWebXMLDefinition = orderedWebXMLDefinitions.get( 2); Assert.assertEquals( "fragment2", thirdWebXMLDefinition.getFragmentName()); }
@Test public void testOrderCustomWebFragments4() throws Exception { List<WebXMLDefinition> webXMLDefinitions = new ArrayList<>(); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-2.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-1.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-4.xml")); WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web.xml"); List<WebXMLDefinition> orderedWebXMLDefinitions = OrderUtil.getOrderedWebXMLDefinitions( webXMLDefinitions, webXMLDefinition.getAbsoluteOrderingNames()); Assert.assertEquals( orderedWebXMLDefinitions.toString(), 3, orderedWebXMLDefinitions.size()); WebXMLDefinition firstWebXMLDefinition = orderedWebXMLDefinitions.get( 0); Assert.assertEquals( "fragment4", firstWebXMLDefinition.getFragmentName()); WebXMLDefinition secondWebXMLDefinition = orderedWebXMLDefinitions.get( 1); Assert.assertEquals( "fragment1", secondWebXMLDefinition.getFragmentName()); WebXMLDefinition thirdWebXMLDefinition = orderedWebXMLDefinitions.get( 2); Assert.assertEquals( "fragment2", thirdWebXMLDefinition.getFragmentName()); }
@Test public void testUnorderedWebFragments() throws Exception { List<WebXMLDefinition> webXMLDefinitions = new ArrayList<>(); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-1.xml")); webXMLDefinitions.add( loadWebXMLDefinition("dependencies/custom-web-fragment-3.xml")); WebXMLDefinition webXMLDefinition = loadWebXMLDefinition( "dependencies/custom-web.xml"); List<WebXMLDefinition> orderedWebXMLDefinitions = OrderUtil.getOrderedWebXMLDefinitions( webXMLDefinitions, webXMLDefinition.getAbsoluteOrderingNames()); Assert.assertEquals( orderedWebXMLDefinitions.toString(), 2, orderedWebXMLDefinitions.size()); WebXMLDefinition firstWebXMLDefinition = orderedWebXMLDefinitions.get( 0); Assert.assertEquals( "fragment1", firstWebXMLDefinition.getFragmentName()); WebXMLDefinition secondWebXMLDefinition = orderedWebXMLDefinitions.get( 1); Assert.assertEquals( "fragment3", secondWebXMLDefinition.getFragmentName()); }