gnodet-bot commented on code in PR #13582: URL: https://github.com/apache/maven/pull/13582#discussion_r4324948640
########## maven-embedder/src/test/java/org/apache/maven/cli/internal/BootstrapCoreExtensionManagerTest.java: ########## @@ -0,0 +1,99 @@ +/* + * 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.maven.cli.internal; + +import org.codehaus.plexus.classworlds.ClassWorld; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Guards the JSR330 visibility filtering bypass: it must apply only when the TCCL is (a child of) a core extension realm. + */ +class BootstrapCoreExtensionManagerTest { + private final ClassLoader origTccl = Thread.currentThread().getContextClassLoader(); + + private ClassWorld world; + + @BeforeEach + void setUp() { + world = new ClassWorld(); + } + + @AfterEach + void tearDown() { + Thread.currentThread().setContextClassLoader(origTccl); + } + + @Test + void plainTcclIsNotCoreExtensionRealm() { + assertFalse(BootstrapCoreExtensionManager.isContextRealmCoreExtensionRealm()); + } + + @Test + void coreExtensionRealmTccl() throws Exception { + Thread.currentThread().setContextClassLoader(world.newRealm("coreExtension>g:a:1", null)); + assertTrue(BootstrapCoreExtensionManager.isContextRealmCoreExtensionRealm()); + } + + /** + * This situation cannot happen in real life, unless Extension itself creates an "isolated" URL classloader beneath itself. + * Notice, how child classloader is not even a realm, but an URL classloader. + */ + @Test + void childClassloaderOfCoreExtensionRealmTccl() throws Exception { + Thread.currentThread() + .setContextClassLoader( + new java.net.URLClassLoader(new java.net.URL[0], world.newRealm("coreExtension>g:a:1", null))); + assertTrue(BootstrapCoreExtensionManager.isContextRealmCoreExtensionRealm()); + } + + /** + * This situation cannot happen in real life, unless Extension itself creates an "isolated" class realm beneath itself. + * Notice, how realm name in this case is unrelated and do not follow the "core extension realm" naming scheme. + */ + @Test + void childRealmWithoutNameOfCoreExtensionRealmTccl() throws Exception { + Thread.currentThread() + .setContextClassLoader( + world.newRealm("child", world.newRealm("coreExtension>g:a:1", null))); + assertTrue(BootstrapCoreExtensionManager.isContextRealmCoreExtensionRealm()); Review Comment: ⚠️ **Spotless (Palantir) formatting violation — CI blocker** These multi-line `setContextClassLoader(...)` calls are flagged by Spotless/Palantir. Run `mvn spotless:apply -pl maven-embedder` to auto-fix all formatting violations. -- 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]
