This is an automated email from the ASF dual-hosted git repository. dsoumis pushed a commit to branch 11.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 8ae5e5aa66d64ff1113ab1a816170b261d893e34 Author: Dimitris Soumis <[email protected]> AuthorDate: Mon Sep 1 15:21:20 2025 +0300 Create two tests, one testing that the XML validation works (configured using the Context attributes) and one testing that setting STRICT_SERVLET_COMPLIANCE sets the attributes it is documented to set. --- .../TestStrictServletComplianceAttributes.java | 76 ++++++++++++++++++++++ ...ent.java => TestXmlValidationUsingContext.java} | 61 +++++------------ 2 files changed, 94 insertions(+), 43 deletions(-) diff --git a/test/org/apache/catalina/startup/TestStrictServletComplianceAttributes.java b/test/org/apache/catalina/startup/TestStrictServletComplianceAttributes.java new file mode 100644 index 0000000000..e8ad97aef3 --- /dev/null +++ b/test/org/apache/catalina/startup/TestStrictServletComplianceAttributes.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.catalina.startup; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.apache.catalina.Context; +import org.apache.catalina.Globals; +import org.apache.catalina.Manager; +import org.apache.catalina.session.ManagerBase; + +/** + * Tests STRICT_SERVLET_COMPLIANCE sets the attributes it is documented to set. + */ +public class TestStrictServletComplianceAttributes extends TomcatBaseTest { + private static final String STRICT_SERVLET_COMPLIANCE = "org.apache.catalina.STRICT_SERVLET_COMPLIANCE"; + private static String originalPropertyValue; + + @BeforeClass + public static void setup() { + originalPropertyValue = System.getProperty(STRICT_SERVLET_COMPLIANCE); + System.setProperty(STRICT_SERVLET_COMPLIANCE, "true"); + + // If Globals was already initialised in the same JVM (during the tests run through IDE), + // before the test sets the value to true, skip. + boolean globalsStrict = Globals.STRICT_SERVLET_COMPLIANCE; + Assume.assumeTrue("Globals was initialised before setting the property", globalsStrict); + } + + @AfterClass + public static void restoreStrictServletCompliance() { + if (originalPropertyValue == null) { + System.clearProperty(STRICT_SERVLET_COMPLIANCE); + } else { + System.setProperty(STRICT_SERVLET_COMPLIANCE, originalPropertyValue); + } + } + + @Test + public void contextFlagsSetWhenStrictComplianceIsEnabled() { + Context ctx = getProgrammaticRootContextWithManager(); + Assert.assertTrue("xmlValidation should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getXmlValidation()); + Assert.assertTrue("xmlNamespaceAware should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getXmlNamespaceAware()); + Assert.assertTrue("tldValidation should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getTldValidation()); + Assert.assertFalse("useRelativeRedirects should be false under STRICT_SERVLET_COMPLIANCE.", ctx.getUseRelativeRedirects()); + Assert.assertTrue("alwaysAccessSession should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getAlwaysAccessSession()); + Assert.assertTrue("contextGetResourceRequiresSlash should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getContextGetResourceRequiresSlash()); + Assert.assertTrue("dispatcherWrapsSameObject should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getDispatcherWrapsSameObject()); + Assert.assertFalse("All extension mapped servlets should be checked against welcome files under STRICT_SERVLET_COMPLIANCE.", ctx.isResourceOnlyServlet("jsp")); + + Manager manager = ctx.getManager(); + if (manager instanceof ManagerBase managerBase) { + Assert.assertTrue("ManagerBase.sessionActivityCheck should be true under STRICT", managerBase.getSessionActivityCheck()); + Assert.assertTrue("ManagerBase.sessionLastAccessAtStart should be true under STRICT", managerBase.getSessionLastAccessAtStart()); + } + } + +} diff --git a/test/org/apache/catalina/startup/TestStrictComplianceDeployment.java b/test/org/apache/catalina/startup/TestXmlValidationUsingContext.java similarity index 55% rename from test/org/apache/catalina/startup/TestStrictComplianceDeployment.java rename to test/org/apache/catalina/startup/TestXmlValidationUsingContext.java index 55e3fcf9d0..8528b97f94 100644 --- a/test/org/apache/catalina/startup/TestStrictComplianceDeployment.java +++ b/test/org/apache/catalina/startup/TestXmlValidationUsingContext.java @@ -14,76 +14,52 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.catalina.startup; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.util.logging.Level; -import org.junit.AfterClass; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.Test; import org.apache.catalina.Context; /** - * Verification for STRICT_SERVLET_COMPLIANCE and web.xml parsing. + * Tests XML validation works on the Context. */ -public class TestStrictComplianceDeployment extends TomcatBaseTest { - private static final String STRICT_SERVLET_COMPLIANCE = "org.apache.catalina.STRICT_SERVLET_COMPLIANCE"; - private static String originalPropertyValue; - @BeforeClass - public static void enableStrictServletCompliance() { - originalPropertyValue = System.getProperty(STRICT_SERVLET_COMPLIANCE); - System.setProperty(STRICT_SERVLET_COMPLIANCE, "true"); - } - - @AfterClass - public static void restoreStrictServletCompliance() { - if (originalPropertyValue == null) { - System.clearProperty(STRICT_SERVLET_COMPLIANCE); - } else { - System.setProperty(STRICT_SERVLET_COMPLIANCE, originalPropertyValue); - } - } - +public class TestXmlValidationUsingContext extends TomcatBaseTest { @Test - public void testWebAppDeployWithStrictComplianceWithSaxParseException() throws Exception { + public void contextValidationWithInvalidWebXml() throws Exception { File appDir = getTemporaryDirectory(); File webInf = new File(appDir, "WEB-INF"); Assert.assertTrue(webInf.isDirectory() || webInf.mkdirs()); writeInvalidXml(new File(webInf, "web.xml")); Tomcat tomcat = getTomcatInstance(); - Context ctx = tomcat.addWebapp(null,"", appDir.getAbsolutePath()); - - try(WebappLogCapture capture = attachWebappLogCapture( - ctx, Level.SEVERE,"org.apache.tomcat.util.digester.Digester")) { - tomcat.start(); - Assert.assertTrue("A 'Parse error' was found in the logs.", capture.containsText("Parse error at line")); - Assert.assertTrue("A SAXParseException was found in the logs.", capture.hasException(org.xml.sax.SAXParseException.class)); - - } + Context ctx = tomcat.addWebapp(null, "", appDir.getAbsolutePath()); + ctx.setXmlValidation(true); + ctx.setXmlNamespaceAware(true); + tomcat.start(); + Assert.assertFalse("Context should not be available when web.xml is invalid and validation is enabled", + ctx.getState().isAvailable()); } @Test - public void testWebAppDeployWithStrictComplianceNoSaxParseException() throws Exception { + public void contextValidationWithValidWebXml() throws Exception { File appDir = getTemporaryDirectory(); File webInf = new File(appDir, "WEB-INF"); Assert.assertTrue(webInf.isDirectory() || webInf.mkdirs()); writeValidXml(new File(webInf, "web.xml")); Tomcat tomcat = getTomcatInstance(); - Context ctx = tomcat.addWebapp(null,"", appDir.getAbsolutePath()); - - try(WebappLogCapture capture = attachWebappLogCapture( - ctx, Level.SEVERE,"org.apache.tomcat.util.digester.Digester")) { - tomcat.start(); - Assert.assertFalse("A 'Parse error' was found in the logs.", capture.containsText("Parse error at line")); - Assert.assertFalse("A SAXParseException was found in the logs.", capture.hasException(org.xml.sax.SAXParseException.class)); - - } + Context ctx = tomcat.addWebapp(null, "", appDir.getAbsolutePath()); + ctx.setXmlValidation(true); + ctx.setXmlNamespaceAware(true); + tomcat.start(); + Assert.assertTrue("Context should be available when web.xml is valid and validation is enabled", + ctx.getState().isAvailable()); } + private void writeValidXml(File webXml) throws IOException { try (FileWriter fw = new FileWriter(webXml)) { fw.write( @@ -108,5 +84,4 @@ public class TestStrictComplianceDeployment extends TomcatBaseTest { """); } } - } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
