This is an automated email from the ASF dual-hosted git repository. dsoumis pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit f77362b4f53ba45c24876020856edf4f0680f9c6 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. --- .../startup/TestStrictComplianceDeployment.java | 112 --------------------- .../TestStrictServletComplianceAttributes.java | 65 ++++++++++++ .../startup/TestXmlValidationUsingContext.java | 86 ++++++++++++++++ 3 files changed, 151 insertions(+), 112 deletions(-) diff --git a/test/org/apache/catalina/startup/TestStrictComplianceDeployment.java b/test/org/apache/catalina/startup/TestStrictComplianceDeployment.java deleted file mode 100644 index 55e3fcf9d0..0000000000 --- a/test/org/apache/catalina/startup/TestStrictComplianceDeployment.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 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. - */ -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); - } - } - - @Test - public void testWebAppDeployWithStrictComplianceWithSaxParseException() 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)); - - } - } - - @Test - public void testWebAppDeployWithStrictComplianceNoSaxParseException() 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)); - - } - } - private void writeValidXml(File webXml) throws IOException { - try (FileWriter fw = new FileWriter(webXml)) { - fw.write( - """ - <?xml version="1.0" encoding="UTF-8"?> - <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee - https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" - version="6.0"> - </web-app> - """); - } - } - private void writeInvalidXml(File webXml) throws IOException { - try (FileWriter fw = new FileWriter(webXml)) { - fw.write( - """ - <?xml version="1.0" encoding="UTF-8"?> - <web-app> - </web-app> - """); - } - } - -} diff --git a/test/org/apache/catalina/startup/TestStrictServletComplianceAttributes.java b/test/org/apache/catalina/startup/TestStrictServletComplianceAttributes.java new file mode 100644 index 0000000000..600e664d82 --- /dev/null +++ b/test/org/apache/catalina/startup/TestStrictServletComplianceAttributes.java @@ -0,0 +1,65 @@ +/* + * 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; + +/** + * 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.assertFalse("All extension mapped servlets should be checked against welcome files under STRICT_SERVLET_COMPLIANCE.", ctx.isResourceOnlyServlet("jsp")); + } + +} diff --git a/test/org/apache/catalina/startup/TestXmlValidationUsingContext.java b/test/org/apache/catalina/startup/TestXmlValidationUsingContext.java new file mode 100644 index 0000000000..35ec0c4bf8 --- /dev/null +++ b/test/org/apache/catalina/startup/TestXmlValidationUsingContext.java @@ -0,0 +1,86 @@ +/* + * 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 java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.Context; + +/** + * Tests XML validation works on the Context. + */ +public class TestXmlValidationUsingContext extends TomcatBaseTest { + @Test + 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()); + 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 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()); + 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( + """ + <?xml version="1.0" encoding="UTF-8"?> + <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee \ + http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" + version="4.0"> + </web-app>"""); + } + } + private void writeInvalidXml(File webXml) throws IOException { + try (FileWriter fw = new FileWriter(webXml)) { + fw.write( + """ + <?xml version="1.0" encoding="UTF-8"?> + <web-app> + </web-app> + """); + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
