gnodet-bot commented on code in PR #13602:
URL: https://github.com/apache/maven/pull/13602#discussion_r4313663962


##########
impl/maven-di/src/test/java/org/apache/maven/di/impl/InjectorDiagnosticsTest.java:
##########
@@ -0,0 +1,533 @@
+/*
+ * 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.di.impl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import org.apache.maven.api.di.Named;
+import org.apache.maven.api.di.Provides;
+import org.apache.maven.api.di.Qualifier;
+import org.apache.maven.api.di.SessionScoped;
+import org.apache.maven.api.di.Singleton;
+import org.apache.maven.di.Key;
+import org.apache.maven.di.Scope;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.api.parallel.ResourceLock;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@ResourceLock("java.lang.System.properties")
+@ResourceLock("java.lang.System.out")
+@Timeout(30)
+class InjectorDiagnosticsTest {
+    private String previousProperty;
+    private PrintStream previousOutput;
+
+    @BeforeEach
+    void enableDiagnostics() {
+        previousProperty = System.getProperty("sisu.debug");
+        previousOutput = System.out;
+        System.setProperty("sisu.debug", "true");
+    }
+
+    @AfterEach
+    void restoreEnvironment() {
+        if (previousProperty == null) {
+            System.clearProperty("sisu.debug");
+        } else {
+            System.setProperty("sisu.debug", previousProperty);
+        }
+        System.setOut(previousOutput);
+    }
+
+    @Test
+    void snapshotDoesNotProvisionOrInvokeUserToString() {
+        RecordingInjector injector = new RecordingInjector();
+        injector.bindInstance(Hostile.class, new Hostile());
+        injector.bindInstance(CharSequence.class, "secret-value");
+        Key<Long> initialized = Key.of(Long.class);
+        injector.bind(
+                initialized,
+                new Binding<Long>(initialized, Set.of()) {
+                    @Override
+                    public Supplier<Long> compile(Function<Dependency<?>, 
Supplier<?>> compiler) {
+                        throw new AssertionError("binding compiled for 
diagnostics");
+                    }
+
+                    @Override
+                    public String toString() {
+                        throw new AssertionError("binding toString");
+                    }
+                }.initializeWith(new BindingInitializer<>(Set.of()) {
+                    @Override
+                    public Consumer<Long> compile(Function<Dependency<?>, 
Supplier<?>> compiler) {
+                        throw new AssertionError("initializer compiled for 
diagnostics");
+                    }
+                }));
+        injector.bindSupplier(String.class, new Supplier<String>() {
+            @Override
+            public String get() {
+                throw new AssertionError("supplier called for diagnostics");
+            }
+
+            @Override
+            public String toString() {
+                throw new AssertionError("supplier toString");
+            }
+        });
+        injector.bindScope(SessionScoped.class, () -> {
+            throw new AssertionError("scope provisioned");
+        });
+        Unconstructed.CALLS.set(0);
+        injector.bindImplicit(Unconstructed.class);
+        injector.bindImplicit(Provider.class);
+        String snapshot = injector.dumpBindings();
+        injector.checkpoint();
+        assertEquals(0, Unconstructed.CALLS.get());
+        assertTrue(snapshot.contains("initialized by"));
+        assertTrue(snapshot.contains("Provider.provide"));
+        assertTrue(snapshot.contains("scope " + 
SessionScoped.class.getName()));
+        assertTrue(snapshot.contains("supplier "));
+        assertFalse(snapshot.contains("secret-value"));
+        assertEquals(body(snapshot), body(injector.dumps.get(0)));
+    }
+
+    @Test
+    void snapshotsRenderWildcardsInOwnerTypes() {
+        class Outer<T> {
+            class Inner<U> {}
+        }
+
+        // Key simplifies its type arguments, but preserves wildcards in 
parameterized owners.
+        String bound = Number.class.getName() + " [loader=null]";
+        Map<Key<?>, String> cases = Map.of(
+                new Key<Outer<?>.Inner<String>>() {}, "<?>",
+                new Key<Outer<? extends Number>.Inner<String>>() {}, "<? 
extends " + bound + ">",
+                new Key<Outer<? super Number>.Inner<String>>() {}, "<? super " 
+ bound + ">");
+        cases.forEach((key, expected) -> {
+            InjectorImpl injector = new InjectorImpl();
+            injector.getBindings().put(key, Set.of());
+            String dump = injector.dumpBindings();
+            assertTrue(dump.contains(expected), dump);

Review Comment:
   💡 **Test does not guard against regression of this commit's fix**
   
   `snapshotsRenderWildcardsInOwnerTypes` verifies that wildcard strings 
(`<?>`, `<? extends ...>`, `<? super ...>`) are *present* in the dump — but it 
doesn't assert that the owner class name is *not* repeated.
   
   With the old code (before `699661ca`), the dump for `Outer<?>.Inner<String>` 
would contain `Outer$Inner [loader=...]` as the inner type name (binary name 
includes the enclosing class). The test would still pass because `<?>` *is* 
present in `Outer [loader=L]<?>.Outer$Inner [loader=L]<String...>`. So this 
test doesn't catch a regression of this commit's fix.
   
   Consider adding a negative assertion:
   ```
   assertFalse(dump.contains("$Inner"), "Inner class should be rendered without 
enclosing owner prefix: " + dump);
   ```
   (Or rename the test to `snapshotsRenderInnerTypesWithoutOwnerRepetition` and 
add the appropriate assertion.)



-- 
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]

Reply via email to