Copilot commented on code in PR #16041: URL: https://github.com/apache/grails-core/pull/16041#discussion_r3629986216
########## grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy: ########## @@ -0,0 +1,58 @@ +/* + * 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 + * + * https://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.grails.cli.boot + +import jakarta.servlet.ServletContext +import spock.lang.Specification + +/** + * Verifies that {@link SpringApplicationWebApplicationInitializer} stays inert when it is + * discovered on the classpath of a standard {@code bootWar} deployed to an external servlet + * container, i.e. when the {@code Spring-Application-Source-Classes} manifest entry is absent. + * + * Regression test for <a href="https://github.com/apache/grails-core/issues/15377">#15377</a>. + */ +class SpringApplicationWebApplicationInitializerSpec extends Specification { + + void "onStartup is inert when the source-classes manifest entry is absent"() { + given: 'an initializer discovered in a non CLI-packaged WAR' + def initializer = new SpringApplicationWebApplicationInitializer() + ServletContext servletContext = Mock(ServletContext) { + getResourceAsStream('/META-INF/MANIFEST.MF') >> manifestStream + } Review Comment: The spec validates the inert/no-throw behavior, but it doesn’t cover the “manifest entry present” path to ensure the initializer still attempts to proceed (i.e., doesn’t accidentally return early when valid sources exist). Adding a positive test case with a non-blank `Spring-Application-Source-Classes` value (and asserting the initializer progresses to the bootstrapping path via a test subclass/spy seam) would guard against regressions in the early-return logic. ########## grails-shell-cli/src/main/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializer.java: ########## @@ -51,15 +51,28 @@ public void onStartup(ServletContext servletContext) throws ServletException { catch (IOException ex) { throw new IllegalStateException(ex); } + // This initializer only applies to CLI-packaged WARs produced by the Grails shell 'war' + // command, which records the application source classes in the WAR manifest via the + // 'Spring-Application-Source-Classes' entry. When that entry is absent (for example a + // standard 'bootWar' archive deployed to an external servlet container such as Tomcat), + // this initializer is not applicable and must stay inert so it does not interfere with + // the application's own SpringBootServletInitializer. + // See https://github.com/apache/grails-core/issues/15377 + if (this.sources == null || this.sources.length == 0) { + return; + } super.onStartup(servletContext); } private String[] getSources(ServletContext servletContext) throws IOException { Manifest manifest = getManifest(servletContext); if (manifest == null) { - throw new IllegalStateException("Unable to read manifest"); + return null; } String sources = manifest.getMainAttributes().getValue(SOURCE_ENTRY); + if (sources == null || sources.isBlank()) { + return null; + } return sources.split(","); Review Comment: `sources.split(",")` doesn’t trim tokens, so a manifest value like `com.foo.App, com.foo.Other` (comma+space) will produce `" com.foo.Other"` and can break class resolution downstream. Consider splitting, trimming each element, and filtering out empty results before returning (e.g., treat all-whitespace/empty tokens as absent). ########## grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy: ########## @@ -0,0 +1,58 @@ +/* + * 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 + * + * https://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.grails.cli.boot + +import jakarta.servlet.ServletContext +import spock.lang.Specification + +/** + * Verifies that {@link SpringApplicationWebApplicationInitializer} stays inert when it is + * discovered on the classpath of a standard {@code bootWar} deployed to an external servlet + * container, i.e. when the {@code Spring-Application-Source-Classes} manifest entry is absent. + * + * Regression test for <a href="https://github.com/apache/grails-core/issues/15377">#15377</a>. + */ +class SpringApplicationWebApplicationInitializerSpec extends Specification { + + void "onStartup is inert when the source-classes manifest entry is absent"() { + given: 'an initializer discovered in a non CLI-packaged WAR' + def initializer = new SpringApplicationWebApplicationInitializer() + ServletContext servletContext = Mock(ServletContext) { + getResourceAsStream('/META-INF/MANIFEST.MF') >> manifestStream + } + + when: 'the servlet container invokes onStartup' + initializer.onStartup(servletContext) + + then: 'the application boot is not disturbed and no NPE is raised' + noExceptionThrown() + + where: 'the manifest is missing, lacks the entry, or has a blank/whitespace-only entry' + manifestStream << [ + null, + new ByteArrayInputStream('Manifest-Version: 1.0\r\n\r\n'.getBytes('UTF-8')), + new ByteArrayInputStream( + 'Manifest-Version: 1.0\r\nImplementation-Title: my-app\r\n\r\n'.getBytes('UTF-8')), Review Comment: Using `'UTF-8'` string literals and repeating `new ByteArrayInputStream(...getBytes(...))` makes the test data a bit error-prone and verbose. Consider using `StandardCharsets.UTF_8` (or Groovy’s `.bytes` with an explicit charset) and extracting a small helper to build the manifest streams to reduce duplication. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
