Skip to content

Commit 079cc1e

Browse files
committed
Add TestStrictComplianceDeployment for STRICT_SERVLET_COMPLIANCE web.xml parsing
1 parent ca12722 commit 079cc1e

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 java.io.File;
20+
import java.io.FileWriter;
21+
import java.io.IOException;
22+
import java.util.logging.Level;
23+
24+
import org.junit.AfterClass;
25+
import org.junit.Assert;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
29+
import org.apache.catalina.Context;
30+
31+
/**
32+
* Verification for STRICT_SERVLET_COMPLIANCE and web.xml parsing.
33+
*/
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+
52+
@Test
53+
public void testWebAppDeployWithStrictComplianceWithSaxParseException() throws Exception {
54+
File appDir = getTemporaryDirectory();
55+
File webInf = new File(appDir, "WEB-INF");
56+
Assert.assertTrue(webInf.isDirectory() || webInf.mkdirs());
57+
writeInvalidXml(new File(webInf, "web.xml"));
58+
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+
}
68+
}
69+
70+
@Test
71+
public void testWebAppDeployWithStrictComplianceNoSaxParseException() throws Exception {
72+
File appDir = getTemporaryDirectory();
73+
File webInf = new File(appDir, "WEB-INF");
74+
Assert.assertTrue(webInf.isDirectory() || webInf.mkdirs());
75+
writeValidXml(new File(webInf, "web.xml"));
76+
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+
}
86+
}
87+
private void writeValidXml(File webXml) throws IOException {
88+
try (FileWriter fw = new FileWriter(webXml)) {
89+
fw.write(
90+
"""
91+
<?xml version="1.0" encoding="UTF-8"?>
92+
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
93+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
94+
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
95+
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
96+
version="6.0">
97+
</web-app>
98+
""");
99+
}
100+
}
101+
private void writeInvalidXml(File webXml) throws IOException {
102+
try (FileWriter fw = new FileWriter(webXml)) {
103+
fw.write(
104+
"""
105+
<?xml version="1.0" encoding="UTF-8"?>
106+
<web-app>
107+
</web-app>
108+
""");
109+
}
110+
}
111+
112+
}

0 commit comments

Comments
 (0)