WebXmlParser { static void parseStream(SessionConfiguration conf, InputStream is) { if (is != null) { try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) { return new InputSource(new StringReader("")); } }); Document document = builder.parse(is); XPath xpath = XPathFactory.newInstance().newXPath(); lookForSessionTimeout(conf, document, xpath); lookForSessionConf(conf, document, xpath); checkDistributable(conf, document, xpath); } catch (SAXException | IOException | XPathExpressionException | ParserConfigurationException e) { throw new IllegalStateException("An exception occured while parsing web.xml. " + "Using default configuration: " + conf, e); } } } private  WebXmlParser();   }
@Test public void testExternalEntity() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(externalEntity.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); sessionConfiguration.setDistributable(false); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals(1800, sessionConfiguration.getMaxInactiveInterval()); assertFalse(sessionConfiguration.isDistributable()); } }
@Test public void testDistributable() throws IOException { String webXml = Descriptors.create(WebAppDescriptor.class).version("3.0").distributable().exportAsString(); try (ByteArrayInputStream bais = new ByteArrayInputStream(webXml.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); sessionConfiguration.setDistributable(false); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals(1800, sessionConfiguration.getMaxInactiveInterval()); assertTrue(sessionConfiguration.isDistributable()); } }
@Test public void testNotDistributable() throws IOException { String webXml = Descriptors.create(WebAppDescriptor.class).version("3.0").exportAsString(); try (ByteArrayInputStream bais = new ByteArrayInputStream(webXml.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); sessionConfiguration.setDistributable(true); WebXmlParser.parseStream(sessionConfiguration, bais); } }
@Test public void testNoDescriptor() throws IOException { SessionConfiguration sessionConfiguration = new SessionConfiguration(); sessionConfiguration.setDistributable(true); WebXmlParser.parseStream(sessionConfiguration, null); }
@Test public void testParseStreamDefaultSession0() throws IOException { String webXml = Descriptors.create(WebAppDescriptor.class).version("3.0").createSessionConfig().sessionTimeout(0) .up().exportAsString(); try (ByteArrayInputStream bais = new ByteArrayInputStream(webXml.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals(0, sessionConfiguration.getMaxInactiveInterval()); } }
@Test public void testParseStreamDefaultSession1000() throws IOException { String webXml = Descriptors.create(WebAppDescriptor.class).version("3.0").createSessionConfig().sessionTimeout(1000) .up().exportAsString(); try (ByteArrayInputStream bais = new ByteArrayInputStream(webXml.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals(1000, sessionConfiguration.getMaxInactiveInterval()); } }
@Test public void testParseStreamDefaultSessionEmpty() throws IOException { String webXml = Descriptors.create(WebAppDescriptor.class).version("3.0").exportAsString(); try (ByteArrayInputStream bais = new ByteArrayInputStream(webXml.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals(1800, sessionConfiguration.getMaxInactiveInterval()); } }
@Test(expected = NumberFormatException.class) public void testParseStreamDefaultSessionInvalid() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(withInvalidSessionDuration.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); } }
@Test(expected = NumberFormatException.class) public void testParseStreamDefaultSessionAbsent() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(withEmptySessionDuration.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); } }
@Test(expected = IllegalStateException.class) public void testInvalidDescriptor() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(invalidDescriptor.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); } }
@Test public void testInvalidTrackingMode() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(withInvalidTrackingMode.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals(1, sessionConfiguration.getSessionTracking().length); assertEquals("DEFAULT", sessionConfiguration.getSessionTracking()[0]); } }
@Test public void testMultipleTrackingModes() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(withMultipleTrackingModes.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals(2, sessionConfiguration.getSessionTracking().length); assertEquals("COOKIE", sessionConfiguration.getSessionTracking()[0]); assertEquals("URL", sessionConfiguration.getSessionTracking()[1]); } }
@Test public void testWithSessionConfig() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(withSessionConfig.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals("COOKIE", sessionConfiguration.getSessionTracking()[0]); assertEquals("true", sessionConfiguration.getAttribute(CookieSessionTracking.COOKIE_HTTP_ONLY_PARAMETER, null)); assertEquals("false", sessionConfiguration.getAttribute(CookieSessionTracking.SECURE_COOKIE_PARAMETER, null)); assertEquals("/test", sessionConfiguration.getAttribute(CookieSessionTracking.COOKIE_CONTEXT_PATH_PARAMETER, null)); } }
@Test public void testWithUrlTracking() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(withUrlTrackingMode.getBytes("UTF-8"))) { SessionConfiguration sessionConfiguration = new SessionConfiguration(); WebXmlParser.parseStream(sessionConfiguration, bais); assertEquals(1, sessionConfiguration.getSessionTracking().length); assertEquals("URL", sessionConfiguration.getSessionTracking()[0]); } }