vijaykriishna commented on code in PR #11410: URL: https://github.com/apache/maven/pull/11410#discussion_r2567370915
########## impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultDependencyResolverResultTest.java: ########## @@ -0,0 +1,371 @@ +/* + * 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.impl; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.jar.Attributes; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; + +import org.apache.maven.api.Dependency; +import org.apache.maven.api.JavaPathType; +import org.apache.maven.api.Node; +import org.apache.maven.api.PathType; +import org.apache.maven.api.services.DependencyResolverRequest; +import org.apache.maven.impl.resolver.type.DefaultType; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** Unit tests for {@link DefaultDependencyResolverResult}. */ +public class DefaultDependencyResolverResultTest { + + @Test + public void testAddDependencyWithNullDependencyAddsNodeOnly() throws Exception { + DependencyResolverRequest req = mock(DependencyResolverRequest.class); + List<Exception> exceptions = new ArrayList<>(); + Node root = mock(Node.class); + PathModularizationCache cache = new PathModularizationCache(Runtime.version()); + + DefaultDependencyResolverResult result = new DefaultDependencyResolverResult(req, cache, exceptions, root, 4); + + Node node = mock(Node.class); + // addDependency with null dependency should only add the node + result.addDependency(node, null, (Predicate<PathType>) (t) -> true, null); + + assertEquals(1, result.getNodes().size()); + assertEquals(0, result.getDependencies().size()); + assertEquals(0, result.getPaths().size()); + assertTrue(result.getDispatchedPaths().isEmpty()); + } + + @Test + public void testAddDependencyDuplicateThrows() throws Exception { + DependencyResolverRequest req = mock(DependencyResolverRequest.class); + List<Exception> exceptions = new ArrayList<>(); + Node root = mock(Node.class); + PathModularizationCache cache = new PathModularizationCache(Runtime.version()); + + DefaultDependencyResolverResult result = new DefaultDependencyResolverResult(req, cache, exceptions, root, 4); + + Dependency dep = mock(Dependency.class); + when(dep.getGroupId()).thenReturn("g"); + when(dep.getArtifactId()).thenReturn("a"); + when(dep.getType()) + .thenReturn(new DefaultType( + "jar", org.apache.maven.api.Language.JAVA_FAMILY, "jar", null, false, JavaPathType.MODULES)); + + Node node = mock(Node.class); + Path p = Files.createTempFile("dup", ".jar"); + + result.addDependency(node, dep, (Predicate<PathType>) (t) -> true, p); + + // adding the same dependency again should throw + IllegalStateException ex = assertThrows( + IllegalStateException.class, + () -> result.addDependency(node, dep, (Predicate<PathType>) (t) -> true, p)); + assertTrue(ex.getMessage().contains("Duplicated key")); + } + + @Test + public void testAddDependencyWithAutomaticModuleNameAndGetModuleName() throws Exception { + DependencyResolverRequest req = mock(DependencyResolverRequest.class); + List<Exception> exceptions = new ArrayList<>(); + Node root = mock(Node.class); + PathModularizationCache cache = new PathModularizationCache(Runtime.version()); + + DefaultDependencyResolverResult result = new DefaultDependencyResolverResult(req, cache, exceptions, root, 4); + + // create a jar with Automatic-Module-Name manifest attribute + Path jar = Files.createTempFile("auto-module", ".jar"); + Manifest mf = new Manifest(); + mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + mf.getMainAttributes().put(new Attributes.Name("Automatic-Module-Name"), "auto.mod"); + try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar), mf)) { + // empty jar with manifest + } + + Dependency dep = mock(Dependency.class); + when(dep.getGroupId()).thenReturn("g"); + when(dep.getArtifactId()).thenReturn("a"); + when(dep.getType()) + .thenReturn(new DefaultType( + "jar", org.apache.maven.api.Language.JAVA_FAMILY, "jar", null, false, JavaPathType.MODULES)); Review Comment: > Consider using a method `createJarType(JavaPathType.MODULES)` that can be reused at the 5 locations. Alternatively, using constants could work too. Came up with a reusable method. -- 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]
