Skip to content

Commit 377875c

Browse files
committed
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.
1 parent b698a6d commit 377875c

File tree

2 files changed

+94
-43
lines changed

2 files changed

+94
-43
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.catalina.startup;
18+
19+
import org.junit.AfterClass;
20+
import org.junit.Assert;
21+
import org.junit.Assume;
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
24+
25+
import org.apache.catalina.Context;
26+
import org.apache.catalina.Globals;
27+
import org.apache.catalina.Manager;
28+
import org.apache.catalina.session.ManagerBase;
29+
30+
/**
31+
* Tests STRICT_SERVLET_COMPLIANCE sets the attributes it is documented to set.
32+
*/
33+
public class TestStrictServletComplianceAttributes extends TomcatBaseTest {
34+
private static final String STRICT_SERVLET_COMPLIANCE = "org.apache.catalina.STRICT_SERVLET_COMPLIANCE";
35+
private static String originalPropertyValue;
36+
37+
@BeforeClass
38+
public static void setup() {
39+
originalPropertyValue = System.getProperty(STRICT_SERVLET_COMPLIANCE);
40+
System.setProperty(STRICT_SERVLET_COMPLIANCE, "true");
41+
42+
// If Globals was already initialised in the same JVM (during the tests run through IDE),
43+
// before the test sets the value to true, skip.
44+
boolean globalsStrict = Globals.STRICT_SERVLET_COMPLIANCE;
45+
Assume.assumeTrue("Globals was initialised before setting the property", globalsStrict);
46+
}
47+
48+
@AfterClass
49+
public static void restoreStrictServletCompliance() {
50+
if (originalPropertyValue == null) {
51+
System.clearProperty(STRICT_SERVLET_COMPLIANCE);
52+
} else {
53+
System.setProperty(STRICT_SERVLET_COMPLIANCE, originalPropertyValue);
54+
}
55+
}
56+
57+
@Test
58+
public void contextFlagsSetWhenStrictComplianceIsEnabled() {
59+
Context ctx = getProgrammaticRootContextWithManager();
60+
Assert.assertTrue("xmlValidation should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getXmlValidation());
61+
Assert.assertTrue("xmlNamespaceAware should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getXmlNamespaceAware());
62+
Assert.assertTrue("tldValidation should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getTldValidation());
63+
Assert.assertFalse("useRelativeRedirects should be false under STRICT_SERVLET_COMPLIANCE.", ctx.getUseRelativeRedirects());
64+
Assert.assertTrue("alwaysAccessSession should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getAlwaysAccessSession());
65+
Assert.assertTrue("contextGetResourceRequiresSlash should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getContextGetResourceRequiresSlash());
66+
Assert.assertTrue("dispatcherWrapsSameObject should be true under STRICT_SERVLET_COMPLIANCE.", ctx.getDispatcherWrapsSameObject());
67+
Assert.assertFalse("All extension mapped servlets should be checked against welcome files under STRICT_SERVLET_COMPLIANCE.", ctx.isResourceOnlyServlet("jsp"));
68+
69+
Manager manager = ctx.getManager();
70+
if (manager instanceof ManagerBase managerBase) {
71+
Assert.assertTrue("ManagerBase.sessionActivityCheck should be true under STRICT", managerBase.getSessionActivityCheck());
72+
Assert.assertTrue("ManagerBase.sessionLastAccessAtStart should be true under STRICT", managerBase.getSessionLastAccessAtStart());
73+
}
74+
}
75+
76+
}

test/org/apache/catalina/startup/TestStrictComplianceDeployment.java renamed to test/org/apache/catalina/startup/TestXmlValidationUsingContext.java

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,76 +14,52 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
1718
package org.apache.catalina.startup;
1819

1920
import java.io.File;
2021
import java.io.FileWriter;
2122
import java.io.IOException;
22-
import java.util.logging.Level;
2323

24-
import org.junit.AfterClass;
2524
import org.junit.Assert;
26-
import org.junit.BeforeClass;
2725
import org.junit.Test;
2826

2927
import org.apache.catalina.Context;
3028

3129
/**
32-
* Verification for STRICT_SERVLET_COMPLIANCE and web.xml parsing.
30+
* Tests XML validation works on the Context.
3331
*/
34-
public class TestStrictComplianceDeployment extends TomcatBaseTest {
35-
private static final String STRICT_SERVLET_COMPLIANCE = "org.apache.catalina.STRICT_SERVLET_COMPLIANCE";
36-
private static String originalPropertyValue;
37-
@BeforeClass
38-
public static void enableStrictServletCompliance() {
39-
originalPropertyValue = System.getProperty(STRICT_SERVLET_COMPLIANCE);
40-
System.setProperty(STRICT_SERVLET_COMPLIANCE, "true");
41-
}
42-
43-
@AfterClass
44-
public static void restoreStrictServletCompliance() {
45-
if (originalPropertyValue == null) {
46-
System.clearProperty(STRICT_SERVLET_COMPLIANCE);
47-
} else {
48-
System.setProperty(STRICT_SERVLET_COMPLIANCE, originalPropertyValue);
49-
}
50-
}
51-
32+
public class TestXmlValidationUsingContext extends TomcatBaseTest {
5233
@Test
53-
public void testWebAppDeployWithStrictComplianceWithSaxParseException() throws Exception {
34+
public void contextValidationWithInvalidWebXml() throws Exception {
5435
File appDir = getTemporaryDirectory();
5536
File webInf = new File(appDir, "WEB-INF");
5637
Assert.assertTrue(webInf.isDirectory() || webInf.mkdirs());
5738
writeInvalidXml(new File(webInf, "web.xml"));
5839
Tomcat tomcat = getTomcatInstance();
59-
Context ctx = tomcat.addWebapp(null,"", appDir.getAbsolutePath());
60-
61-
try(WebappLogCapture capture = attachWebappLogCapture(
62-
ctx, Level.SEVERE,"org.apache.tomcat.util.digester.Digester")) {
63-
tomcat.start();
64-
Assert.assertTrue("A 'Parse error' was found in the logs.", capture.containsText("Parse error at line"));
65-
Assert.assertTrue("A SAXParseException was found in the logs.", capture.hasException(org.xml.sax.SAXParseException.class));
66-
67-
}
40+
Context ctx = tomcat.addWebapp(null, "", appDir.getAbsolutePath());
41+
ctx.setXmlValidation(true);
42+
ctx.setXmlNamespaceAware(true);
43+
tomcat.start();
44+
Assert.assertFalse("Context should not be available when web.xml is invalid and validation is enabled",
45+
ctx.getState().isAvailable());
6846
}
6947

7048
@Test
71-
public void testWebAppDeployWithStrictComplianceNoSaxParseException() throws Exception {
49+
public void contextValidationWithValidWebXml() throws Exception {
7250
File appDir = getTemporaryDirectory();
7351
File webInf = new File(appDir, "WEB-INF");
7452
Assert.assertTrue(webInf.isDirectory() || webInf.mkdirs());
7553
writeValidXml(new File(webInf, "web.xml"));
7654
Tomcat tomcat = getTomcatInstance();
77-
Context ctx = tomcat.addWebapp(null,"", appDir.getAbsolutePath());
78-
79-
try(WebappLogCapture capture = attachWebappLogCapture(
80-
ctx, Level.SEVERE,"org.apache.tomcat.util.digester.Digester")) {
81-
tomcat.start();
82-
Assert.assertFalse("A 'Parse error' was found in the logs.", capture.containsText("Parse error at line"));
83-
Assert.assertFalse("A SAXParseException was found in the logs.", capture.hasException(org.xml.sax.SAXParseException.class));
84-
85-
}
55+
Context ctx = tomcat.addWebapp(null, "", appDir.getAbsolutePath());
56+
ctx.setXmlValidation(true);
57+
ctx.setXmlNamespaceAware(true);
58+
tomcat.start();
59+
Assert.assertTrue("Context should be available when web.xml is valid and validation is enabled",
60+
ctx.getState().isAvailable());
8661
}
62+
8763
private void writeValidXml(File webXml) throws IOException {
8864
try (FileWriter fw = new FileWriter(webXml)) {
8965
fw.write(
@@ -108,5 +84,4 @@ private void writeInvalidXml(File webXml) throws IOException {
10884
""");
10985
}
11086
}
111-
11287
}

0 commit comments

Comments
 (0)