This is an automated email from the ASF dual-hosted git repository. remm pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new f14fb48671 Expand coverage further f14fb48671 is described below commit f14fb486711300b6b5c0338e40246e8b4c20972c Author: remm <r...@apache.org> AuthorDate: Tue Nov 26 13:47:45 2024 +0100 Expand coverage further --- .../catalina/startup/TestTomcatStandalone.java | 140 ++++++++++++++++----- 1 file changed, 106 insertions(+), 34 deletions(-) diff --git a/test/org/apache/catalina/startup/TestTomcatStandalone.java b/test/org/apache/catalina/startup/TestTomcatStandalone.java index aa4a92021b..1b1f3d752a 100644 --- a/test/org/apache/catalina/startup/TestTomcatStandalone.java +++ b/test/org/apache/catalina/startup/TestTomcatStandalone.java @@ -18,9 +18,12 @@ package org.apache.catalina.startup; import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.net.Inet4Address; import java.net.InetAddress; +import java.net.MalformedURLException; import java.net.NetworkInterface; import java.net.Socket; import java.net.URI; @@ -43,55 +46,117 @@ import org.apache.tomcat.util.file.ConfigFileLoader; public class TestTomcatStandalone extends LoggingBaseTest { + private static final String TEST_WEBAPP_CONTEXT_XML = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<Context docBase=\"${user.dir}/test/webapp-2.2\" ignoreAnnotations=\"true\">\n" + + "</Context>"; + + private static final long LAST_MODIFIED = System.currentTimeMillis(); protected class ServerXml extends CatalinaBaseConfigurationSource { + + class MemoryResource extends Resource { + MemoryResource(InputStream inputStream, URI uri) { + super(inputStream, uri); + } + @Override + public long getLastModified() throws MalformedURLException, IOException { + return LAST_MODIFIED; + } + } + public ServerXml() { super(getTemporaryDirectory(), null); } - private static final String SERVER_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - + "<Server port=\"8005\" shutdown=\"SHUTDOWN\">\n" - + " <Listener className=\"org.apache.catalina.startup.VersionLoggerListener\" />\n" - + " <Listener className=\"org.apache.catalina.core.AprLifecycleListener\" SSLEngine=\"on\" />\n" - + " <Listener className=\"org.apache.catalina.core.JreMemoryLeakPreventionListener\" />\n" - + " <Listener className=\"org.apache.catalina.mbeans.GlobalResourcesLifecycleListener\" />\n" - + " <Listener className=\"org.apache.catalina.core.ThreadLocalLeakPreventionListener\" />\n" - + "\n" + " <GlobalNamingResources>\n" - + " <Resource name=\"UserDatabase\" auth=\"Container\"\n" - + " type=\"org.apache.catalina.UserDatabase\"\n" - + " description=\"User database that can be updated and saved\"\n" - + " factory=\"org.apache.catalina.users.MemoryUserDatabaseFactory\"\n" - + " pathname=\"conf/tomcat-users.xml\" />\n" - + " </GlobalNamingResources>\n" + "\n" - + " <Service name=\"Catalina\">\n" + "\n" - + " <Connector port=\"0\" protocol=\"HTTP/1.1\"\n" - + " connectionTimeout=\"20000\"\n" - + " redirectPort=\"8443\" />\n" - + " <Engine name=\"Catalina\" defaultHost=\"localhost\">\n" - + "\n" - + " <Realm className=\"org.apache.catalina.realm.LockOutRealm\">\n" - + " <Realm className=\"org.apache.catalina.realm.UserDatabaseRealm\"\n" - + " resourceName=\"UserDatabase\"/>\n" - + " </Realm>\n" + "\n" - + " <Host name=\"localhost\" appBase=\"webapps\"\n" - + " unpackWARs=\"true\" autoDeploy=\"true\">\n" - + "\n" - + " <Valve className=\"org.apache.catalina.valves.AccessLogValve\" directory=\"logs\"\n" - + " prefix=\"localhost_access_log\" suffix=\".txt\"\n" - + " pattern=\"%h %l %u %t "%r" %s %b\" />\n" - + "\n" + " </Host>\n" + " </Engine>\n" - + " </Service>\n" + "</Server>"; + private static final String SERVER_XML = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<Server port=\"8005\" shutdown=\"SHUTDOWN\">\n" + + " <Listener className=\"org.apache.catalina.startup.VersionLoggerListener\" />\n" + + " <Listener className=\"org.apache.catalina.core.AprLifecycleListener\" SSLEngine=\"on\" />\n" + + " <Listener className=\"org.apache.catalina.core.JreMemoryLeakPreventionListener\" />\n" + + " <Listener className=\"org.apache.catalina.mbeans.GlobalResourcesLifecycleListener\" />\n" + + " <Listener className=\"org.apache.catalina.core.ThreadLocalLeakPreventionListener\" />\n" + + " <GlobalNamingResources>\n" + + " <Resource name=\"UserDatabase\" auth=\"Container\"\n" + + " type=\"org.apache.catalina.UserDatabase\"\n" + + " description=\"User database that can be updated and saved\"\n" + + " factory=\"org.apache.catalina.users.MemoryUserDatabaseFactory\"\n" + + " pathname=\"conf/tomcat-users.xml\" />\n" + + " </GlobalNamingResources>\n" + "\n" + + " <Service name=\"Catalina\">\n" + + " <Connector port=\"0\" protocol=\"HTTP/1.1\"\n" + + " connectionTimeout=\"20000\"\n" + + " redirectPort=\"8443\" />\n" + + " <Engine name=\"Catalina\" defaultHost=\"localhost\">\n" + "\n" + + " <Realm className=\"org.apache.catalina.realm.LockOutRealm\">\n" + + " <Realm className=\"org.apache.catalina.realm.UserDatabaseRealm\"\n" + + " resourceName=\"UserDatabase\"/>\n" + + " </Realm>\n" + "\n" + + " <Host name=\"localhost\" appBase=\"webapps\"\n" + + " unpackWARs=\"true\" autoDeploy=\"true\">\n" + "\n" + + " <Valve className=\"org.apache.catalina.valves.AccessLogValve\" directory=\"logs\"\n" + + " prefix=\"localhost_access_log\" suffix=\".txt\"\n" + + " pattern=\"%h %l %u %t "%r" %s %b\" />\n" + + " </Host>\n" + + " </Engine>\n" + + " </Service>\n" + + "</Server>"; + + private static final String CONTEXT_XML = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<Context>\n" + + " <JarScanner scanClassPath=\"false\" />\n" + + " <WatchedResource>WEB-INF/web.xml</WatchedResource>\n" + + " <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>\n" + + " <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>\n" + + " <Manager pathname=\"SESSIONS.ser\" />\n" + + "</Context>"; + + private static final String TOMCAT_USERS_XML = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<tomcat-users xmlns=\"http://tomcat.apache.org/xml\"\n" + + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + + " xsi:schemaLocation=\"http://tomcat.apache.org/xml tomcat-users.xsd\"\n" + + " version=\"1.0\">\n" + + " <user username=\"tomcat\" password=\"tomcat\" roles=\"tomcat,manager-status\"/>\n" + + "</tomcat-users>"; @Override public Resource getServerXml() throws IOException { Resource resource; try { - resource = new Resource(new ByteArrayInputStream(SERVER_XML.getBytes(StandardCharsets.ISO_8859_1)), - new URI("file:server.xml")); + resource = new MemoryResource(new ByteArrayInputStream(SERVER_XML.getBytes(StandardCharsets.UTF_8)), + new URI("file:conf/server.xml")); } catch (URISyntaxException e) { throw new IOException(e); } return resource; } + + @Override + public Resource getResource(String name) throws IOException { + if (Constants.DefaultContextXml.equals(name)) { + Resource resource; + try { + resource = new MemoryResource(new ByteArrayInputStream(CONTEXT_XML.getBytes(StandardCharsets.UTF_8)), + new URI("file:conf/context.xml")); + } catch (URISyntaxException e) { + throw new IOException(e); + } + return resource; + } else if ("conf/tomcat-users.xml".equals(name)) { + Resource resource; + try { + resource = new MemoryResource(new ByteArrayInputStream(TOMCAT_USERS_XML.getBytes(StandardCharsets.UTF_8)), + new URI("file:conf/tomcat-users.xml")); + } catch (URISyntaxException e) { + throw new IOException(e); + } + return resource; + } + return super.getResource(name); + } + } @Test @@ -166,6 +231,13 @@ public class TestTomcatStandalone extends LoggingBaseTest { public void testStandalone() throws Exception { ConfigFileLoader.setSource(new ServerXml()); + // Add descriptor to deploy + File descriptorsFolder = new File(getTemporaryDirectory(), "conf/Catalina/localhost"); + Assert.assertTrue(descriptorsFolder.mkdirs()); + try (FileOutputStream os = new FileOutputStream(new File(descriptorsFolder, "test.xml"))) { + os.write(TEST_WEBAPP_CONTEXT_XML.getBytes(StandardCharsets.UTF_8)); + } + Catalina catalina = new Catalina(); catalina.setAwait(true); File generatedCodeLocation = new File(getTemporaryDirectory(), "generated"); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org